Solved

output row from mysql table

Posted on 2002-05-05
2
214 Views
Last Modified: 2006-11-17
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

0
Comment
Question by:jarmstro12
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 6

Expert Comment

by:andreif
ID: 6990917
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>";
     }
?>
0
 
LVL 4

Accepted Solution

by:
lokeshv earned 350 total points
ID: 6990919
try this..
<?
$data=get_user_data();
?>
<table width=100% >
<?while(list($key,$value)=each($data)){?>
<tr><td><?=$key?></td>
<?while(list($key1,$value1)=each($data[$key])){?>
<td><?=$data[$key]][$key1][start]?></td>
<td><?=$data[$key][$key1][end]?></td>
<?}?>
</tr>
<?}?>
</table>


<?
function get_user_data(){
$query="select * from urtable";

$qid=mysql_query($query);

if($qid){
        $data=array();
        $i=0;
        while($row=mysql_fetch_array($qid)){
            $data[$row[user]][$i++]=$row;    
        }
return $data;
}
else{
       echo 'Error :'.mysql_error();
       exit;  

}
}
?>

hope this helps..

Lk
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question