Having problem with if and switch statements

Discussion in 'PHP' started by bits, May 31, 2008.

  1. #1
    Hello, What is happening is the program I have written is getting stuck on the second case. I need to have the last two cases accessible as well... The solution has been eluding me for the past week. Maybe someone on here can point out the issue and give a solution or alternative to the code I have.

    
    //check to see if variables are being used on form.
    if(isset($_GET['thick1']) || isset($_GET['thick2'])){
    //if true cycle through cases.
    switch(1)
    {
    case ($_GET['thick1']!="" && $_GET['thick2']!=""):
    $q="select * from $table where pthick between $_GET[thick1] and $_GET[thick2] order by pthick LIMIT $start,$limit";
    break;
    case ($_GET['thick1']=="" && $_GET['thick2']==""):
    $q="select * from $table where pthick like '%$_GET[thick1]%' order by pthick LIMIT $start,$limit";
    //this case is where I seem to have the problem on each of the three switch //statements
    break;
    case ($_GET['thick1']!="" && $_GET['thick2']==""):
    $q="select * from $table where pthick = $_GET[thick1] order by pthick LIMIT $start,$limit";
    break;
    case ($_GET['thick1']=="" && $_GET['thick2']!=""):
    $q="select * from $table where pthick = $_GET[thick2] order by pthick LIMIT $start,$limit";
    break;
    }
    }
    
    if(isset($_GET['height1']) || isset($_GET['height2'])){
    switch(1)
    {
    case ($_GET['height1']!="" && $_GET['height2']!=""):
    $q="select * from $table where pwidth between $_GET[height1] and $_GET[height2] order by pwidth LIMIT $start,$limit";
    break;
    case ($_GET['height1']=="" && $_GET['height2']==""):
    $q="select * from $table where pwidth like '%$_GET[height1]%' order by pwidth LIMIT $start,$limit";
    break;
    case ($_GET['height1']!="" && $_GET['height2']==""):
    $q="select * from $table where pwidth = $_GET[height1] order by pwidth LIMIT $start,$limit";
    break;
    case ($_GET['height1']=="" && $_GET['height2']!=""):
    $q="select * from $table where pwidth = $_GET[height2] order by pwidth LIMIT $start,$limit";
    break;
    }
    }
    
    if(isset($_GET['date1']) || isset($_GET['date2'])){
    switch(1)
    {
    case ($_GET['date1']!="" && $_GET['date2']!=""):
    $q="select * from $table where pdate between $_GET[date1] and $_GET[date2] order by pdate LIMIT $start,$limit";
    break;
    case ($_GET['date1']=="" && $_GET['date2']==""):
    $q="select * from $table where pdate like '%$_GET[date1]%' order by pdate LIMIT $start,$limit";
    break;
    case ($_GET['date1']!="" && $_GET['date2']==""):
    $q="select * from $table where pdate = $_GET[date1] order by pdate LIMIT $start,$limit";
    break;
    case ($_GET['date1']=="" && $_GET['date2']!=""):
    $q="select * from $table where pdate = $_GET[date2] order by pdate LIMIT $start,$limit";
    break;
    }
    }
    PHP:

     
    bits, May 31, 2008 IP
  2. AFitch

    AFitch Peon

    Messages:
    45
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    //check to see if variables are being used on form.
    if(isset($_GET['thick1']) || isset($_GET['thick2'])){
    //if true cycle through cases.
    if ($_GET['thick1']!="" && $_GET['thick2']!="") {
    $q="select * from $table where pthick between $_GET[thick1] and $_GET[thick2] order by pthick LIMIT $start,$limit";
    } elseif ($_GET['thick1']=="" && $_GET['thick2']=="") {
    $q="select * from $table where pthick like '%$_GET[thick1]%' order by pthick LIMIT $start,$limit";
    } elseif ($_GET['thick1']!="" && $_GET['thick2']=="") {
    $q="select * from $table where pthick = $_GET[thick1] order by pthick LIMIT $start,$limit";
    } elseif ($_GET['thick1']=="" && $_GET['thick2']!="") {
    $q="select * from $table where pthick = $_GET[thick2] order by pthick LIMIT $start,$limit";
    }
    }
    
    PHP:
    Let me know if that works.
     
    AFitch, May 31, 2008 IP
  3. cornetofreak

    cornetofreak Peon

    Messages:
    170
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Afitch is on the ball today ++
     
    cornetofreak, May 31, 2008 IP
  4. bits

    bits Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hello you can test out the current version at
    http://www.bitsonline.us/don/advsearch.html

    I will have the query shown so you can see where it gets stuck. I will add your code you gave me and comment it out until you have seen what is going on. Btw Thanks for the snippet of code. Hope it works.
     
    bits, May 31, 2008 IP
  5. bits

    bits Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Sorry Afitch. That still didnt work. It gets hung up in the same area.... :(
     
    bits, May 31, 2008 IP
  6. AFitch

    AFitch Peon

    Messages:
    45
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    instead of using $_GET['thick2']!="" to see if it is blank, use the empty variable like this:
    
    if (!empty($_GET['thick1']) && (!empty($_GET['thick2']) {
    
    PHP:
    Change all your statements to do it that way instead. "!empty" makes sure it is not empty, and "empty" makes sure it is empty.
     
    AFitch, Jun 1, 2008 IP