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:
- <?php
- include(CART_DIR."SimpleImage.php");
- $image = new SimpleImage();
- include(CART_DIR."ps_pagination.php");
- echo "<table border=\"0\" cellpadding=\"20\" cellspacing=\"0\" align=center width='100%'><tr>";
- $sql = 'SELECT * FROM products';
- $pager = new PS_Pagination($db, $sql, 3, 5);
- $rs = $pager->paginate();
- while($row = mysql_fetch_assoc($rs)) {
- $image->load(CART_DIR."gallery/$row[image].jpg");
- $image->resize(100,100);
- $image->save(CART_DIR."gallery/$row[image]2.jpg");
- list($width, $height, $type, $attr) = getimagesize(CART_DIR."gallery/$row[image].jpg");
- if(preg_match("/group(4)([^\d]|$)/", $user_info['group'])) {
- $cost = $row[cost] - ($row[cost] * 0.10);
- } else {
- $cost = $row[cost];
- }
- echo "<td>
- <img src=\"".CART_DIR."gallery/$row[image]2.jpg\" border=0>
- <br />Name: $row[title]
- <br />Type: $row[product_type]
- <br />Cost: <b> $cost C</b>
- <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 />
- <form method=post action=\"index.php\" name=\"frm_add_to_cart\">
- <input type=hidden name=\"product_id\" value=\"$row[id]\">
- <input type=hidden name=\"session_id\" value=\"$_SERVER[REMOTE_ADDR]\">
- <input type=hidden name=\"title\" value=\"$row[title]\">
- <input type=hidden name=\"cost\" value=\"$cost\">
- <input type=hidden name=\"product_type\" value=\"$row[product_type]\">";
- if($logged_in) {
- echo "<input type=submit value=\"Add to cart\" name=\"add_to_cart\" class=\"atc\"></form>";
- }
- echo "</td>";
- }
- echo "</tr></table>";
- echo "<center>" . $pager->renderFullNav() . "</center>";
- ?>
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:
- <?php
- require("../includes/Database.class.php");
- $db = new Database($DBhost, $DBuser, $DBpass, $DBname, $DBpre);
- $db->connect();
- $inventory = "SELECT product_id, session_id, title, cost FROM stock WHERE session_id LIKE '". $db->escape($_SERVER[REMOTE_ADDR]) ."%' ORDER BY product_id DESC";
- $inventorycount = $db->query($inventory);
- $num_rows = mysql_num_rows($inventorycount);
- if($_POST[add_to_cart]) {
- $data['product_id'] = $_POST[product_id];
- $data['session_id'] = $_POST[session_id];
- $data['title'] = $_POST[title];
- $data['cost'] = $_POST[cost];
- $data['product_type'] = $_POST[product_type];
- $db->query_insert("stock", $data);
- header( 'refresh: 0' );
- }
- $total = 0;
- $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";
- $countRows0 = $db->query($sql0);
- while ($countRow0 = $db->fetch_array($countRows0)) {
- $total = $total+$countRow0[cost];
- $products = $countRow0[product_id];
- $title = $$countRow0[title];
- }
- include(CART_DIR."SimpleImage.php");
- $image = new SimpleImage();
- include(CART_DIR."ps_pagination.php");
- $sql = 'SELECT * FROM products';
- $pager = new PS_Pagination($db, $sql, 9, 5);
- $rs = $pager->paginate();
- echo "<table border=\"0\" cellpadding=\"20\" cellspacing=\"0\" align=center width='100%'>";
- while($row = mysql_fetch_assoc($rs)) {
- $image->load(CART_DIR."gallery/$row[image].jpg");
- $image->resize(100,100);
- $image->save(CART_DIR."gallery/$row[image]2.jpg");
- list($width, $height, $type, $attr) = getimagesize(CART_DIR."gallery/$row[image].jpg");
- if(preg_match("/group(4)([^\d]|$)/", $user_info['group'])) {
- $cost = $row[cost] - ($row[cost] * 0.10);
- } else {
- $cost = $row[cost];
- }
- echo"<tr>";
- while($row = mysql_fetch_assoc($rs)) {
- echo "<td>
- <img src=\"".CART_DIR."gallery/$row[image]2.jpg\" border=0>
- <br />Name: $row[title]
- <br />Type: $row[product_type]
- <br />Cost: <b> $cost C</b>
- <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 />
- <form method=post action=\"index.php\" name=\"frm_add_to_cart\">
- <input type=hidden name=\"product_id\" value=\"$row[id]\">
- <input type=hidden name=\"session_id\" value=\"$_SERVER[REMOTE_ADDR]\">
- <input type=hidden name=\"title\" value=\"$row[title]\">
- <input type=hidden name=\"cost\" value=\"$cost\">
- <input type=hidden name=\"product_type\" value=\"$row[product_type]\">";
- if($logged_in) {
- echo "<input type=submit value=\"Add to cart\" name=\"add_to_cart\" class=\"atc\"></form>";
- }
- echo "</td>";
- }
- echo "</tr>";
- }
- echo "</table>";
- echo "<center>" . $pager->renderFullNav() . "</center>";
- $db->close();
- ?>
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 /![]()
You'll have to wait to see the code, I'm not sure if that would work with how I have the loop setup
To add on to what he just said, since it is every 3 then use something like:
Thinking about it, that wont work at all with how my loop is setupI'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 problemsIts not like I haven't tried XD
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!![]()
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 showi'm thinking about just adding in a dropdown manu
![]()
You can use GET to get how many per page... something like &ppage=50
or something![]()
i figured that, but i mean I don't know how to take the get value
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?
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.
![]()
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 systemit removes it after going to another page XD
Edit: Ok i have a problemmy 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:
- <?php
- class PS_Pagination {
- var $php_self;
- var $rows_per_page; //Number of records to display per page
- var $total_rows; //Total number of rows returned by the query
- var $links_per_page; //Number of links to display per page
- var $sql;
- var $debug;
- var $conn;
- var $page;
- var $max_pages;
- var $offset;
- /**
- * Constructor
- *
- * @param resource $connection Mysql connection link
- * @param string $sql SQL query to paginate. Example : SELECT * FROM users
- * @param integer $rows_per_page Number of records to display per page. Defaults to 10
- * @param integer $links_per_page Number of links to display per page. Defaults to 5
- */
- function PS_Pagination($connection, $sql, $rows_per_page = 10, $links_per_page = 5) {
- $this->conn = $connection;
- $this->sql = $sql;
- $this->rows_per_page = $rows_per_page;
- $this->links_per_page = $links_per_page;
- $this->php_self = htmlspecialchars($_SERVER['PHP_SELF']);
- if(isset($_GET['page'])) {
- $this->page = intval($_GET['page']);
- }
- }
- /**
- * Executes the SQL query and initializes internal variables
- *
- * @access public
- * @return resource
- */
- function paginate() {
- if(!$this->conn) {
- if($this->debug) echo "MySQL connection missing<br />";
- return false;
- }
- $all_rs = @mysql_query($this->sql);
- if(!$all_rs) {
- if($this->debug) echo "SQL query failed. Check your query.<br />";
- return false;
- }
- $this->total_rows = mysql_num_rows($all_rs);
- @mysql_close($all_rs);
- $this->max_pages = ceil($this->total_rows/$this->rows_per_page);
- //Check the page value just in case someone is trying to input an aribitrary value
- if($this->page > $this->max_pages || $this->page <= 0) {
- $this->page = 1;
- }
- //Calculate Offset
- $this->offset = $this->rows_per_page * ($this->page-1);
- //Fetch the required result set
- $rs = @mysql_query($this->sql." LIMIT {$this->offset}, {$this->rows_per_page}");
- if(!$rs) {
- if($this->debug) echo "Pagination query failed. Check your query.<br />";
- return false;
- }
- return $rs;
- }
- /**
- * Display the link to the first page
- *
- * @access public
- * @param string $tag Text string to be displayed as the link. Defaults to 'First'
- * @return string
- */
- function renderFirst($tag='First') {
- if($this->page == 1) {
- return $tag;
- }
- else {
- return '<a href="'.$this->php_self.'?page=1">'.$tag.'</a>';
- }
- }
- /**
- * Display the link to the last page
- *
- * @access public
- * @param string $tag Text string to be displayed as the link. Defaults to 'Last'
- * @return string
- */
- function renderLast($tag='Last') {
- if($this->page == $this->max_pages) {
- return $tag;
- }
- else {
- return '<a href="'.$this->php_self.'?page='.$this->max_pages.'">'.$tag.'</a>';
- }
- }
- /**
- * Display the next link
- *
- * @access public
- * @param string $tag Text string to be displayed as the link. Defaults to '>>'
- * @return string
- */
- function renderNext($tag=' >>') {
- if($this->page < $this->max_pages) {
- return '<a href="'.$this->php_self.'?page='.($this->page+1).'">'.$tag.'</a>';
- }
- else {
- return $tag;
- }
- }
- /**
- * Display the previous link
- *
- * @access public
- * @param string $tag Text string to be displayed as the link. Defaults to '<<'
- * @return string
- */
- function renderPrev($tag='<<') {
- if($this->page > 1) {
- return '<a href="'.$this->php_self.'?page='.($this->page-1).'">'.$tag.'</a>';
- }
- else {
- return $tag;
- }
- }
- /**
- * Display the page links
- *
- * @access public
- * @return string
- */
- function renderNav() {
- for($i=1;$i<=$this->max_pages;$i+=$this->links_per_page) {
- if($this->page >= $i) {
- $start = $i;
- }
- }
- if($this->max_pages > $this->links_per_page) {
- $end = $start+$this->links_per_page;
- if($end > $this->max_pages) $end = $this->max_pages+1;
- }
- else {
- $end = $this->max_pages;
- }
- $links = '';
- for( $i=$start ; $i<$end ; $i++) {
- if($i == $this->page) {
- $links .= " $i ";
- }
- else {
- $links .= ' <a href="'.$this->php_self.'?page='.$i.'">'.$i.'</a> ';
- }
- }
- return $links;
- }
- /**
- * Display full pagination navigation
- *
- * @access public
- * @return string
- */
- function renderFullNav() {
- return $this->renderFirst().' '.$this->renderPrev().' '.$this->renderNav().' '.$this->renderNext().' '.$this->renderLast();
- }
- /**
- * Set debug mode
- *
- * @access public
- * @param bool $debug Set to TRUE to enable debug messages
- * @return void
- */
- function setDebug($debug) {
- $this->debug = $debug;
- }
- }
- ?>
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)