Jump to content

Help - 4 queries within an if-then-else. 2 work and 2 don't.


Recommended Posts

Hello. Is there anything wrong with this snipet of code?  I want to run a query based on a condition set forth in the if-then statment.  The first two shown in all green work perfectly.  The second two don't work.

 

$cat_id = $_GET['cat_id'];
$state = $_GET['state'];

if (($cat_id == "00") && ($state == "00")) {
[color=green]$query = "SELECT name,address,city,state,zip,phone,web,other FROM ydc ORDER BY name ASC";[/color]
} else if (($cat_id != "00") && ($state == "00")) {
[color=green]$query = "SELECT name,address,city,state,zip,phone,web,other FROM ydc WHERE cat_id = $cat_id ORDER BY name ASC";[/color]
} else if (($cat_id == "00") && ($state != "00")) {
[color=red]$query = "SELECT name,address,city,state,zip,phone,web,other FROM ydc WHERE state = $state ORDER BY name ASC";[/color]
} else {
[color=red]$query = "SELECT name,address,city,state,zip,phone,web,other FROM ydc WHERE cat_id = $cat_id AND state = $state ORDER BY name ASC";[/color]
}
$result = mysql_query($query) or die('Sorry, could not get the listings at this time, please try later');
if (mysql_num_rows($result) == 0) {
   echo "<h3>Sorry, there are no listings that match your selection.</h3>";
} else {
      while($row=mysql_fetch_array($result, MYSQL_ASSOC))
      {
$name = $row['name'];
$address = $row['address'];
$city = $row['city'];
$state = $row['state'];
$zip = $row['zip'];
$phone = $row['phone'];
$web = $row['web'];
$other = $row['other'];
echo "<h3>$name</h3>";
echo "$address<br />";
echo "$city, $state $zip<br />";
echo "$phone<br />";
echo "<a href=http://$web>Visit Website</a><br />";
echo "$other";
echo "<h2> </h2>";
      }
}

Thanks.  Will try.  Do you see any other problems?

 

Well, I actually created a live test of your code on my server and it's operating exactly as intended.

 

http://www.widocomputers.com/smarttime/EmptyPHP.php

 

Just click the different conditions at the bottom of the page to see it in action.

 

I'm not using the queries though, I'm just testing the conditions.

$cat_id = $_GET['cat_id'];
$state = $_GET['state'];

if (($cat_id == "00") && ($state == "00")) {
$query = "SELECT name,address,city,state,zip,phone,web,other FROM ydc ORDER BY name ASC";
} else if (($cat_id != "00") && ($state == "00")) {
$query = "SELECT name,address,city,state,zip,phone,web,other FROM ydc WHERE cat_id = $cat_id ORDER BY name ASC";
} else if (($cat_id == "00") && ($state != "00")) {
$query = "SELECT name,address,city,state,zip,phone,web,other FROM ydc WHERE state = '$state' ORDER BY name ASC";
} else {
$query = "SELECT name,address,city,state,zip,phone,web,other FROM ydc WHERE cat_id = $cat_id AND state = '$state' ORDER BY name ASC";
}
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) == 0) {
   echo "<h3>Sorry, there are no listings that match your selection.</h3>";
} else {
      while($row=mysql_fetch_array($result, MYSQL_ASSOC))
      {
$name = $row['name'];
$address = $row['address'];
$city = $row['city'];
$state = $row['state'];
$zip = $row['zip'];
$phone = $row['phone'];
$web = $row['web'];
$other = $row['other'];
echo "<h3>$name</h3>";
echo "$address<br />";
echo "$city, $state $zip<br />";
echo "$phone<br />";
echo "<a href=http://$web>Visit Website</a><br />";
echo "$other";
echo "<h2> </h2>";
      }
}

 

Try that. I think that will work because you have to use quotes whenever the data is not a integer.

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.