Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

incrementing or counting after each loop

Posted on 2001-06-07
2
Medium Priority
?
214 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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 This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…
The viewer will learn how to count occurrences of each item in an array.

670 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