0

I have a php page that runs and collects some data. This works great and each time I reload the page it collects new data.

I am trying to set up a second php page that includes the first page multiple times. This will save me having to reload the first page.

Here is some code putting the include in a for loop.

<?php


$x=0;

while($x <= 2) {
    include('marker.php');
    echo $x;
    $x++;
} 

?>

When running this loop the first page marker.php is only called once? The marker.php page runs successfully and outputs values to the page.

Also the $x variable is not outputted. The $x variable only shows when i run the loop without the include.

Here is the code for marker.php

include('dbconn2.php');

$date='2017-05-13';
$insert_date = mysqli_query($link, "INSERT INTO `marker_test` (`date`, `value`) VALUES ('$date', '1') ON DUPLICATE KEY UPDATE `date` = VALUES(`date`) ");

$select_date = mysqli_query($link, "SELECT * from `marker_test` WHERE `date`='$date' ");

$select_marker = mysqli_query($link, "SELECT * from `marker_test` WHERE `date`='$date' ");
while ($rowGetMarker = mysqli_fetch_array($select_marker)){
          $marker_test=$rowGetMarker['value'];
}

$select_location = mysqli_query($link, "SELECT * from `locations` WHERE `locationID`='$marker_test' ");
while ($rowGetLocation = mysqli_fetch_array($select_location)){
          $location=$rowGetLocation['location'];
           $url=$rowGetLocation['url'];
}

echo '<BR>'; 

$marker_update=$marker_test+1;

$update_value = mysqli_query($link, " UPDATE `marker_test` SET  `value` = '$marker_update' WHERE `date`= '$date' ");

                      include('curl.php');

                      $dom = new DOMDocument;

                      //Get date from scraped page.
                      $dom->loadHTML($output);
                      foreach ($dom->getElementsByTagName('h2') as $node) {
                          $date =substr($node->nodeValue,-10);
                          $year=substr($node->nodeValue,-4);
                          $month=substr($node->nodeValue,-7,2);
                          $day=substr($node->nodeValue,-10,2);
                          $date=$year.$month.$day;
                          $date=ltrim ($date);       
                          $date=rtrim ($date);
                      }

                      $check_location=mysqli_query($link,"SELECT COUNT(`location`) FROM `Events_08052017_test` WHERE `location`='$location' AND `date`='$date'");
                      $row = mysqli_fetch_row($check_location);
                      $check_value=$row[0];
                      if($check_value!=0){echo '<br>'; echo $location; echo ' already exists on '; echo $date;}
                      else{
                            $total=101;
                            $space=12;

                            $array_data=array();
                            $array_barcode=array();
                            $array_runner=array();
                            $array_time=array();
                            $array_gender=array();
                            $array_gender_pos=array();
                            $array_score=array();
                            $array_date=array();
                            $array_club=array();
                            $array_age=array();


                            $dom->loadHTML($output);
                                  foreach ($dom->getElementsByTagName('td') as $node) {
                                        if($node->nodeValue=='Unknown') {$array_data[]='Unknown';}
                                        else {
                                             foreach ($node->getElementsByTagName('a') as $node) {
                                                if ($node->getAttribute('target') === '_top') {
                                                   $array_data[ ] = substr($node->getAttribute( 'href' ),29);
                                                }
                                              }
                                          }
                                      $array_data[ ] = $node->nodeValue;
                                  }

                                  echo '<BR><BR><BR><BR><BR>';
                                  echo $location;
                                  echo '<BR><BR>';
                                  print_r($array_data);

                          for($i=1;$i<count($array_data);$i=$i+$space){
                          $array_barcode[]=$array_data[$i];
                          }

                          for($i=8;$i<count($array_data);$i=$i+$space){
                          $array_club[]=htmlspecialchars($array_data[$i], ENT_QUOTES, 'UTF-8');
                          }

                          for($i=2;$i<count($array_data);$i=$i+$space){
                                $array_runner[]=htmlspecialchars($array_data[$i], ENT_QUOTES, 'UTF-8');
                          }
                          $array_runner=array_splice($array_runner,0,-3);

                          for($i=3;$i<count($array_data);$i=$i+$space){
                          $array_time[]=$array_data[$i];
                          }

                          for($i=4;$i<count($array_data);$i=$i+$space){
                          $array_age[]=$array_data[$i];
                          }

                           for($i=6;$i<count($array_data);$i=$i+$space){
                                $array_gender[]=$array_data[$i];
                                }

                          for($i=7;$i<count($array_data);$i=$i+$space){
                          $array_gender_pos[]=$array_data[$i];
                          }

                           for($i=0;$i<count($array_runner);$i++){
                                   if($array_gender[$i]=="M"){
                                       $score=$total - $array_gender_pos[$i];
                                       if($score<1){$score=1;}
                                       $array_score[ ] = $score;
                                    }

                                  if($array_gender[$i]=="F"){
                                      $score=$total - $array_gender_pos[$i];
                                      if($score<1){$score=1;}
                                      $array_score[ ] = $score;
                                  }
                                  if($array_gender[$i]==""){
                                      $score=' ';
                                      $array_score[ ] = $score;
                                  }
                            }

  // prepare and bind
                            $stmt = $link->prepare("INSERT INTO `Events_08052017_test` (`eventID`,`location`,`date`,`barcode`,`runner`,`time`,`Run Points`,`Volunteer Points`,`Gender`, `Gender pos`,`club`,`age_cat`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)");
                            $stmt->bind_param("isssssiisiss", $eventID,$location,$date,$barcode,$runner,$time,$runpoints,$volpoints,$gender,$genderpos,$club,$age);

                            // set parameters and execute
                            for( $x=0; $x < count($array_runner); $x++ ){
                                $eventID=null;
                                $barcode=$array_barcode[$x];
                                $runner=$array_runner[$x];
                                $time=$array_time[$x];
                                $runpoints=$array_score[$x];
                                $volpoints=' ';
                                $gender=$array_gender[$x];
                                $genderpos=$array_gender_pos[$x];
                                $club=$array_club[$x];
                                $age=$array_age[$x];

$stmt->execute();
}

}

I have tested the marker.php without the database $stmt part that inserts the values to the Events_08052017_test database table.

Why does the control page run the marker.php page successfully multiple times when the data is not being entered to the database but does not work when asking to insert the data to the database?

7
  • 1
    Why not include file only once and use it's function? It would be much faster. Commented May 19, 2017 at 11:31
  • What's in marker.php, maybe $x somewhere inside :-) Commented May 19, 2017 at 11:31
  • 3
    Please post the relevant contents of marker.php. An educated guess is that it includes an exit() or die(), or possibly even a fatal error. Commented May 19, 2017 at 11:31
  • Just wrap your first script in a function instead. Then you just need to include the function once and call it as many times you need Commented May 19, 2017 at 11:32
  • 1
    and what is exactly your question ? include something in a loop is a total non-sense. call include at the beginning and then everything is ok, no need to call it multiple times Commented May 19, 2017 at 11:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.