mtnmchgrl Posted October 13, 2009 Share Posted October 13, 2009 Can someone PLEASE help me understand this code. And before you ask YES it WAS a homework assignment that I spent hours++/days++ on, got help from my teacher, classmates, etc. but no one can figure out what I am doing wrong and why it is not working. I have already turned it in for a grade, so I'm not trying to cheat, I just want to understand what I am doing and why it is not working. I will post what i have so far. The requirements for this are that I am to combine a simple select query within MySQL and use functions to break down the requirements which are: 1. determineNumberOfPages 2. previousButton 3. nextButton 4. makeNumberOfPages We have a small database of birds in MySQL. The select query is to show all 31 records with 10 per page. We were to write functions that will determine the number of pages and make them (in this case it should be 4) and to have previous and next buttons as links that you can flip back and forth between. I have the required functions in a separate function file. I have spent hours reading our text book, comparing code, Googling etc. and I would greatly appreciate any insight as to why this is not working. Here is the first file (birdsPagination.php) code: <?php include('includes/birdsHeader.inc.html'); require_once('includes/mysqli_connect.php'); include('dbFunctions.inc.php'); //Connect to the db $dbc = openDB(); //$currentPage = 'dbFunctions.inc.php'; // this variable will set the number of records to show per page: $display = 10; //DETERMINE HOW MANY PAGES THERE ARE $pages = determineNumberOfPages ($dbc, $display); echo "pages: $pages<br />"; // Determine where in the database to start returning results... if (isset($_GET['s']) && is_numeric($_GET['s'])) { $start = $_GET['s']; } else { $start = 0; } // Make the query: $q = "SELECT nameGeneral, nameSpecific, populationTrend FROM birds ORDER BY nameGeneral LIMIT $start, $display;"; $r = @mysqli_query ($dbc, $q); // Table header: echo '<table border="1" align="center" cellspacing="0" cellpadding="5" width="75%"> <tr><td align="left"><b>General</b></td><td align="left"><b>Specific</b></td><td align="left"><b>Population Trend</b></td></tr>'; // Fetch and print all the records.... $bg = '#eeeeee'; // Set the initial background color. while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { $bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee'); // Switch the background color. //rid ugly empty table cell display if ($row['populationTrend'] == "") { $row['populationTrend'] = " "; } echo '<tr bgcolor="' . $bg . '"> <td align="left">' . $row['nameGeneral'] . '</td> <td align="left">' . $row['nameSpecific'] . '</td> <td align="left">' . $row['populationTrend'] . '</td> </tr> '; } // End of WHILE loop. echo '</table>'; mysqli_free_result ($r); mysqli_close($dbc); echo $pages; // Make the links to other pages, if necessary. if ($pages > 1) { echo "start: $start display: $display<br />"; // Add some spacing and start a paragraph: echo '<br /><p>'; // Determine what page the script is on: $current_page = ($start/$display) + 1; echo $current_page . "<br>"; //Call Previous Button Function $prev = previousButton ($current_page, $display, $pages); //Call the function to create the number of pages $makePage = makeNumberOfPages ($current_page, $thisPage, $pages); //Call Next Button Function $next = nextButton ($current_page, $display, $pages); echo '</p>'; // Close the paragraph. include ('includes/birdsFooter.inc.html'); } // End of links section. ?> Here is the function file code: <?php //Function to Open MySQL & Db function openDB() { DEFINE('DB_USER', 'root'); DEFINE('DB_PASSWORD', ''); DEFINE('DB_HOST', 'localhost'); DEFINE('DB_NAME', 'birds_db'); $dbc = @mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR exit ('Could not connect to MySQL: ' . mysqli_connect_error()); return $dbc; } //end of function //Determine how many pages there are... function determineNumberOfPages($dbc, $display) { if (isset($_GET['p']) && is_numeric($_GET['p'])) { // Already been determined. $pages = $_GET['p']; } else { // Need to determine. // Count the number of records: $q = "SELECT COUNT(birdID) FROM birds"; $r = @mysqli_query ($dbc, $q); $row = @mysqli_fetch_array ($r, MYSQLI_NUM); $records = $row[0]; // Calculate the number of pages... if ($records > $display) { // More than 1 page. $pages = ceil ($records/$display); } else { $pages = 1; } } return $pages; } //end of function //Function to make each query result listing have a page number: function makeNumberOfPages ($current_page, $thisPage, $pages) { // Make all the numbered pages: for ($i = 1; $i <= $pages; $i++) { if ($i != $current_page) { echo '<a href="birdsPagination.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '">' . $i . '</a> '; } else { echo $i . ' '; } } // End of FOR loop. } //end of function //Function for the Previous Button (to go back); If it's not the first page, make a Previous button: function previousButton ($current_page, $display, $pages) { if ($pages >1) { echo '<br /> <p>'; $current_page=($start/$display) +1; if ($current_page != 1) { echo '<a href="birdsPagination.php?s=' . ($start - $display) . '&p=' . $pages . '">Previous</a>'; } } } //end of function //Function for the next button, if multiple pages exist; If it's not the last page, make a Next button: function nextButton ($current_page, $display, $pages) { if ($current_page != $pages) { echo '<a href="birdsPagination.php?s=' . ($start + $display) . '&p=' . $pages . '">Next</a>'; } } //end of function ?> Quote Link to comment https://forums.phpfreaks.com/topic/177561-function-help/ Share on other sites More sharing options...
GKWelding Posted October 13, 2009 Share Posted October 13, 2009 any clues from you about what exactly isn't working or do we have to guess? Quote Link to comment https://forums.phpfreaks.com/topic/177561-function-help/#findComment-936321 Share on other sites More sharing options...
mikesta707 Posted October 13, 2009 Share Posted October 13, 2009 please put your code in code tags Quote Link to comment https://forums.phpfreaks.com/topic/177561-function-help/#findComment-936323 Share on other sites More sharing options...
Psycho Posted October 13, 2009 Share Posted October 13, 2009 I see at least one siginificant problem. You should really use CODE tags when posting. You are passing 'p' on the query string to determine the current page to display. But, in your logic for determineNumberOfPages() if $_GET['p'] is defined you are returning that value as the number of pages. The number of pages has no bearing on the current page. function determineNumberOfPages($dbc, $display) { if (isset($_GET['p']) && is_numeric($_GET['p'])) { // Already been determined. $pages = $_GET['p']; } else { // Need to determine. Quote Link to comment https://forums.phpfreaks.com/topic/177561-function-help/#findComment-936329 Share on other sites More sharing options...
mtnmchgrl Posted October 13, 2009 Author Share Posted October 13, 2009 any clues from you about what exactly isn't working or do we have to guess? I'm sorry, I thought i had stated properly in the question... My Previous, Next buttons are not working, nor are the functions which should display the total number of pages from the query results (should be 1,2,3,4, previous and next buttons all of which are hyperlinks). Does that help? Quote Link to comment https://forums.phpfreaks.com/topic/177561-function-help/#findComment-936340 Share on other sites More sharing options...
mtnmchgrl Posted October 13, 2009 Author Share Posted October 13, 2009 I added code tags........ Pagination File: <?php include('includes/birdsHeader.inc.html'); require_once('includes/mysqli_connect.php'); include('dbFunctions.inc.php'); //Connect to the db $dbc = openDB(); //$currentPage = 'dbFunctions.inc.php'; // this variable will set the number of records to show per page: $display = 10; //DETERMINE HOW MANY PAGES THERE ARE $pages = determineNumberOfPages ($dbc, $display); echo "pages: $pages<br />"; // Determine where in the database to start returning results... if (isset($_GET['s']) && is_numeric($_GET['s'])) { $start = $_GET['s']; } else { $start = 0; } // Make the query: $q = "SELECT nameGeneral, nameSpecific, populationTrend FROM birds ORDER BY nameGeneral LIMIT $start, $display;"; $r = @mysqli_query ($dbc, $q); // Table header: echo '<table border="1" align="center" cellspacing="0" cellpadding="5" width="75%"> <tr><td align="left"><b>General</b></td><td align="left"><b>Specific</b></td><td align="left"><b>Population Trend</b></td></tr>'; // Fetch and print all the records.... $bg = '#eeeeee'; // Set the initial background color. while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { $bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee'); // Switch the background color. //rid ugly empty table cell display if ($row['populationTrend'] == "") { $row['populationTrend'] = " "; } echo '<tr bgcolor="' . $bg . '"> <td align="left">' . $row['nameGeneral'] . '</td> <td align="left">' . $row['nameSpecific'] . '</td> <td align="left">' . $row['populationTrend'] . '</td> </tr> '; } // End of WHILE loop. echo '</table>'; mysqli_free_result ($r); mysqli_close($dbc); echo $pages; // Make the links to other pages, if necessary. if ($pages > 1) { echo "start: $start display: $display<br />"; // Add some spacing and start a paragraph: echo '<br /><p>'; // Determine what page the script is on: $current_page = ($start/$display) + 1; echo $current_page . "<br>"; //Call Previous Button Function $prev = previousButton ($current_page, $display, $pages); //Call the function to create the number of pages $makePage = makeNumberOfPages ($current_page, $thisPage, $pages); //Call Next Button Function $next = nextButton ($current_page, $display, $pages); echo '</p>'; // Close the paragraph. include ('includes/birdsFooter.inc.html'); } // End of links section. ?> Functions File: <?php //Function to Open MySQL & Db function openDB() { DEFINE('DB_USER', 'root'); DEFINE('DB_PASSWORD', ''); DEFINE('DB_HOST', 'localhost'); DEFINE('DB_NAME', 'birds_db'); $dbc = @mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR exit ('Could not connect to MySQL: ' . mysqli_connect_error()); return $dbc; } //end of function //Determine how many pages there are... function determineNumberOfPages($dbc, $display) { if (isset($_GET['p']) && is_numeric($_GET['p'])) { // Already been determined. $pages = $_GET['p']; } else { // Need to determine. // Count the number of records: $q = "SELECT COUNT(birdID) FROM birds"; $r = @mysqli_query ($dbc, $q); $row = @mysqli_fetch_array ($r, MYSQLI_NUM); $records = $row[0]; // Calculate the number of pages... if ($records > $display) { // More than 1 page. $pages = ceil ($records/$display); } else { $pages = 1; } } return $pages; } //end of function //Function to make each query result listing have a page number: function makeNumberOfPages ($current_page, $thisPage, $pages) { // Make all the numbered pages: for ($i = 1; $i <= $pages; $i++) { if ($i != $current_page) { echo '<a href="birdsPagination.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '">' . $i . '</a> '; } else { echo $i . ' '; } } // End of FOR loop. } //end of function //Function for the Previous Button (to go back); If it's not the first page, make a Previous button: function previousButton ($current_page, $display, $pages) { if ($pages >1) { echo '<br /> <p>'; $current_page=($start/$display) +1; if ($current_page != 1) { echo '<a href="birdsPagination.php?s=' . ($start - $display) . '&p=' . $pages . '">Previous</a>'; } } } //end of function //Function for the next button, if multiple pages exist; If it's not the last page, make a Next button: function nextButton ($current_page, $display, $pages) { if ($current_page != $pages) { echo '<a href="birdsPagination.php?s=' . ($start + $display) . '&p=' . $pages . '">Next</a>'; } } //end of function ?> Quote Link to comment https://forums.phpfreaks.com/topic/177561-function-help/#findComment-936343 Share on other sites More sharing options...
mikesta707 Posted October 13, 2009 Share Posted October 13, 2009 one thing $prev = previousButton ($current_page, $display, $pages); //Call the function to create the number of pages $makePage = makeNumberOfPages ($current_page, $thisPage, $pages); //Call Next Button Function $next = nextButton ($current_page, $display, $pages); those functions don't return anything, so there is no need to assign them to anything but from skimming over the code, I don't see anything horrible going on. What is happening when you run this script? some insight on what is happening, and what you hope to happen will help figure out what is wrong Quote Link to comment https://forums.phpfreaks.com/topic/177561-function-help/#findComment-936347 Share on other sites More sharing options...
mtnmchgrl Posted October 13, 2009 Author Share Posted October 13, 2009 one thing $prev = previousButton ($current_page, $display, $pages); //Call the function to create the number of pages $makePage = makeNumberOfPages ($current_page, $thisPage, $pages); //Call Next Button Function $next = nextButton ($current_page, $display, $pages); those functions don't return anything, so there is no need to assign them to anything but from skimming over the code, I don't see anything horrible going on. What is happening when you run this script? some insight on what is happening, and what you hope to happen will help figure out what is wrong When I run the script, only 20 birds show up. The 31 birds SHOULD be on 4 total pages, in alphabetical order based on the query. Page 1, will have 10. Page 2 will have 10. Page 3, will have 10 and page 4 should have 1. I am only seeing 20 of the 31 birds. The "previous" link (which should take you from page 4 to page 3, 3 to 2 and so on and so forth) doesn't show up AT ALL. The next button, shows up but it will only let me go from page 1 to page 2. Then it stops. When I click on the page 3 link it seems to go back to the results from page 1. The page 4 link seems to do nothing. When you say: those functions don't return anything, so there is no need to assign them to anything I'm confused by this. The main part of this assignment is making the previous, next and page number links into separate functions so they need to probably return something in order for them to work and for me to complete what my teacher was looking for. Sorry if I am asking dumb question(s). Quote Link to comment https://forums.phpfreaks.com/topic/177561-function-help/#findComment-936447 Share on other sites More sharing options...
mtnmchgrl Posted October 13, 2009 Author Share Posted October 13, 2009 I see at least one siginificant problem. You should really use CODE tags when posting. You are passing 'p' on the query string to determine the current page to display. But, in your logic for determineNumberOfPages() if $_GET['p'] is defined you are returning that value as the number of pages. The number of pages has no bearing on the current page. function determineNumberOfPages($dbc, $display) { if (isset($_GET['p']) && is_numeric($_GET['p'])) { // Already been determined. $pages = $_GET['p']; } else { // Need to determine. I'm very sorry but I am totally lost in what you are saying here......i think this code is to get how many pages based on the number of records. If there are only enough records for 1 page then it would just show up as one page but if its more, then 2, 3, 4, etc. would show up. If it helps, I"m going to upload a screen shot of what this looks like when it runs....you can kind of get the idea of what i am trying to achieve on the end result. Quote Link to comment https://forums.phpfreaks.com/topic/177561-function-help/#findComment-936456 Share on other sites More sharing options...
mtnmchgrl Posted October 14, 2009 Author Share Posted October 14, 2009 Can anyone help? I decided to start all the way over and take out ALL functions and do the code EXACTLY as it is in the book and lo and behold there are STILL problems with the pages as links so i don't think its the functions that are the problems........... see below: <?php include('includes/birdsHeader.inc.html'); require_once('includes/mysqli_connect.php'); // this variable will set the number of records to show per page: $display = 10; //determine pg # if (isset($_GET['p']) && is_numeric($_GET['p'])) { $pages = $_GET['p']; } else { // Count the number of records: $q = "SELECT COUNT(birdID) FROM birds"; $r = @mysqli_query ($dbc, $q); $row = @mysqli_fetch_array ($r, MYSQLI_NUM); $records = $row[0]; // Calculate the number of pages... if ($records > $display) { $pages = ceil ($records/$display); } else { //IF the query run is 10 records or less....... $pages = 1; //then the only page listed will be page 1 } } // Determine where in the database to start returning results... if (isset($_GET['s']) && is_numeric($_GET['s'])) { $start = $_GET['s']; } else { $start = 0; } // Make the query with the LIMIT clause: $q = "SELECT nameGeneral, nameSpecific, populationTrend FROM birds ORDER BY nameGeneral LIMIT $start, $display"; $r = @mysqli_query ($dbc, $q); // HTML FOR THE TABLE HEADER/COLUMN NAMES/ALT COLOR: echo '<table border="3" align="center" cellspacing="5" cellpadding="5" width="50%"> <tr><td align="left"><b>General Name</b></td><td align="left"><b>Specific Name</b></td><td align="left"><b>Population Trend</b></td></tr>'; // Fetch and print all the records... $bg = '#eeeeee'; // Set the initial background color. while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { $bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee'); // Switch the background color. echo '<tr bgcolor="' . $bg . '"> <td align="left">' . $row['nameGeneral'] . '</td> <td align="left">' . $row['nameSpecific'] . '</td> <td align="left">' . $row['populationTrend'] . '</td> </tr> '; } // End of WHILE loop. echo '</table>'; mysqli_free_result ($r); mysqli_close($dbc); //THIS IS THE END OF THE HTML TABLE SET UP //Links to other pages if necc if ($pages > 1) { echo '<br /><p>'; $current_page=($start/$display) +1; //previous if ($current_page != 1) { echo '<a href="birdsPagination.php?s=' . ($start - $display) . '&p=' . $pages . '">Previous</a>'; } // Make all the numbered pages: for ($i = 1; $i <= $pages; $i++) { if ($i != $current_page) { echo '<a href="birdsPagination.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '">' . $i . '</a> '; } else { echo $i . ' '; } } // End of FOR loop. //NEXT if ($current_page != $pages) { echo '<a href="birdsPagination.php?s=' . ($start + $display) . '&p=' . $pages . '">Next</a>'; } echo '</p>'; } //end of links include ('includes/birdsFooter.inc.html'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/177561-function-help/#findComment-936505 Share on other sites More sharing options...
mtnmchgrl Posted October 14, 2009 Author Share Posted October 14, 2009 To add to this ongoing saga, I am also supposed to have the columns be sortable by ascending or descending order by clicking on them. I only have them in ascending sort order by a switch statement. Any idea how to make them sortable by descending as well? <?php include('includes/birdsHeader.inc.html'); require_once('includes/mysqli_connect.php'); // this variable will set the number of records to show per page: $display = 10; //determine pg # if (isset($_GET['p']) && is_numeric($_GET['p'])) { $pages = $_GET['p']; } else { // Count the number of records: $q = "SELECT COUNT(birdID) FROM birds"; $r = @mysqli_query ($dbc, $q); $row = @mysqli_fetch_array ($r, MYSQLI_NUM); $records = $row[0]; // Calculate the number of pages... if ($records > $display) { $pages = ceil ($records/$display); } else { //IF the query run is 10 records or less....... $pages = 1; //then the only page listed will be page 1 } } // Determine where in the database to start returning results... if (isset($_GET['s']) && is_numeric($_GET['s'])) { $start = $_GET['s']; } else { $start = 0; } //DETERMINE THE SORT $sort = (isset($_GET['sort'])) ? $_GET ['sort'] : 'ng'; //DETERMINE THE SORT ORDER switch ($sort) { case 'ng': $order_by = 'nameGeneral ASC'; break; case 'ns': $order_by = 'nameSpecific ASC'; break; case 'pt': $order_by = 'populationTrend ASC'; break; } // Make the query with the LIMIT clause: $q = "SELECT nameGeneral, nameSpecific, populationTrend, birdID FROM birds ORDER BY $order_by LIMIT $start, $display"; $r = @mysqli_query ($dbc, $q); // HTML FOR THE TABLE HEADER/COLUMN NAMES/ALT COLOR: echo '<table border="3" align="center" cellspacing="5" cellpadding="5" width="50%"> <tr><td align="left"><b><a href="TestNoFuncAddSort.php?sort=ng">General Name</a></b></td><td align="left"><b><a href="TestNoFuncAddSort.php?sort=ns">Specific Name</a></b></td><td align="left"><b><a href="TestNoFuncAddSort.php?sort=pt">Population Trend</a></b></td></tr>'; // Fetch and print all the records... $bg = '#eeeeee'; // Set the initial background color. while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { $bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee'); // Switch the background color. echo '<tr bgcolor="' . $bg . '"> <td align="left">' . $row['nameGeneral'] . '</td> <td align="left">' . $row['nameSpecific'] . '</td> <td align="left">' . $row['populationTrend'] . '</td> </tr> '; } // End of WHILE loop. echo '</table>'; mysqli_free_result ($r); mysqli_close($dbc); //THIS IS THE END OF THE HTML TABLE SET UP //Links to other pages if necc if ($pages > 1) { echo '<br /><p>'; $current_page=($start/$display) +1; //previous if ($current_page != 1) { echo '<a href="birdsPagination.php?s=' . ($start - $display) . '&p=' . $pages . '&sort' . $sort . '">Previous</a>'; } // Make all the numbered pages: for ($i = 1; $i <= $pages; $i++) { if ($i != $current_page) { echo '<a href="birdsPagination.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '&sort=' . $sort . '">' . $i . '</a> '; } else { echo $i . ' '; } } // End of FOR loop. //NEXT if ($current_page != $pages) { echo '<a href="birdsPagination.php?s=' . ($start + $display) . '&p=' . $pages . '&sort=' . $sort . '">Next</a>'; } echo '</p>'; } //end of links include ('includes/birdsFooter.inc.html'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/177561-function-help/#findComment-936537 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.