tleisher Posted September 8, 2006 Share Posted September 8, 2006 Alright, so here's how my database works. If someone adds a book as their favorite it takes their current favorite book column, adds "," and then the book name so it comes up like "1,2,3".Then in the favbook.php file, I want to have it display all the usernames of the people who have the book id in their fav_book column.So far I figured that I should read that column with a mysql query, then mysql_fetch_array the query, and then explode(",", $fav_book); But how would I do this in a loop so that it reads through the array and if it doesnt find $_GET["id"] then it doesn't print that username, then continues on to the next row with the new username and so on until it gets to the bottom of the table? Link to comment https://forums.phpfreaks.com/topic/20128-explode-and-arrays/ Share on other sites More sharing options...
.josh Posted September 8, 2006 Share Posted September 8, 2006 [code]foreach ($fav_book as $val) { if $(_GET['id'] == $val) { // print username or whatever }}[/code] Link to comment https://forums.phpfreaks.com/topic/20128-explode-and-arrays/#findComment-88453 Share on other sites More sharing options...
Jenk Posted September 8, 2006 Share Posted September 8, 2006 Just a note.. this method of storage (delimeted list) is looked upon as 'sloppy' because, well, it is sloppy.Use a table to store all the users entries with a foreign key for the user id and a foreign key for the book details. Essentiall 3 columns: index, userid, bookid.Then select the rows from that table, with joins on users and books if necessary. It will be MUCH more efficient. Link to comment https://forums.phpfreaks.com/topic/20128-explode-and-arrays/#findComment-88458 Share on other sites More sharing options...
.josh Posted September 8, 2006 Share Posted September 8, 2006 in other words, read up on relational databases. Link to comment https://forums.phpfreaks.com/topic/20128-explode-and-arrays/#findComment-88460 Share on other sites More sharing options...
tleisher Posted September 8, 2006 Author Share Posted September 8, 2006 So have a completely different table for favorites? Like:Userid - Book ID1 21 53 2and so on? Link to comment https://forums.phpfreaks.com/topic/20128-explode-and-arrays/#findComment-88462 Share on other sites More sharing options...
Jenk Posted September 8, 2006 Share Posted September 8, 2006 yes, with it's own index field:[code]CREATE TABLE `favorites`( `id` INT NOT NULL AUTO_INCREMENT, `bookid` INT NOT NULL, `userid` INT NOT NULL) PRIMARY KEY(`id`);[/code] Link to comment https://forums.phpfreaks.com/topic/20128-explode-and-arrays/#findComment-88483 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.