Formatting output of a foreach loop

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
asparak
Forum Newbie
Posts: 13
Joined: Tue Feb 24, 2009 3:38 pm

Formatting output of a foreach loop

Post by asparak »

Hi Everyone,

Boy am I glad I found this place. Using PHP for the first time for a pet project and I've sort of got most of the basics figured out now, so I can write and read data from a MySQL database. Haven't figured out how to edit or update it once I have it in the DB yet, but that's the next problem.

The issue I have for now is: How can I format the output of a foreach loop, to make the data returned to a browser loop a bit neater? I messed about with HTML <p> tags and Tables, but nothing really works very well. I'm comfortable with HTML and XHTML, but I can't tell if its PHP is just throwing me or I'm missing something.

The code I have so far is

<?php if(sizeof($events) > 0): ?>
<p class="blkbld"><?php foreach($events as $event): ?>
<?php echo $event->name; ?> <?php echo date("D jS M, Y", strtotime($event->date)); ?> <a href="index2.php?id=<?php echo $event->event_id; ?>">Full Details</a><br />
<?php endforeach; ?></p>
<?php else: ?>
<p class="bluebld">No events currently defined.</p>
<?php endif; ?>

The event table contains about 50 rows already and thats a fraction of the live data it will eventually have, so I want to solve this now, before I code myself completely into a corner. Be gentle with me, I hadn't really touched PHP until last Saturday. If I'm doing it all wrong, tell me nicely :)

Thanks

Rick
~BlitZ
Forum Newbie
Posts: 22
Joined: Mon Feb 23, 2009 5:36 pm
Location: United Kingdom

Re: Formatting output of a foreach loop

Post by ~BlitZ »

Just try to include tables, You can pick tables up pretty quickly, That way you can section out what you want and where you want it.
asparak
Forum Newbie
Posts: 13
Joined: Tue Feb 24, 2009 3:38 pm

Re: Formatting output of a foreach loop

Post by asparak »

I thought about using <TD>'s, but how can I get the PHP loop to create a new row after say 5 columns? Thats where I'm getting thrown I guess
Randwulf
Forum Commoner
Posts: 63
Joined: Wed Jan 07, 2009 7:07 am

Re: Formatting output of a foreach loop

Post by Randwulf »

Create a $counter and increment it by one each time the loop loops ($counter++;)

Then put this in the loop too:

Code: Select all

 
if (($counter % 5) == 0)
{
echo "</tr><tr>";
}
 
asparak
Forum Newbie
Posts: 13
Joined: Tue Feb 24, 2009 3:38 pm

Re: Formatting output of a foreach loop

Post by asparak »

You are a genius. Thank you thank you thank you :)
~BlitZ
Forum Newbie
Posts: 22
Joined: Mon Feb 23, 2009 5:36 pm
Location: United Kingdom

Re: Formatting output of a foreach loop

Post by ~BlitZ »

Code: Select all

<?
if ( $events > 5 )
{
<tr>
}
else
{
<table>
<tr>
<td></td>
</tr>
</table>
?>
EDIT: Sorry someone already posted before me, And great idea, never thought of that hehe.
semlar
Forum Commoner
Posts: 61
Joined: Fri Feb 20, 2009 10:45 pm

Re: Formatting output of a foreach loop

Post by semlar »

It's personal preference, but I couldn't possibly write code like you're doing it and make any sense out of it later.

I'm not saying you should change if you're comfortable with how you're coding, but I'd do it up more like this..

Code: Select all

if(sizeof($events) > 0) {
    echo "<p class='blkbld'>";
    foreach($events as $event) {
        echo $event->name;
        echo date("D jS M, Y", strtotime($event->date));
        echo " <a href='index2.php?id={$event->event_id}'>Full Details</a></p>";
    }
} else echo "<p class='bluebld'>No events currently defined.</p>";
asparak
Forum Newbie
Posts: 13
Joined: Tue Feb 24, 2009 3:38 pm

Re: Formatting output of a foreach loop

Post by asparak »

Thanks for the suggestions. Just getting my head around not putting the php tag at the beginning of every separate command. I think the book I bought on saturday is teaching me bad habits.
~BlitZ
Forum Newbie
Posts: 22
Joined: Mon Feb 23, 2009 5:36 pm
Location: United Kingdom

Re: Formatting output of a foreach loop

Post by ~BlitZ »

It most likely is giving you bad habbits. You still can close the PHP tages like to make it easier writing things, Like html, Plain text.
asparak
Forum Newbie
Posts: 13
Joined: Tue Feb 24, 2009 3:38 pm

Re: Formatting output of a foreach loop

Post by asparak »

Almost got it now, only issue now is the first row only contains 1 table cell, then it inserts the TR's and then works fine with 5 cells per row.

Code: Select all

<?php if(sizeof($events) > 0) {
           echo "<table border='1'>";
           echo "<tr>";
               foreach($events as $event) { 
                   echo "<td valign='top'>";
                   echo $event->name;
                   echo " ";
                   echo date("D jS M, Y", strtotime($event->date));
                   echo " <a href='index2.php?id={$event->event_id}'>Full Details</a>";
                   echo "</td>";
                   if (($counter % 5) == 0) {
                       echo "<tr></tr>";
                   }
               $counter++;
               }
           echo "</tr>";
           echo "</table>";
       }
       else {
           echo "<p><em>No events currently defined.</em></p>";
       }
       ?>
Really appreciate you guys helping me do this right. I'll tidy the table up with the correct CSS references tomorrow, just hacked it in for now so I can see the formatting is working.
Last edited by asparak on Wed Feb 25, 2009 3:09 am, edited 1 time in total.
~BlitZ
Forum Newbie
Posts: 22
Joined: Mon Feb 23, 2009 5:36 pm
Location: United Kingdom

Re: Formatting output of a foreach loop

Post by ~BlitZ »

Not to backseat mod or anything but next time you post some of your code, Could you please put it in the Code tags because its easier to read :D. Thanks, Anyway, To add another cell on the top row just add another <td> On the first row <tr>.
Your code should then look something like this.

Code: Select all

<?php if(sizeof($events) > 0) {
echo "<table border='1'>";
echo "<tr>";
foreach($events as $event) {
echo "<td valign='top'>";
echo $event->name;
echo " ";
echo date("D jS M, Y", strtotime($event->date));
echo " <a href='index2.php?id={$event->event_id}'>Full Details</a>";
echo "</td>";
echo "<td>whatever you want in the second cell</td>";
if (($counter % 5) == 0) {
echo "<tr></tr>";
}
$counter++;
}
echo "</tr>";
echo "</table>";
}
else {
echo "<p><em>No events currently defined.</em></p>";
}
?>
I'm a bit bored, as semlar already said, It is easier to understand your own code and read it better while formatting it, I would like to show you how you can do the table in HTML and then when you want some php elements in it, Just use the opening php tag, Write whatever php function you want, Then close it again, This way you don't have to worry about many php errors.
Heres what i got so far.

Code: Select all

<?php if(sizeof($events) > 0) { ?>
<table border='1'>
    <tr>
<?php foreach($events as $event) { ?>
<td valign='top'>
<?php echo $event->name; 
echo date("D jS M, Y", strtotime($event->date)); ?>
<a href='index2.php?id=<?php{$event->event_id}?>'>Full Details</a>
</td>
<td>whatever you want in the second cell</td>
<?php if (($counter % 5) == 0) { ?>
<tr></tr>
<?php }
$counter++;
} ?>
</tr>
</table>
<?php }
else { ?>
<p><em>No events currently defined.</em></p>
<?php } ?>
Correct me if im wrong people, Im not used to looking at php like this, I format mine so i can read it easily in Notepad++, You should download that, Its pretty usefull.
asparak
Forum Newbie
Posts: 13
Joined: Tue Feb 24, 2009 3:38 pm

Re: Formatting output of a foreach loop

Post by asparak »

Yeah. In my copy, the code is all indented properly, I used to program in Cobol many years ago. Like I said, I've been doing PHP for 4 days, so be gentle
~BlitZ
Forum Newbie
Posts: 22
Joined: Mon Feb 23, 2009 5:36 pm
Location: United Kingdom

Re: Formatting output of a foreach loop

Post by ~BlitZ »

Hehe ive been learning php for around 2 years, Im just really rubbish at it because i pick it up and then forget it like a month later, I think its just lazyness hehe
Randwulf
Forum Commoner
Posts: 63
Joined: Wed Jan 07, 2009 7:07 am

Re: Formatting output of a foreach loop

Post by Randwulf »

asparak wrote:Almost got it now, only issue now is the first row only contains 1 table cell, then it inserts the TR's and then works fine with 5 cells per row.

Code: Select all

<?php if(sizeof($events) > 0) {
           echo "<table border='1'>";
           echo "<tr>";
               foreach($events as $event) { 
                   echo "<td valign='top'>";
                   echo $event->name;
                   echo " ";
                   echo date("D jS M, Y", strtotime($event->date));
                   echo " <a href='index2.php?id={$event->event_id}'>Full Details</a>";
                   echo "</td>";
                   if (($counter % 5) == 0) {
                       echo "<tr></tr>";
                   }
               $counter++;
               }
           echo "</tr>";
           echo "</table>";
       }
       else {
           echo "<p><em>No events currently defined.</em></p>";
       }
       ?>
Really appreciate you guys helping me do this right. I'll tidy the table up with the correct CSS references tomorrow, just hacked it in for now so I can see the formatting is working.
I believe that 0 % 5 = 0. So instead of if (($counter % 5) == 0) try:

Code: Select all

 
if ((($counter % 5) == 0) and ($counter != 0))
 
asparak
Forum Newbie
Posts: 13
Joined: Tue Feb 24, 2009 3:38 pm

Re: Formatting output of a foreach loop

Post by asparak »

Thanks. That works better than my hacked solution, but gives 6 table cells on the first row, then five every other row. Only way I found that seems to work is:

Code: Select all

if (($counter % 5) == 4)
However, if I set $counter = 1; then it works just a well.
All looking good now. Just need to add some error checking to stop Mickey Mouse getting entered for the 20th time today, or idiots entering their name multiple times :)
Post Reply