Page 1 of 1

Organizing these items by id?

Posted: Sun Jul 15, 2007 8:37 pm
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?

Posted: Sun Jul 15, 2007 8:48 pm
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.

Posted: Sun Jul 15, 2007 8:50 pm
by beloveddoll
I have never used the sort() before, how do I apply that into my codes?

Posted: Sun Jul 15, 2007 9:36 pm
by harrisonad

Posted: Sun Jul 15, 2007 9:52 pm
by beloveddoll
Thanks, guys, that did the trick!