shane85 Posted March 31, 2010 Share Posted March 31, 2010 hey guys....I have included my whole script below....what im trying to do is show 5 results per page. Ive created a function to display a table with information from my database. I have it so either a) it works but displays all the results from my database on the page or b) the pagination works, displays 5 results per page, however it only shows the last record in my database. I have included the full code, but will highlight the part that I think is causing the problem. Any help is much appreciated. Thanks in advance <html> <head> </head> <body> <?php // Define db details //db details mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or trigger_error(mysql_error()); mysql_select_db(DB_DATABASE) or trigger_error(mysql_error()); $tbl_name="prospects"; //your table name // How many adjacent pages should be shown on each side? $adjacents = 3; /* First get total number of rows in data table. If you have a WHERE clause in your query, make sure you mirror it here. */ $query = "SELECT COUNT(*) as num FROM $tbl_name"; $total_pages = mysql_fetch_array(mysql_query($query)); $total_pages = $total_pages[num]; /* Setup vars for query. */ $targetpage = "view_prospect.php"; //your file name (the name of this file) $limit = 5; //how many items to show per page $page = $_GET['page']; if($page) $start = ($page - 1) * $limit; //first item to display on this page else $start = 0; //if no page var is given, set start to 0 /* Get data. */ $sql = "SELECT prospect_id FROM $tbl_name LIMIT $start, $limit"; $result = mysql_query($sql); /* Setup page vars for display. */ if ($page == 0) $page = 1; //if no page var is given, default to 1. $prev = $page - 1; //previous page is page - 1 $next = $page + 1; //next page is page + 1 $lastpage = ceil($total_pages/$limit); //lastpage is = total pages / items per page, rounded up. $lpm1 = $lastpage - 1; //last page minus 1 /* Now we apply our rules and draw the pagination object. We're actually saving the code to a variable in case we want to draw it more than once. */ $pagination = ""; if($lastpage > 1) { $pagination .= "<div class=\"pagination\">"; //previous button if ($page > 1) $pagination.= "<a href=\"$targetpage?page=$prev\">« previous</a>"; else $pagination.= "<span class=\"disabled\">« previous</span>"; //pages if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up { for ($counter = 1; $counter <= $lastpage; $counter++) { if ($counter == $page) $pagination.= "<span class=\"current\">$counter</span>"; else $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>"; } } elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some { //close to beginning; only hide later pages if($page < 1 + ($adjacents * 2)) { for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++) { if ($counter == $page) $pagination.= "<span class=\"current\">$counter</span>"; else $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>"; } $pagination.= "..."; $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>"; $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>"; } //in middle; hide some front and some back elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) { $pagination.= "<a href=\"$targetpage?page=1\">1</a>"; $pagination.= "<a href=\"$targetpage?page=2\">2</a>"; $pagination.= "..."; for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) { if ($counter == $page) $pagination.= "<span class=\"current\">$counter</span>"; else $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>"; } $pagination.= "..."; $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>"; $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>"; } //close to end; only hide early pages else { $pagination.= "<a href=\"$targetpage?page=1\">1</a>"; $pagination.= "<a href=\"$targetpage?page=2\">2</a>"; $pagination.= "..."; for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) { if ($counter == $page) $pagination.= "<span class=\"current\">$counter</span>"; else $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>"; } } } //next button if ($page < $counter - 1) $pagination.= "<a href=\"$targetpage?page=$next\">next »</a>"; else $pagination.= "<span class=\"disabled\">next »</span>"; $pagination.= "</div>\n"; } // Create function to display the news entries function print_prospectEntry($arrEntry) { // convert \n linebreaks to HTML formatted <br> breaks $arrEntry['comments'] = str_replace("\n", '<br>', $arrEntry['comments']); ?> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="10"></td> <td width="300">Representative:</td> <td style="padding: 5px;"> <?php echo $arrEntry['representative']; ?> </td> </tr> <tr> <td width="10"></td> <td width="300">Company Name:</td> <td style="padding: 5px;"> <a href="prospect.php?prospect_id=<?php echo $arrEntry['prospect_id']; ?>"><?php echo $arrEntry['company_name']; ?></a> </td> </tr> <tr> <td width="10"></td> <td width="300">Phone Number:</td> <td style="padding: 5px;"> <?php echo $arrEntry['phone']; ?> </td> </tr> <tr> <td width="10"></td> <td width="300">First Name:</td> <td style="padding: 5px;"><?php echo $arrEntry['firstname']; ?> </td> </tr> <tr> <td width="10"></td> <td width="300">Last Name:</td> <td style="padding: 5px;"> <?php echo $arrEntry['lastname']; ?> </td> </tr> <tr> <td width="10"></td> <td width="300">Last Contacted:</td> <td style="padding: 5px;"> <?php echo $arrEntry['last_contact']; ?> </td> </tr> <tr> <td width="10"></td> <td width="300">Comments:</td> <td style="padding: 5px;"> <?php echo $arrEntry['comments']; ?> </td> </tr> </table> <br> <br> <?php } // Get all news entries $prospectEntries = mysql_query('SELECT * FROM prospects ORDER BY prospect_id DESC') or trigger_error(mysql_error()); while ($prospectEntry = mysql_fetch_array($prospectEntries)) { //print_prospectEntry($prospectEntry); when printing here it prints all the records while($row = mysql_fetch_array($result)) { // Your while loop here print_prospectEntry($prospectEntry); // when printing here, it does 5 results per page like I want, however it just prints the same record //echo $row; } } ?> <?=$pagination?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/197179-pagination-help-please/ Share on other sites More sharing options...
shane85 Posted March 31, 2010 Author Share Posted March 31, 2010 also, since I posted the whole code I might as well as my 2nd question that I will have once this problem gets fixed - as you can see I have my table which shows different info per row - I want it so that every 2nd row the table has a different color bg so its easy on the eyes - how is this accomplished?? thanks again Quote Link to comment https://forums.phpfreaks.com/topic/197179-pagination-help-please/#findComment-1034996 Share on other sites More sharing options...
andrewgauger Posted March 31, 2010 Share Posted March 31, 2010 Wrong reference Change the highlighted section to: // Get all news entries $prospectEntries = mysql_query('SELECT * FROM prospects ORDER BY prospect_id DESC') or trigger_error(mysql_error()); while ($prospectEntry = mysql_fetch_array($prospectEntries)) { //print_prospectEntry($prospectEntry); when printing here it prints all the records while($row = mysql_fetch_array($result)) { // Your while loop here print_prospectEntry($row['prospect_id']); // when printing here, it does 5 results per page like I want, however it just prints the same record //echo $row; } } Quote Link to comment https://forums.phpfreaks.com/topic/197179-pagination-help-please/#findComment-1035000 Share on other sites More sharing options...
andrewgauger Posted March 31, 2010 Share Posted March 31, 2010 Oh, I forgot: if(++$i%2 == 0) { //effect code } Quote Link to comment https://forums.phpfreaks.com/topic/197179-pagination-help-please/#findComment-1035001 Share on other sites More sharing options...
shane85 Posted March 31, 2010 Author Share Posted March 31, 2010 thanks for your responce Andrew however now it still doesnt print right - it just prints the product_id number 1 for the first few records, then 2 for the next few. I want it to print each records information (5 per page) in the table Quote Link to comment https://forums.phpfreaks.com/topic/197179-pagination-help-please/#findComment-1035004 Share on other sites More sharing options...
andrewgauger Posted April 1, 2010 Share Posted April 1, 2010 I had the concept backwards when I looked at it first. Instead of a nestled loop try just changing the highlighted to: // Get all news entries $prospectEntries = mysql_query('SELECT * FROM prospects ORDER BY prospect_id DESC LIMIT $start, $limit') or trigger_error(mysql_error()); while ($prospectEntry = mysql_fetch_array($prospectEntries)) { print_prospectEntry($prospectEntry); } Sorry I had a brain fart earlier. Quote Link to comment https://forums.phpfreaks.com/topic/197179-pagination-help-please/#findComment-1035022 Share on other sites More sharing options...
jcbones Posted April 1, 2010 Share Posted April 1, 2010 Try this, and let me know. <html> <head> </head> <body> <?php // Define db details //db details mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or trigger_error(mysql_error()); mysql_select_db(DB_DATABASE) or trigger_error(mysql_error()); $tbl_name="prospects"; //your table name // How many adjacent pages should be shown on each side? $adjacents = 3; /* First get total number of rows in data table. If you have a WHERE clause in your query, make sure you mirror it here. */ $query = "SELECT COUNT(*) as num FROM $tbl_name"; $total_pages = mysql_fetch_array(mysql_query($query)); $total_pages = $total_pages['num']; /* Setup vars for query. */ $targetpage = "view_prospect.php"; //your file name (the name of this file) $limit = 5; //how many items to show per page $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1; $start = ($page - 1) * $limit; //first item to display on this page $queryLimit = 'LIMIT ' . $start . ',' . $limit; /* Get data. */ // $sql = "SELECT prospect_id FROM $tbl_name LIMIT $start, $limit"; // $result = mysql_query($sql); /* Setup page vars for display. */ if ($page == 0) $page = 1; //if no page var is given, default to 1. $prev = $page - 1; //previous page is page - 1 $next = $page + 1; //next page is page + 1 $lastpage = ceil($total_pages/$limit); //lastpage is = total pages / items per page, rounded up. $lpm1 = $lastpage - 1; //last page minus 1 /* Now we apply our rules and draw the pagination object. We're actually saving the code to a variable in case we want to draw it more than once. */ $pagination = ""; if($lastpage > 1) { $pagination .= "<div class=\"pagination\">"; //previous button if ($page > 1) $pagination.= "<a href=\"$targetpage?page=$prev\">« previous</a>"; else $pagination.= "<span class=\"disabled\">« previous</span>"; //pages if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up { for ($counter = 1; $counter <= $lastpage; $counter++) { if ($counter == $page) $pagination.= "<span class=\"current\">$counter</span>"; else $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>"; } } elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some { //close to beginning; only hide later pages if($page < 1 + ($adjacents * 2)) { for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++) { if ($counter == $page) $pagination.= "<span class=\"current\">$counter</span>"; else $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>"; } $pagination.= "..."; $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>"; $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>"; } //in middle; hide some front and some back elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) { $pagination.= "<a href=\"$targetpage?page=1\">1</a>"; $pagination.= "<a href=\"$targetpage?page=2\">2</a>"; $pagination.= "..."; for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) { if ($counter == $page) $pagination.= "<span class=\"current\">$counter</span>"; else $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>"; } $pagination.= "..."; $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>"; $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>"; } //close to end; only hide early pages else { $pagination.= "<a href=\"$targetpage?page=1\">1</a>"; $pagination.= "<a href=\"$targetpage?page=2\">2</a>"; $pagination.= "..."; for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) { if ($counter == $page) $pagination.= "<span class=\"current\">$counter</span>"; else $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>"; } } } //next button if ($page < $counter - 1) $pagination.= "<a href=\"$targetpage?page=$next\">next »</a>"; else $pagination.= "<span class=\"disabled\">next »</span>"; $pagination.= "</div>\n"; } function findColor($n) { return ($n % 2 == 0) ? '#0026FF' : '#0094FF'; } // Create function to display the news entries function print_prospectEntry($arrEntry) { // convert \n linebreaks to HTML formatted <br> breaks $arrEntry['comments'] = str_replace("\n", '<br>', $arrEntry['comments']); $i = 1; ?> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr style="background-color:<?php echo findColor($i++); ?>"> <td width="10"></td> <td width="300">Representative:</td> <td style="padding: 5px;"> <?php echo $arrEntry['representative']; ?> </td> </tr> <tr style="background-color:<?php echo findColor($i++); ?>"> <td width="10"></td> <td width="300">Company Name:</td> <td style="padding: 5px;"> <a href="prospect.php?prospect_id=<?php echo $arrEntry['prospect_id']; ?>"><?php echo $arrEntry['company_name']; ?></a> </td> </tr> <tr style="background-color:<?php echo findColor($i++); ?>"> <td width="10"></td> <td width="300">Phone Number:</td> <td style="padding: 5px;"> <?php echo $arrEntry['phone']; ?> </td> </tr> <tr style="background-color:<?php echo findColor($i++); ?>"> <td width="10"></td> <td width="300">First Name:</td> <td style="padding: 5px;"><?php echo $arrEntry['firstname']; ?> </td> </tr> <tr style="background-color:<?php echo findColor($i++); ?>"> <td width="10"></td> <td width="300">Last Name:</td> <td style="padding: 5px;"> <?php echo $arrEntry['lastname']; ?> </td> </tr> <tr style="background-color:<?php echo findColor($i++); ?>"> <td width="10"></td> <td width="300">Last Contacted:</td> <td style="padding: 5px;"> <?php echo $arrEntry['last_contact']; ?> </td> </tr> <tr style="background-color:<?php echo findColor($i++); ?>"> <td width="10"></td> <td width="300">Comments:</td> <td style="padding: 5px;"> <?php echo $arrEntry['comments']; ?> </td> </tr> </table> <br> <br> <?php } // Get all news entries $prospectEntries = mysql_query('SELECT * FROM prospects ORDER BY prospect_id DESC ' . $queryLimit) or trigger_error(mysql_error()); while ($prospectEntry = mysql_fetch_array($prospectEntries)) { // Your while loop here print_prospectEntry($prospectEntry); // when printing here, it does 5 results per page like I want, however it just prints the same record //echo $row; } ?> <?php echo $pagination; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/197179-pagination-help-please/#findComment-1035027 Share on other sites More sharing options...
shane85 Posted April 1, 2010 Author Share Posted April 1, 2010 Andrew: when trying your code I get the following errors Notice: 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 '$start, $limit' at line 1 in /home/content/view_prospect.php on line 198 aswell as Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/content/view_prospect.php on line 199 where as line 198 and 199 are as follows $prospectEntries = mysql_query('SELECT * FROM prospects ORDER BY prospect_id DESC LIMIT $start, $limit') or trigger_error(mysql_error()); while ($prospectEntry = mysql_fetch_array($prospectEntries)) Quote Link to comment https://forums.phpfreaks.com/topic/197179-pagination-help-please/#findComment-1035087 Share on other sites More sharing options...
shane85 Posted April 1, 2010 Author Share Posted April 1, 2010 JCBONES: perfect! you fixed my problem with the pages! However for the color thing what I was thinking of I want the whole table for the first record to be 1 color, then the next time it displays for the next record to be a different color. Not the rows. How do I accomplish this? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/197179-pagination-help-please/#findComment-1035088 Share on other sites More sharing options...
jcbones Posted April 2, 2010 Share Posted April 2, 2010 <html> <head> </head> <body> <?php // Define db details //db details mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or trigger_error(mysql_error()); mysql_select_db(DB_DATABASE) or trigger_error(mysql_error()); $tbl_name="prospects"; //your table name // How many adjacent pages should be shown on each side? $adjacents = 3; /* First get total number of rows in data table. If you have a WHERE clause in your query, make sure you mirror it here. */ $query = "SELECT COUNT(*) as num FROM $tbl_name"; $total_pages = mysql_fetch_array(mysql_query($query)); $total_pages = $total_pages['num']; /* Setup vars for query. */ $targetpage = "view_prospect.php"; //your file name (the name of this file) $limit = 5; //how many items to show per page $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1; $start = ($page - 1) * $limit; //first item to display on this page $queryLimit = 'LIMIT ' . $start . ',' . $limit; /* Get data. */ // $sql = "SELECT prospect_id FROM $tbl_name LIMIT $start, $limit"; // $result = mysql_query($sql); /* Setup page vars for display. */ if ($page == 0) $page = 1; //if no page var is given, default to 1. $prev = $page - 1; //previous page is page - 1 $next = $page + 1; //next page is page + 1 $lastpage = ceil($total_pages/$limit); //lastpage is = total pages / items per page, rounded up. $lpm1 = $lastpage - 1; //last page minus 1 /* Now we apply our rules and draw the pagination object. We're actually saving the code to a variable in case we want to draw it more than once. */ $pagination = ""; if($lastpage > 1) { $pagination .= "<div class=\"pagination\">"; //previous button if ($page > 1) $pagination.= "<a href=\"$targetpage?page=$prev\">« previous</a>"; else $pagination.= "<span class=\"disabled\">« previous</span>"; //pages if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up { for ($counter = 1; $counter <= $lastpage; $counter++) { if ($counter == $page) $pagination.= "<span class=\"current\">$counter</span>"; else $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>"; } } elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some { //close to beginning; only hide later pages if($page < 1 + ($adjacents * 2)) { for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++) { if ($counter == $page) $pagination.= "<span class=\"current\">$counter</span>"; else $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>"; } $pagination.= "..."; $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>"; $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>"; } //in middle; hide some front and some back elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) { $pagination.= "<a href=\"$targetpage?page=1\">1</a>"; $pagination.= "<a href=\"$targetpage?page=2\">2</a>"; $pagination.= "..."; for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) { if ($counter == $page) $pagination.= "<span class=\"current\">$counter</span>"; else $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>"; } $pagination.= "..."; $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>"; $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>"; } //close to end; only hide early pages else { $pagination.= "<a href=\"$targetpage?page=1\">1</a>"; $pagination.= "<a href=\"$targetpage?page=2\">2</a>"; $pagination.= "..."; for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) { if ($counter == $page) $pagination.= "<span class=\"current\">$counter</span>"; else $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>"; } } } //next button if ($page < $counter - 1) $pagination.= "<a href=\"$targetpage?page=$next\">next »</a>"; else $pagination.= "<span class=\"disabled\">next »</span>"; $pagination.= "</div>\n"; } function findColor($n) { return ($n % 2 == 0) ? '#0026FF' : '#0094FF'; } // Create function to display the news entries function print_prospectEntry($arrEntry,$i) { // convert \n linebreaks to HTML formatted <br> breaks $arrEntry['comments'] = str_replace("\n", '<br>', $arrEntry['comments']); ?> <table style="background-color:<?php echo findColor($i); ?>" width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="10"></td> <td width="300">Representative:</td> <td style="padding: 5px;"> <?php echo $arrEntry['representative']; ?> </td> </tr> <tr> <td width="10"></td> <td width="300">Company Name:</td> <td style="padding: 5px;"> <a href="prospect.php?prospect_id=<?php echo $arrEntry['prospect_id']; ?>"><?php echo $arrEntry['company_name']; ?></a> </td> </tr> <tr> <td width="10"></td> <td width="300">Phone Number:</td> <td style="padding: 5px;"> <?php echo $arrEntry['phone']; ?> </td> </tr> <tr> <td width="10"></td> <td width="300">First Name:</td> <td style="padding: 5px;"><?php echo $arrEntry['firstname']; ?> </td> </tr> <tr> <td width="10"></td> <td width="300">Last Name:</td> <td style="padding: 5px;"> <?php echo $arrEntry['lastname']; ?> </td> </tr> <tr> <td width="10"></td> <td width="300">Last Contacted:</td> <td style="padding: 5px;"> <?php echo $arrEntry['last_contact']; ?> </td> </tr> <tr> <td width="10"></td> <td width="300">Comments:</td> <td style="padding: 5px;"> <?php echo $arrEntry['comments']; ?> </td> </tr> </table> <br> <br> <?php } // Get all news entries $prospectEntries = mysql_query('SELECT * FROM prospects ORDER BY prospect_id DESC ' . $queryLimit) or trigger_error(mysql_error()); $i = 1; while ($prospectEntry = mysql_fetch_array($prospectEntries)) { // Your while loop here print_prospectEntry($prospectEntry,$i++); // when printing here, it does 5 results per page like I want, however it just prints the same record //echo $row; } ?> <?php echo $pagination; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/197179-pagination-help-please/#findComment-1035936 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.