SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Location
    God's Country (Middlesbrough to everyone else)
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    List Boxes used with switch statements

    I'm trying to search through a series of band name links, I want to give the user the option of searching by genre or band name, then being able to enter the relevant search into an edit box.

    I can't get it to work. Ignore the queries because they work I think, I'm really not sure what goes in the switch brackets because there isn't much on the internet about using switch statements in conjunction with list boxes.

    There's the html for the list box below.

    Code:
    <select name="select">
              <option value="genre">Genre</option>
              <option value="name">Band Name</option>
            </select>
    <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
            <input type="text" name="search">
            <input type="submit" name="Submit" value="Search">
    </form>
    Here's the switch statement, apologies for the side scrolling but it's done in my web hosts text editor which isn't the best.

    PHP Code:

    $search
    =$_POST['search'];

                                    
    switch (
    "select"

                                            case 
    "genre"
                                            
    $sql="SELECT * FROM band where genre='$search'"
                                 
    $result=mysql_query($query) or die(mysql_error());
        
                      while (
    $row mysql_fetch_assoc($result))      
            
            {
                
                echo 
    '<p><a href="bandview.php?bandname='.$row['band_name'].'">'.$row['band_name'].'</a></p>';             
            
            }

                                            
                                            break; 

                                            case 
    "band"
                                                   
    $sql="SELECT * FROM band where band_name='$search'"
    $result=mysql_query($query) or die(mysql_error());
        
                      while (
    $row mysql_fetch_assoc($result))      
            
            {
                
                echo 
    '<p><a href="bandview.php?bandname='.$row['band_name'].'">'.$row['band_name'].'</a></p>';             
            
            }

                                            
                                            break; 


                        default:
       
        
    $query="SELECT * FROM band";
            
    $result=mysql_query($query) or die(mysql_error());
            while (
    $row mysql_fetch_assoc($result))      
            
            {
                
                echo 
    '<p><a href="bandview.php?bandname='.$row['band_name'].'">'.$row['band_name'].'</a></p>';             
            
            }
                                            


  2. #2
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You'll want your select inside the form tags and use:
    $search=$_POST['select']; (or rename the select tag to 'search')

  3. #3
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you want switch($select), not switch('select'). Going switch('select'); is comparing the string "select" to each of the cases.

    Oh, and you shouldn't use short tags.

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Location
    God's Country (Middlesbrough to everyone else)
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <form action="<?php=$_SERVER['PHP_SELF']?>" method="post">
            <select name="select">
              <option value="genre">Genre</option>
              <option value="name">Band Name</option>
            </select>
            <input type="text" name="search">
            <input type="submit" name="Submit" value="Search">
    </form>
    Better?

    I put in:

    PHP Code:
    $search=$_POST['select'];

    //and

    switch ($_POST['select']) 
    It's responding now but it keeps saying the query is empty, as far as I know I've used the correct syntax on the queries and I've checked the database field names are correct.

  5. #5
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $result=mysql_query($query)
    should be:
    $result=mysql_query($sql)

    as you do $sql = above it

  6. #6
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should be using <?php echo $_SERVER['PHP_SELF']; ?>.

  7. #7
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Location
    God's Country (Middlesbrough to everyone else)
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can't believe I missed that, I'm tired. Thanks for the help you two, just getting nothing coming up now rather then query empty. Works fine when the page loads up and all of the bands are being displayed though just not when I'm searching.

  8. #8
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Location
    God's Country (Middlesbrough to everyone else)
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Done now, thanks.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •