displaying records on user choice

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jasone
    New Member
    • Mar 2007
    • 66

    #1

    displaying records on user choice

    Hi all!
    im developing a system where a user can select what flower goes into their bouquet, and ive become a little bit stuck... heres what ive done to date:

    currently ive got a page displaying a tables records (the flowers) the page has a checkbox next to each record where the record primary key has been assigned to the checkbox.

    once the user select the items and clicks sumbit it takes them to a 'choice' page to display the flowers they have chosen. ive tried both the get and post functions and found that the get function displays all the keys in the url but when i try to read out the urls it only displays the last one, confused? ok

    so ive selcted items 3 and 4 in the list and select submit... in the url it shows: choice.php?flow erid=3&flowerid =4.
    but when i try to call these on the screen... records 3 and 4 it will only show record for...

    so heres the code im using for the submit page and action page:
    -------------------------submit page------------------------------------
    Code:
    <?PHP 
    $qry = mysql_query ('SELECT * FROM flowers');
    $result = mysql_fetch_array($qry);
    ?>
    <body>
    <form name="choice" action="choice.php" method="GET">
    <?PHP 
    
    while ($row = mysql_fetch_array($qry)){ 
    
    echo  $row['flowername']; ?>
    <input type="checkbox" value="<?php echo $row['flowerid']; ?>" name='flowerid'>
    <?PHP
    }
    
    ?> 
    <input type="submit" value="submit" />
    </form>
    -------------------------------------------------------------------------
    -------------------action page------------------------------------
    Code:
    <?php
        include "constants/flowertimedbcnx.php";
    	 $r = @$_GET['flowerid']; //users choice..
    	 $s = @$_GET['flowername'];
    	 
    	 ?>
              <?php 
    	  echo "You have chosen to buy flowers for your
    	  
    	  
    	   <b>$r, $s</b>.";
    ?>
    -------------------------------------------------------------------------

    if you need anymore information to help please let me know...

    ive also tried replacing the 'GET' with 'POST' but it still on displays the last record passed., but using 'GET' it showed that the 2 record ids were being passed.


    many thanks in advance!
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Firstly, the first statement [php]$result = mysql_fetch_arr ay($qry);
    [/php] must be removed, otherwise you skip the first row of your result.

    Secondly, the name 'flowerid' is identical for each input statement, so your $_GET array can contain only one entry $_GET['flowerid'] because there can only be 1 associative key flowerid in the $_GET array.
    So you must make the selection name an array by defining the name of the input field as 'flowerid[]' as in:
    [php]<input type="checkbox" value="<?php echo $row['flowerid']; ?>" name='flowerid[]'>[/php] This will make flowerid an array in the $_GET array. So when you want to check the checked flowerids you must walk array flowerid to get all choices. Add the following statements at the top of your choice.php script and you will see how it works.
    [php]echo 'You have chosen the following flowerids:<br>' ;
    foreach($_GET['flowerid'] as $flower) {
    echo "$flower<br >";
    }[/php]

    Good luck.

    Ronald

    Comment

    • jasone
      New Member
      • Mar 2007
      • 66

      #3
      I bow down before you!

      thanks very much for your help... i will probably be posting allot more queires for this system. but this is a big step in the right direction... thanks bery much ronald !

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        You are welcome and I'll see you again with more.

        Ronald

        Comment

        • jasone
          New Member
          • Mar 2007
          • 66

          #5
          ok, now im stuck again.. trying to get the flowerids to display other relevant fields in the database:

          heres what ive tried to display relating data:
          Code:
          <?php
              include "constants/flowertimedbcnx.php";
           
                echo 'You have chosen the following flowerids:<br>';
            
                foreach($_GET['flowerid'] as $flower) {
            
                    echo "$flower<br>";
                }
          	  ?>
                
           <?PHP 
           $sql = 'SELECT * FROM flowers WHERE flowerid = '. $flower;
           
           while ($row = mysql_fetch_array($sql)){ 
          
          echo  $row['flowername']; 
          echo "<br>";
          }
           
           
           ?>
          hope this can be done easily :-)

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            Yes it can. Best way is to display the flowername within the foreach construct, as follows:
            [php]
            echo 'You have chosen the following flowerids:<br>' ;
            foreach($_GET['flowerid'] as $flower) {
            $res = mysql_query ("SELECT * FROM flowers WHERE flowerid = $flower");
            while ($row = mysql_fetch_arr ay($res)) {
            echo "$flower = {$row[flowername]} <br>";
            }
            }[/php]
            Ronald

            Comment

            • jasone
              New Member
              • Mar 2007
              • 66

              #7
              hey, ive sent you a pm ron!

              next step is adding the selected flowers to a table... so multiple rows, identifed by a session username... have psoted a thread for this as i didnt want to be realy cheeky and ask for your support on this... but here's the link to it.:

              http://www.thescripts. com/forum/thread770700.ht ml

              many thanks

              jason!

              Comment

              Working...