Solved

output row from mysql table

Posted on 2002-05-05
2
191 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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now