Jump to content

Recommended Posts

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.

 

// 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;
                       


[a href=\"http://root.danielforsyth.com/mysql/image_upload/view5.php?category=hand\" target=\"_blank\"]http://

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 - thanks

Link to comment
https://forums.phpfreaks.com/topic/221407-order-by-isnt-working/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.