danielforsyth Posted March 3, 2006 Share Posted March 3, 2006 I am trying to display the contents of a mysql table in descending order by "id". The script works fine until I add in the ORDER BY to the query. You can see below that I have the ORDER BY coded only if $category = hand, so I get an error now if category = hand, but not if category = baby or if cat = makeup.[code]// how many rows to show per page$rowsPerPage = 6;// by default we show first page$pageNum = 1;$category = $_GET['category'];// if $_GET['page'] defined, use it as page numberif(isset($_GET['page'])){ $pageNum = $_GET['page'];}// counting the offset$offset = ($pageNum - 1) * $rowsPerPage;if ($category == "hand"){$query = "SELECT price, title, id FROM tm_inventory WHERE category='hand' ORDER BY id DESC";} elseif ($category == "baby"){$query = "SELECT price, title, id FROM tm_inventory WHERE category='baby'";} elseif ($category == "makeup"){$query = "SELECT price, title, id FROM tm_inventory WHERE category='makeup'";} $pagingQuery = "LIMIT $offset, $rowsPerPage";$result = mysql_query($query . $pagingQuery) or die('Error 1, query failed');// print the inventory info in tableecho '<table border="0" cellpadding="2" cellspacing="0">';while($row = mysql_fetch_array($result)){ $row2 = mysql_fetch_array($result) or $row2['id'] = "1" and $row2['title'] = " "; $row3 = mysql_fetch_array($result) or $row3['id'] = "1" and $row3['title'] = " "; printf("<tr><td align='center'> <img src=\"image3.php?id=%s\" width='150'><br> %s <br> %s </td> \n", $row["id"], $row["title"], $row["price"]); printf("<td align='center'> <img src=\"image3.php?id=%s\" width='150'><br> %s <br> %s </td> \n", $row2["id"], $row2["title"], $row2["price"]); printf("<td align='center'> <img src=\"image3.php?id=%s\" width='150'><br> %s <br> %s </td></tr>", $row3["id"], $row3["title"], $row3["price"]);}echo '</table>';echo '<br>';// how many rows we have in database$result = mysql_query($query) or die('Error 2, query failed');$numrows = mysql_num_rows($result);// how many pages we have when using paging?$maxPage = ceil($numrows/$rowsPerPage);$self = $_SERVER['PHP_SELF'];// creating 'previous' and 'next' link// plus 'first page' and 'last page' link// print 'previous' link only if we're not// on page oneif ($pageNum > 1){ $page = $pageNum - 1; $prev = " <a href=\"$self?category=$category&page=$page\">[Prev]</a> "; $first = " <a href=\"$self?category=$category&page=1\">[First Page]</a> ";}else{ $prev = ' [Prev] '; // we're on page one, don't enable 'previous' link $first = ' [First Page] '; // nor 'first page' link}// print 'next' link only if we're not// on the last pageif ($pageNum < $maxPage){ $page = $pageNum + 1; $next = " <a href=\"$self?category=$category&page=$page\">[Next]</a> "; $last = " <a href=\"$self?category=$category&page=$maxPage\">[Last Page]</a> ";}else{ $next = ' [Next] '; // we're on the last page, don't enable 'next' link $last = ' [Last Page] '; // nor 'last page' link}// print the page navigation link//echo $first . $prev . " Page <strong>$pageNum</strong> of <strong>$maxPage</strong> " . $next . $last;echo $prev . " Page <strong>$pageNum</strong> of <strong>$maxPage</strong> " . $next;[/code]Here are some links so you can see what happens:[a href=\"http://root.danielforsyth.com/mysql/image_upload/view5.php?category=hand\" target=\"_blank\"]http://root.danielforsyth.com/mysql/image_...p?category=hand[/a][a href=\"http://root.danielforsyth.com/mysql/image_upload/view5.php?category=baby\" target=\"_blank\"]http://root.danielforsyth.com/mysql/image_...p?category=baby[/a]Please tell me how to fix this - thx Quote Link to comment https://forums.phpfreaks.com/topic/3961-order-by-isnt-working-please-help/ Share on other sites More sharing options...
wickning1 Posted March 3, 2006 Share Posted March 3, 2006 PHP error, when you concatenate the string later you end up with:SELECT price, title, id FROM tm_inventory WHERE category='hand' ORDER BY id DESCLIMIT x,yDESCLIMIT is bad mmkay? Quote Link to comment https://forums.phpfreaks.com/topic/3961-order-by-isnt-working-please-help/#findComment-13751 Share on other sites More sharing options...
danielforsyth Posted March 3, 2006 Author Share Posted March 3, 2006 got it, so I just need a space after the DESC? Quote Link to comment https://forums.phpfreaks.com/topic/3961-order-by-isnt-working-please-help/#findComment-13824 Share on other sites More sharing options...
wickning1 Posted March 3, 2006 Share Posted March 3, 2006 Yep Quote Link to comment https://forums.phpfreaks.com/topic/3961-order-by-isnt-working-please-help/#findComment-13831 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.