Jump to content

[SOLVED] Basic foreach and displaying the array values... I'm too tired, can't figure it.


suttercain

Recommended Posts

<?php

if (isset($_GET['q'])) {
require('../vdb/includes/connection.php');
$q=$_GET["q"];
echo $q;
$statement = "SELECT engine_family_name FROM eo_engine_family WHERE engine_family_name LIKE '".$q."%'";
$results = mysql_query($statement) or die(mysql_error());
while($row=mysql_fetch_assoc($results))
{
$items[]=$row;
} 

//print_r($items);

foreach ($items as $key => $value) {
    echo "Value: $value<br />\n";
}
}
?> 

 

When I use the print_r on the items, I can see the array keys and values, but trying to loop through the array with the foreach is just outing putting Array over and over.

Because $row is an array.

 

<?php

if (isset($_GET['q'])) {
require('../vdb/includes/connection.php');
$q=$_GET["q"];
echo $q;
$statement = "SELECT engine_family_name FROM eo_engine_family WHERE engine_family_name LIKE '".$q."%'";
$results = mysql_query($statement) or die(mysql_error());
while($row=mysql_fetch_assoc($results))
{
$items[]=$row;
} 

//print_r($items);

foreach ($items as $value) {
    foreach ($value as $key => $newval) {
        echo "Value: $newval<br />\n";
    }
}
}
?> 

 

Should give you what you are looking for.

or move it inside the while loop:

 

<?php

if (isset($_GET['q'])) {
require('../vdb/includes/connection.php');
$q=$_GET["q"];
echo $q;
$statement = "SELECT engine_family_name FROM eo_engine_family WHERE engine_family_name LIKE '".$q."%'";
$results = mysql_query($statement) or die(mysql_error());
while($row=mysql_fetch_assoc($results))
{
  foreach ($row as $key=>$value) {
    echo "$key: $value<br />\n";
  }
  echo "<hr>\n";
}
}
?> 

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.