Jump to content

b455m4573r

New Members
  • Posts

    5
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

b455m4573r's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Hey Everyone, So I have cleaned up my code a bit, but now I'm running into a whole new mess of issues. Well, just one really. When I click the category links to sort the while loop echo based on the category in the database it works fine. But when I click on "next page" it just goes to the 2nd page of all the products, not just the ones in the category. Let me explain it a different way. When I open the page, it shows all items in the database, with all the pageination working properly, when I click the link to sort them by category, that works as well, but when I click the next button while in the category to go to the next page, it goes to the second page of all items. Once again sorry about the messy code, I feel like I'm missing a conditional statement for when I click the category, but I don't know how to approach it. If you want to see the full code...http://pastebin.com/embed.php?i=0cFf6QkH <?php require_once("includes/connect.php"); if(isset($_GET['category'])) { $cat = mysql_real_escape_string($_GET['category']); $base_query = "SELECT guns_id,guns_brand,guns_model,guns_img,guns_price FROM tbl_guns WHERE guns_cat = '".$cat."' ORDER BY guns_id DESC"; }else{ $base_query = "SELECT guns_id,guns_brand,guns_model,guns_img,guns_price FROM tbl_guns ORDER BY guns_id DESC"; } $result = mysql_query($base_query); $total = mysql_num_rows($result); $rowsperpage = 3; if(isset($_GET['page'])) { $pagenum = $_GET['page']; }else{ $pagenum=1; } $offset = ($pagenum - 1) * $rowsperpage; $maxpage = ceil($total/$rowsperpage); $self = $_SERVER['PHP_SELF']; $query = "$base_query LIMIT $offset,$rowsperpage"; $result = mysql_query($query); if ($pagenum > 1) { $page = $pagenum - 1; $prev = "<a href=\"".$self."?page=".$page."\">prev</a>"; $first = " <a href=\"".$self."?page=1\">first</a> "; }else{ $prev = " "; $first = " "; } if ($pagenum < $maxpage) { $page = $pagenum + 1; $next = "<a href=\"".$self."?page=".$page."\">next</a>"; $last = " <a href=\"".$self."?page=".$maxpage."\">last</a>"; }else{ $next = " "; $last = " "; } ?> </div> <div id="productsInfo"> <?php while($row = mysql_fetch_array($result)) { echo "<div class=\"productIcon\"> <a href=\"productdetails.php?id=".$row['guns_id']."\"> <img class=\"thumbReSize\" src=\"images/".$row['guns_img']."\" /> ".$row['guns_brand']. "<br />".$row['guns_model']. "<br />$".$row['guns_price']. "</a> </div>"; } ?> <div id="paginationContainer"><?php echo $first." ".$prev." Page ".$pagenum." of ".$maxpage." pages ".$next." ".$last; ?> </div>
  2. Thanks PFMaBiSmAd! That solved the sorting based on the query, but if there isn't enough items to need pagination is coughs out these huge errors because the $prev = "<a href=\"".$self."?page=".$page."\">prev</a>"; Is no longer needed right? I guess I would need to write a conditional for that variable? Also, I would like to apologize for my lack knowledge and general neediness. I'm trying to grasp this.
  3. Thanks for the reply, So litebearer you purpose that I change $Fixit = mysql_query($qstuff); To something like $Fixit = $qstuff; and run the query later all at once?
  4. Hey, Thanks for the speedy reply. I tried that chunk of code you provided and the mysql_error spat out this "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #5,0,2' at line 1"
  5. Hey PHP Freaks, First of all I want to express my sincerest gratitude to whoever helps me solve this problem. I'm new to PHP, I started about 2 weeks ago. This is for a products page I was writing. The client updates his files which populates the SQL Database, then the PHP writes all the icons out. I thought pagination would make it look a little cleaner. But I didn't think it would be this much work. The conditional statement at the top is for when the user wants to sort the products by category (It's for a gun shop, so its categorized my centerfire, rimfire, ect...), if not it just posts everything. The error I get is... Warning: mysql_fetch_array() expects parameter 1 to be resource, string given in C:\wamp\www\Guns N' Gadgets Site\Products.php on line 151 Call Stack # Time Memory Function Location 1 0.0009 698840 {main}( ) ..\Products.php:0 2 0.0060 713416 mysql_fetch_array ( ) ..\Products.php:151 Line 151 is the while loop. Here is the code. <?php //error_reporting(0); require_once("includes/connect.php"); if(isset($_GET['category'])) { $cat = $_GET['category']; $qstuff = "SELECT guns_id,guns_brand,guns_model,guns_img,guns_price FROM tbl_guns WHERE guns_cat = '".$cat."' ORDER BY guns_id DESC"; $Fixit = mysql_query($qstuff); }else{ $qstuff = "SELECT guns_id,guns_brand,guns_model,guns_img,guns_price FROM tbl_guns ORDER BY guns_id DESC"; $Fixit = mysql_query($qstuff); } $rowsperpage = 2; if(isset($_GET['page'])) { $pagenum = $_GET['page']; }else{ $pagenum=1; } $offset = ($pagenum - 1) * $rowsperpage; $totalnum = mysql_query("SELECT guns_id FROM tbl_guns"); $total = mysql_num_rows($totalnum); $maxpage = ceil($total/$rowsperpage); $self = $_SERVER['PHP_SELF']; $qstring = $Fixit.",".$offset.",".$rowsperpage; $results = $qstring; if($total > $rowsperpage) { if ($pagenum > 1) { $page = $pagenum - 1; $prev = "<a href=\"".$self."?page=".$page."\">prev</a>"; $first = " <a href=\"".$self."?page=1\">first</a> "; }else{ $prev = " "; $first = " "; } if ($pagenum < $maxpage) { $page = $pagenum + 1; $next = "<a href=\"".$self."?page=".$page."\">next</a>"; $last = " <a href=\"".$self."?page=".$maxpage."\">last</a>"; }else{ $next = " "; $last = " "; } } ?> <div id="productsInfo"> <?php while($row = mysql_fetch_array($results)){ echo "<div class=\"productIcon\"><a href=\"productdetails.php?id=".$row['guns_id']."\"><img class=\"thumbReSize\" src=\"images/".$row['guns_img']."\" />".$row['guns_brand']."<br />".$row['guns_model']."<br />$".$row['guns_price']."</a></div>"; } ?> <div id="paginationContainer"><?php echo $first." ".$prev." Page ".$pagenum." of ".$maxpage." pages ".$next." ".$last; ?></div> Thanks again.
×
×
  • 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.