Jump to content


Photo

PHP Drop Down Menu


  • Please log in to reply
1 reply to this topic

#1 JackJack

JackJack
  • Members
  • PipPipPip
  • Advanced Member
  • 38 posts

Posted 03 April 2006 - 08:02 PM

I wish to create something that would have the data from another page which was a drop down box/menu.

Example:
echo "Choose select a <b>motor</b> for<br> your robot:    ";

echo "<select name='part1'>";

echo "<option value='null'>Select a Motor</option>";

                    $romotor = mysql_query("select * from `parts` where `owner` = '$stat[id]' and `slot` = 'Motor'");
                    while ($partuno = mysql_fetch_array($romotor)) {


echo "

<option value='$partuno[id]'>$partuno[name] </option>";
}
echo "</select>";

echo "<br><br>Now add a <b>shell</b> to<br> your robot:    ";

echo "<select name='part2'>";

echo "<option value='null'>Select a Shell</option>";

                    $roshell = mysql_query("select * from `parts` where `owner` = '$stat[id]' and `slot` = 'Shell'");
                    while ($partdeux = mysql_fetch_array($roshell)) {


echo "

<option value='$partdeux[id]'>$partdeux[name]</option>";

}
echo "</select>";

Next Page
        mysql_query("insert into bots (motor, shell) values('???','??????')")


Thank You For Your Help

JJ

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 03 April 2006 - 08:36 PM

You seemed to be repeating chunks of code so I put the option list build into a function. For clarity I also renamed the <SELECTS> as "motor" and "shell".

function getOptions($id, $type) {
         $str = "<OPTION value=''> - Select a $type -</OPTION>\n";
         $res = mysql_query("select id, name from `parts`
                where `owner` = '$id' and `slot` = '$type'");
         while (list($id, $name) = mtsql_fetch_row($res)) {
                $str .= "<OPTION value='$id'> $name</OPTION>\n";
         }
         return $str;
}

echo "<FORM action='nextpage.php' method='post'>\n";

echo "<p>Choose select a <b>motor</b> for<br> your robot:</p> ";
echo "<select name='motor'>";
echo getOptions($stat['id'], 'Motor');
echo  "</select>\n";

echo "<p>Now add a <b>shell</b> to<br> your robot:</p>";
echo "<select name='shell'>";
echo getOptions($stat['id'], 'Shell');
echo  "</select>\n";

echo "<p><INPUT TYPE='SUBMIT'  name='submit' value='Submit'></p>\n"
echo "</FORM>\n";


:: nextpage.php ::

  $motor = $_POST['motor'];
  $shell = $_POST['shell'];

  mysql_query ("INSERT INTO bots (motor, shell) VALUES ('$motor', '$shell')");

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users