Hey everyone. I'm looking to print mysql information in a table. Basically, I want it to follow this type of format...
Day1 Day2 Day3
Time9 Time9 Time9 ...
Day1 Day2 Day3
Time10 Time10 Time10 ...
Day1 Day2 Day3
Time11 Time11 Time11 ...
etc...
What I want to do, is while Day is 1 print in the first column, while Day = 2 print to the second column, and so on. Any help in appreciated, thanks!
Printing to specific rows and columns
Moderator: General Moderators
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: Printing to specific rows and columns
Try this php code:
Code: Select all
<?php $query = mysql_query("SELECT * FROM timetbl");
echo '<table>';
while ($pointer = mysql_fetch_array($query)) {
echo "<tr><td>Day 1</td><td>Day 2</td><td>Day 3</td></tr>";
echo "<tr><td>".$pointer['time1']."</td><td>".$pointer['time2']."
</td><td>".$pointer['time3']."</td></tr>";
}
echo '</table>'; ?>“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Re: Printing to specific rows and columns
Hey, thanks, that works for that specific example, but I should have been more clear with my problem. What I'm try to do is display a schedule in a meaningful way. My mysql table looks like this...
Day Time Course
1 9 CP2000
1 10 CP2200
2 10 CP3000
4 12 CP2000
So, I'd like to print the days horizontally and it's corresponding Time and Course vertically.
Day Time Course
1 9 CP2000
1 10 CP2200
2 10 CP3000
4 12 CP2000
So, I'd like to print the days horizontally and it's corresponding Time and Course vertically.
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: Printing to specific rows and columns
Yes, it would have saved me a few minutes.but I should have been more clear with my problem
Code: Select all
<?php $query = mysql_query("SELECT * FROM yourTable");
$rr = mysql_num_rows($query);
echo "<table>";
echo "<tr>";
while ($pointer = mysql_fetch_array($query)) {
echo "<td>".
$pointer['day']
."<br />".
$pointer['time']
."<br />".
$pointer['course']
."</td>";
}
echo '</tr>';
echo "</table>";
?>“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Re: Printing to specific rows and columns
Excellent, thank you. I was treating the table as being column based, where I tried to print everything in the first column, then the second, etc. When I loaded up your example I seen that you treated it as being row based, I modified my code to run similarly and it worked perfect. Thank you for your help.
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: Printing to specific rows and columns
I had similar problems with my thought process on this then i thought of trying it using line breaks. 
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering