Link to home
Start Free TrialLog in
Avatar of jarmstro12
jarmstro12

asked on

output row from mysql table

Do I need to use some storage object to do this?
I have a mysql table TimeTable

user  day  start end
jim    1     800  400
jim    2     700   300
jim    3     600   200

I would like to output a row -

user   start1   end1   start2  end2  start3  end3
jim      800     400     700    300    600   200

Avatar of andreif
andreif
Flag of Canada image

I suggest you to create an array
$users[] where $users["jim"] will contain data for user jim

User's data itself can be also an array, so:

$users["jim"]["start1"] = 800;
$users["jim"]["end1"] = 400;
$users["jim"]["start2"] = 700;
$users["jim"]["end2"] = 300;

(I assume that start1 means start for day 1 from your table)

here is the code:

$result = mysql_query("select * from TimeTable order by day");

while ($row = mysql_fetch_array($result)) {
     $user[$row[user]]["start$row[day]"] = $row["start"];
     $user[$row[user]]["end$row[day]"] = $row["end"];
}
?>

<p><b>Let's print array in human readable format:</b><br>
<pre>
<?
print_r($user);
?>
</pre>

<p><b>Print formatted rows for users:</b><br>
<?
     foreach ($user as $username => $data) {
          print "$username: ";
          print join(" ",$data);
          print "<br>";
     }
?>
ASKER CERTIFIED SOLUTION
Avatar of lokeshv
lokeshv

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial