Hello -
I'm new to PHP, only been learning it for about a month, but I think I am starting to get the hang of it. I am working on a project that involves the Google Maps API which uses markers that are populated by values from a MySQL database. I am esentially attempting to create a profile option for each marker location on the map.
The javascript code that populates the map markers is the following.
var html = '<b>' + name + '</b> <br/>' + address + '<br />' + '<a href=\"/locations/' + state + '/' + city + '/' + name + '\">View Profile</a>';
As you can see there is a link created using the values for state, city, and address.
My goal now is to use PHP to run through that database and actually create a file for each row in the database. So it has to create a file in the location "root/locations/state(value of $state)/city(value of $city)/name(value of $name)".
I have written a code that I think should do the job, however it is not creating the file in the location. I worked through all the errors, and now none are reported, so I am stumped as to why it will not work.
Here is my code:
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
$contents = 'Testing to see if this makes it onto the page';
include ('mysql_connect.php');
$query = "SELECT id, name, state, city, as sd FROM markers";
$result = @mysql_query($query);
if ($result) {
while ($row = mysql_fetch_assoc($result)) {
$mydir = $_SERVER['DOCUMENT_ROOT'].'/locations/';
if(!empty($row['state']) && !empty($row['city']) && !empty($row['name'])){
$mydir .= $row['state'].'/'.$row['city'].'/';
if(file_exists($mydir) && is_writable($mydir)){
$mydir .= $row['name'].'.php';
// put the rest of the code to write the file here
$handle = fopen($_SERVER['DOCUMENT_ROOT'].'/locations/'.$row['state'].'/'.$row['city'].'/'.$row['name'].'.php',
"w") or die("Could Not Open File");
$lock = flock($handle, LOCK_EX);
if ($lock){
fputs($handle, $contents);
flock($handle, LOCK_UN);
}
fclose($handle);
}
}
}
}
?>
If I remove all the SQL and database stuff, it does work, the code for that is:
<?php
$contents = 'Testing to see if this makes it onto the page';
$handle = fopen(test.'.'.txt, "w");
$lock = flock($handle, LOCK_EX);
if ($lock){
fputs($handle, $contents);
flock($handle, LOCK_UN);
}
fclose($handle);
?>
Any help I can get is greatly apreciated. I have asked in a couple of other places yet no one can seem to figure out why the values in the database are not passed to the variables in my code.
Any links to guides or examples that acheive the same goal would alos work. I simply cant find any that use variables in their path anywhere.
Thanks