Jump to content

ahelp

New Members
  • Posts

    1
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

ahelp's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. I'm making a php page using mySQL for an online class that I'm taking and having trouble getting the pagination to work properly and my teacher doesn't seem to care. I'm using divs and menukeys to populate the content portion of my page instead of actually changing to a different web page each time. The first page of my results load fine when I try to load them, however, when I click any of the links for other pages, I just get defaulted back to the homepage. What seems to be the problem to me is that the code I was given to work with uses $this->php_self = htmlspecialchars($_SERVER['PHP_SELF']); to get the address to build the pages off of and this returns index.php instead of view_customers.php. If I enter the url manually with view_customers.php?page=2 it works, but it's just the results without the rest of my page. I tried pointing a menukey to inlude this url manually, but that won't even work. I'm at a loss as to what to do and my teacher will offer me absolutely no advice besides "figure it out". I know there's a lot of code here, but I don't know where else to go for help. Here is the code for my index page. Each link on my page assigns a menukey which tells index.php what content to open. <body> <div id="outer"> <div id="header"> <?php include "header.php"; ?> </div> <div id="menu"> <?php include "menu.php"; ?> </div> <div id="content"> <div id="primaryContentContainer"> <div id="primaryContent"> <!-- primaryContent - START --> <?php if (isset($_GET['menukey'])) { $menukey = $_GET['menukey']; } else { $menukey = 0; } switch ($menukey) { case 1: include "primarycontent.php"; break; case 2: include "http://www.m4ba.com/IT5201/IT5320_Assignments_files/DonaldYoungpeter.htm"; break; case 3: include "http://itd1.cincinnatistate.edu/abank/answerbank/OtherFiles/IMPORTANTDATESCHART20082009.htm"; break; case 4: include "http://itd1.cincinnatistate.edu/abank/answerbank/OtherFiles/IMPORTANTDATESCHART20092010.htm"; break; case 5: include "http://itd1.cincinnatistate.edu/abank/answerbank/CourseOutline.asp?CourseOutlineCourse=ENG-1001"; break; case 6: include "http://itd1.cincinnatistate.edu/abank/answerbank/CourseOutline.asp?CourseOutlineCourse=IT-5201"; break; case 7: include "http://itd1.cincinnatistate.edu/abank/answerbank/CourseOutline.asp?CourseOutlineCourse=IT-5291"; break; case 8: include "http://itd1.cincinnatistate.edu/abank/answerbank/CourseOutline.asp?CourseOutlineCourse=IT-5320"; break; case 9: include "http://itd1.cincinnatistate.edu/abank/answerbank/CourseOutline.asp?CourseOutlineCourse=IT-5255"; break; case 10: include "http://itd1.cincinnatistate.edu/abank/answerbank/CourseOutline.asp?CourseOutlineCourse=FYE-9001"; break; case 11: include "http://itd1.cincinnatistate.edu/abank/answerbank/CourseOutline.asp?CourseOutlineCourse=IT-9350"; break; case 12: include "cpdm.php"; break; case 13: include "AdvisorsEdit.php"; break; case 14: include "AdvisorsEditPost.php"; break; case 15: include "register.php"; break; case 16: include "paginate_customers.php"; break; default: // case else include "primarycontent.php"; break; } ?> </div> <!-- primaryContent - END --> </div> </div> <div id="secondaryContent"> <?php include "sidemenu.php"; ?> </div> <div class="clear"> </div> <div id="footer"> <?php include "footer.php"; ?> </div> </div> </body> Here is the code for my mySQL query that I'm trying to paginate. <?php // This script retrieves all the records from the Customers table. echo '<h1>Registered Customers:</h1>'; include('ps_pagination.php'); require_once ('mysql_connect.php'); // Connect to the db. /* Write the query... Note: CONCAT_WS is a function that concatenates items together with a given separator. In this case, the last name and first name are added together , and separated by a comma and a space. */ $sql = 'SELECT Customers.CustomerID, Customers.OldCustomerID, Customers.LastName , Customers.FirstName , Customers.MiddleName, CONCAT((LEFT(Customers.FirstName, 1)), (LEFT(Customers.MiddleName, 1)) , Customers.LastName) AS Username, zlu_Cars.Description AS Car, zlu_CarColor.Description AS CarColor , zlu_Computers.Description AS Computer,Customers.IsLaptop as Laptop, zlu_Race.Description AS Race , zlu_Residence.Description AS Residence, zlu_BirthMonth.Description AS BirthMonth FROM Customers INNER JOIN zlu_BirthMonth ON Customers.BirthMonthID = zlu_BirthMonth.BirthMonthID INNER JOIN zlu_Cars ON Customers.CarID = zlu_Cars.CarID INNER JOIN zlu_CarColor ON Customers.CarColorID = zlu_CarColor.CarColorID INNER JOIN zlu_Computers ON Customers.ComputerID = zlu_Computers.ComputerID INNER JOIN zlu_Race ON Customers.RaceID = zlu_Race.RaceID INNER JOIN zlu_Residence ON Customers.ResidenceID = zlu_Residence.ResidenceID WHERE Customers.CustomerID > 499 AND Customers.CustomerID < 601'; /* , (CASE Customers.IsLaptop WHEN 0 THEN 'No' WHEN 1 THEN 'Yes' ELSE 'Unknown') AS Laptop */ /* $sql = "SELECT Customers.CustomerID, Customers.OldCustomerID, Customers.LastName + ', ' + Customers.FirstName + ' ' + Customers.MiddleName AS CustomerName, CONCAT((LEFT(Customers.FirstName, 1)), (LEFT(Customers.MiddleName, 1)), Customers.LastName) AS Username, zlu_Cars.Description AS Car, zlu_CarColor.Description AS CarColor, zlu_Computer.Description AS Computer, zlu_Race.Description AS Race, zlu_Residence.Description AS Residence, zlu_BirthMonth.Description AS BirthMonth FROM Customers, zlu_BirthMonth, zlu_Cars, zlu_CarColors, zlu_Computer, zlu_Race, zlu_Residence WHERE Customers.CarID = Cars.CarID, AND Customers.CarColorID = CarColor.CarColorID, AND Customers.ComputerID = Computers.ComputerID, AND Customers.RaceID = Race.RaceID, AND Customers.ResidenceID = Residence.ResidenceID ORDER BY CustomerID ASC"; */ /* $sql = "SELECT CustomerID, OldCustomerID, LastName + ', ' + FirstName + ' ' + MiddleName AS CustomerName, CONCAT(LOWER(LEFT(FirstName, 1)), LOWER(LEFT(MiddleName, 1)), LastName) AS Username, zlu_Cars.Description AS Car, zlu_CarColor.Description AS CarColor, zlu_Computer.Description AS Computer, (CASE IsLaptop WHEN 0 THEN 'No' WHEN 1 THEN 'Yes' ELSE 'Unknown') AS Laptop, zlu_Race.Description AS Race, zlu_Residence.Description AS Residence, zlu_BirthMonth.Description AS BirthMonth FROM Customers WHERE CarID = Cars.CarID, AND CarColorID = CarColor.CarColorID, AND ComputerID = Computers.ComputerID, AND RaceID = Race.RaceID, AND ResidenceID = Residence.ResidenceID ORDER BY CustomerID ASC"; */ // $rs = @mysqli_query ($conn, $sql) or die(mysqli_error($conn)." Q=".$sql); // Run the query. // Count the number of returned rows: //Create a PS_Pagination object $countperpage = 20; $pager = new PS_Pagination($conn,$sql,$countperpage,6); //The paginate() function returns a mysql result set $rs = $pager->paginate(); $num = mysql_num_rows($rs); if ($num > 0) { // If it ran OK, display the records. // Table header. echo '<table align="center" cellspacing="3" cellpadding="3" width="75%" border=1> <tr> <td><b>Customer ID</b></td> <td><b>Old Customer ID</b></td> <td><b>Customer Name</b></td> <td><b>Username</b></td> <td><b>Car</b></td> <td><b>Car Color</b></td> <td><b>Computer</b></td> <td><b>Laptop</b></td> <td><b>Race</b></td> <td><b>Residence</b></td> <td><b>Birth Month</b></td> </tr>'; /* Fetch and print all the records: fetch_array Fetch a result row as an associative, a numeric array, or both. Parameters: type - one of MYSQLI_ASSOC, MYSQLI_NUM, or MYSQLI_BOTH (default). By using the MYSQLI_ASSOC constant this function will behave identically to the mysqli_fetch_assoc(), while MYSQLI_NUM will behave identically to the mysqli_fetch_row() function. The final option MYSQLI_BOTH will create a single array with the attributes of both. Returns: a result row as an associative, a numeric array, or both or null if there are no more rows in the result set */ while($row = mysql_fetch_assoc($rs)) { while ($row = mysql_fetch_array($rs, MYSQLI_ASSOC)) { switch ($row['Laptop']){ case 0: $laptop = 'No'; break; case 1: $laptop = 'Yes'; break; default: $laptop = 'Unknown'; break; } echo '<tr><td>' . $row['CustomerID'] . '</td><td>' . $row['OldCustomerID'] . '</td><td>' . $row['LastName'] . ', ' . $row['FirstName'] . ' ' . $row['MiddleName'] . '</td><td>' . $row['Username'] . '</td><td>' . $row['Car'] . '</td><td>' . $row['CarColor'] . '</td><td>' . $row['Computer'] . '</td><td>' . $laptop . '</td><td>' . $row['Race'] . '</td><td>' . $row['Residence'] . '</td><td>' . $row['BirthMonth'] . '</td></tr>'; }} /* <td>' . $row['IsLaptop'] . '</td> */ echo '</table>'; // Close the table. mysql_free_result ($rs); // Free up the resources. } else { // If no records were returned. echo '<p class="error">There are currently no registered Customers.</p>'; } echo "<br><center><font face=verdana size=3 color=blue>"; echo $pager->renderFullNav(); echo "</font></center>"; mysql_close($conn); // Close the database connection. ?> And here's the code that creates the pagination. <?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 = false; 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; } } ?>
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.