Organizing these items by id?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
beloveddoll
Forum Commoner
Posts: 40
Joined: Sat Jul 14, 2007 6:18 pm

Organizing these items by id?

Post by beloveddoll »

I have the following code:

Code: Select all

$res = mysql_query("SELECT coll_inven,id FROM userdata WHERE login='$log'");
$rows = mysql_fetch_row($res);
mysql_free_result($res);
$favs = explode(',',$rows[0]);

//for each favorite get url and display
foreach($favs as $fav){
   if( $fav ){
      $res = mysql_query("SELECT id,name,description,url,store,price,preview FROM collection WHERE id=$fav");
      $img = mysql_fetch_row($res);
      
      
      if( $colCount%20 == 0 ){
	echo "<tr>";
      }
      $colCount = $colCount + 1;
      echo "<td><a href=collectionview.php?id=$fav><img src='collection/$img[6]' border=0></a>";
      if(($_SESSION['level2']>6) || ( $_SESSION['level2']>1 && $_SESSION['sess_name'])){
         echo "";
      }
     
   }
}
The items are being displayed fine but how do I get them to be displayed in order of their id? I tried ORDER BY id ASC in this string:

Code: Select all

$res = mysql_query("SELECT id,name,description,url,store,price,preview FROM collection WHERE id=$fav");
      $img = mysql_fetch_row($res);
but that did not work. So is there a way to make those items list via id?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

sort() $favs.

It would appear your database isn't designed to use typical normalization techniques. For example there would typically be a table containing separate rows referring to the particular user's "favs" which would contain a reference to the "collection" table. Selection would be a single query and the user would have an unlimited (nearly) number of "favs" versus the oddly limited form your code appears to use.
beloveddoll
Forum Commoner
Posts: 40
Joined: Sat Jul 14, 2007 6:18 pm

Post by beloveddoll »

I have never used the sort() before, how do I apply that into my codes?
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Post by harrisonad »

beloveddoll
Forum Commoner
Posts: 40
Joined: Sat Jul 14, 2007 6:18 pm

Post by beloveddoll »

Thanks, guys, that did the trick!
Post Reply