GetAWeapon Posted June 3, 2014 Share Posted June 3, 2014 Hello everyone, I new to the php world and I'm only working with it for 5 days now. So I'm a noob But I've got a good start atm, I already could connect a database with my webpage and show all my coloms I need. Now I got a problem with the fact, my database is way to big to display on 1 page. To much load-time, so that's the reason why I want to split up all my products. 20 for each page, like in this tutorial: http://www.phpjabbers.com/php--mysql-select-data-and-split-on-pages-php25.html?err=1 I think I'm almost there, my problem is that when I click on the number from the next page (like nr 5), it doesn't show my next 20 products. But when I look to the url, that seems ok, because I get something like this; index.php?page=1 Can someone help me to find out, what I did wrong? And plz, remember, if you give me an answer, plz keep it short and simple. I'm a noob at this. This is my code: <?php include('connect-mysql.php'); if (!empty($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; }; $start_from = ($page-1) * 20; $sqlget = "SELECT * FROM artikel, images LIMIT 0, 20 "; $sqldata = mysqli_query($dbcon, $sqlget) or die('error getting'); echo "<table>"; echo "<tr><th>A_ARTCODE</th><th>A_NUMMER</th><th>A_OMSCHRN</th><th>A_REFLEV</th><th>A_WINKEL</th><th>I_ARTCODE</th><th>I_FILE</th></tr>"; while($row = mysqli_fetch_array($sqldata)){ echo "<tr><td align='right'>"; echo $row['A_ARTCODE']; echo "</td><td align='left'>"; echo $row['A_NUMMER']; echo "</td><td align='left'>"; echo $row['A_OMSCHRN']; echo "</td><td align='left'>"; echo $row['A_REFLEV']; echo "</td><td align='right'>"; echo $row['A_WINKEL']; echo "</td><td align='right'>"; echo $row['I_ARTCODE']; echo "</td><td align='right'>"; echo $row['I_FILE']; //echo "<img src='000000-001000".$row['I_ID']."' />"; echo "</td></tr>"; } echo "</table>"; $sql = "SELECT COUNT(A_ARTCODE) FROM artikel"; $rs_result = mysqli_query($dbcon, $sql) or die ("mysqli query dies"); $row = mysqli_fetch_row($rs_result) or die ("mysqli fetch row dies"); $total_records = $row[0]; $total_pages = ceil($total_records / 20); for ($i=1; $i<=$total_pages; $i++) { echo "<a href='index.php?page=".$i."'>".$i."</a> "; }; ?> I hope someone can help me. Link to comment https://forums.phpfreaks.com/topic/288960-split-database-20-rows-a-page/ Share on other sites More sharing options...
cyberRobot Posted June 3, 2014 Share Posted June 3, 2014 Have you tried using the offset created here in the query? $start_from = ($page-1) * 20; The query is currently hard coded to always get the first 20 entries: LIMIT 0, 20 Link to comment https://forums.phpfreaks.com/topic/288960-split-database-20-rows-a-page/#findComment-1481748 Share on other sites More sharing options...
GetAWeapon Posted June 3, 2014 Author Share Posted June 3, 2014 Oooooh yes, ofcourse, why didn't I think of that xD I just needed to replace this: $sqlget = "SELECT * FROM artikel, images LIMIT 0, 20 "; to $sqlget = "SELECT * FROM artikel, images LIMIT $start_from, 20 "; Thanks for the fast support, I'm really greatfull, but I also got another question, but I'll guess I better make another topic for it or not? Link to comment https://forums.phpfreaks.com/topic/288960-split-database-20-rows-a-page/#findComment-1481749 Share on other sites More sharing options...
cyberRobot Posted June 3, 2014 Share Posted June 3, 2014 Thanks for the fast support, I'm really greatfull... No problem. Side note: MySQL doesn't always return results in the same order. If you haven't done so already, you should look into specifying a sort order. More information can be found here: http://dev.mysql.com/doc/refman/5.0/en/order-by-optimization.html Link to comment https://forums.phpfreaks.com/topic/288960-split-database-20-rows-a-page/#findComment-1481750 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.