Forum Moderators: coopster

Message Too Old, No Replies

foreach help & alternating row colors

         

cybershark

8:56 pm on May 11, 2006 (gmt 0)

10+ Year Member



What do I need to add to $day foreach loop to make the days which have no activities not to show?

And what do I need to add to $activities foreach loop to alternate table row colors?

$day = getDay();
foreach ($day as $keyday => $valday){

print("<tr><th colspan=5>".$valday["day"]."</th></tr>\n\r");

$activities = getActivities(array("where"=>"eventsid=".$_GET["eventsid"],"order"=>"time"));
$rowCount = 1;
foreach($activities as $key => $valact){
if($valday["dayid"]==$valact["dayid"]){

$time = date('g:i A' , strtotime($valact["time"]));
$bgColor = ($rowCount %2)? 'F2F2F2' : 'CCCCCC';
print("<tr bgColor=$bgColor><td align=\"right\" style=\"padding-right:5px;\">".$time."</td>
<td>".$valact["activities"]."</td>
<td>".$valact["venue"]."</td>
<td>".$valact["leader"]."</td>
<td>".$valact["contact"]."</td>
</tr>");

}
}
}

[edited by: coopster at 9:11 pm (utc) on May 11, 2006]
[edit reason] removed url per TOS [webmasterworld.com] [/edit]

jatar_k

9:22 pm on May 11, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld cybershark

I assume this colour switch isn't working?

$bgColor = ($rowCount %2)? 'F2F2F2' : 'CCCCCC';

I would think it is because your $rowCount is not incrementing

at the end of the inside loop you need to increment it like so

$rowCount++;

for not showing a day with no activities you would need something to test on the first line of the loop

if (sometest) continue;

coopster

9:28 pm on May 11, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I think if you were to count() [php.net] the corresponding day array and it is not greater than 0 you know that you have no days to process so skip that day.

cybershark

10:23 pm on May 11, 2006 (gmt 0)

10+ Year Member



Thanks for the welcome.

I highlighted in bold what I've added.

I added $rowCount++; in, but the row colors aren't alternating properly. See the url I posted in my first post.

I'm not sure what to count for the days with no activities not to show.

$day = getDay();
foreach ($day as $keyday => $valday) {

$activities = getActivities(array("where"=>"eventsid=".$_GET["eventsid"],"order"=>"time"));

if(count($activities)) {
print("<tr><th colspan=5>".$valday["day"]."</th></tr>\n\r");
}

$rowCount = 1;
foreach($activities as $key => $valact) {
if($valday["dayid"]==$valact["dayid"]){

$time = date('g:i A' , strtotime($valact["time"]));

$bgColor = ($rowCount %2)? 'F2F2F2' : 'CCCCCC';
print("<tr bgColor=$bgColor><td align=\"right\" style=\"padding-right:5px;\">".$time."</td>
<td>".$valact["activities"]."</td>
<td>".$valact["venue"]."</td>
<td>".$valact["leader"]."</td>
<td>".$valact["contact"]."</td>
</tr>");

}
$rowCount++;
}

}

coopster

10:37 pm on May 11, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



$day = getDay(); 
foreach ($day as $keyday => $valday) {
$activities = getActivities(array("where"=>"eventsid=".$_GET["eventsid"],"order"=>"time"));
$rowCount = 1;
foreach($activities as $key => $valact) {
if(count($valact) && $rowCount == 1) {
print("<tr><th colspan=5>".$valday["day"]."</th></tr>\n\r");
}

if($valday["dayid"]==$valact["dayid"]){
$time = date('g:i A' , strtotime($valact["time"]));
$bgColor = (++$rowCount %2)? 'F2F2F2' : 'CCCCCC';
print("<tr bgColor=$bgColor><td align=\"right\" style=\"padding-right:5px;\">".$time."</td>
<td>".$valact["activities"]."</td>
<td>".$valact["venue"]."</td>
<td>".$valact["leader"]."</td>
<td>".$valact["contact"]."</td>
</tr>");
}
}
}

I think I got that right ...

cybershark

11:03 pm on May 11, 2006 (gmt 0)

10+ Year Member



If fixed the row problem I put $rowCount++; in the wrong place

cybershark

11:45 pm on May 11, 2006 (gmt 0)

10+ Year Member



Thanks coopster, your incrementing method is easier for me to follow, that fixes the the alternating row colors.

But it's now printing out each day 13 times(there are 13 records in the activities table)

jatar_k

2:58 pm on May 12, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



have you looked to see if that is one of the loops pr is that a bad query?

cybershark

4:52 pm on May 13, 2006 (gmt 0)

10+ Year Member



I added another foreach and it's fixed the problem.

$activityExist = false;

foreach($activities as $xVal) {
if($valday["dayid"]==$xVal["dayid"])
$activityExist = true;
}

if($activityExist) {
print("<tr><th colspan=5>".$valday["day"]."</th></tr>\n\r");
}