Jump to content

How to put multiple values using Array


draconlock

Recommended Posts

Could someone please help me.

 

table1 information with column and values:

 

ID Username Items

1 Joe Pencil

2 Joe Paper

3 Joe Pen

4 Sue Crayons

5 Sue Plastic

6 Sue Eraser

 

here is my current code, currently it does display the values but it shows a new line for every single item:

 

$reportdata["tableheadings"] = array("UserID","Name","Items");

 

$query = "SELECT * FROM table1";

 

$result = mysql_query($query);

 

while ($data = mysql_fetch_object($result)) {

$userid = $data->id;

$name = $data->username;

$item = $data->items;

$reportdata["tablevalues"][] = array($userid,$name,$item);

}

mysql_free_result($result);

 

 

 

here is my question and problem:

 

How would I go on about to have it print out in this format below?  I can't get it to post right but it will be in a table with two columns and 3 rows for this example.

 

Username Items

Joe Pencil

Paper

Pen

Sue Crayons

Plastic

Eraser

 

I can't figure it out from this point if anyone could help me I would greatly appreciate it.

 

Thanks,

Joe

Link to comment
https://forums.phpfreaks.com/topic/187495-how-to-put-multiple-values-using-array/
Share on other sites

Try something like this:

 

<?php
echo "<table><tr><th>Username</th><th>Items</th></tr>";

$firstRow = mysql_fetch_array($result);
$lastName = $row['Username'];
echo "<tr><td>$lastName</td><td>".$firstrow['Items']."</td></tr>";

while ($row = mysql_fetch_array($result))
{
   if ($row['Username'] == $lastName)
      echo "<tr><td></td><td>".$row['Items']."</td><tr>";
   else
   {
      $lastName = $row['Username'];
      echo "<tr><td>$lastName</td><td>".$row['Items']."</td></tr>";
   }
}

echo "</table>";
?>

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.