+1  A: 

Hmm... There are a few issues in here...

for($c=0; $c<count($links); $c++)

This loop is executing just the next line:

$racepage = file_get_contents($links[$i]);

However, $i isn't defined, I suspect you want $c. Also, you need to place some braces around various parts... Now, this is untested, but I think you want something like:

date_default_timezone_set("australia/sydney");


$host = 'http://www.tabonline.com.au/';
$day = date(d);
$month = date(m);
$year = date(Y);
$slash = '/';
$mtgraces = '/mtgraces.html';


//Gallops Meetings on Todays racing page
$content = file_get_contents($host . $year . "/". $month . "/" . $day . $mtgraces);
preg_match_all('#<a[^<>]+href\s*=\s*[\'"](.R[0-9]+.html*)[\'"]#i', $content, $matches);
foreach ($matches[1] as $url) $links[] =  "$host$year$slash$month$slash$day$slash$url";


//get the runners from each page
$final_number = array();
$final_rating = array();
$final_location = array();
$final_locationcode = array();

for($c=0; $c<count($links); $c++)
{
  $racepage = file_get_contents($links[$c]);
  preg_match_all('#<td align="right" height="18"><font color="\#ffffff">[0-9]{1,2}</font></td>#', $racepage, $number);
  preg_match_all('#<font color="\#00ffff">[0-9]{1,3}</font>#', $racepage, $rating);
  preg_match_all('#<B>[\w]+([\s][A-Z]+)?</B>#', $racepage, $location);
  preg_match_all('#<B>[\w]+\s[0-9]+</B>#', $racepage, $locationcode);

  //strip tags for storage in DB
  $number_data = implode(",", $number[0]);
  $dbnumber = strip_tags($number_data);
  $final_number[] = explode(",", $dbnumber);

  $rating_data = implode(",", $rating[0]);
  $dbrating = strip_tags($rating_data);
  $final_rating[] = explode(",", $dbrating);

  $location_data = implode(",", $location[0]);
  $dblocation = strip_tags($location_data);
  $final_location[] = explode(",", $dblocation);

  $locationcode_data = implode(",", $locationcode[0]);
  $dblocationcode = strip_tags($locationcode_data);
  $final_locationcode[] = explode(",", $dblocationcode);
}

//Insert into database
$data = array();
for($i=0; $i<count($final_number); $i++)
    $data[] = "('" . $final_location[0] . "', '" . $final_locationcode[0] . "', '" . $final_number[$i] . "', '" . $final_rating[$i] . "')";


if(count($queries) != 0)
{
  $query = "insert into ratings(location, location_code, tab_no, rating) values " . implode(", ", $data);
  $hostname = "%hostname%";   // eg. mysql.yourdomain.com (unique)
  $username = "%username%";   // the username specified when setting-up the database
  $password = "%password";   // the password specified when setting-up the database
  $database = "%database";   // the database name chosen when setting-up the database (unique)
  mysql_connect($hostname,$username,$password);
  mysql_select_db($database) or die("Unable to select database");
  mysql_query($query) OR die(mysql_error())
}
John Cavan
Hi John,Thanks for that, I had actually ended up copying out of two different areas and got the copy wrong. I had the bracesa dn the $c but I am still just getting the last enrty of the array inserted in to the database.
Tim Aitken
There's still an issue in the code I pasted (like I said, untested...) in the building of the $data array as $final_location and $final_locationcode are using index 0 instead of $i. In any case, I suspect it is closer to what you want (and probably the best I can do on 5 beers). Cheers.
John Cavan
The $final_location and $finallocationcode variables are the same for each DB entry, it is only $final_number and $final_rating that I want to loop. This page shows (http://www.timmy8ken.com/links_temp.php) the current output of the query, the amount of time it takes for the link to load kinda makes me think that this is actually working, it's just the output that is not quite there (either that or my coding is shit!!). This is correct for one run of the code i.e. one entry for the $links array, now I just need to to loop thorugh and give me every
Tim Aitken
A: 

$final_number is something you get from a racepage link right? You are using it to as $i<count($final_number). Instead i think you should use $i<count($links) there as what you want to insert is a row for each link. What you can do is move the:

$data[] = "('" . $final_location[0] . "', '" . $final_locationcode[0] . "', '" . $final_number[$i] . "', '" . $final_rating[$i] . "')";

...line to the bottom of for($c=0; $c<count($links); $c++) line which would make you code look like this starting from that point, (notice $data=array() is defined before the loop):

$data = array();
for($c=0; $c<count($links); $c++)
{
  $racepage = file_get_contents($links[$c]);
  preg_match_all('#<td align="right" height="18"><font color="\#ffffff">[0-9]{1,2}</font></td>#', $racepage, $number);
  preg_match_all('#<font color="\#00ffff">[0-9]{1,3}</font>#', $racepage, $rating);
  preg_match_all('#<B>[\w]+([\s][A-Z]+)?</B>#', $racepage, $location);
  preg_match_all('#<B>[\w]+\s[0-9]+</B>#', $racepage, $locationcode);

  //strip tags for storage in DB
  $number_data = implode(",", $number[0]);
  $dbnumber = strip_tags($number_data);
  $final_number[] = explode(",", $dbnumber);

  $rating_data = implode(",", $rating[0]);
  $dbrating = strip_tags($rating_data);
  $final_rating[] = explode(",", $dbrating);

  $location_data = implode(",", $location[0]);
  $dblocation = strip_tags($location_data);
  $final_location[] = explode(",", $dblocation);

  $locationcode_data = implode(",", $locationcode[0]);
  $dblocationcode = strip_tags($locationcode_data);
  $final_locationcode[] = explode(",", $dblocationcode);

  $data[] = "('" . $final_location[0] . "', '" . $final_locationcode[0] . "', '" . $final_number[0] . "', '" . $final_rating[0] . "')";
}

if(count($queries) != 0)
{
  $query = "insert into ratings(location, location_code, tab_no, rating) values " . implode(", ", $data);
  $hostname = "%hostname%";   // eg. mysql.yourdomain.com (unique)
  $username = "%username%";   // the username specified when setting-up the database
  $password = "%password";   // the password specified when setting-up the database
  $database = "%database";   // the database name chosen when setting-up the database (unique)
  mysql_connect($hostname,$username,$password);
  mysql_select_db($database) or die("Unable to select database");
  mysql_query($query) OR die(mysql_error())
}

I think there are some problems with this code still.
Edit:I also noticed that on this line

$number_data = implode(",", $number[0]);

Wouldn't $number[0] be a string, it couldn't be an array because $number is an array of matched strings so $number[0] would be the whole matched string. This would apply to 'number_data', 'rating_data', 'location_data' and 'locationcode_data' so you can

$number_data = strip_tags($number[0]);

and then when creating the insert data:

$data[] = "('" . $final_location . "', '" . $final_locationcode . "', '" . $final_number . "', '" . $final_rating . "')";
andho
Thanks for your response andho. I am quite sure there are some problems with the code!!I think from what you have proposed you trying to get the 2 for loops combined into one, but I don't think that is going to work as I need to loop through the links first then as I am going through each link I want ot loop through the results to provide the final outcome.
Tim Aitken
My understanding of array is still a bit green but this is what I am getting so far:<pre><code>print_r($number) returns: Array ( [0] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 [9] => 10 [10] => 11 [11] => 12 [12] => 13 [13] => 14 ) ) print_r($number[0] returns: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 [9] => 10 [10] => 11 [11] => 12 [12] => 13 [13] => 14 ) print_r($number[0][1]) returns: <td align="right" height="18"><font color="#ffffff">2</font></td>
Tim Aitken
Sorry about the formatting above, I though comments allowed the code tags too!From the above comment I would say that my array is sitting at $number[0] hence the reason I am using implode(",", $number[0])
Tim Aitken
Oh yes! you're using preg_match_all, I didn't notice the _all part. But, within each link loop, wouldn't you have the desired number you want in the $dbnumber variable. Then isn't putting into the db your goal?
andho
Sorry didn't see you answer below. Guess I didn't understand your data
andho
A: 
Tim Aitken