Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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