Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

output row from mysql table

Posted on 2002-05-05
2
Medium Priority
?
226 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
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 1400 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

877 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