O'Reilly Forums: Chapter 9 Page Number Doesn't Change - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Chapter 9 Page Number Doesn't Change

#1 User is offline   bluethundr 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 30
  • Joined: 05-May 09
  • Gender:Male
  • Location:Summit, NJ

Posted 22 January 2013 - 09:46 AM

Hello,

I've done the 'RiskyJobs' job search exercise from chapter 9. I've just added the links so that you can navigate from page to page if you have more pages to navigate than will fit on a (defined in code) number of results per page. I've also artificially inflated my database so that search terms would return more results.

I've noticed that the previous page indicator (<-) never becomes a link. So I've echoed the '$cur_page' value and finds that it always remains at '1' even if I generate 4 pages of results and can navigate to page 1-4 by clicking the numbers. I was just wondering if some kind knowledgeable soul out there could help me find where the flaw in my code was.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Risky Jobs - Search</title>
  <link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
  <img src="riskyjobs_title.gif" alt="Risky Jobs" />
  <img src="riskyjobs_fireman.jpg" alt="Risky Jobs" style="float:right" />
  <center><h3>Risky Jobs - Search Results</h3></center>

<?php

  // Build Search Query
  function build_query($user_search, $sort) {
	$search_query = "SELECT * FROM riskyjobs";
	// Extract the search keywords into an array
	$clean_search = str_replace(',', ' ', $user_search);
	$search_words = explode(' ', $clean_search);
	$final_search_words = array();
	if (count($search_words) > 0) {
  	foreach ($search_words as $word) {
    	if (!empty($word)) {
 		$final_search_words[] = $word;
    	}
  	}
	}
   // Generate a WHERE clause using all of the search keywords
   $where_list = array();
   if (count($final_search_words) > 0) {
 	foreach($final_search_words as $word) {
   	$where_list[] = "description LIKE '%$word%'";
 	}
   } 
   $where_clause = implode(' OR ', $where_list);
   // Add the keyword WHERE clause to the search query
   if (!empty($where_clause)) {
  	$search_query .= " WHERE $where_clause";
   }
   // Sort the search query using the sort setting
   switch ($sort) {
  // Ascending by job title
  case 1:
	$search_query .= " ORDER BY title";
	break;
  //Descending by job title
  case 2:
	$search_query .= " ORDER BY title DESC";
	break;
  //Ascending by state
  case 3:
	$search_query .= " ORDER BY state";
	break;
   //Descending by state
	case 4:
	$search_query .= " ORDER BY state DESC";
	break;
  //Ascending by date posted (oldest first)
  case 5:
	$search_query .= " ORDER BY date_posted";
	break;
  //Descending by date posted (newest first)
  case 6:
	$search_query .= " ORDER BY date_posted DESC";
	break;
  default:
	// No sort setting provided, so don't sort the query
   }
   
 	return $search_query;
  }
  
  // Generate the search result headings
  function generate_sort_links($user_search, $sort) {
 	$sort_links = '';
 	
 	switch ($sort) {
 	case 1:
    	$sort_links .= '<td><a href = "' . $_SERVER['PHP_SELF'] . '?usersearch=' . $user_search .
    	'&sort=2">Job Title</a></td><td>Description</td>';   
    	$sort_links .= '<td><a href = "' . $_SERVER['PHP_SELF'] . '?usersearch=' . $user_search .
    	'&sort=3">State</a></td>';
    	$sort_links .= '<td><a href = "' . $_SERVER['PHP_SELF'] . '?usersearch=' . $user_search .
    	'&sort=5">Date Posted</a></td>';
 	break;
 	case 3:
   	$sort_links .= '<td><a href = "' . $_SERVER['PHP_SELF'] . '?usersearch=' . $user_search .
    	'&sort=1">Job Title</a></td><td>Description</td>';   
    	$sort_links .= '<td><a href = "' . $_SERVER['PHP_SELF'] . '?usersearch=' . $user_search .
    	'&sort=4">State</a></td>';
    	$sort_links .= '<td><a href = "' . $_SERVER['PHP_SELF'] . '?usersearch=' . $user_search .
    	'&sort=5">Date Posted</a></td>';
  	break;
  	
  	case 5:
 		$sort_links .= '<td><a href = "' . $_SERVER['PHP_SELF'] . '?usersearch=' . $user_search .
 		'&sort=1">Job Title</a></td><td>Description</td>';   
 		$sort_links .= '<td><a href = "' . $_SERVER['PHP_SELF'] . '?usersearch=' . $user_search .
 		'&sort=3">State</a></td>';
 		$sort_links .= '<td><a href = "' . $_SERVER['PHP_SELF'] . '?usersearch=' . $user_search .
 		'&sort=6">Date Posted</a></td>';
   	break;
    	
  	default:
 		$sort_links .= '<td><a href = "' . $_SERVER['PHP_SELF'] . '?usersearch=' . $user_search .
    	'&sort=1">Job Title</a></td><td>Description</td>';   
    	$sort_links .= '<td><a href = "' . $_SERVER['PHP_SELF'] . '?usersearch=' . $user_search .
    	'&sort=3">State</a></td>';
    	$sort_links .= '<td><a href = "' . $_SERVER['PHP_SELF'] . '?usersearch=' . $user_search .
    	'&sort=5">Date Posted</a></td>';      	
  	}	
  	return $sort_links;
   }

  // Builds navigational page links
  function generate_page_links($user_search, $sort, $cur_page, $num_pages) {
	echo '<br /><br /><font face=verdana size="3" color="red"> This is the value of cur_page:' . $cur_page . '</font><br /><br />';
  
	$page_links = '';
	
	// If this page is not the first page, generate the "Previous" link
	if ($cur_page > 1) {
	$page_links .= '<center><a href="' . $_SERVER['PHP_SELF'] . 
	'?usersearch=' . $user_search .
	'&sort=' . $sort .
	'&page=' . ($cur_page - 1) . '"><-</a></center>'; 
	}
 	else {
    	$page_links .= '<center><-</center>';
 	}
   // Loop through the pages generating the page number links
   for ($i = 1; $i <= $num_pages; $i++) {
   	if ($cur_page == $i) {
 		$page_links .= ' ' . '<center>' . $i . '</center>';
	}
	else {
  	$page_links .= '<center><a href="' . $_SERVER['PHP_SELF'] .
  	'?usersearch=' . $user_search .
  	'&sort=' . $i . '"> ' . $i . '</a></center>';
	}
   } 
   // If this page is not the last page, generate the "Next" link
   if ($cur_page < $num_pages) {
 	$page_links .= '<center><a href="' . $_SERVER['PHP_SELF'] . 
 	'?usersearch=' . $user_search . 
 	'&sort=' . $sort . 
 	'&page=' . ($cur_page + 1) . '">-></a></center>';
   }
   else {
	$page_links .= '<center>-></center>';
   }
   return $page_links;
 }  

   // Grab the sort setting and search keywords from the URL using GET
  $sort = $_GET['sort'];
  $user_search = $_GET['usersearch'];
  
  // Calculate pagination information
	$cur_page = isset($GET['page']) ? $_GET['page'] : 1;
	$results_per_page = 5; // number of results per page
	$skip = (($cur_page - 1) * $results_per_page);
	
  // Start generating the table of results
  echo '<table border="0" cellpadding="2">';
	
 // Generate the search results headings
 echo '<tr class="heading">';
 echo generate_sort_links($user_search, $sort);
 echo '</tr>';

 // Connect to the database
 require_once('connectvars.php');
 $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
	or die(DB_CONNECT_ERR . mysqli_connect_error() . DB_CONNECT_ERR_NO . mysqli_connect_errno());
  
   // Query to get the total results
   $query = build_query($user_search, $sort);
   $result = mysqli_query($dbc,$query)
 	or die( SQL_SELECT_ERR . mysqli_error($dbc) . SQL_ERR_NO . mysqli_errno($dbc) . QUERY_USED . $query);
;
   $total = mysqli_num_rows($result);
   $num_pages = ceil($total / $results_per_page);
   
   
   // Query again to get just the subset of results
   $query = $query . " LIMIT $skip, $results_per_page";
   $result = mysqli_query($dbc, $query)
  	or die( SQL_SELECT_ERR . mysqli_error($dbc) . SQL_ERR_NO . mysqli_errno($dbc) . QUERY_USED . $query);
	while ($row = mysqli_fetch_array($result)) {
	echo '<tr class="results">';
	echo '<td valign="top" width="20%">' . $row['title'] . '</td>';
	echo '<td valign="top" width="50%">' . substr($row['description'], 0, 100) . '...</td>';
	echo '<td valign="top" width="10%">' . $row['state'] . '</td>';
	echo '<td valign="top" width="20%">' . substr($row['date_posted'], 0, 10) . '</td>';
	echo '</tr>';
  } 
  echo '</table>';
  
  // Generate navigational page links if we have more than one page
  if ($num_pages > 1) {
	echo generate_page_links($user_search, $sort, $cur_page, $num_pages);
  }

  mysqli_close($dbc);
?>

</body>
</html>


I believe the problem is in the generate_page_links function.

As you can see from this pick, I'm clicked on page 3 but for some reason the echoed $cur_page value is still one and the previous page link is still static.

Attached Image: riskyjobs.png

Any thoughts, hints, suggestions on how to solve this? Any ideas why this is happening?

Thanks!
Tim
0

#2 User is offline   drewdin 

  • Super Veteran Member
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: Members
  • Posts: 589
  • Joined: 11-February 10
  • Gender:Male
  • Location:Boston

Posted 22 January 2013 - 12:58 PM

can you post another screenshot showing the page navigation?

based on this

$cur_page = isset($GET['page']) ? $_GET['page'] : 1


if it does not get set, its is being set to 1.
0

#3 User is offline   drewdin 

  • Super Veteran Member
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: Members
  • Posts: 589
  • Joined: 11-February 10
  • Gender:Male
  • Location:Boston

Posted 22 January 2013 - 01:34 PM

I found your issue, i wont give you the answer but ill give you the location. Lets see if you pass the test.

for ($i = 1; $i <= $num_pages; $i++) {
        if ($cur_page == $i) {
                $page_links .= ' ' . '<center>' . $i . '</center>';
        }
        else {
        $page_links .= '<center><a href="' . $_SERVER['PHP_SELF'] .'?usersearch=' . $user_search .'&sort=' . $i . '"> ' . $i . '</a></center>';
        }
   } 

0

#4 User is offline   bluethundr 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 30
  • Joined: 05-May 09
  • Gender:Male
  • Location:Summit, NJ

Posted 22 January 2013 - 04:09 PM

View Postlemuel, on 22 January 2013 - 01:40 PM, said:

$GET['page'] will never be set (the variable should be $_GET['page']) so $cur_page will always be 1.



Hi Lemuel,

Genuinely appreciate the help. That got it working! Working on training my eye to catch these little inconsistencies.

Best,
Tim
0

#5 User is offline   drewdin 

  • Super Veteran Member
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: Members
  • Posts: 589
  • Joined: 11-February 10
  • Gender:Male
  • Location:Boston

Posted 23 January 2013 - 05:51 AM

you had two issues...
0

#6 User is offline   bluethundr 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 30
  • Joined: 05-May 09
  • Gender:Male
  • Location:Summit, NJ

Posted 23 January 2013 - 06:37 AM

View Postdrewdin, on 22 January 2013 - 01:34 PM, said:

I found your issue, i wont give you the answer but ill give you the location. Lets see if you pass the test.

for ($i = 1; $i <= $num_pages; $i++) {
        if ($cur_page == $i) {
                $page_links .= ' ' . '<center>' . $i . '</center>';
        }
        else {
        $page_links .= '<center><a href="' . $_SERVER['PHP_SELF'] .'?usersearch=' . $user_search .'&sort=' . $i . '"> ' . $i . '</a></center>';
        }
   } 



Hello drewdin,

Thanks for following up. Yep! I found those too, but first I had to figure out why the page didn't understand it was on anything other than page one. But yes this was a rather major formatting gaffe on my part. So I basically got rid of those ridiculous '<center></center>' placements and ended up achieving the desired affect by placing those center tags a bit more judiciously:

// Generate navigational page links if we have more than one page
  if ($num_pages > 1) {
	echo '<br />';
	echo '<center>';
	echo generate_page_links($user_search, $sort, $cur_page, $num_pages);
	echo '</center>';
  }


Thank you for your input as well.
0

#7 User is offline   drewdin 

  • Super Veteran Member
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: Members
  • Posts: 589
  • Joined: 11-February 10
  • Gender:Male
  • Location:Boston

Posted 23 January 2013 - 11:34 AM

 bluethundr, on 23 January 2013 - 06:37 AM, said:

Hello drewdin,

Thanks for following up. Yep! I found those too, but first I had to figure out why the page didn't understand it was on anything other than page one. But yes this was a rather major formatting gaffe on my part. So I basically got rid of those ridiculous '<center></center>' placements and ended up achieving the desired affect by placing those center tags a bit more judiciously:

// Generate navigational page links if we have more than one page
  if ($num_pages > 1) {
	echo '<br />';
	echo '<center>';
	echo generate_page_links($user_search, $sort, $cur_page, $num_pages);
	echo '</center>';
  }


Thank you for your input as well.


You should look into css to take care of formatting and separate the html, php and css to keep your code clean.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users