Jump to content

[SOLVED] foreach special entry after three times


XeroXer

Recommended Posts

naw,  then if he had exactly 9 images, you're left with an extra tr.

 

 

Use a ticker still though :)

$tick=0;
foreach ($images as $img)
{
     $td.="\t<td>".$img."<td>\n";
     $tick++;
      
     if($tick==3)
     {
         $rows.="<tr>".$td."</tr>\n";
         unset($td);
         $tick=0;
     }
}

if ($td)   // left over images
   $rows.="<tr>".$td."</tr>\n";

$table= "<table>\n".$rows."</table>\n";

echo $table;

 

edit: my spelling

edit2: reset my tick

Link to comment
Share on other sites

Little snippet to help u ok, For the formating of html with php useing <tables> <td> <tr> in a foreach and array.

 

pic to add should be easy ok.

 

<html>
<head>
</head>

<title>redarrow example</title>

<table bordercolor="black" border="4">

<?php

$array_one=array("hello there i am","redarrow");

$array_two=array("i love","php");

foreach($array_one as $one){

echo"<tr><td>";

echo $one;

echo"</tr></td>";
}

foreach($array_two as $two){

echo"<tr><td>";

echo $two;

echo"</tr></td>";
}
?>

</table>

</html>

Link to comment
Share on other sites

even an easer way ok

 

<html>

<head>

</head>

<title>image example</title>

<table bordercolor="black" border="4">

<?php

$images="image_folder/";

$array_one=array("img1.gif","img2.gif","img3.gif");

foreach($array_one as $one){

echo"<tr><td>";

echo "<img src='$images/$one'></img>";

echo"</tr></td>";
}

?>

</table>

</html>

Link to comment
Share on other sites

Thanks all!

I used a edited version of ProjectFear's idea.

 

At the top of the page I set $count to 0

<?php
$count = "0";
?>

At the start of my foreach I check the $count and if it is 3 I enter the <tr>

<?php
foreach ($bilder as $bilderna) {
if($count == 3){ $count = 0; echo "</tr><tr>"; echo "\n"; } else { echo ""; }
?>

After the image code is entered I increse the $count by 1.

<?php
echo "<td><a href='$sokvag' target='_blank'><img src='$thumbsokvag' border='0' alt='$bilderna / $filstorlek bytes'></a><br>";
echo $bilderna . " - " . $filstorlek . " bytes</td>";
$count++;
?>

And after the whole foreach section I make an echo out </tr> because that will always be needed because the other </tr> adding is at the beginning of the foreach.

<?php
echo "</tr>";
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.