Php Quickie

Associate
Joined
3 Oct 2006
Posts
2,304
Location
London
Hi Got some code here to dynamically generate drop down boxes in forms, however my query runs and the box appears with the correct amount of lines but they're all blank. I'm aware of where the error is, it's with my $drop_down_asoc arrays, but not sure how to format them properly, any ideas?

Code:
function make_dropdown($tablename, $columnid, $columnname)
{
	$drop_down = select_all($tablename, $columnname.' Asc');
	$drop_down_numrows = querynumrows($drop_down);
	print '<select name="'.$columnid.'">';
	for ($dd=0; $dd <$drop_down_numrows; $dd++)
	{
		$drop_down_asoc = query_asoc($drop_down);
		print '<option value="'.$drop_down_asoc['$columnid'].'>'.$drop_down_asoc['$columnname'].'</option>';
	}
	print '</select>';

}
 
Try this:

echo "<pre>";
echo print_r($drop_down_asoc);
echo "</pre>";

what's the output?
 
Code:
[U][B]File 1:[/B][/U]
make_dropdown($db->SOFTWARE_PUBLISHERS, 'PublisherID', 'PublisherName');

[U][B]File 2:[/B][/U]
function make_dropdown($tablename, $columnid, $columnname)
{
	$drop_down = select_all($tablename, $columnname.' Asc');
	$drop_down_numrows = querynumrows($drop_down);
	print '<select name="'.$columnid.'">';
	for ($dd=0; $dd <$drop_down_numrows; $dd++)
	{

		$drop_down_asoc = query_asoc($drop_down);
		echo "<pre>";
		echo print_r($drop_down_asoc);
		echo "</pre>";
		[I][B]print '<option value="'.$drop_down_asoc['$columnid'].'">'.$drop_down_asoc['$columnname'].'</option>';[/B][/I]
	}
	print '</select>';

}

[U][B]File 3:[/B][/U]
	//general select all from table. eg. select_all($db->COMPUTER_HARDWARE, ASC)
	function select_all($table, $order = NULL)
	{
		global $db;
		$query = "SELECT * FROM ";
		$query .= $table;
		if (isset ($order)){
			$query .= " ORDER BY ".$order;
		}
		$result = mssql_query($query);
		return $result;
	}
 
It's an error on the bolded italiced line in file 2. I can't dynamically assign the array I want to display.
 
No closing " for attrib value.

EDIT: You've changed it.

'$columnid' will be the literal, not the variable.

turn your E_NOTICE on and you won't need to post a thread like this ...
 
Not in you OP and see my edit..

Code:
print '<option value="'.$drop_down_asoc['$columnid'].'>'.$drop_down_asoc['$columnname'].'</option>';
 
Yeah, I'm aware it's the literal not the value. How do I make it so its the value? I've tried all sorts of combinations.
 
Ah yes sorry, it was wrong in my OP. I was mid way through changing one of the combinations to get it to print the literal value!
 
Back
Top Bottom