SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help displaying previously selected data

    Hi! I am new to PHP. I am trying to make a summary page for previously selected information. My problem is I can only show
    the first information that is selected. I can't show the rest. Can anybody please help me?
    Here is the code that I wrote:

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html lang="EN" dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
    <head>
      <title>
          Pick a Place to Dine
      </title>
      <link rel = "stylesheet" type = "text/css" href = "" />
    </head>
     
    <body>

    <h1>Pick a Place to Dine</h1>

    <?php


    $restaurant 
    filter_input(INPUT_POST"restaurant");
    $italian filter_input(INPUT_POST"italian");
    $american filter_input(INPUT_POST"american");
    $asian filter_input(INPUT_POST"asian");
    $drink filter_input(INPUT_POST"drink");
    $dessert filter_input(INPUT_POST"dessert");

    if(!
    filter_has_var(INPUT_POST"destination"))
    {
      
    restaurant();
    }
       if (
    $restaurant == "Italian")
       {
             
    showRestaurant();
          
    italian();
            if (
    $italian == "Pasta")
            {
                
    showRestaurant();
                
    showItalian();
                
    drink();
                  if (
    $drink == "Soda")
                  {
                        
    showRestaurant();
                      
    showItalian();
                      
    showDrink();
                      
    dessert();
                      
                        if(
    dessert == "Cake")
                        {
                            
    showRestaurant();
                            
    showItalian();
                            
    showDrink();
                            
    showDessert();
                        }
                        else if(
    $dessert == "Cobbler")
                        {
                            
    showRestaurant();
                            
    showItalian();
                            
    showDrink();
                            
    showDessert();
                        }
                        else if(
    $dessert == "Pie")
                        {
                            
    showRestaurant();
                            
    showItalian();
                            
    showDrink();
                            
    showDessert();
                        }
                  }
                  else if (
    $drink == "Tea")
                  {
                        
    showRestaurant();
                      
    showItalian();
                      
    showDrink();
                      
    dessert();
                         if(
    dessert == "Cake")
                        {
                            
    showRestaurant();
                            
    showItalian();
                            
    showDrink();
                            
    showDessert();
                        }
                        else if(
    $dessert == "Cobbler")
                        {
                            
    showRestaurant();
                            
    showItalian();
                            
    showDrink();
                            
    showDessert();
                        }
                        else if(
    $dessert == "Pie")
                        {
                            
    showRestaurant();
                            
    showItalian();
                            
    showDrink();
                            
    showDessert();
                        }
                        
                  }
                  else if (
    $drink == "Juice")
                  {
                        
    showRestaurant();
                      
    showItalian();
                      
    showDrink();
                      
    dessert();
                      
                         if(
    dessert == "Cake")
                        {
                            
    showRestaurant();
                            
    showItalian();
                            
    showDrink();
                            
    showDessert();
                        }
                        else if(
    $dessert == "Cobbler")
                        {
                            
    showRestaurant();
                            
    showItalian();
                            
    showDrink();
                            
    showDessert();
                        }
                        else if(
    $dessert == "Pie")
                        {
                            
    showRestaurant();
                            
    showItalian();
                            
    showDrink();
                            
    showDessert();
                        }
                  }
            }
            else if (
    $italian == "Pizza")
            {
                
    showRestaurant();
                
    showItalian();
                
    drink();
                    if (
    $drink == "Soda")
                    {
                         
    showRestaurant();
                       
    showItalian();
                       
    showDrink();
                       
    dessert();
                        if(
    dessert == "Cake")
                        {
                            
    showRestaurant();
                            
    showItalian();
                            
    showDrink();
                            
    showDessert();
                        }
                        else if(
    $dessert == "Cobbler")
                        {
                            
    showRestaurant();
                            
    showItalian();
                            
    showDrink();
                            
    showDessert();
                        }
                        else if(
    $dessert == "Pie")
                        {
                            
    showRestaurant();
                            
    showItalian();
                            
    showDrink();
                            
    showDessert();
                        }
                    }
                    else if (
    $drink == "Tea")
                    {
                         
    showRestaurant();
                       
    showItalian();
                       
    showDrink();
                       
    dessert();
                       
                        if(
    dessert == "Cake")
                        {
                            
    showRestaurant();
                            
    showItalian();
                            
    showDrink();
                            
    showDessert();
                        }
                        else if(
    $dessert == "Cobbler")
                        {
                            
    showRestaurant();
                            
    showItalian();
                            
    showDrink();
                            
    showDessert();
                        }
                        else if(
    $dessert == "Pie")
                        {
                            
    showRestaurant();
                            
    showItalian();
                            
    showDrink();
                            
    showDessert();
                        }
                    }
                    else if (
    $drink == "Juice")
                    {
                         
    showRestaurant();
                       
    showItalian();
                       
    showDrink();
                       
    dessert();
                       
                         if(
    dessert == "Cake")
                        {
                            
    showRestaurant();
                            
    showItalian();
                            
    showDrink();
                            
    showDessert();
                        }
                        else if(
    $dessert == "Cobbler")
                        {
                            
    showRestaurant();
                            
    showItalian();
                            
    showDrink();
                            
    showDessert();
                        }
                        else if(
    $dessert == "Pie")
                        {
                            
    showRestaurant();
                            
    showItalian();
                            
    showDrink();
                            
    showDessert();
                        }
                    }  
            }
            else if (
    $italian == "Lasagna")
            {
                
    showRestaurant();
                
    showItalian();
                
    drink();
                    if (
    $drink == "Soda")
                    {
                        
    showRestaurant();
                      
    showItalian();
                      
    showDrink();
                      
    dessert();
                        
                        if(
    dessert == "Cake")
                        {
                            
    showRestaurant();
                            
    showItalian();
                            
    showDrink();
                            
    showDessert();
                        }
                        else if(
    $dessert == "Cobbler")
                        {
                            
    showRestaurant();
                            
    showItalian();
                            
    showDrink();
                            
    showDessert();
                        }
                        else if(
    $dessert == "Pie")
                        {
                            
    showRestaurant();
                            
    showItalian();
                            
    showDrink();
                            
    showDessert();
                        }
                      
                    }
                    else if (
    $drink == "Tea")
                    {
                        
    showRestaurant();
                      
    showItalian();
                      
    showDrink();
                      
    dessert();
                        
                        if(
    dessert == "Cake")
                        {
                            
    showRestaurant();
                            
    showItalian();
                            
    showDrink();
                            
    showDessert();
                        }
                        else if(
    $dessert == "Cobbler")
                        {
                            
    showRestaurant();
                            
    showItalian();
                            
    showDrink();
                            
    showDessert();
                        }
                        else if(
    $dessert == "Pie")
                        {
                            
    showRestaurant();
                            
    showItalian();
                            
    showDrink();
                            
    showDessert();
                        }
                    }
                    else if (
    $drink == "Juice")
                    {
                        
    showRestaurant();
                      
    showItalian();
                      
    showDrink();
                      
    dessert();
                      
                        if(
    dessert == "Cake")
                        {
                            
    showRestaurant();
                            
    showItalian();
                            
    showDrink();
                            
    showDessert();
                        }
                        else if(
    $dessert == "Cobbler")
                        {
                            
    showRestaurant();
                            
    showItalian();
                            
    showDrink();
                            
    showDessert();
                        }
                        else if(
    $dessert == "Pie")
                        {
                            
    showRestaurant();
                            
    showItalian();
                            
    showDrink();
                            
    showDessert();
                        }
                      
                   }
            }
       }
       else if (
    $restaurant == "American")
       {
          
    showRestaurant();
          
    american();
             if (
    $american == "Steak")
             {
                 
    showRestaurant();
                
    showAmerican();
                
    drink();
                    
                    if (
    $drink == "Soda")
                    {
                        
    showRestaurant();
                      
    showAmerican();
                      
    showDrink();
                    }
                    else if (
    $drink == "Tea")
                    {
                        
    showRestaurant();
                      
    showAmerican();
                      
    showDrink();
                    }
                    else if (
    $drink == "Juice")
                    {
                        
    showRestaurant();
                      
    showAmerican();
                      
    showDrink();
                    }
                   
             }
             else if (
    $american == "Burger")
             {
                 
    showRestaurant();
                
    showAmerican();
                
    drink();
                    
                    if (
    $drink == "Soda")
                    {
                        
    showRestaurant();
                      
    showAmerican();
                      
    showDrink();
                    }
                    else if (
    $drink == "Tea")
                    {
                        
    showRestaurant();
                      
    showAmerican();
                      
    showDrink();
                    }
                    else if (
    $drink == "Juice")
                    {
                        
    showRestaurant();
                      
    showAmerican();
                      
    showDrink();
                    }
             }
             else if (
    $american == "Rib")
             {
                 
    showRestaurant();
                
    showAmerican();
                
                   if (
    $drink == "Soda")
                    {
                        
    showRestaurant();
                      
    showAmerican();
                      
    showDrink();
                    }
                    else if (
    $drink == "Tea")
                    {
                        
    showRestaurant();
                      
    showAmerican();
                      
    showDrink();
                    }
                    else if (
    $drink == "Juice")
                    {
                        
    showRestaurant();
                      
    showAmerican();
                      
    showDrink();
                    }
             }
       }
       else if (
    $restaurant == "Asian")
       {
             
    showRestaurant();
          
    asian();
             
             if (
    $asian == "Noodles")
             {
                 
    showRestaurant();
                
    showAsian();
                
    drink();
                 if (
    $drink == "Soda")
                    {
                        
    showRestaurant();
                      
    showAsian();
                      
    showDrink();
                    }
                    else if (
    $drink == "Tea")
                    {
                        
    showRestaurant();
                      
    showAsian();
                      
    showDrink();
                    }
                    else if (
    $drink == "Juice")
                    {
                        
    showRestaurant();
                      
    showAsian();
                      
    showDrink();
                    }
                  
             }
             else if (
    $asian == "Sushi")
             {
                 
    showRestaurant();
                
    showAsian();
                
    drink();
                if (
    $drink == "Soda")
                    {
                        
    showRestaurant();
                      
    showAsian();
                      
    showDrink();
                    }
                    else if (
    $drink == "Tea")
                    {
                        
    showRestaurant();
                      
    showAsian();
                      
    showDrink();
                    }
                    else if (
    $drink == "Juice")
                    {
                        
    showRestaurant();
                      
    showAsian();
                      
    showDrink();
                    }
                  
             }
             else if (
    $asian == "Soba")
             {
                 
    showRestaurant();
                
    showAsian();
                
    drink();
                
                if (
    $drink == "Soda")
                    {
                        
    showRestaurant();
                      
    showAsian();
                      
    showDrink();
                    }
                    else if (
    $drink == "Tea")
                    {
                        
    showRestaurant();
                      
    showAsian();
                      
    showDrink();
                    }
                    else if (
    $drink == "Juice")
                    {
                        
    showRestaurant();
                      
    showAsian();
                      
    showDrink();
                    }
                  
             }
       }


    function 
    restaurant()
    {
        
          print <<<HERE

                   <form action = "" method = "post">
                      <fieldset>
                            <select size ="1" name = "restaurant">
                               <option selected = "selected">--- Please Choose Your Restaurant ---</option>
                               <option value = "Italian">Italian</option> 
                               <option value = "American">American</option> 
                               <option value = "Asian">Asian</option>
                            </select> 
                            
                            <input type = "submit" value = "go" /> 
                                
                      </fieldset>
                   </form>
            
    HERE;
    }
    //end of restaurant
        
    function italian()    
    {
         
     print <<<HERE

                   <form action = "" method = "post">
                      <fieldset>
                            <select size ="1" name = "italian">
                               <option selected = "selected">--- Please Choose an Item ---</option>
                               <option value = "Pasta">Pasta</option> 
                               <option value = "Pizza">Pizza</option> 
                               <option value = "Lasagna">Lasagna</option>
                            </select>   
                            <input type = "submit" value = "go" /> 
                            <br />
                      </fieldset>
                   </form>
    HERE;
    }
    //end of italian
      
    function american()
    {
          
    print <<<HERE
       
       <form action = "" method = "post">
                      <fieldset>
                            <select size ="1" name = "american">
                               <option selected = "selected">--- Please Choose an Item ---</option>
                               <option value = "Steak">Steak</option> 
                               <option value = "Burger">Burger</option> 
                               <option value = "Rib">Rib</option>
                            </select>   
                            <input type = "submit" value = "go" /> 
                      </fieldset>
                   </form>
            
    HERE;
    }
    //end of american
       
    function asian()
    {
        
    print <<<HERE
       
       <form action = "" method = "post">
                      <fieldset>
                            <select size ="1" name = "asian">
                               <option selected = "selected"> --- Please Choose an Item --- </option>
                               <option value = "Noodles">Noodles</option> 
                               <option value = "Sushi">Sushi</option> 
                               <option value = "Soba">Soba</option>
                            </select>   
                            <input type = "submit" value = "go" /> 
                      </fieldset>
                   </form>
            
    HERE;
    }
    //end of asian

    function drink()
    {
        
    print <<<HERE
       
       <form action = "" method = "post">
                      <fieldset>
                            <select size ="1" name = "drink">
                               <option selected = "selected">--- Please Choose a Drink ---</option>
                               <option value = "Soda">Soda</option> 
                               <option value = "Tea">Tea</option> 
                               <option value = "Juice">Juice</option>
                            </select>   
                            <input type = "submit" value = "go" /> 
                            <br />
                      </fieldset>
                   </form>
            
    HERE;
    }
    //end of drink

    function dessert()
    {
        
         
    print <<<HERE
       
       <form action = "" method = "post">
                      <fieldset>
                            <select size ="1" name = "dessert">
                               <option selected = "selected">--- Please Choose a Dessert ---</option>
                               <option value = "Cake">Cake</option> 
                               <option value = "Cobbler">Cobbler</option> 
                               <option value = "Pie">Pie</option>
                            </select>     
                            <br />
                      </fieldset>
                   </form>
    HERE;
    //end of dessert

    function showRestaurant()
    {
        global 
    $restaurant;
        print 
    "<h3>You choose an $restaurant restaurant.</h3><br />";
    }

    function 
    showItalian()
    {
        global 
    $italian;
        print 
    "<h3>You choose $italian</h3><br />";    
    }  

    function 
    showAmerican()
    {
        global 
    $american;
        print 
    "<h3>You choose $american</h3><br />";    
    }

    function 
    showAsian()
    {
        global 
    $asian;
        print 
    "<h3>You choose $asian</h3><br />";    
    }

    function 
    showDrink()
    {
        global 
    $drink;
        print 
    "<h3>You choose $drink</h3> <br />";
    }

    function 
    showDessert()
    {
        global 
    $dessert;
        print 
    "<h3>You choose $dessert";
    }


    ?>
    </body>
    </html>
    Last edited by Mittineague; Oct 23, 2011 at 21:19. Reason: reformatting bbcode tags

  2. #2
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    your code confuses me. to me it seems there is "ALOT" of uneccessary repeated code. small example..

    PHP Code:
     if(dessert == "Cake"
                        { 
                            
    showRestaurant(); 
                            
    showItalian(); 
                            
    showDrink(); 
                            
    showDessert(); 
                        } 
                        else if(
    $dessert == "Cobbler"
                        { 
                            
    showRestaurant(); 
                            
    showItalian(); 
                            
    showDrink(); 
                            
    showDessert(); 
                        } 
                        else if(
    $dessert == "Pie"
                        { 
                            
    showRestaurant(); 
                            
    showItalian(); 
                            
    showDrink(); 
                            
    showDessert(); 
                        } 
    each Else statement shows the exact same thing no matter if$dessert is equal to cake, pie, cobbler they all show the same functions. maybe this can be shortened? (just a suggestion)

    probably just me but if the code was smaller i may be able to help sorry but hey maybe someone here could have a solution for you

  3. #3
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,501
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by AndyMak View Post
    your code confuses me. to me it seems there is "ALOT" of uneccessary repeated code. small example..
    Indeed.
    And it doesn't seem like a summary page? It seems like the page where you show the form to select data?

    Also, I get the feeling that you expect the code to wait for user input when you 'print' a form, and then continue at that point in the php code when the user sends the form? Is that true?
    If so, then let me tell you, it doesn't. Whenever the user sends a form, the php code called by the form is executed from the very beginning.

    Maybe you could explain to us in plain english what you want to do?

  4. #4
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am trying to let the user choose from the drop-down list. For example if the user selects "Asian", it prints Asian and shows the choices for Asian food. For example the user selects "Sushi", it will display "Asian", and "Sushi" and the drop-down list for drinks. After selecting a drink for example "Tea", it will display "Asian", "Sushi", "Tea", and the drop-down list for dessert. I don't know how to correct the codes to make the next selection possible.

  5. #5
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,501
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    With a 'normal' form, you'd have all the drop down boxes visible and populated from the start.
    If you want a form that gradually displays new dropdown boxes based on the choises made so far, you have a couple of possibilities:

    1) only PHP: You first create a form with the first drop down. The user makes a choice and sends the form. The PHP script receives the form values, and creates the new form based on the received values. Etc etc.

    2) PHP and Javascript: You create a form with the first drop down, and store all data needed for all drop down boxes in javascript variables (or you create a form with all possible populated drop down boxes needed), and then use javascript to display/populate the following drop downs when needed based on the user choices.

    3) PHP, Javascript and AJAX: You create a form with the first drop down. You use javascript to send an AJAX request to the server whenever the user changes the value of a dropdown. The PHP script called by the AJAX request creates the HTML code for the next dropdown and sends it back to the browser, where the javascript will put it in the right place.

  6. #6
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you. I displayed initial form and used your option number 2. I used array to hold the values. I also used <input type="hidden" name="" value="<?php print $_POST['asian'];?>"> (for example) to keep the data the user has selected. It works. Thank you.


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
  •