Page 1 of 1

Formatting output of a foreach loop

Posted: Tue Feb 24, 2009 3:51 pm
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

Re: Formatting output of a foreach loop

Posted: Tue Feb 24, 2009 4:00 pm
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.

Re: Formatting output of a foreach loop

Posted: Tue Feb 24, 2009 4:03 pm
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

Re: Formatting output of a foreach loop

Posted: Tue Feb 24, 2009 4:10 pm
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>";
}
 

Re: Formatting output of a foreach loop

Posted: Tue Feb 24, 2009 4:12 pm
by asparak
You are a genius. Thank you thank you thank you :)

Re: Formatting output of a foreach loop

Posted: Tue Feb 24, 2009 4:16 pm
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.

Re: Formatting output of a foreach loop

Posted: Tue Feb 24, 2009 4:22 pm
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>";

Re: Formatting output of a foreach loop

Posted: Tue Feb 24, 2009 4:32 pm
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.

Re: Formatting output of a foreach loop

Posted: Tue Feb 24, 2009 5:08 pm
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.

Re: Formatting output of a foreach loop

Posted: Tue Feb 24, 2009 5:37 pm
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.

Re: Formatting output of a foreach loop

Posted: Tue Feb 24, 2009 7:22 pm
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.

Re: Formatting output of a foreach loop

Posted: Wed Feb 25, 2009 2:48 am
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

Re: Formatting output of a foreach loop

Posted: Wed Feb 25, 2009 8:16 am
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

Re: Formatting output of a foreach loop

Posted: Fri Feb 27, 2009 1:04 pm
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))
 

Re: Formatting output of a foreach loop

Posted: Fri Feb 27, 2009 2:37 pm
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 :)