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

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>

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>

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.