O'Reilly Forums: Chapter 9 Sort Problem - O'Reilly Forums

Jump to content

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

Chapter 9 Sort Problem

#1 User is offline   bluethundr 

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

Posted 07 January 2013 - 07:24 PM

Hello,

I'm working on the 'riskyjob's example website. I've gotten to the point on page 546 where it asks you to incorporate the changes from page 544 and 545.


I've added the generate_sort_links to the search.php file:


// 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>';
    	
  	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;

 	}


And made the necessary changes to build_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;
  }


And so far I've had luck getting the Job Title and State columns to sort correctly in both forward and reverse direction.

Attached Image: rj1.png


But for some reason if I sort by date a second time to reverse the order the text becomes disjointed as I show in the second image.

Attached Image: rj2.png

I've pored over the code and can't quite seem to nail what could be causing this. I'd certainly appreciate any advice you could provide.

Here's the complete code from search.php in case that might help:

<!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
  // 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());
  
   // Grab the sort setting and search keywords from the URL using GET
	$sort = $_GET['sort'];
	$user_search = $_GET['usersearch'];
 	
	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;
  }
  
  $search_query = build_query($user_search, $sort);
  


  // 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>';
    	
  	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;

 	}
 	
 	
 	$links = generate_sort_links($user_search, $sort);
  
 	// Start generating the table of results
 	echo '<table border="0" cellpadding="2">';
 	echo $links;

 	$result = mysqli_query($dbc, $search_query)
  	or die( SQL_SELECT_ERR . mysqli_error($dbc) . SQL_ERR_NO . mysqli_errno($dbc) . QUERY_USED . $search_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>';
  
  echo '<br /><center>Back to <a href=' . 'search.html' . '>Search</a></center><br />';

  mysqli_close($dbc);
?>

</body>
</html>


Thank you
0

#2 User is offline   drewdin 

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

Posted 08 January 2013 - 02:28 PM

Try echoing out your query's and see what you got, try both of these.

$search_query


$search_query = build_query($user_search, $sort)

0

#3 User is offline   bluethundr 

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

Posted 08 January 2013 - 03:22 PM

View Postdrewdin, on 08 January 2013 - 02:28 PM, said:

Try echoing out your query's and see what you got, try both of these.

$search_query


$search_query = build_query($user_search, $sort)




Hey there,

Thanks so much for your reply! Actually I was able to find the source of the problem. It was the switch ($sort) statement in my code. Case 5 had no terminating break; statement. So if you were to select case 5 it would print the table data in case 5 AND the table in the default case. Causing the visual muddle if you tried to sort by it. This is what I had previously:


 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>';
        
      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>';



Changing the code to this allowed the page to work perfectly:


 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>';


Once again thank you for your input, but I'm glad i was able to work this one out on my own. It's definitely a skill I'll need if I intend to do this for a living some day (something I say with hope). Posted Image
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