Solved

output row from mysql table

Posted on 2002-05-05
2
199 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 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Scope of $_SESSION 17 39
Determining creation & modification dates on MySQL tables 4 38
insert into database through form with dynamic fields. 2 28
Cookie not unsetting 7 18
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

808 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