Okay, I’m kinda stuck on really silly problem, but still can’t figure out.
I’m having an array from database with mysql_fetch_array. So far I have only 1 record in the table and after print_r(….) I have this:
Array (
[0] => 1
[customer_id] => 1
[1] => Test Client
[customer_name] => Test Client
[2] =>
[customer_image] =>
[3] => Test Address
[customer_address] => Test Address
[4] => 2272723
[customer_phone] => 2272723
)
I’m trying to make a foreach() {...} from where I’ll get customer_id and customer_name
and somehow I can’t make correct foreach to get them and I’m getting some hilarious results…
any ideas?
=================
$sql = "SELECT * FROM customers";
$result_set = mysql_query($sql, $this->connection);
$records = mysql_fetch_array($result_set);
function generateSelect($recordName, $label, $default) {
$records = parent::selectTableRecord($recordName);
$html = "<label for=\"".$recordName."\">". $label ."</label>";
$html .= "<select name=\"". $recordName ."\" id=\"". $recordName ."\">";
$html .= "<option value=\"\">". $default ."</option>";
while ($record = mysql_fetch_assoc($records)) {
$html .= "<option value=\"".$record['customer_id']."\">".$record['customer_name']."</option>";
}
$html .= "</select>";
return $html;
}
and somewhere on other page I’m echoing this function:
<?php echo Project::generateSelect('customer', 'client', '--'); ?>
here’s what I have
the easiest way would be to use this code :
Updated answer to reflect the exact vars used in the question
now #customer_name can be replaced by ANY field name you have in that table to get that data.
and if you wanted to use foreach to have $i as a number to keep track of the number of results, then you can create
$i = 0;outside thewhile()and at the end before closing it, just add$i++.