Two switch statements on same page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mendenha
Forum Newbie
Posts: 19
Joined: Tue Jan 12, 2010 2:24 pm

Two switch statements on same page

Post by mendenha »

Ok, I've been messing around with this for a while and I can't seem to figure it out. I'm wanting to have 2 different switch statements on the same page. The page works great with 1, but when I add in the second one, nothing happens. Any help would be great, the code is below.

If you take out the second switch, everything is great. Once I add in the alpha switch back in, the page no longer loads anything. I'm not 100% sure this should even work like this, but I thought I would post it anyway.

Code: Select all

<?php
include("../include/opendbconnection.php");
$category = $_GET['category'];
switch($category)
{
case "American";
case "Asian";
	$q="Select * from tbl_restaurantinfo WHERE category='".$category."' and hours != 'Closed' ORDER BY name";
	break;

default;
       echo "Something Went Wrong";
}
$alpha = $_GET['alpha'];
switch($alpha)
{
case "$alpha";
	$q="Select * FROM tbl_restaurantinfo Where name Like '$alpha' and hours != 'Closed' ORDER BY name";
	break;
}
$rs=mysql_query($q) or die(mysql_error()); 
$nr = mysql_num_rows($rs); //Number of rows found with LIMIT in action 
$q0="Select FOUND_ROWS()"; 
$rs0=mysql_query($q0) or die(mysql_error()); 
$row0=mysql_fetch_array($rs0); 
$nr0 = $row0["FOUND_ROWS()"];
while ($row=mysql_fetch_array($rs))
{
echo $row['display_name'];
echo "<br />";
}
?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Two switch statements on same page

Post by Celauran »

I see a few problems here. First, you're naming both query strings $q. If $_GET['category'] and $_GET['alpha'] are both set, $_GET ['category'] will be ignored since it's query string, $q, will be overwritten by the alpha query string, also called $q. Next, aside from not escaping $_GET, your alpha query is looking for something like WHERE name LIKE 'foo' rather than WHERE name LIKE '%foo%'. Finally, I'm not really sure I see the point of using switch statements here at all.
mendenha
Forum Newbie
Posts: 19
Joined: Tue Jan 12, 2010 2:24 pm

Re: Two switch statements on same page

Post by mendenha »

After posting this I found my issue with alpha not returning anything. Just like you said, my Where name Like wasn't working correctly.

As for using switch statements, I'm just playing around and getting a feel for them. I've always used if elseif statements and throught I would give these a try and see how they work.

Thanks for the post Celauran
Post Reply