vForums Support Banner



 

Welcome Guest! Please Login or Register
vForums Support :: Programming & Coding :: Programming Discussion :: Pagination Help :-/ - View Topic
Topic Rating: *****
Printable View
dog199200
Guest
Pagination Help :-/ (3rd Feb 10 at 11:52am UTC)
OK i have been working on a store system for a site of mine for a few days now and I have been working out one problems after the next and luckily I have only a few bugs left to work out and everything will work perfectly fine. One of the remaining problems is the pagination system I have setup. It works great for the most part, but when i try to call info from my database it prints it out into a single row and I honestly have no idea at all how do break it down into 3 columns of items with 3 items per column, like so:

<------>
1 | 2 | 3
4 | 5 | 6
7 | 8 | 9
<------>

instead its printing out like:

<---------------------------->
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
<---------------------------->

And in printing like that, it hides most of the content cause it doesn't fit within the boundaries of my site. So can someone please help me out? Here is the code I am currently use, or at least the part of it that has any relevance to the request.

Code:
 
  1. <?php
  2. include(CART_DIR."SimpleImage.php");
  3. $image = new SimpleImage();
  4. include(CART_DIR."ps_pagination.php");
  5.  
  6. echo "<table border=\"0\" cellpadding=\"20\" cellspacing=\"0\" align=center width='100%'><tr>";
  7.     
  8. $sql = 'SELECT * FROM products';
  9. $pager = new PS_Pagination($db, $sql, 3, 5);
  10.  
  11. $rs = $pager->paginate();
  12.     
  13. while($row = mysql_fetch_assoc($rs)) {
  14.     $image->load(CART_DIR."gallery/$row[image].jpg");
  15.     $image->resize(100,100);
  16.     $image->save(CART_DIR."gallery/$row[image]2.jpg");
  17.  
  18.     list($width, $height, $type, $attr) = getimagesize(CART_DIR."gallery/$row[image].jpg");
  19.   
  20.     if(preg_match("/group(4)([^\d]|$)/", $user_info['group'])) {
  21.         $cost = $row[cost] - ($row[cost] * 0.10);
  22.     } else {
  23.         $cost = $row[cost];
  24.     }
  25.   
  26.     echo "<td>
  27.         <img src=\"".CART_DIR."gallery/$row[image]2.jpg\" border=0>
  28.         <br />Name: $row[title]
  29.         <br />Type: $row[product_type]
  30.         <br />Cost: <b> $cost C</b>
  31.         <br /><a href=\"http://www.divineshadowsonline.com/store/includes/desc.php\" target=\"_blank\" onClick=\"window.open(this.href, this.target, 'width=300px,height=100px'); return false;\">View Description</a><br /><br />
  32.                   
  33.         <form method=post action=\"index.php\" name=\"frm_add_to_cart\">
  34.         <input type=hidden name=\"product_id\" value=\"$row[id]\">
  35.         <input type=hidden name=\"session_id\" value=\"$_SERVER[REMOTE_ADDR]\">
  36.         <input type=hidden name=\"title\" value=\"$row[title]\">
  37.         <input type=hidden name=\"cost\" value=\"$cost\">
  38.         <input type=hidden name=\"product_type\" value=\"$row[product_type]\">";
  39.  
  40.         if($logged_in) {
  41.             echo "<input type=submit value=\"Add to cart\" name=\"add_to_cart\" class=\"atc\"></form>";
  42.         }
  43.         echo "</td>";
  44.     }
  45.     
  46. echo "</tr></table>";
  47. echo "<center>"  .  $pager->renderFullNav() . "</center>";
  48. ?>
 


Any help at all would be greatly appreciated, even if its giving me the general idea on how to do it.




Here is my latest attempt, I think its at least a lot closer then I was before:

Code:
 
  1. <?php
  2. require("../includes/Database.class.php");
  3. $db = new Database($DBhost, $DBuser, $DBpass, $DBname, $DBpre);
  4. $db->connect();
  5.  
  6. $inventory = "SELECT product_id, session_id, title, cost FROM stock WHERE session_id LIKE '". $db->escape($_SERVER[REMOTE_ADDR]) ."%' ORDER BY product_id DESC";
  7. $inventorycount = $db->query($inventory);
  8. $num_rows = mysql_num_rows($inventorycount);
  9.  
  10. if($_POST[add_to_cart]) {
  11.     $data['product_id'] = $_POST[product_id];
  12.     $data['session_id'] = $_POST[session_id];
  13.     $data['title'] = $_POST[title];
  14.     $data['cost'] = $_POST[cost];
  15.     $data['product_type'] = $_POST[product_type];
  16.     $db->query_insert("stock", $data);
  17.     header( 'refresh: 0' );
  18. }
  19.  
  20. $total = 0;
  21. $sql0 = "SELECT id, product_id, session_id, title, cost, product_type FROM stock WHERE session_id LIKE '". $db->escape($_SERVER[REMOTE_ADDR]) ."%' ORDER BY product_id DESC";
  22. $countRows0 = $db->query($sql0);
  23.  
  24. while ($countRow0 = $db->fetch_array($countRows0)) {
  25.     $total = $total+$countRow0[cost];
  26.     $products = $countRow0[product_id];
  27.     $title = $$countRow0[title];
  28. }
  29.  
  30. include(CART_DIR."SimpleImage.php");
  31. $image = new SimpleImage();
  32. include(CART_DIR."ps_pagination.php");
  33.     
  34. $sql = 'SELECT * FROM products';
  35. $pager = new PS_Pagination($db, $sql, 9, 5);
  36.  
  37. $rs = $pager->paginate();
  38.     
  39. echo "<table border=\"0\" cellpadding=\"20\" cellspacing=\"0\" align=center width='100%'>";
  40.  
  41. while($row = mysql_fetch_assoc($rs)) {
  42.  
  43.     $image->load(CART_DIR."gallery/$row[image].jpg");
  44.     $image->resize(100,100);
  45.     $image->save(CART_DIR."gallery/$row[image]2.jpg");
  46.  
  47.     list($width, $height, $type, $attr) = getimagesize(CART_DIR."gallery/$row[image].jpg");
  48.   
  49.     if(preg_match("/group(4)([^\d]|$)/", $user_info['group'])) {
  50.         $cost = $row[cost] - ($row[cost] * 0.10);
  51.     } else {
  52.         $cost = $row[cost];
  53.     }
  54.     
  55.     echo"<tr>";
  56.     while($row = mysql_fetch_assoc($rs)) {
  57.     
  58.         echo "<td>
  59.             <img src=\"".CART_DIR."gallery/$row[image]2.jpg\" border=0>
  60.             <br />Name: $row[title]
  61.             <br />Type: $row[product_type]
  62.             <br />Cost: <b> $cost C</b>
  63.             <br /><a href=\"http://www.divineshadowsonline.com/store/includes/desc.php\" target=\"_blank\" onClick=\"window.open(this.href, this.target, 'width=300px,height=100px'); return false;\">View Description</a><br /><br />
  64.                   
  65.             <form method=post action=\"index.php\" name=\"frm_add_to_cart\">
  66.             <input type=hidden name=\"product_id\" value=\"$row[id]\">
  67.             <input type=hidden name=\"session_id\" value=\"$_SERVER[REMOTE_ADDR]\">
  68.             <input type=hidden name=\"title\" value=\"$row[title]\">
  69.             <input type=hidden name=\"cost\" value=\"$cost\">
  70.             <input type=hidden name=\"product_type\" value=\"$row[product_type]\">";
  71.  
  72.             if($logged_in) {
  73.                 echo "<input type=submit value=\"Add to cart\" name=\"add_to_cart\" class=\"atc\"></form>";
  74.             }
  75.         echo "</td>";
  76.  
  77.     }
  78.     echo "</tr>";
  79. }
  80.     
  81. echo "</table>";
  82.  
  83. echo "<center>"  .  $pager->renderFullNav() . "</center>";
  84.  
  85. $db->close();
  86. ?>
 



Michael
Moderator
*****

[Avatar]
Recoding the future

Posts: 4,043
Status: Offline
Gender: Male
Location: UK
Joined:  

Additional Groups:
Coding Team
***


Reputation: 58%  


pmtwittervForum
Re: Pagination Help :-/ (3rd Feb 10 at 5:15pm UTC)
on my phone atm so can't read the code. When you loop through information obtained with the query, have an integer increment. If it is a certain number then output br / {Smile}
dog199200
Guest
Re: Pagination Help :-/ (3rd Feb 10 at 11:38pm UTC)
You'll have to wait to see the code, I'm not sure if that would work with how I have the loop setup
Aiken
Full Member
***

[Avatar]

Posts: 333
Status: Offline
Gender: Male
Location: In a very lonely place, the domain of despair
Age: 35
Joined:  
Reputation: 12%  


pmtwitterskypemsnyahooxfire
Re: Pagination Help :-/ (3rd Feb 10 at 11:43pm UTC)
To add on to what he just said, since it is every 3 then use something like:

Code:
 
  1. <?php
  2. if (($variable % 3) == 0)
  3.   echo "<br />";
  4. ?>
 

vF forums:
Perfect World RP
BLEACH Universe - Under Construction / Testing Phase
Xen Qarna RP - Stage I
Vyren Entar Battle Network RP - Under Construction / Maintenance Mode Enabled
the Final Anime Hideout
dog199200
Guest
Re: Pagination Help :-/ (3rd Feb 10 at 11:54pm UTC)
Thinking about it, that wont work at all with how my loop is setup {Wink} I'm not just calling a single time, i'm looping a whole array of date. and I have it all displayed inside of a table, the think is that if I up the <tr> inside the loop it loops over and over, creating a new line per, but if i keep it outside the loop, everything will show up in the same row no matter what because of the table.

Now i did think of using an increment and if it matches then display the <tr></tr> tags, but the problem is if the Increment isn't matched then there would be no <tr></tr> at all.

So as it stands I need to know how i can use an Increment inside my code in sequence with the <tr></tr> tags, in a place that wont cause problems {Unsure} Its not like I haven't tried XD
Michael
Moderator
*****

[Avatar]
Recoding the future

Posts: 4,043
Status: Offline
Gender: Male
Location: UK
Joined:  

Additional Groups:
Coding Team
***


Reputation: 58%  


pmtwittervForum
Re: Pagination Help :-/ (4th Feb 10 at 3:19am UTC)
Instead of <br /> output </tr><tr>Then keep the <tr> before the loop and a </tr> after.
Should work fine.

Failing that, post the loop please! {Smile}
dog199200
Guest
Re: Pagination Help :-/ (4th Feb 10 at 6:05am UTC)
OK before I do that I came up with something and I want someone honest opinion on it please:

http://www.divineshadowsonline.com/store/index.php

How does the shop look so far? Look to the right where it says Inventory to view how the shop looks as is it now. I modified it a bit to see how it look and would like someones opinion what looks better, as it is now (only displays 4 to prevent major stretching), or would it look better showing 9, each one being stacked up in the format seen in the New Store Item area: http://www.divineshadowsonline.com


I am personally torn between the two, displaying 9 in rows and columns would display more so there would be less pages, but it would also display less information, but doing the stack of 4 provides more info, but at the same time it means there would be more pages. I also have no idea how i can go about setting up part of the url as a variable on how many entries to display so the user can set how many they want to show {Unsure} i'm thinking about just adding in a dropdown manu {Unsure}
Michael
Moderator
*****

[Avatar]
Recoding the future

Posts: 4,043
Status: Offline
Gender: Male
Location: UK
Joined:  

Additional Groups:
Coding Team
***


Reputation: 58%  


pmtwittervForum
Re: Pagination Help :-/ (4th Feb 10 at 12:55pm UTC)
You can use GET to get how many per page... something like &ppage=50

or something {Smile}
dog199200
Guest
Re: Pagination Help :-/ (4th Feb 10 at 1:00pm UTC)
{Tongue Out} i figured that, but i mean I don't know how to take the get value {Unsure} if i was able to assign it to a variable I could then use the variable inside the display per page, but would also need to find a way to set a default.

Anyways do you think stacking it how I have looks good, or does it look tacky?
Michael
Moderator
*****

[Avatar]
Recoding the future

Posts: 4,043
Status: Offline
Gender: Male
Location: UK
Joined:  

Additional Groups:
Coding Team
***


Reputation: 58%  


pmtwittervForum
Re: Pagination Help :-/ (4th Feb 10 at 1:14pm UTC)
The way you have it currently looks good.

You use $_GET['ppage'].

I'd do something like:
if(isset($_GET['ppage']) && $_GET['ppage'] > 20)
     $per_page = $_GET['ppage'];
else
     $per_page = 20;


That code checks if ppage is in the url, if it is, it returns the amount assigned after the equals. Checks if it's larger than 20, if it's either Not set in the URL, or is less than 20, then it sets the default per page to 20.

{Smile}
dog199200
Guest
Re: Pagination Help :-/ (4th Feb 10 at 1:58pm UTC)
ok i think i can get it to work, the thing is that the page is set in this: $pager = new PS_Pagination($db, $sql, 3, 5); to be exact its the part in red, so i'd have to do a bit of improvising.

Ok thanks that helped a lot, not i need to see about fixing my paging system {Unsure} it removes it after going to another page XD

Edit: Ok i have a problem {Unsure} my pagination source is causing a problem, its removed the GET functions that area added, and i can admit i don't know enough to understand what some fo the stuff in the source means in order to edit it, so when someone has time can someone please edit this code so it allows other Get tags:

Code:
 
  1. <?php
  2. class PS_Pagination {
  3.     var $php_self;
  4.     var $rows_per_page; //Number of records to display per page
  5.     var $total_rows; //Total number of rows returned by the query
  6.     var $links_per_page; //Number of links to display per page
  7.     var $sql;
  8.     var $debug;
  9.     var $conn;
  10.     var $page;
  11.     var $max_pages;
  12.     var $offset;
  13.     
  14.     /**
  15.      * Constructor
  16.      *
  17.      * @param resource $connection Mysql connection link
  18.      * @param string $sql SQL query to paginate. Example : SELECT * FROM users
  19.      * @param integer $rows_per_page Number of records to display per page. Defaults to 10
  20.      * @param integer $links_per_page Number of links to display per page. Defaults to 5
  21.      */
  22.      
  23.     function PS_Pagination($connection, $sql, $rows_per_page = 10, $links_per_page = 5) {
  24.         $this->conn = $connection;
  25.         $this->sql = $sql;
  26.         $this->rows_per_page = $rows_per_page;
  27.         $this->links_per_page = $links_per_page;
  28.         $this->php_self = htmlspecialchars($_SERVER['PHP_SELF']);
  29.         if(isset($_GET['page'])) {
  30.             $this->page = intval($_GET['page']);
  31.         }
  32.     }
  33.     
  34.     /**
  35.      * Executes the SQL query and initializes internal variables
  36.      *
  37.      * @access public
  38.      * @return resource
  39.      */
  40.     function paginate() {
  41.         if(!$this->conn) {
  42.             if($this->debug) echo "MySQL connection missing<br />";
  43.             return false;
  44.         }
  45.         
  46.         $all_rs = @mysql_query($this->sql);
  47.         if(!$all_rs) {
  48.             if($this->debug) echo "SQL query failed. Check your query.<br />";
  49.             return false;
  50.         }
  51.         $this->total_rows = mysql_num_rows($all_rs);
  52.         @mysql_close($all_rs);
  53.         
  54.         $this->max_pages = ceil($this->total_rows/$this->rows_per_page);
  55.         //Check the page value just in case someone is trying to input an aribitrary value
  56.         if($this->page > $this->max_pages || $this->page <= 0) {
  57.             $this->page = 1;
  58.         }
  59.         
  60.         //Calculate Offset
  61.         $this->offset = $this->rows_per_page * ($this->page-1);
  62.         
  63.         //Fetch the required result set
  64.         $rs = @mysql_query($this->sql." LIMIT {$this->offset}, {$this->rows_per_page}");
  65.         if(!$rs) {
  66.             if($this->debug) echo "Pagination query failed. Check your query.<br />";
  67.             return false;
  68.         }
  69.         return $rs;
  70.     }
  71.     
  72.     /**
  73.      * Display the link to the first page
  74.      *
  75.      * @access public
  76.      * @param string $tag Text string to be displayed as the link. Defaults to 'First'
  77.      * @return string
  78.      */
  79.     function renderFirst($tag='First') {
  80.         if($this->page == 1) {
  81.             return $tag;
  82.         }
  83.         else {
  84.             return '<a href="'.$this->php_self.'?page=1">'.$tag.'</a>';
  85.         }
  86.     }
  87.     
  88.     /**
  89.      * Display the link to the last page
  90.      *
  91.      * @access public
  92.      * @param string $tag Text string to be displayed as the link. Defaults to 'Last'
  93.      * @return string
  94.      */
  95.     function renderLast($tag='Last') {
  96.         if($this->page == $this->max_pages) {
  97.             return $tag;
  98.         }
  99.         else {
  100.             return '<a href="'.$this->php_self.'?page='.$this->max_pages.'">'.$tag.'</a>';
  101.         }
  102.     }
  103.     
  104.     /**
  105.      * Display the next link
  106.      *
  107.      * @access public
  108.      * @param string $tag Text string to be displayed as the link. Defaults to '>>'
  109.      * @return string
  110.      */
  111.     function renderNext($tag=' >>') {
  112.         if($this->page < $this->max_pages) {
  113.             return '<a href="'.$this->php_self.'?page='.($this->page+1).'">'.$tag.'</a>';
  114.         }
  115.         else {
  116.             return $tag;
  117.         }
  118.     }
  119.     
  120.     /**
  121.      * Display the previous link
  122.      *
  123.      * @access public
  124.      * @param string $tag Text string to be displayed as the link. Defaults to '<<'
  125.      * @return string
  126.      */
  127.     function renderPrev($tag='<<') {
  128.         if($this->page > 1) {
  129.             return '<a href="'.$this->php_self.'?page='.($this->page-1).'">'.$tag.'</a>';
  130.         }
  131.         else {
  132.             return $tag;
  133.         }
  134.     }
  135.     
  136.     /**
  137.      * Display the page links
  138.      *
  139.      * @access public
  140.      * @return string
  141.      */
  142.     function renderNav() {
  143.         for($i=1;$i<=$this->max_pages;$i+=$this->links_per_page) {
  144.             if($this->page >= $i) {
  145.                 $start = $i;
  146.             }
  147.         }
  148.         
  149.         if($this->max_pages > $this->links_per_page) {
  150.             $end = $start+$this->links_per_page;
  151.             if($end > $this->max_pages) $end = $this->max_pages+1;
  152.         }
  153.         else {
  154.             $end = $this->max_pages;
  155.         }
  156.             
  157.         $links = '';
  158.         
  159.         for( $i=$start ; $i<$end ; $i++) {
  160.             if($i == $this->page) {
  161.                 $links .= " $i ";
  162.             }
  163.             else {
  164.                 $links .= ' <a href="'.$this->php_self.'?page='.$i.'">'.$i.'</a> ';
  165.             }
  166.         }
  167.         
  168.         return $links;
  169.     }
  170.     
  171.     /**
  172.      * Display full pagination navigation
  173.      *
  174.      * @access public
  175.      * @return string
  176.      */
  177.     function renderFullNav() {
  178.         return $this->renderFirst().' '.$this->renderPrev().' '.$this->renderNav().' '.$this->renderNext().' '.$this->renderLast();    
  179.     }
  180.     
  181.     /**
  182.      * Set debug mode
  183.      *
  184.      * @access public
  185.      * @param bool $debug Set to TRUE to enable debug messages
  186.      * @return void
  187.      */
  188.     function setDebug($debug) {
  189.         $this->debug = $debug;
  190.     }
  191. }
  192. ?>
 


The pages for example that use the GET function are my sort pages:

http://www.divineshadowsonline.com/store/inventory.php?sort=all
http://www.divineshadowsonline.com/store/inventory.php?sort=weapons
http://www.divineshadowsonline.com/store/inventory.php?sort=armor
http://www.divineshadowsonline.com/store/inventory.php?sort=skills
http://www.divineshadowsonline.com/store/inventory.php?sort=enhancements

but when using this:

http://www.divineshadowsonline.com/store/inventory.php?sort=all&ppage=1

if you go and turn the page it turn the url into http://www.divineshadowsonline.com/store/inventory.php?page=2 but since i have the inventory.php redirecting to inventory.php?sort=all it just outputs the page as inventory.php?sort=all. (I have a redirect on inventory.php cause its the source of ?sort= on all inventory pages)
 Printable View

All times are GMT+0 :: The current time is 6:19am
Page generated in 1.3857 seconds
This Forum is Powered By vForums (v2.4)
Create a Forum for Free | Find Forums