ppetree
ppetree

Reputation: 816

php foreach stops after first iteration

OK, I'm baffled.

I have a multidemionsal array with 660 records in it. When I run my foreach I should get one record for each pass but what I get is just the first record. I upped my error reporting and I get nothing back and I'm not allocating any memory in the foreach (just outputting table contents).

Code looks like this:

$totCount = count($funnel);

echo "We found $totCount log entries<br><br>\n";  //  66x records!
echo "<table border='1'>\n";
echo "<tr><th>Visitor</th><th>Date</th><th>Time/Page ---></th></tr>\n";
$nCount = 0;  // number of records
$nRec = 0;
$ip   = "";   // current records ip address
$date = "";   // current records date
$time = "";   // current records time
$file = "";   // file that was viewed

foreach($funnel as $page);
{
  // process each record
  $nRec++;

  // if the ip's are different, this is a new visitor
  if( strcmp($page['ip'], $ip) != 0 )
  {
    if($nCount > 0) // only end a row if its not the first one
      echo "</tr>\n"; // close out this row

    // update the variables
    $ip = $page['ip'];      // save the new visitors ip
    $date = $page['date'];  // save the new visitors date
    $time = $page['time'];  // save the new visitors time
    $file = $page['path'];  // save the new visitors file viewed

    echo "<tr>\n"; // start a new row
    echo "<td>$ip</td><td>$date</td>\n";
    echo "<td>$time<br>$file</td>";  

    $nCount++;        
  }
  else
  {
    // not a new visit
    $time = $page['time'];  // what time was this page viewed?
    $file = $page['path'];  // where did they go next?
    echo "<td>$time<br>$file</td>";  
  }
}
echo "</table>";
echo "<br>Processing Complete. $nCount visitors of $totCount/$nRec";

The above line says I had 1 visitor of 66x and nRec is 1 showing the foreach executed 1 time.

Any help would be appreciated!

Upvotes: 2

Views: 3112

Answers (2)

wkm
wkm

Reputation: 1762

Semi colon after the foreach?

foreach($funnel as $page);

Should be:

foreach($funnel as $page)

Upvotes: 4

Narf
Narf

Reputation: 14752

Remove the ';' after your foreach statement - it basically eliminates it.

Upvotes: 1

Related Questions