Jump to content

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


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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.