Jump to content

switches work fine separately but together they dont.


kb9yjg

Recommended Posts

Hello can someone tell me why this is working out the way it is? I have three sets of switches. One for thickness, one for height and the last for date. when I comment two of the three out the one that isnt commented out works perfectly fine. It is when I uncomment all three that i have issues with the switches. Heres the code with the top two commented out and the date uncommented. If someone can explain to me why its not working and how to fix it I would be eternally grateful!

/*if(isset($_GET['thick1']) || isset($_GET['thick2'])){
switch(1)
{
case ($_GET['thick1']!="" && $_GET['thick2']==""):
$q="select * from $table where pthick = $_GET[thick1] order by pthick ASC LIMIT $start,$limit";
break;
case ($_GET['thick1']=="" && $_GET['thick2']!=""):
$q="select * from $table where pthick = $_GET[thick2] order by pthick ASC LIMIT $start,$limit";
break;
default:
$q="select * from $table where pthick between $_GET[thick1] and $_GET[thick2] order by pthick ASC LIMIT $start,$limit";
break;
}
}
if(isset($_GET['height1']) || isset($_GET['height2'])){
switch(1)
{
case ($_GET['height1']!="" && $_GET['height2']==""):
$q="select * from $table where pwidth = $_GET[height1] order by pwidth ASC LIMIT $start,$limit";
break;
case ($_GET['height1']=="" && $_GET['height2']!=""):
$q="select * from $table where pwidth = $_GET[height2] order by pwidth ASC LIMIT $start,$limit";
break;
default:
$q="select * from $table where pwidth between $_GET[height1] and $_GET[height2] order by pwidth ASC LIMIT $start,$limit";
break;
}
}
*/
if(isset($_GET['date1']) || isset($_GET['date2'])){
switch(1)
{
case ($_GET['date1']!="" && $_GET['date2']==""):
$q="select * from $table where pdate = $_GET[date1] order by pdate ASC LIMIT $start,$limit";
break;
case ($_GET['date1']=="" && $_GET['date2']!=""):
$q="select * from $table where pdate = $_GET[date2] order by pdate ASC LIMIT $start,$limit";
break;
default:
$q="select * from $table where pdate between $_GET[date1] and $_GET[date2] order by pdate ASC LIMIT $start,$limit";
break;
}
}

The way it is set up it looks like the var $q is over written.

 

The first switch executes and sets $q = 'something', the second executes and sets $q = 'something else' then the 3 one runs and again sets it to something else...

 

for shits and giggles, uncomment all 3 echo $q at the bottom of the page and then run the code.. I think you will find $q equals whatever the last switch sets it as.

 

Nate

HAHAHA!! You hit it right on the head! It is set to the last statement in the switch for date! So now how would you rewrite this so that it takes each one into consideration. The funny part is that using if else and if elseif statements has not worked.

wait... so your trying to narrow down your query the best you can? I think you need to step back and thick about the logic, it seems to me that your just trying to jump in a get 'r done.

 

I see that your having $q set for all of them, but maybe this was initially intentional, but with the conditional statements and the fact that PHP run mostly from the top down, then the last true is being used...

 

so maybe you should have it set where the user sets the listing order,

 

and then have the conditional statements help build the query down to properly reduce the results.

 

That's my feedback...

Here is something I came up with, you can modify it as you wish, or if there is errors, (since it's untested)

 

<?php
/* Starts the Query String */
$q = "SELECY * FROM $table WHERE"

if(isset($_GET['thick1']) || isset($_GET['thick2'])){
switch(1) { // Adds to the Query as Necessary
	case ($_GET['thick1']!="" && $_GET['thick2']==""):
		$q.=" pthick = $_GET[thick1]";
		break;
	case ($_GET['thick1']=="" && $_GET['thick2']!=""):
		$q.=" pthick = $_GET[thick2]";
		break;
	default:
		$q.=" pthick between $_GET[thick1] and $_GET[thick2]";
		break;
}
$cond1 = true;
}

if(isset($_GET['height1']) || isset($_GET['height2'])){
if($cond1) // Adds the "AND" Statment to the Query if there was something was added from the previous condition
	$q.=" AND";
switch(1) { //Adds to the Query as Necessary
	case ($_GET['height1']!="" && $_GET['height2']==""):
		$q.=" pwidth = $_GET[height1]";
		break;
	case ($_GET['height1']=="" && $_GET['height2']!=""):
		$q.=" pwidth = $_GET[height2]";
		break;
	default:
		$q.=" between $_GET[height1] and $_GET[height2]";
		break;
}
$cond2 = true;
}

if(isset($_GET['date1']) || isset($_GET['date2'])){
if($cond1 || $cond2)
	$q.= " AND";
switch(1) {
	case ($_GET['date1']!="" && $_GET['date2']==""):
		$q.=" pdate = $_GET[date1]";
		break;
	case ($_GET['date1']=="" && $_GET['date2']!=""):
		$q.=" pdate = $_GET[date2]";
		break;
	default:
		$q.=" between $_GET[date1] and $_GET[date2]";
		break;
}
}

switch($_GET['sortby']) { // Sets the sorting based on user input (Select Drop-Down recommended)
case "thick":
	$q.=" ORDER BY pthick";
	break;
case "height":
	$q.=" ORDER BY pwidth";
	break;
default:
	$q.=" ORDER BY pdate";
}

$q.=" ASC LIMIT $start,$limit"; //Finishes the Query
?>

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.