Jump to content

Multidimensional Array help


JChilds

Recommended Posts

I am trying to find all people in a particular category.

I then want to group by name.

 

 

All people have more than one entry in each category. I want to print out every person in this category, and each entry they have.

 

eg/

 

Bill - 23, 45, 34, 56

Bob - 77, 43, 35, 98

 

 

I am getting the array the following way, but am unsure how to extract the data after this.

 

$sql = mysql_query("SELECT * FROM $table WHERE category LIKE '$term' GROUP BY name");

Link to comment
https://forums.phpfreaks.com/topic/140428-multidimensional-array-help/
Share on other sites

$row will be an array of each column for the current row you are on.  So

 

echo $row['blah'];

 

would echo out the column named 'blah'.  So for example, if you have the following db table:

 

sometable

id name

1 Bob

2 Jan

3 James

 

you can for instance, echo out each column in the current row like this:

 

$sql = "select * from sometable";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
   echo "id: {$row['id']} name: {$row['name']} <br/>";
}

 

This would be the output:

 

id: 1 name: Bob

id: 2 name: Jan

id: 3 name: James

 

So basically you would do $row['columnname']  if you are using _fetch_array or _fetch_assoc and you would refer to them individually, or you can loop through each one, inside your while loop:

 

while ($row = mysql_fetch_assoc($result)) {
   foreach ($row as $column => $value) {
      echo "$column : $value <br/>";
   }
   echo "<br/>";
}

 

 

 

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.