Jump to content


Photo

explode() and arrays


  • Please log in to reply
5 replies to this topic

#1 tleisher

tleisher
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts

Posted 08 September 2006 - 03:31 PM

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?

#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 08 September 2006 - 03:44 PM

foreach ($fav_book as $val) {
   if $(_GET['id'] == $val) {
      // print username or whatever
   }
}

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 08 September 2006 - 03:55 PM

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.

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 08 September 2006 - 03:58 PM

in other words, read up on relational databases.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 tleisher

tleisher
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts

Posted 08 September 2006 - 03:59 PM

So have a completely different table for favorites?

Like:

Userid - Book ID
1            2
1            5
3            2

and so on?

#6 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 08 September 2006 - 04:27 PM

yes, with it's own index field:

CREATE TABLE `favorites`(
    `id` INT NOT NULL AUTO_INCREMENT,
    `bookid` INT NOT NULL,
    `userid` INT NOT NULL
) PRIMARY KEY(`id`);





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users