PHP - Paging A List (divide To Separate Pages)
I have a list generated from an array by foreach as
Code: [Select] foreach ($list_array as $item) { echo "$item<br />"; } This is a long list, how can I separate the list to different pages? I mean the simplest way to do so Similar TutorialsThis topic has been moved to PHP Applications. http://www.phpfreaks.com/forums/index.php?topic=356195.0 Hello everyone, i'm trying to get the following scenario done: I've got a shop system which is kind of a cms and shop in one thing but that's not really important here, now there's some kind of vouchers which can be used in my shop to fund the account balance aka "Credits", now everything works fine, my script checks the voucher for validity and balance and the user gets the voucher balance credited as his account balance, in short: User1 uses Voucher with amount 10$ Script sets User1 Shop-Credits/Balance to 10$ Now what i want to achieve is, that the user get's only 50% of the original voucher amount credited to his shop-credits/balance basically just divide the voucher amount by 2 before it get's credited to the account. My code to write/update the credits in mySQL looks like this: $query2 = "UPDATE `shop_user` SET `credits` = `credits`+'".$psc->value."' WHERE `userid` = '".$_SESSION["userid"]."' AND `username` = '".$_SESSION["username"]."'"; Now this .$psc->value. is the part where it checks for the value first and then sets the same amount as "credits" in the shop. Now isn't there just some simple piece of code that would let me divide this by 2, which would be 50% obviously. something like .$psc->value /2. which obviously doesn't work, but should give you an idea of what i need. Any help is greatly appreciated. Sabine Hi everyone. I have this code that suppose to divide the query result 3 columns in a row. Like this : Shoes Hats Jeans Shorts Shirts Belts But for some reason this one doesn't work. It gave me everything in a single row. Like this: Shoes Hats jeans Shorts Shirts Belts. I noticed that before I put this two 2 lines of code, it's working fine. It didn't give me any error messages or anythings. Can someone point me out what did I did wrong? This is two lines I put in: Code: [Select] $vuong_query = tep_db_query("select categories_id from " . TABLE_GENDER_CATEGORIES . " where parent_id = 1"); while ($product_check = tep_db_fetch_array($vuong_query)) { This is the whole piece of code: Code: [Select] <tr> <td><table width = "100%" border="1" bordercolor="maroon"> <tr> <?php $vuong_query = tep_db_query("select categories_id from " . TABLE_GENDER_CATEGORIES . " where parent_id = 1"); while ($product_check = tep_db_fetch_array($vuong_query)) { $categories_querys = tep_db_query("select c.categories_id, cd.categories_name, c.categories_image from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.categories_id = '" . $product_check['categories_id'] . "' and c.categories_id = cd.categories_id and cd.language_id = '" . (int)$languages_id . "' order by sort_order, cd.categories_name"); $number_of_categories = tep_db_num_rows($categories_querys); $rows = 0; while ($categories = tep_db_fetch_array($categories_querys)) { $rows++; $cPath_new = tep_get_path($categories['categories_id']); $width = (int)(100 / MAX_DISPLAY_CATEGORIES_PER_ROW) . '%'; echo ' <td align="center" class="smallText" width="' . $width . '" valign="top"><a href="' . tep_href_link(FILENAME_DEFAULT, $cPath_new) . '">' . tep_image(DIR_WS_IMAGES . $categories['categories_image'], $categories['categories_name'], SUBCATEGORY_IMAGE_WIDTH, SUBCATEGORY_IMAGE_HEIGHT) . '<br>' . $categories['categories_name'] . '</a></td>' . "\n"; if ((($rows / MAX_DISPLAY_CATEGORIES_PER_ROW) == floor($rows / MAX_DISPLAY_CATEGORIES_PER_ROW)) && ($rows != $number_of_categories)) { echo ' </tr>' . "\n"; echo ' <tr>' . "\n"; } } } ?> </tr> </table> </td> </tr> Thanks anyone for helping me out. Vanvoquan What I am looking for is a small script that works in the way that http://www.groupsort.com/ works. This is for a sports organization. Loads of registrations entered into a MySQL database which need to be automatically sorted into classes that have already been created with an age range specified (Class: S1, From: 1997, To: 1999, Sex: Mixed) Whereas there are some classes with the same set of rules, I have no idea of how I should divide the correct amount of children into correct classes. Can you help? This topic has been moved to PHP Math Help. http://www.phpfreaks.com/forums/index.php?topic=351485.0 I have a MySQL query that pulls multiple columns with many rows. I want to break each column into its own array. Here is what I have to this point but doesn't seem to be working. Is this the right direction or is there something easier? Thanks! Code: [Select] $query = "SELECT name, address, city, state FROM customers WHERE sku = '12345'"; $result = mysqli_query($dbc, $query) or die(); $name = array(); $address = array(); $city = array(); state = array(); while ($data = mysqli_fetch_assoc($result)) { $name = $data['name']; $address = $data['address']; $city = $data['city']; $state = $data['state']; } I have a list of x,y coordinates. (drill positions) I have Three Primary x,y co-ords. (station positions) I want to assign drill positions to stations based on how close they are and the priority of the station(1, 2 or 3). I also want the option of making all stations have a priority or 1 so they divide equally.(for drill positions that are close to two stations) I have absolutely no idea how to tackle this. Is there a formula anyone is aware of? Can anyone even attempt to show me how to tackle this. I am 100% lost. I see several great PHP/MySQL paging systems on google, but how can I make a paging system when I'm just showing all images in a directory, not showing all images from a database? I have no idea how I could convert one of those mysql systems to work for my needs. Here is my code: <?php $columns = "4"; $count = "0"; echo '<div style="overflow:auto; width:500px; height:266px;"><table cellpadding="0" cellspacing="10">'; if ($handle = opendir('.')) { while (false !== ($image = readdir($handle))) { if ($image != "." && $image != ".." && $image != "index.php") { if (strlen($image) > 11) { $short = substr($image,0,11); echo '<td align="center"><a href="'.$image.'" title="'.$image.'"><img src="'.$image.'" width="100" height="100"><br><div style="overflow:auto; width:100px;">'.$short.'...</div></a></td>'; } else { echo '<td align="center"><a href="'.$image.'" title="'.$image.'"><img src="'.$image.'" width="100" height="100"><br><div style="overflow:auto; width:100px;">'.$image.'</div></a></td>'; } $count++; if ($count == 4) { echo "</tr><tr>"; $count = 0; } } } closedir($handle); } echo "</table></div>"; ?> This code shows all images in a directory, 4 per row. I'd really appreciate some help with adding a paging feature to this. Thank you so much for the help! Hi Everyone, I need help in paging, i have a form with a lot of text boxes and drop down boxes, i need to use paging "Next..1..2..3", Please someone help me with it, i tried some codes but didn't work out. Thanks RAM so my database has a page number next to each field. What I need to know how to do is make it so that the paging number, ie being able to select the page, if greater than one, will display 1-whatever the highest page number is. does this make sense? any help is appreciated. Hello, everyone. I am having some trouble figuring this out. I am trying to create a paging bar(so to speak) to display results by page number. Trying to get the paging bar to go as follows... Requested Page The resulting paging bar Page 1 1 2 3 4 5 Page 2 1 2 3 4 5 Page 3 1 2 3 4 5 Page 4 1 2 3 4 5 Page 5 4 5 6 7 8 Page 6 4 5 6 7 8 Page 7 4 5 6 7 8 Page 8 7 8 9 10 11 ...... This is what I have right now that I am trying to get to work. I have changed this so many times and been working on it for 2 days. I think I'm just confusing my self. This can't be that hard.. Code: [Select] <?php echo '<html>'; //---these values changed based on query---- $Page=$_GET["page"]; //the page number requested $AvailablePages=5; //number of pages available after the page requested //---calculations--- $StartPage=round($Page/5)*5; $EndPage=ceil($StartPage/5)*5 ; //the last page number to be displayed in paging if($StartPage<5) {$StartPage=1;} //just looking at variables echo 'page is: ' . $Page . '<br>'; echo '<br>Starting @: ' . $StartPage; echo '<br>Ending @: ' . $EndPage . '<br>'; //--create the paging bar-- $PagingBar=''; while($StartPage<=$EndPage) { if($Page==$StartPage) { $PagingBar=$PagingBar . $StartPage . ''; } else { $PagingBar=$PagingBar . ' <a href="test.php?page=' . $StartPage . '">' . $StartPage . '</a> '; } $StartPage++; } //---show the paging bar-- echo $PagingBar; echo '</html>'; ?> Dear all, I have code for paging The problem is, one of them do not work properly This is OK http://www.amec-berca.co.id/abi/index.php?action=past-projects This is not OK http://www.parnaraya.net/beta/index.php?link=news My question: Is there any issues about PHP/Apache version? or something else? I used this script very often, and no problem so far except this time FYI, the host server using Virtualmin version 3.89 Thanks Hello everybody,
i have page called cat.php
$cat_name get the data submited from othe .php page see line 77
this page show records WHERE category='$cat_name' see line 78
i have 2 problems:
first problem: if i dont have this "where" condition, the sort function works fine see line 74
second problem: if i dont have this "where" condition, the paging function works fine see line from 108
here is my code
thank you very much for your help
Rafal
<html> <head> <title></title> </head> <body> <?php> include("config.php"); ?> <?php $verbindung1 = mysql_connect("$hoster", "$nameuser", "$password") or die ("connection error"); mysql_select_db("$basedata") or die ("database error"); $abfrage = "SELECT cat_id, cat_name FROM $table3 ORDER BY cat_name ASC"; $ergebnis = mysql_query($abfrage); $num = mysql_num_rows($ergebnis); ?> <table> <tr> <?php while ($row = mysql_fetch_object($ergebnis)) { ?> <td> <font face="Arial" size="2"> <form action="cat.php" method="post" name="cat_form" id="cat_form"> <input type="hidden" name="inp_cat_name" value="<?php echo $row->cat_name; ?>"> <input type="submit" value="<?php echo $row->cat_name; ?>"> </form> </font> </td> <?php } ?> </tr></table> <?php> include("config.php"); ?> <?Php try { $dbo = new PDO('mysql:host=db.abcdef.fr;dbname='.$basedata, $nameuser, $password); } catch (PDOException $e) { print "Error!: " . $e->getMessage() . "<br/>"; die(); } $page_name="cat.php"; // page name @$sort=$_GET['sort']; if(strlen($sort)>0 and !ctype_alnum($sort)) { echo "Data Error"; exit; } @$start=$_GET['start']; if(strlen($start) > 0 and !is_numeric($start)) { echo "Data Error"; exit; } $zero = ($start - 0); $limit = 20; // number of rows per page $this1 = $zero + $limit; $back = $zero - $limit; $next = $zero + $limit; $query2=" SELECT destination, stars, price FROM $table1 WHERE vis='yes'"; $count=$dbo->prepare($query2); $count->execute(); $nume=$count->rowCount(); ?> <font face="Arial" size="2">Sortieren nach:<br> <?php echo "<a href='$page_name?sort=destination'>Zielort</a>"; ?> | <?php echo "<a href='$page_name?sort=stars'>Sterne</a>"; ?> | <?php echo "<a href='$page_name?sort=price'>Preis</a>"; ?><br><br> </font> <?php $cat_name = $_POST["inp_cat_name"]; $query=" SELECT * FROM $table1 WHERE category='$cat_name'"; if(isset($sort) and strlen($sort)>0){ $query = $query . " order by $sort"; } $query = $query. " limit $zero, $limit "; foreach ($dbo->query($query) as $row) { ?> <table cellspacing="0" cellpadding="0" border="1" width="600px" class="table"> <tr> <td rowspan="2" valign="top" class="td"> <img src="<?php if ($row[photo1] != '') { ?><?php echo $row[photo1] ?><?php } ?><?php if ($row[photo1] == '') { ?><?php echo 'cloud/500x282.gif' ?><?php } ?>" width="200" height="113"> </td> <th width="400px" class="th"><font face="Arial" size="2"><b><?php echo $row[product] ?></b> <?php if ($row[stars] == "1") { ?><?php echo "<img src='cloud/star.png'>"; } ?> <?php echo $row[destination] ?> (<?php echo $row[category] ?>)</font></th> </tr> <tr> <td valign="top" class="td" style="padding:7px;"> <font face="Arial" size="2"> <?php echo $row[shortdesc] ?><br> from <font size="4"><b><?php echo $row[price] ?> €</b></font> <font face="Arial" size="1">Nr. <?php echo $row[inventar] ?></font><br> <br> </font> </td></tr> </table> <br> <?php } if($back >=0) { echo "<a href='$page_name?start=$back&sort=$sort'><font face='Verdana' size='2'>back</font></a>"; } echo " "; $i=0; $l=1; for($i=0;$i < $nume;$i=$i+$limit){ if($i <> $zero){ echo " <a href='$page_name?start=$i&sort=$sort'><font face='Verdana' size='2'>$l</font></a> "; } else { echo "<font face='arial' size='2' color='black'>$l</font>";} $l=$l+1; } echo " "; if($this1 < $nume) { echo "<a href='$page_name?start=$next&sort=$sort'><font face='Verdana' size='2'>next</font></a>";} ?> </body> </html> Any one no a simple way to create a numbering system for a basic comment system. Code: [Select] <body> <?php $commentsTable = "comments"; $comments = "SELECT * FROM $commentsTable"; $commentResults = mysql_query($comments); $commentRows = mysql_fetch_array($commentResults); ?> <div id="BodyWrapper"> <?php while($commentRows = mysql_fetch_array($commentResults)){?> <div id="comments"> <div id="CommentWrapper"> <div id="comment"> <div id="UserName"><? echo $commentRows['name'];?></div> <div id="UserComment"><? echo $commentRows['comment'];?></div> <div id="UserEmail"><? echo $commentRows['email'];?></div> <div id="PostDateTime"><? echo $commentRows['datatime'];?></div> </div> </div> </div> <?php } mysql_close(); ?> </div> </body> This is what i have, i want to create a limit of 5 comment but with nummber so you can view older posts. Guys i badly need need to add paging in this . 20 records per page how to accomplish ? Code: [Select] <?php ini_set ("display_errors", "1"); error_reporting(E_ALL); $http = 'myurl.com/other.php?like=' ; $conn=odbc_connect('apple','',''); if (!$conn) {exit("Connection Failed: " . $conn);} $sql="SELECT * FROM mytable "; $rs=odbc_exec($conn,$sql); if (!$rs) {exit("Error in SQL");} echo "<table border= 1><tr>"; echo "<th>ID</th>"; echo "<th>Like</th>"; echo "<th>title</th></tr>"; while (odbc_fetch_row($rs)) { $ids=odbc_result($rs,"ID"); // $links=odbc_result($rs,"links"); $title=odbc_result($rs,"title"); echo "<tr><td>$ids</td>"; echo "<td><fb:like href=\"{$http}{$ids}\" layout=\"button_count\" show_faces=\"false\" width=\"100\" font=\"tahoma\" colorscheme=\"dark\"></fb:like> </td>"; echo "<td>$title</td></tr>"; } echo "</table>"; odbc_close($conn); ?> Hi. I'm trying to make a sort of a forum, and this is the part of the code that needs to be changed, only I don't know how. Code: [Select] mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name ORDER BY id DESC"; // OREDER BY id DESC is order result by descending $result=mysql_query($sql); ?> <table width='50%' border="0" align="center" cellpadding="3" cellspacing="1"> <?php $limit=10; $page=0; $numrows = mysql_num_rows($result); $pages = intval($numrows/$limit); if ($numrows % $limit) { $pages++;} $current = ($page/$limit) + 1; if (($pages < 1) || ($pages == 0)) { $total = 1;} else { $total = $pages;} $result = mysql_query("SELECT * FROM $tbl_name ORDER BY id DESC LIMIT $page,$limit"); while($rows=mysql_fetch_array($result)){?> <tr><td><font size="+2"><a href="view_topic.php?id=<? echo $rows['id']; ?>"><? echo $rows['topic']; ?></a><BR></font></td> <td width='0*'><? echo $rows['datetime']; ?></td></tr> <tr><td><? echo substr($rows['detail'], 0, 250)."..."; ?> <a href="view_topic.php?id=<? echo $rows['id']; ?>"><? echo "Citeste mai mult";} ?></a> </td></tr> <tr><tr><td> <? if ($page != 0) { // Don't show back link if current page is first page. $back_page = $page - $limit; echo("<a href=\"$PHP_SELF?query=$query&page=$back_page&limit=$limit\">back</a> \n");} for ($i=1; $i <= $pages; $i++) // loop through each page and give link to it. { $ppage = $limit*($i - 1); if ($ppage == $page){ echo("<b>$i</b>\n");} // If current page don't give link, just text. else{ echo("<a href=\"$PHP_SELF?query=$query&page=$ppage&limit=$limit\">$i</a> \n");} } if (!((($page+$limit) / $limit) >= $pages) && $pages != 1) { // If last page don't give next link. $next_page = $page + $limit; echo(" <a href=\"$PHP_SELF?query=$query&page=$next_page&limit=$limit\">next</a>");} ?> </td></tr></tr><?php mysql_close(); ?> </table> I made a database with 4 fields and 11 entries. The script shows the last 10 entries just as it should, but when I click on the 'next' button, nothing happens (it should show the first entry, but it just refreshes the page, and still the last 10 entries/1st page are shown). I think it has something to do with the link, but I can't figure out what exactly. Can someone help, or at least give me a hint? Thanks in advance. Bye. Hello everyone! I'm completely new to PHP and Databases. I've managed to do some nice work by reading, but now I've encountered a problem I can't solve. I made a site with trailers and got tierd of adding them manually. So I created a form which sends the trailer info to my database. There's now trailers in specific organized genres. But the list is beginning to get really big so I wanted to show just 10 trailers per page. And so I found out I could use a paging system which I ALMOST got working. Just one last thing that needs to be fixed and that is when I click on next page "http://www.xxxxxxxx.com/test.php&page=2" it doesn't show the content. While "http://www.xxxxxxxx.com/test.php" does. So I just need that next button to work, I don't know if theres something missing or what it is... Note: I'm new to all this so please excuse my very very bad coding. Here is my code: test.php <link rel="stylesheet" type="text/css" media="all" href="http://www.xxxxxxxxxxxxx.n.nu/createdfiles/trailers/style-twentyten.css" /> <link rel="stylesheet" type="text/css" media="all" href="http://www.xxxxxxxxxxxxx.com/paginate.css" /> <?php $rpp = 10; // results per page $adjacents = 4; $page = intval($_GET["page"]); if(!$page) $page = 1; $reload = $_SERVER['PHP_SELF']; // connect to your DB: $link_id = mysql_connect("server","username","my password"); mysql_select_db("database"); // select appropriate results from DB: $sql = "SELECT * FROM blablabla WHERE Genre LIKE '%Drama%' ORDER BY `blablabla`.`sort` DESC LIMIT 0, 9999"; $result = mysql_query($sql, $link_id); // count total number of appropriate listings: $tcount = mysql_num_rows($result); // count number of pages: $tpages = ($tcount) ? ceil($tcount/$rpp) : 1; // total pages, last page number $count = 0; $i = ($page-1)*$rpp; while(($count<$rpp) && ($i<$tcount)) { mysql_data_seek($result,$i); $query = mysql_fetch_array($result); // output each row: echo "<br />--------------------------------------------------------------------------------------------------------<br /><br />"; ?><strong> <img src=" <?php echo $query['Poster'];?>" /><?php echo "<br /> <br />"; ////////////////////////////// //SHOW TITLE, YEAR AND GENRE// ////////////////////////////// echo "<H1>"; echo $query['Title']; echo "<br /> <br />"; echo "</h1>"; ?></strong> <?php echo "<H2>"; echo $query['Year']; echo "</H2>"; echo "<br />"; echo "<H3>"; echo $query['Genre']; echo "</H3>"; ?> <a href=" <?php ////////////////////////////// //SHOW IMDB, ACTORS AND PLOT// ////////////////////////////// echo $query['IMDB']; ?> " target="_self">Link to IMDb</a> <?php echo "<br /> <br />"; echo "<H2>Stars:</H2>"; echo "<br />"; echo "<H3>"; echo $query['Actors']; echo "</H3>"; echo "<br />"; echo "<H2>Plot:</H2>"; echo "<br />"; echo "<H3>"; echo $query['Plot']; echo "</H3>"; echo "<br /> <br />"; echo "<H2>"; //////////////// //SHOW TRAILER// //////////////// ?> <div id="<?php echo $query['ID'];?>" style="display: none;"> <?php echo $query['Youtube']; echo "</div>"; ?> <a href="javascript:ReverseDisplay('<?php echo $query['ID'];?>')">Click to show/hide trailer.</a> <?php echo "</H2>"; echo "<br /> <br />"; $i++; $count++; } // call pagination function: include("paginate_one.php"); echo paginate_one($reload, $page, $tpages); ?> <?php //////////////////////////////// //SCRIPT FOR SHOW/HIDE TRAILER// //////////////////////////////// ?> <script type="text/javascript" language="JavaScript"><!-- function HideContent(d) { document.getElementById(d).style.display = "none"; } function ShowContent(d) { document.getElementById(d).style.display = "block"; } function ReverseDisplay(d) { if(document.getElementById(d).style.display == "none") { document.getElementById(d).style.display = "block"; } else { document.getElementById(d).style.display = "none"; } } //--></script> paginate_one.php <?php /************************************************************************* php easy :: pagination scripts set - Version One ========================================================================== Author: php easy code, www.phpeasycode.com Web Site: http://www.phpeasycode.com Contact: webmaster@phpeasycode.com *************************************************************************/ function paginate_one($reload, $page, $tpages) { $firstlabel = "First"; $prevlabel = "Prev"; $nextlabel = "Next"; $lastlabel = "Last"; $out = "<div class=\"pagin\">\n"; // first if($page>1) { $out.= "<a href=\"" . $reload . "\">" . $firstlabel . "</a>\n"; } else { $out.= "<span>" . $firstlabel . "</span>\n"; } // previous if($page==1) { $out.= "<span>" . $prevlabel . "</span>\n"; } elseif($page==2) { $out.= "<a href=\"" . $reload . "\">" . $prevlabel . "</a>\n"; } else { $out.= "<a href=\"" . $reload . "&page=" . ($page-1) . "\">" . $prevlabel . "</a>\n"; } // current $out.= "<span class=\"current\">Page " . $page . " of " . $tpages . "</span>\n"; // next if($page<$tpages) { $out.= "<a href=\"" . $reload . "&page=" .($page+1) . "\">" . $nextlabel . "</a>\n"; } else { $out.= "<span>" . $nextlabel . "</span>\n"; } // last if($page<$tpages) { $out.= "<a href=\"" . $reload . "&page=" . $tpages . "\">" . $lastlabel . "</a>\n"; } else { $out.= "<span>" . $lastlabel . "</span>\n"; } $out.= "</div>"; return $out; } ?> Here you can view a test of it all in action: http://www.mansgullberg.com/test.php please dont hack my site I'm thankfull for all of your answers! Really nice forum this! /kvickan I'm very new to this and really could use some help. I've got a Web app that has one form that collects data from the user and puts it into a mysql database and has another form that allows the user to select critiera to find records in the database and display them on the page. All this is working just fine, but now that my database is getting more data in it, I want to add functionality to display 10 records on a page with results page navigation links so the user can move forward and backward in the results set. This part is not working and I've put in echo statements to figure out what the code is doing. The problem I'm having is that when the selection critiera pulls more than 10 records from the database, the first page of results is correct per the selection criteria entered by the user on the select form. When the 'next' link is selected to review the second page of results, the query is executed again. But this time the form variables have been reset and the results now contains the entire contents of the db. The start record is set to look at the 11th instance of the results set, so the second page starts with the 11th record in the database instead of the 11th record in the original results set. The original select statement is built by determining which criteria is selected using $_POST against each form variable. How can I retain the form variables or the original select statement so the second execution of the select statement results in the same results set as the first? The other option that may be better is to retain the original results set and avoid re-executing the select statement altogether. But I don't know how to do that either. Any suggestions, code samples or adivice is much appreciated! |