Solved

incrementing or counting after each loop

Posted on 2001-06-07
2
194 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 75 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

770 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