Page 1 of 1

Printing to specific rows and columns

Posted: Fri Feb 19, 2010 6:29 am
by Wrathrowe
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!

Re: Printing to specific rows and columns

Posted: Fri Feb 19, 2010 8:34 am
by social_experiment
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>'; ?>

Re: Printing to specific rows and columns

Posted: Fri Feb 19, 2010 11:38 am
by Wrathrowe
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.

Re: Printing to specific rows and columns

Posted: Sun Feb 21, 2010 2:20 am
by social_experiment
but I should have been more clear with my problem
Yes, it would have saved me a few minutes.

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>";
?>

Re: Printing to specific rows and columns

Posted: Tue Feb 23, 2010 5:55 am
by Wrathrowe
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.

Re: Printing to specific rows and columns

Posted: Tue Feb 23, 2010 6:33 am
by social_experiment
I had similar problems with my thought process on this then i thought of trying it using line breaks. :)