Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Andrew Rohwedder
5,272 PointsPHP, MYSQL no duplicates
Hi All,
I am using a mysql database that has a list of books. Columns: id, author, title, year, link(amazon).
So when I create a foreach loop to list out specific columns (such as all of the authors), it lists all of the authors, but if an author is written in the database more than once (because they've written more than one book) it echoes the author more than once as well. I would like to have each author name listed only once. I want it to remove duplicates Here is a snippet of my code.
<?php
function all_books(){
//(PDO database stuff);
try {
$data = $db->query("SELECT id, author, title, year, link FROM books ORDER BY author ASC");
} catch (Exception $e) {
echo "Data could not be retrieved from the database.";
exit;
}
$books= $data->fetchAll(PDO::FETCH_ASSOC);
return $books; }
} function headerNav($mainURL, $otherURL){
$columns = all_books();
echo "<dt><a href=\"../" . $mainURL . "\">" . $mainURL . "<a class='drop' href=''>+</a></dt><dd>";
foreach($columns as $column){ ?>
<li><a href="<?php echo "../results.php?s=" . urlencode($column["" . $otherURL . ""]); ?>"><?php echo $column["" . $otherURL . ""]; ?></li> <?php }?>
<?php echo "</dd>";
}
This creates a simple navigation table, and without errors.
To extract what is important for my question, take the second half of it, specifically the:
<?php echo $column["" . $otherURL . ""]; ?>
for example this could be:
<?php echo $column["author"]; ?>
And it would echo all of the authors. If the same author is listed twice, it will list that authors name twice. I would like for it to only list an author once for that situation.
I have tried using array_unique, but it does not recognize any of these items as arrays, but as strings. Mysql "DISTINCT" also has been giving me trouble, as I still want all of the information in my database (just filtered based on the situation).
Thanks everyone, and I hope I made sense.
Andrew
2 Answers
. Ali
9,799 PointsHi Andrew, Right I am sure you can look at the php functions as array_map( ), array_walk( ) which let you pass an anonymous function. But do you only have one table ??? if thats the case then you need to rethink and redesign your database a little. Have two tables Authors and Books e.g.
-------------------- ------------------
Books Authors
------------------- -------------------
id id
name name
year
This way you having one to many relationships. Then either table you can call for results from php using a joint and you wont have this problem. read this for further info http://stackoverflow.com/questions/2923809/many-to-many-relationships-examples. cheers
Andrew Rohwedder
5,272 PointsMuhammad,
Why didn't I think of that?! I've been slacking on my databasing, so it wasn't in my repertoire of ideas I suppose.
Thanks, Andrew