Jump to content

[SOLVED] A to Z search help...


Scooby08

Recommended Posts

I found a simple snipplet online for a database A to Z search by name and am having troubles understanding what's going on with the do/while section.. Here is the code I'm using:

 

<?php  
$sort = $_REQUEST['letter'];

if($sort = "") {
$query = "SELECT * FROM dw_customers ORDER BY customer_bride ASC" ;
} else {
$query = "SELECT * FROM dw_customers WHERE customer_bride LIKE '$sort%' ORDER BY customer_bride ASC" ;
}

$result = mysql_query($query) or die(mysql_error());

for ($i = 65; $i < 91; $i++) {
printf('<a href = "%s?letter=%s">%s</a> | ', $PHP_SELF, chr($i), chr($i));
}
echo "</p>" ;

if (mysql_num_rows($result) > 0) {
do {
	echo "<p>" .$row['customer_bride']. "</p>" ;
} while ($row = mysql_fetch_assoc($result));
} else {
echo "<p>No customer found.</p>" ;
}
?>

 

Everything seems to work fine except for when I click a letter that is not in my database, I cannot seem to get to the  else { echo "<p>No customer found.</p>"; }

 

Can somebody help me to understand why I cannot get that to echo??

Link to comment
https://forums.phpfreaks.com/topic/120746-solved-a-to-z-search-help/
Share on other sites

Maybe try something like this.....

 

if($sort = "") {
$query = "SELECT * FROM dw_customers ORDER BY customer_bride ASC" ;
} else {
$query = "SELECT * FROM dw_customers WHERE  SUBSTRING(customer_bride,1) = '$sort' ORDER BY customer_bride ASC" ;
}

change this line

 

if($sort = "") {

 

to this

 

if($sort == "") {

 

 

One equal sign is an assignment....meaning it forces it to equal.... nothing.

Two equal signs are a comparison...meaning it compares it against....nothing.

 

And then there is ===

Which is a really strict comparison...usually for true and false comparisons

 

That's the most basic description I can come up with

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.