hey laserlight,
thaks for the code again.. but im having trouble with it.. it seems as though the variable $file is never being given a value. i can't work out why not...
here is my complete script:
<?php
// Function to determin left margin based on chosen grid
function left_margin ($getLeft) {
switch ($getLeft) {
case A:
$left = 29.2;
break;
case B:
$left = 113.2 + 29.2;
break;
case C:
$left = (113.2 * 2) + 29.2;
break;
case D:
$left = (113.2 * 3) + 29.2;
break;
case E:
$left = (113.2 * 4) + 29.2;
break;
case F:
$left = (113.2 * 5) + 29.2;
break;
case G:
$left = (113.2 * 6) + 29.2;
break;
case H:
$left = (113.2 * 7) + 29.2;
break;
case I:
$left = (113.2 * 8) + 29.2;
break;
case J:
$left = (113.2 * 9) + 29.2;
break;
}
return $left;
}
// Function to determine top margin based on chosen grid
function top_margin ($getTop) {
switch ($getTop) {
case 1:
$top = 23.6;
break;
case 2:
$top = 113.2 + 23.6;
break;
case 3:
$top = (113.2 * 2) + 23.6;
break;
case 4:
$top = (113.2 * 3) + 23.6;
break;
case 5:
$top = (113.2 * 4) + 23.6;
break;
case 6:
$top = (113.2 * 5) + 23.6;
break;
case 7:
$top = (113.2 * 6) + 23.6;
break;
}
return $top;
}
//Connection information
$dbhost = "localhost";
$dbuser = "administrator";
$dbpass = "admin";
$dbname = "bahrainlocator";
// Connect to MySql
$connect = mysql_connect($dbhost, $dbuser, $dbpass)
or die("Unable to connect to MySql");
// Connect to Database
$db = mysql_select_db($dbname, $connect)
or die("Unable to connect to Database");
// SQL to retrieve pdf zoom location
$sql = "SELECT * FROM roads WHERE road = '$thisRoad'";
// Get sql result
$result = mysql_query($sql)
or die("Unable to Road data");
while ($row = mysql_fetch_assoc($result)) {
$road = $row['road'];
$page = $row['start page'];
$grid = $row['start grid'];
}
// Get Top and left margins based on returned $grid. i.e.: A-4
$left = left_margin ($grid[0]);
$top = top_margin ($grid[2]);
// Let's see if $page is odd
$odd_flag = $page & 1;
// Set location of pdf's and then open directory
$dir = "pdf";
$dh = opendir($dir)
OR die("Directory '" . $dir . "' could not be opened!");
// Loop through the listing of $dir
while (($file = readdir($dh)) !== false) {
// Let's see if this is a file or a directory
if (is_file($file)) {
// Check if $page is even or odd
if ($odd_flag) {
// $page should precede the underscore
if (preg_match('/' . $page . '\_[02468]+\.pdf/', $file)) {
$rString = "&link=".$file."#viewrect=".$left.",".$top.",113.2,113.2";
echo "check 1 <br>";
}
} else {
//$page should follow the underscore
if (preg_match('/[13579]+\_' . $page . '\.pdf/', $file)) {
$rString = "&link=".$file."#viewrect=".$left.",".$top.",113.2,113.2";
echo "check 2 <br>";
}
}
} else {
echo "NO GO! <br>";
}
}
// Close the directory
closedir($dh);
// Send link back to flash
echo $rString."&";
// Check a few variables
echo "
<br> page: $page
<br>grid: $top -- $left
<br> odd: $odd_flag
<br> file: $file
<br> road: $thisRoad
";
?>
the variable $thisRoad is obtained from the URL. The result i get is an empty rString (except for the ampersand at the very end) and one line of "no go!" for ever pdf file in the directory plus 2 more lines of "no go!" for some odd reason.
does the preg_match snippet where it says: [13579]+ actually allow more than one character on its side of the underscore? like wouldn't the above just find 1 characgter page numbers such 1_5.pdf. or 3_9. pdf?
thanks in advance for you help