getting data from Arrays - PHP [closed]
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
foreach ($data as $id => $val)
{
if ($id == 'customer_id')
echo $val;
}
// $resultSet = mysql_query(...);
for ($data = array(); $row = mysql_fetch_assoc($resultSet); $data[] = $row);
$customerId = $data['customer_id'];
That function only returns one row at a time (the next row). Use a while loop , not a foreach loop (from the manual):
$result = mysql_query("SELECT id, name FROM mytable");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
printf("ID: %s Name: %s", $row[0], $row[1]);
}
you can use mysql_fetch_assoc instead if array then
foreach($arr as $key=>$val)
{
if ($key=='customer_id')
echo $val;
if ($key=='customer_name')
echo $val;
}
foreach($array as $key => $value)
if($key == "customer_id")
// do smth
What kind of "hilarious results?"
If by that you mean that you have two keys for the same value (array[0] === array['customer_id']
),that can be fixed. One way is to get rid of the numeric indexes by casting the array to an object :
foreach((object)$data as $key => $value)
// do smth
the easiest way would be to use this code :
$result = mysql_query("SELECT * FROM customer");
while($posts = mysql_fetch_array($result)){
echo $posts['customer_name'];
}
Updated answer to reflect the exact vars used in the question
$sql = "SELECT * FROM customers";
$result_set = mysql_query($sql, $this->connection);
while($records = mysql_fetch_array($result_set)){
echo $records['customer_name'];
}
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 the while()
and at the end before closing it, just add $i++
.
精彩评论