Page 1 of 1

Two "Counts" One "Foreach"

Posted: Mon Sep 15, 2003 3:27 pm
by AaronSmith
I have a foreach statement that currently counts the results and paces a </TR><TR> after every two items. This allows me to divide the page into two columns for a better display.

Now, I want something to count the results and place a chunk of HTML below the third and fourth item, the seventh and eighth item, and so on.

Code: Select all

<?
	$count = 1;
    foreach ( $item_list as $id => $exp )
    &#123;
	$a = get_classified($id);
&#123;
    ?>
<TD valign=middle width="310">
		<FONT FACE="arial" color="#000080" style='font-size:9.5pt; mso-bidi-font-size:9.5pt;font-family:Arial'><B><A onFocus="if(this.blur)this.blur()" href="get_item.php?id=<? print $a->ID; ?>">
		<? print ($a->title); ?></a><BR></FONT></b>
		<FONT FACE="arial" color="#7A5924" style='font-size:8.5pt; mso-bidi-font-size:8.5pt;font-family:Arial'><B>
		<? print substr($a->description,0,60); ?>...&nbsp;&nbsp;<b><A onFocus="if(this.blur)this.blur()" href="get_item.php?id=<? print $a->ID; ?>">Details!</a>
		<FONT FACE="arial" color="#000000" style='font-size:5pt; mso-bidi-font-size:5pt;font-family:Arial'><BR><BR></font>
		</TD>
<?        &#125;
	if($count == 2)&#123;
	echo "</tr><tr>";
	$count = 1;
	&#125;
	else&#123;
	$count++;
	&#125;
    	&#125;
	echo "</tr>";
?>
Can someone point me in the right direction, or do any of you have a good idea regarding how to go about this?

Re: Two "Counts" One "Foreach"

Posted: Mon Sep 15, 2003 3:40 pm
by SantaGhost
there should be an easyer way, but you can use this:

Code: Select all

<?php
   $count = 1;
   $countsall = 1;
   $counts4 = 1;
   foreach ( $item_list as $id => $exp )
    {
		$a = get_classified($id);
	{
    ?>
	  <TD valign=middle width="310">
      <FONT FACE="arial" color="#000080" style='font-size:9.5pt; mso-bidi-font-size:9.5pt;font-family:Arial'><B><A onFocus="if(this.blur)this.blur()" href="get_item.php?id=<? print $a->ID; ?>">
      <? print ($a->title); ?></a><BR></FONT></b>
      <FONT FACE="arial" color="#7A5924" style='font-size:8.5pt; mso-bidi-font-size:8.5pt;font-family:Arial'><B>
      <? print substr($a->description,0,60); ?>...  <b><A onFocus="if(this.blur)this.blur()" href="get_item.php?id=<? print $a->ID; ?>">Details!</a>
      <FONT FACE="arial" color="#000000" style='font-size:5pt; mso-bidi-font-size:5pt;font-family:Arial'><BR><BR></font>
      </TD>
<?        }
   if($count == 2){
	   echo "</tr><tr>";
	   $count = 1;
   }
   else{
	   $count++;
   }

   if($countall == 4*$counts4){
		echo "4 times ".$counts4." has passed";
        $counts4++;
    }
    $countall++;
}
   echo "</tr>";
?>

Posted: Mon Sep 15, 2003 3:50 pm
by scorphus
So you want them from 4 to 4, like this:

Code: Select all

1 2
3 4
&lt;-- chunks here
5 6
7 8
&lt;-- chunks here
9 10
Take a look to this:

aaron.php:

Code: Select all

<?php
$data = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
$count = 1;
echo "<tr>\n";
foreach ($data as $k => $v) {
	echo "\t<td>$v</td>\n";
	if ($count % 2 == 0)
		echo "</tr>\n<tr>\n";
	if ($count % 4 == 0) {
		echo "\t<td>Chunk1</td>\n";
		echo "\t<td>Chunk2</td>\n";
		echo "</tr>\n<tr>\n";
	}
	$count++;
}
echo "</tr>";
?>
output:

Code: Select all

&lt;tr&gt;
	&lt;td&gt;1&lt;/td&gt;
	&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
	&lt;td&gt;3&lt;/td&gt;
	&lt;td&gt;4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
	&lt;td&gt;Chunk1&lt;/td&gt;
	&lt;td&gt;Chunk2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
	&lt;td&gt;5&lt;/td&gt;
	&lt;td&gt;6&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
	&lt;td&gt;7&lt;/td&gt;
	&lt;td&gt;8&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
	&lt;td&gt;Chunk1&lt;/td&gt;
	&lt;td&gt;Chunk2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
	&lt;td&gt;9&lt;/td&gt;
	&lt;td&gt;10&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;/tr&gt;
Note you could place that second if statement inside the first. Then you would get less flags. But maybe you'll have to change it...

Hope it helps.

Regards,
Scorphus.

*edit:

AaronSmith, I'm sure you can fix that leading <tr></tr>. The point in my post are the if conditions.

SantaGhost, I'm sorry, it seems I delayed a bit editing my post. Could only see yours answer when submited mine.

Please excuse my English.

Posted: Mon Sep 15, 2003 3:53 pm
by AaronSmith
I got it working... Thanks guys!

Posted: Mon Sep 15, 2003 3:54 pm
by SantaGhost
scorphus wrote:

Code: Select all

<?php
$data = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
$count = 1;
echo "<tr>\n";
foreach ($data as $k => $v) {
	echo "\t<td>$v</td>\n";
	if ($count % 2 == 0)
		echo "</tr>\n<tr>\n";
	if ($count % 4 == 0) {
		echo "\t<td>Chunk1</td>\n";
		echo "\t<td>Chunk2</td>\n";
		echo "</tr>\n<tr>\n";
	}
	$count++;
}
echo "</tr>";
?>
output:

Code: Select all

&lt;tr&gt;
	&lt;td&gt;1&lt;/td&gt;
	&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
	&lt;td&gt;3&lt;/td&gt;
	&lt;td&gt;4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
	&lt;td&gt;Chunk1&lt;/td&gt;
	&lt;td&gt;Chunk2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
	&lt;td&gt;5&lt;/td&gt;
	&lt;td&gt;6&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
	&lt;td&gt;7&lt;/td&gt;
	&lt;td&gt;8&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
	&lt;td&gt;Chunk1&lt;/td&gt;
	&lt;td&gt;Chunk2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
	&lt;td&gt;9&lt;/td&gt;
	&lt;td&gt;10&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;/tr&gt;
now heres something ive been looking for a long time :) what does % do? can i find some more info in the manual?

Posted: Mon Sep 15, 2003 4:01 pm
by scorphus
% is the modulus operator. It produces the remainder from integer division. So:

3 % 2 equals 1
5 % 3 equals 2
4 % 4 equals 0

Arithmetic Operators.

Regards,
Scorphus.

Posted: Mon Sep 15, 2003 4:12 pm
by SantaGhost
scorphus wrote:% is the modulus operator. It produces the remainder from integer division. So:

3 % 2 equals 1
5 % 2 equals 3
4 % 4 equals 0

Regards,
Scorphus.
thx for the explanation but wont 5%2 equal to 1 then? :D

Posted: Mon Sep 15, 2003 4:18 pm
by scorphus
Oh yes... my mind betrayed me :). I'm sorry again, I'll edit my post.

Thanks,
Scorphus.