Jump to content

[SOLVED] drop down menu function not working


ba2008

Recommended Posts

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!

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 />";
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.