ba2008 Posted September 22, 2009 Share Posted September 22, 2009 Hi all, I'm a beginner thats trying to keep my code clean, so I'm starting to use functions. Could someone please take a look at the code I've written below for a drop down function and tell me where I've gone wrong? I'm getting a drop down menu with a default value, but none of my database options are showing up. The connectvars.php script pulls in my database connection info: <?php require_once('connectvars.php'); $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); function dropdown($Id, $Name, $Table) { echo "<select id='$Id' name='$Id'>"; echo "<option value=\"0\">Select Value</option>"; $query = "SELECT $Id, $Name FROM $Table"; $data = mysqli_query($dbc, $query); while($row = mysqli_fetch_array($data)) { $strA = $row['$Id']; $strB = $row['$Name']; echo "<option value=\"$strA\">$strB</option>"; } echo "</select><br />"; } dropdown(type_id, name, usertype); ?> Thanks so much for your help, I'm trying to learn as much as I can here! Link to comment https://forums.phpfreaks.com/topic/175126-solved-drop-down-menu-function-not-working/ Share on other sites More sharing options...
ba2008 Posted September 22, 2009 Author Share Posted September 22, 2009 Problem solved. A couple syntax errors and references to outside file problems. The code below works: <?php function dropdown($Id, $Name, $Table) { require_once('connectvars.php'); $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); echo "<select id='$Id' name='$Id'>"; echo "<option value=\"0\">Select Value</option>"; $query = "SELECT $Id, $Name FROM $Table"; $data = mysqli_query($dbc, $query); while($row = mysqli_fetch_array($data)) { $strA = $row[$Id]; $strB = $row[$Name]; echo "<option value=\"$strA\">$strB</option>"; } echo "</select><br />"; } ?> Link to comment https://forums.phpfreaks.com/topic/175126-solved-drop-down-menu-function-not-working/#findComment-923006 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.