SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Apr 2010
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How can I get the value of a switch case coming from the drop down form

    Hi I have a form that is popluated from the database now I am trying to get the switch case to be based on the option the user selects is this possible to make a case a variable popluated by the drop down if so can someone show me what am i missing?
    the value I am trying to get from the form to the switch case is the ( $val->slug) but the value not been set help please

    here the form
    PHP Code:
                                           <form method="post">
                                               <select name="deal-locations" onChange="this.form.submit()">
                                                 <?php
                                                     
    while(list($key,$val) = each($locations)) {
                                                 
    ?><option value="<?php echo $val->slug?>"><?php echo $val->slug?></option><?php
                                                     
    }
                                                  
    ?>
                                               </select>
                                               <input type="submit" value="submit">
                                           </form>
    Here the switch case
    PHP Code:
     switch ($_POST['deal-locations'])
       { 
           case 
    $val->slug": echo "<h3>deal-locations: ASC</h3>";
               
    $args = array(
                     
    's' => $_GET['s'],
                     
    'post_type' => 'deals',
                     
    'deal-locations' => ' $val->slug',
                     
    'where' => ' $val->slug',
                     
    'paged' => 'paged'
                    
    );
               break;


  2. #2
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    switch case is similar to if elseif

    This page gives you example of its usage:
    http://us.php.net/manual/en/control-...res.switch.php

    Your example would be something like this:
    PHP Code:
    <?php
       
    switch ($_POST['deal-locations'])
       { 
           case 
    "ASC": echo "<h3>deal-locations: ASC</h3>";
                   
    $args = array(
                         
    's' => $_GET['s'],
                         
    'post_type' => 'deals',
                         
    'deal-locations' => ' $val->slug',
                         
    'where' => ' $val->slug',
                         
    'paged' => 'paged'
                        
    );
                   break;
           case 
    "..."// ... = from your option values
                // code to handle this case
               
    break;
       }
    ?>

  3. #3
    SitePoint Addict
    Join Date
    Apr 2010
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi thanks for your response. I actually did look at the link you sent me before unfortunnaly the drop down option values is also populated by from the database so I do not want to everytime i create a new location I have to go in the code and add if location = france else location= england, i do not want to do that every single time so I want the case "$variable" so that whatver is on the options and selected will populate that case inside the switch function.
    ($val->slug) I want this variable on the switch case, if thats is possible like the one bellow
    one problem
    PHP Code:
        switch ($_POST['deal-locations'])
       { 
           case 
    "".$val->slug.""->slug: echo "<h3>deal-locations: "".$val->slug.""</h3>";
               
    $args = array(
                     
    's' => $_GET['s'],
                     
    'post_type' => 'deals',
                     
    'deal-locations' => "".$val->slug."",
                     
    'where' => "".$val->slug."",
                     
    'paged' => 'paged'
                    
    );
               break; 
    But it doesnt "".$val->slug."" doesnt get any value from the form?
    PHP Code:
     <form method="post">
                                               <select name="deal-locations" onChange="this.form.submit()">
                                                 <?php
                                                     
    while(list($key,$val) = each($locations)) {
                                                 
    ?><option value="<?php echo $val->slug?>"><?php echo $val->slug?></option><?php
                                                     
    }
                                                  
    ?>
                                               </select>
                                               <input type="submit" value="submit">
                                           </form>

  4. #4
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Although those values are in the database but you still should have a list of all the known values.

    What you need to do is put each one of those values in a case statement and to provide the appropriated code for it. The list maybe long if you were to include all the countries in the world. Or you could just process those that that are important like this:

    PHP Code:
    <?php
       $val
    ->slug $_POST['deal-locations'];
       switch (
    $val->slug)
       { 
           case 
    "United Kingdom": echo "1<h3>deal-locations: $val->slug</h3>";
                      
    $args = array(
                            
    's' => $_GET['s'],
                            
    'post_type' => 'deals',
                            
    'deal-locations' => $val->slug,
                            
    'where' => $val->slug,
                            
    'paged' => 'paged'
                               
    );
                      break;
           case 
    "France": echo "2<h3>deal-locations: $val->slug</h3>";
                      
    $args = array(
                            
    's' => $_GET['s'],
                            
    'post_type' => 'deals',
                            
    'deal-locations' => $val->slug,
                            
    'where' => $val->slug,
                            
    'paged' => 'paged'
                               
    );
                      break;
           case 
    "Germany": echo "3<h3>deal-locations: $val->slug</h3>";
                      
    $args = array(
                            
    's' => $_GET['s'],
                            
    'post_type' => 'deals',
                            
    'deal-locations' => $val->slug,
                            
    'where' => $val->slug,
                            
    'paged' => 'paged'
                               
    );
                      break;
           default: echo 
    "4<h3>deal-locations: $val->slug</h3>This location is not supported";
       }
    ?>
    If all the countries were used the same code for processing then you could simply use this:

    PHP Code:
       $val->slug $_POST['deal-locations'];
       if(
    $val->slug)
       { 
           
    $args = array(
                 
    's' => $_GET['s'],
                 
    'post_type' => 'deals',
                 
    'deal-locations' => $val->slug,
                 
    'where' => $val->slug,
                 
    'paged' => 'paged')
       } 
    There is no need for switch/case.

  5. #5
    SitePoint Addict
    Join Date
    Apr 2010
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help guys, tom8 yeah the if statment is working exacly how I thought I could achieve with the switch statment, it is exacly how I wanted Thanks for all the help everybody.


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
  •