Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

incrementing or counting after each loop

Posted on 2001-06-07
2
Medium Priority
?
219 Views
Last Modified: 2006-11-17


Hi,

I have script below that gets a file, makes an array, then puts parts of the array into an html table.

I am trying to make it so that the table has two columns.  I am not sure how to write it so that $num increments after each foreach statement.  I think I have the rest of it right, just have to work out how to count and recount after each loop so the <TR> and </TR> print out in the right places.

Thank you,

Chris


<TABLE WIDTH="90%" BORDER="0" CELLPADDING="3">
<?
$newsarray = file ("/home/acc/example.com/newsfeed/$filename.txt");
$num = count ($newsarray);

foreach ($newsarray as $hdln) {
$news = explode ("\t",$hdln);
$url =  ($news[0]);
$title = ($news[1]);
$source = ($news[2]);
$date = ($news[7]);

if($num%2 == 0) {
PRINT "<TR>\n";
}

PRINT "<TD><B><FONT SIZE=\"-1\"><A HREF=\"javascript:story('$url')\"
ONMOUSEOVER=\"self.status='news'; return true;\"
ONMOUSEOUT=\"self.status='';\">$title</A></FONT></B><BR>
<FONT COLOR=\"#934900\" SIZE=\"-1\">$source&nbsp;&nbsp;&nbsp;$date</FONT></TD>";

if($num%2 == 0) {
PRINT "</TR>\n";
}
}
?>
</TABLE>
0
Comment
Question by:ChrisAndrews
2 Comments
 
LVL 4

Accepted Solution

by:
ykf2000 earned 300 total points
ID: 6166795
try this:


<TABLE WIDTH="90%" BORDER="0" CELLPADDING="3">
<?
$newsarray = file ("/home/acc/example.com/newsfeed/$filename.txt");
$num = 0;

foreach ($newsarray as $hdln) {
$news = explode ("\t",$hdln);
$url =  ($news[0]);
$title = ($news[1]);
$source = ($news[2]);
$date = ($news[7]);

if($num%2 == 0) {
PRINT "<TR>\n";
}

PRINT "<TD><B><FONT SIZE=\"-1\"><A HREF=\"javascript:story('$url')\"
ONMOUSEOVER=\"self.status='news'; return true;\"
ONMOUSEOUT=\"self.status='';\">$title</A></FONT></B><BR>
<FONT COLOR=\"#934900\" SIZE=\"-1\">$source&nbsp;&nbsp;&nbsp;$date</FONT></TD>";
$num = $num + 1;

if($num%2 == 0) {
PRINT "</TR>\n";
}


}
?>
</TABLE>
0
 

Author Comment

by:ChrisAndrews
ID: 6167842

Works :)

Thank you,

Chris
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question