bob1660 Posted June 5, 2009 Share Posted June 5, 2009 I a a NEWBIE with PHP and MySQL, and would like some help on how to add a drop down selection in a input field. EG. Code - <td class="hr"><?php echo htmlspecialchars("Hotel")." " ?></td> <td class="dr"><input type="text" name="Hotel" maxlength="1" value="<?php echo str_replace('"', '"', trim($row["Hotel"])) ?>"></td> Drop Down should only contain "Westin Grand", "Cullinen" and "Holliday Inn" Any Help will be greatly appreciated. NEWBIE ??? Link to comment https://forums.phpfreaks.com/topic/161084-drop-down-list-in-php-code/ Share on other sites More sharing options...
gevans Posted June 5, 2009 Share Posted June 5, 2009 Are you getting the data for the drop down from the db? If so you should be able to implement the following to do the rest; <?php $the_list = array("1" => "Westin Grand", "2" => "Cullinen", "3" => "Holliday Inn");//data you would get from db $drop_down = '<select>'; foreach($the_list as $k => $v) { $drop_down .= "<option value=\"$k\">$v</option>"; } $drop_down .= '</select>'; Link to comment https://forums.phpfreaks.com/topic/161084-drop-down-list-in-php-code/#findComment-850057 Share on other sites More sharing options...
bob1660 Posted June 5, 2009 Author Share Posted June 5, 2009 Thank you gevans, but the values are not in the DB, and will have to be hard coded. Link to comment https://forums.phpfreaks.com/topic/161084-drop-down-list-in-php-code/#findComment-850076 Share on other sites More sharing options...
gevans Posted June 5, 2009 Share Posted June 5, 2009 well, in the above example i hard coded them for you Link to comment https://forums.phpfreaks.com/topic/161084-drop-down-list-in-php-code/#findComment-850078 Share on other sites More sharing options...
bob1660 Posted June 5, 2009 Author Share Posted June 5, 2009 Tank yoy Gavin, As I said, VERY new. I attach the rest of the code, can you please indicate where I must insert your suggestion. <?php $conn = connect(); $showrecs = 40; $pagerange = 10; $a = @$_GET["a"]; $recid = @$_GET["recid"]; $page = @$_GET["page"]; if (!isset($page)) $page = 1; $sql = @$_POST["sql"]; switch ($sql) { case "insert": sql_insert(); break; case "update": sql_update(); break; } switch ($a) { case "add": addrec(); break; case "edit": editrec($recid); break; default: select(); break; } if (isset($order)) $_SESSION["order"] = $order; if (isset($ordtype)) $_SESSION["type"] = $ordtype; if (isset($filter)) $_SESSION["filter"] = $filter; if (isset($filterfield)) $_SESSION["filter_field"] = $filterfield; if (isset($wholeonly)) $_SESSION["wholeonly"] = $wholeonly; mysql_close($conn); ?> </body> </html> <?php function select() { global $a; global $showrecs; global $page; global $filter; global $filterfield; global $wholeonly; global $order; global $ordtype; if ($a == "reset") { $filter = ""; $filterfield = ""; $wholeonly = ""; $order = ""; $ordtype = ""; } $checkstr = ""; if ($wholeonly) $checkstr = " checked"; if ($ordtype == "asc") { $ordtypestr = "desc"; } else { $ordtypestr = "asc"; } $res = sql_select(); $count = sql_getrecordcount(); if ($count % $showrecs != 0) { $pagecount = intval($count / $showrecs) + 1; } else { $pagecount = intval($count / $showrecs); } $startrec = $showrecs * ($page - 1); if ($startrec < $count) {mysql_data_seek($res, $startrec);} $reccount = min($showrecs * $page, $count); ?> <hr size="1" noshade> <form action="delegate_details.php" method="post"> <table class="bd" border="0" cellspacing="1" cellpadding="4"> <tr> <td><b>Search Criteria</b> </td> <td><input type="text" name="filter" value="<?php echo $filter ?>"></td> <td><select name="filter_field"> <option value="">All Fields</option> <option value="<?php echo "Surname" ?>"<?php if ($filterfield == "Surname") { echo "selected"; } ?>><?php echo htmlspecialchars("Surname") ?></option> <option value="<?php echo "First Name" ?>"<?php if ($filterfield == "First Name") { echo "selected"; } ?>><?php echo htmlspecialchars("First Name") ?></option> <option value="<?php echo "Ministry" ?>"<?php if ($filterfield == "Ministry") { echo "selected"; } ?>><?php echo htmlspecialchars("Ministry") ?></option> <option value="<?php echo "Flight No" ?>"<?php if ($filterfield == "Flight No") { echo "selected"; } ?>><?php echo htmlspecialchars("Flight No") ?></option> <option value="<?php echo "Arrival Status" ?>"<?php if ($filterfield == "Arrival Status") { echo "selected"; } ?>><?php echo htmlspecialchars("Arrival Status") ?></option> <option value="<?php echo "Baggage Status" ?>"<?php if ($filterfield == "Baggage Status") { echo "selected"; } ?>><?php echo htmlspecialchars("Baggage Status") ?></option> <option value="<?php echo "Baggage No" ?>"<?php if ($filterfield == "Baggage No") { echo "selected"; } ?>><?php echo htmlspecialchars("Baggage No") ?></option> <option value="<?php echo "Clothing Req" ?>"<?php if ($filterfield == "Clothing Req") { echo "selected"; } ?>><?php echo htmlspecialchars("Clothing Req") ?></option> <option value="<?php echo "Hotel" ?>"<?php if ($filterfield == "Hotel") { echo "selected"; } ?>><?php echo htmlspecialchars("Hotel") ?></option> <option value="<?php echo "Room No" ?>"<?php if ($filterfield == "Room No") { echo "selected"; } ?>><?php echo htmlspecialchars("Room No") ?></option> </select></td> <td><input type="checkbox" name="wholeonly"<?php echo $checkstr ?>>Whole words only</td> </td></tr> <tr> <td> </td> <td><input type="submit" name="action" value="Search"></td> <td><a href="delegate_details.php?a=reset">Reset</a></td> </tr> </table> </form> <hr size="1" noshade> <?php showpagenav($page, $pagecount); ?> <br> <table class="tbl" border="0" cellspacing="1" cellpadding="5"width="100%"> <tr> <td class="hr"> </td> <td class="hr"><a class="hr" href="delegate_details.php?order=<?php echo "Surname" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Surname") ?></a></td> <td class="hr"><a class="hr" href="delegate_details.php?order=<?php echo "First Name" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("First Name") ?></a></td> <td class="hr"><a class="hr" href="delegate_details.php?order=<?php echo "Ministry" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Ministry") ?></a></td> <td class="hr"><a class="hr" href="delegate_details.php?order=<?php echo "Flight No" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Flight No") ?></a></td> <td class="hr"><a class="hr" href="delegate_details.php?order=<?php echo "Arrival Status" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Arrival Status") ?></a></td> <td class="hr"><a class="hr" href="delegate_details.php?order=<?php echo "Baggage Status" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Baggage Status") ?></a></td> <td class="hr"><a class="hr" href="delegate_details.php?order=<?php echo "Baggage No" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Baggage No") ?></a></td> <td class="hr"><a class="hr" href="delegate_details.php?order=<?php echo "Clothing Req" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Clothing Req") ?></a></td> <td class="hr"><a class="hr" href="delegate_details.php?order=<?php echo "Hotel" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Hotel") ?></a></td> <td class="hr"><a class="hr" href="delegate_details.php?order=<?php echo "Room No" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Room No") ?></a></td> </tr> <?php for ($i = $startrec; $i < $reccount; $i++) { $row = mysql_fetch_assoc($res); $style = "dr"; if ($i % 2 != 0) { $style = "sr"; } ?> <tr> <td class="<?php echo $style ?>"><a href="delegate_details.php?a=edit&recid=<?php echo $i ?>">Edit</a></td> <td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["Surname"]) ?></td> <td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["First Name"]) ?></td> <td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["Ministry"]) ?></td> <td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["Flight No"]) ?></td> <td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["Arrival Status"]) ?></td> <td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["Baggage Status"]) ?></td> <td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["Baggage No"]) ?></td> <td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["Clothing Req"]) ?></td> <td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["Hotel"]) ?></td> <td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["Room No"]) ?></td> </tr> <?php } mysql_free_result($res); ?> </table> <br> <?php showpagenav($page, $pagecount); ?> <?php } ?> <?php function showroweditor($row, $iseditmode) { global $conn; ?> <table class="tbl" border="0" cellspacing="1" cellpadding="5"width="50%"> <tr> <td class="hr"><?php echo htmlspecialchars("Surname")." " ?></td> <td class="dr"><input type="text" name="Surname" maxlength="25" value="<?php echo str_replace('"', '"', trim($row["Surname"])) ?>"></td> </tr> <tr> <td class="hr"><?php echo htmlspecialchars("First Name")." " ?></td> <td class="dr"><input type="text" name="First_Name" maxlength="25" value="<?php echo str_replace('"', '"', trim($row["First Name"])) ?>"></td> </tr> <tr> <td class="hr"><?php echo htmlspecialchars("Ministry")." " ?></td> <td class="dr"><input type="text" name="Ministry" maxlength="3" value="<?php echo str_replace('"', '"', trim($row["Ministry"])) ?>"></td> </tr> <tr> <td class="hr"><?php echo htmlspecialchars("Flight No")." " ?></td> <td class="dr"><input type="text" name="Flight_No" maxlength="10" value="<?php echo str_replace('"', '"', trim($row["Flight No"])) ?>"></td> </tr> <tr> <td class="hr"><?php echo htmlspecialchars("Arrival Status")." " ?></td> <td class="dr"><input type="text" name="Arrival_Status" maxlength="7" value="<?php echo str_replace('"', '"', trim($row["Arrival Status"])) ?>"></td> </tr> <tr> <td class="hr"><?php echo htmlspecialchars("Baggage Status")." " ?></td> <td class="dr"><input type="text" name="Baggage_Status" maxlength="10" value="<?php echo str_replace('"', '"', trim($row["Baggage Status"])) ?>"></td> </tr> <tr> <td class="hr"><?php echo htmlspecialchars("Baggage No")." " ?></td> <td class="dr"><input type="text" name="Baggage_No" maxlength="10" value="<?php echo str_replace('"', '"', trim($row["Baggage No"])) ?>"></td> </tr> <tr> <td class="hr"><?php echo htmlspecialchars("Hotel")." " ?></td> <td class="dr"><input type="text" name="Hotel" maxlength="1" value="<?php echo str_replace('"', '"', trim($row["Hotel"])) ?>"></td> </tr> <tr> <td class="hr"><?php echo htmlspecialchars("Room No")." " ?></td> <td class="dr"><input type="text" name="Room_No" maxlength="20" value="<?php echo str_replace('"', '"', trim($row["Room No"])) ?>"></td> </tr> <tr> <td class="hr"><?php echo htmlspecialchars("Clothing Req")." " ?></td> <td class="dr"><input type="text" name="Clothing_Req" value="<?php echo str_replace('"', '"', trim($row["Clothing Req"])) ?>"></td> </tr> </table> <?php } ?> <?php function showpagenav($page, $pagecount) { ?> <table class="bd" border="0" cellspacing="1" cellpadding="4"> <tr> <td><a href="delegate_details.php?a=add">Add Record</a> </td> <?php if ($page > 1) { ?> <td><a href="delegate_details.php?page=<?php echo $page - 1 ?>"><< Prev</a> </td> <?php } ?> <?php global $pagerange; if ($pagecount > 1) { if ($pagecount % $pagerange != 0) { $rangecount = intval($pagecount / $pagerange) + 1; } else { $rangecount = intval($pagecount / $pagerange); } for ($i = 1; $i < $rangecount + 1; $i++) { $startpage = (($i - 1) * $pagerange) + 1; $count = min($i * $pagerange, $pagecount); if ((($page >= $startpage) && ($page <= ($i * $pagerange)))) { for ($j = $startpage; $j < $count + 1; $j++) { if ($j == $page) { ?> <td><b><?php echo $j ?></b></td> <?php } else { ?> <td><a href="delegate_details.php?page=<?php echo $j ?>"><?php echo $j ?></a></td> <?php } } } else { ?> <td><a href="delegate_details.php?page=<?php echo $startpage ?>"><?php echo $startpage ."..." .$count ?></a></td> <?php } } } ?> <?php if ($page < $pagecount) { ?> <td> <a href="delegate_details.php?page=<?php echo $page + 1 ?>">Next >></a> </td> <?php } ?> </tr> </table> <?php } ?> <?php function showrecnav($a, $recid, $count) { ?> <table class="bd" border="0" cellspacing="1" cellpadding="4"> <tr> <td><a href="delegate_details.php">Index Page</a></td> <?php if ($recid > 0) { ?> <td><a href="delegate_details.php?a=<?php echo $a ?>&recid=<?php echo $recid - 1 ?>">Prior Record</a></td> <?php } if ($recid < $count - 1) { ?> <td><a href="delegate_details.php?a=<?php echo $a ?>&recid=<?php echo $recid + 1 ?>">Next Record</a></td> <?php } ?> </tr> </table> <hr size="1" noshade> <?php } ?> <?php function addrec() { ?> <table class="bd" border="0" cellspacing="1" cellpadding="4"> <tr> <td><a href="delegate_details.php">Index Page</a></td> </tr> </table> <hr size="1" noshade> <form enctype="multipart/form-data" action="delegate_details.php" method="post"> <p><input type="hidden" name="sql" value="insert"></p> <?php $row = array( "Surname" => "", "First Name" => "", "Ministry" => "", "Flight No" => "", "Arrival Status" => "", "Baggage Status" => "", "Baggage No" => "", "Clothing Req" => "", "Hotel" => "", "Room No" => ""); showroweditor($row, false); ?> <p><input type="submit" name="action" value="Post"></p> </form> <?php } ?> <?php function editrec($recid) { $res = sql_select(); $count = sql_getrecordcount(); mysql_data_seek($res, $recid); $row = mysql_fetch_assoc($res); showrecnav("edit", $recid, $count); ?> <br> <form enctype="multipart/form-data" action="delegate_details.php" method="post"> <input type="hidden" name="sql" value="update"> <input type="hidden" name="xSurname" value="<?php echo $row["Surname"] ?>"> <?php showroweditor($row, true); ?> <p><input type="submit" name="action" value="Post"></p> </form> <?php mysql_free_result($res); } ?> <?php function connect() { $conn = mysql_connect("localhost", "root", ""); mysql_select_db("iam2010"); return $conn; } function sqlvalue($val, $quote) { if ($quote) $tmp = sqlstr($val); else $tmp = $val; if ($tmp == "") $tmp = "NULL"; elseif ($quote) $tmp = "'".$tmp."'"; return $tmp; } function sqlstr($val) { return str_replace("'", "''", $val); } function sql_select() { global $conn; global $order; global $ordtype; global $filter; global $filterfield; global $wholeonly; $filterstr = sqlstr($filter); if (!$wholeonly && isset($wholeonly) && $filterstr!='') $filterstr = "%" .$filterstr ."%"; $sql = "SELECT `Surname`, `First Name`, `Ministry`, `Flight No`, `Arrival Status`, `Baggage Status`, `Baggage No`, `Clothing Req`, `Hotel`, `Room No` FROM `delegate_details`"; if (isset($filterstr) && $filterstr!='' && isset($filterfield) && $filterfield!='') { $sql .= " where " .sqlstr($filterfield) ." like '" .$filterstr ."'"; } elseif (isset($filterstr) && $filterstr!='') { $sql .= " where (`Surname` like '" .$filterstr ."') or (`First Name` like '" .$filterstr ."') or (`Ministry` like '" .$filterstr ."') or (`Flight No` like '" .$filterstr ."') or (`Arrival Status` like '" .$filterstr ."') or (`Baggage Status` like '" .$filterstr ."') or (`Baggage No` like '" .$filterstr ."') or (`Clothing Req` like '" .$filterstr ."') or (`Hotel` like '" .$filterstr ."') or (`Room No` like '" .$filterstr ."')"; } if (isset($order) && $order!='') $sql .= " order by `" .sqlstr($order) ."`"; if (isset($ordtype) && $ordtype!='') $sql .= " " .sqlstr($ordtype); $res = mysql_query($sql, $conn) or die(mysql_error()); return $res; } function sql_getrecordcount() { global $conn; global $order; global $ordtype; global $filter; global $filterfield; global $wholeonly; $filterstr = sqlstr($filter); if (!$wholeonly && isset($wholeonly) && $filterstr!='') $filterstr = "%" .$filterstr ."%"; $sql = "SELECT COUNT(*) FROM `delegate_details`"; if (isset($filterstr) && $filterstr!='' && isset($filterfield) && $filterfield!='') { $sql .= " where " .sqlstr($filterfield) ." like '" .$filterstr ."'"; } elseif (isset($filterstr) && $filterstr!='') { $sql .= " where (`Surname` like '" .$filterstr ."') or (`First Name` like '" .$filterstr ."') or (`Ministry` like '" .$filterstr ."') or (`Flight No` like '" .$filterstr ."') or (`Arrival Status` like '" .$filterstr ."') or (`Baggage Status` like '" .$filterstr ."') or (`Baggage No` like '" .$filterstr ."') or (`Clothing Req` like '" .$filterstr ."') or (`Hotel` like '" .$filterstr ."') or (`Room No` like '" .$filterstr ."')"; } $res = mysql_query($sql, $conn) or die(mysql_error()); $row = mysql_fetch_assoc($res); reset($row); return current($row); } function sql_insert() { global $conn; global $_POST; $sql = "insert into `delegate_details` (`Surname`, `First Name`, `Ministry`, `Flight No`, `Arrival Status`, `Baggage Status`, `Baggage No`, `Hotel`, `Room No`, `Clothing Req`) values (" .sqlvalue(@$_POST["Surname"], true).", " .sqlvalue(@$_POST["First_Name"], true).", " .sqlvalue(@$_POST["Ministry"], true).", " .sqlvalue(@$_POST["Flight_No"], true).", " .sqlvalue(@$_POST["Arrival_Status"], true).", " .sqlvalue(@$_POST["Baggage_Status"], true).", " .sqlvalue(@$_POST["Baggage_No"], true).", " .sqlvalue(@$_POST["Hotel"], true).", " .sqlvalue(@$_POST["Room_No"], false).", " .sqlvalue(@$_POST["Clothing_Req"], true).")"; mysql_query($sql, $conn) or die(mysql_error()); } function sql_update() { global $conn; global $_POST; $sql = "update `delegate_details` set `Surname`=" .sqlvalue(@$_POST["Surname"], true).", `First Name`=" .sqlvalue(@$_POST["First_Name"], true).", `Ministry`=" .sqlvalue(@$_POST["Ministry"], true).", `Flight No`=" .sqlvalue(@$_POST["Flight_No"], true).", `Arrival Status`=" .sqlvalue(@$_POST["Arrival_Status"], true).", `Baggage Status`=" .sqlvalue(@$_POST["Baggage_Status"], true).", `Baggage No`=" .sqlvalue(@$_POST["Baggage_No"], true).", `Hotel`=" .sqlvalue(@$_POST["Hotel"], true).", `Room No`=" .sqlvalue(@$_POST["Room_No"], false).", `ClothingReq`=" .sqlvalue(@$_POST["Clothing_Req"], true) ." where " .primarykeycondition(); mysql_query($sql, $conn) or die(mysql_error()); } function primarykeycondition() { global $_POST; $pk = ""; $pk .= "(`Surname`"; if (@$_POST["xSurname"] == "") { $pk .= " IS NULL"; }else{ $pk .= " = " .sqlvalue(@$_POST["xSurname"], true); }; $pk .= ")"; return $pk; } ?> Link to comment https://forums.phpfreaks.com/topic/161084-drop-down-list-in-php-code/#findComment-850099 Share on other sites More sharing options...
gevans Posted June 5, 2009 Share Posted June 5, 2009 OK, first you have to learn to use tags... ...code goes in here Second, it's up to you. That's all the code will do is assign the html to a variable. Then you can output the variable wherever you want it, I'd advise in your form! Link to comment https://forums.phpfreaks.com/topic/161084-drop-down-list-in-php-code/#findComment-850104 Share on other sites More sharing options...
bob1660 Posted June 5, 2009 Author Share Posted June 5, 2009 Still confused where the code must go Link to comment https://forums.phpfreaks.com/topic/161084-drop-down-list-in-php-code/#findComment-850133 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.