Printing to specific rows and columns

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Wrathrowe
Forum Newbie
Posts: 7
Joined: Fri Feb 19, 2010 6:18 am

Printing to specific rows and columns

Post 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!
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Printing to specific rows and columns

Post 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>'; ?>
“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
Wrathrowe
Forum Newbie
Posts: 7
Joined: Fri Feb 19, 2010 6:18 am

Re: Printing to specific rows and columns

Post 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.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Printing to specific rows and columns

Post 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>";
?>
“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
Wrathrowe
Forum Newbie
Posts: 7
Joined: Fri Feb 19, 2010 6:18 am

Re: Printing to specific rows and columns

Post 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.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Printing to specific rows and columns

Post by social_experiment »

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
Post Reply