Problem with file copy script

DaveBC

New Member
Messages
13
Reaction score
0
Points
0
Hi,

I'm converting a static site that a friend of mine was building into a dynamic site. Part of the work he had already done involved a piece of javascript which worked as a basic search engine/index. The user types into a text input field and their entry is autocompleted. If they then click the 'submit' button they are taken to the relevant page. The list of possible 'completions' is created manually in a file called autoCompleteEntries.js, like so:

Code:
Links = new Array();
countries = new Array();
countries[0] = "Dave Juice";
Links["Dave Juice"] = "display_product.php?id=1";
countries[1] = "Portishead";
Links["Portishead"] = "display_product.php?id=3";
countries[2] = "Kow Korn";
Links["Kow Korn"] = "display_product.php?id=4";
countries[3] = "Kow Korn 2";
Links["Kow Korn 2"] = "display_product.php?id=5";

…skip a bit… 

countries[20] = "Shiny shoes";
Links["Shiny shoes"] = "display_company.php?id=14";
countries[21] = "room 101";
Links["room 101"] = "display_company.php?id=15";
countries[22] = "Untitled";
Links["Untitled"] = "display_company.php?id=16";

…skip a bit more, you get the idea…

countries[28] = "boo";
Links["boo"] = "display_term.php?id=7";
countries[29] = "Yoghurt";
Links["Yoghurt"] = "display_term.php?id=8";
countries[30] = "Lidl";
Links["Lidl"] = "display_term.php?id=9";

As you can see, one array (countries) is for the possible entries, and the other (Links) is for the webpages they link to.
Now, as this is supposed to be a dynamic site I don’t want to have to manually update this list every time a new Product, Company or Term is added. So, I thought I would come up with a bit of php to generate this list and then copy and save the output as a brand new autoCompleteEntries.js file. The piece of php to generate the list is saved in a file called generate_search_list.php and looks like:
Code:
echo ("Links = new Array();\n");
echo ("countries = new Array();\n");

$counter = 0;

require_once('the_php_file_that_has_database_connection_details_in_it.php');
$query = "SELECT ID, Name FROM Products";
$result = mysql_query ($query);
while ($details = mysql_fetch_array($result)) {
	echo ('countries[' . $counter . '] = "' . $details['Name'] . '";' . "\n");
	echo ('Links["' . $details['Name'] . '"] = "display_product.php?id=' . $details['ID'] . '";' . "\n");
	$counter += 1;
	}
	
$query = "SELECT ID, Name FROM Companies";
$result = mysql_query ($query);
while ($details = mysql_fetch_array($result)) {
	echo ('countries[' . $counter . '] = "' . $details['Name'] . '";' . "\n");
	echo ('Links["' . $details['Name'] . '"] = "display_company.php?id=' . $details['ID'] . '";' . "\n");
	$counter += 1;
	}

$query = "SELECT ID, Name FROM terms";
$result = mysql_query ($query);
while ($details = mysql_fetch_array($result)) {
	echo ('countries[' . $counter . '] = "' . $details['Name'] . '";' . "\n");
	echo ('Links["' . $details['Name'] . '"] = "display_term.php?id=' . $details['ID'] . '";' . "\n");
	$counter += 1;
	}

To run this script and save the output as autoCompleteEntries.js, a site administrator actually runs the following script (update_search.php):

Code:
  //Sets the files to be used.
  $srcurl = 'http://localhost/knowledgebase/generate_search_list.php';
  $tempfilename = 'scripts/autoCompleteEntries_temp.js';
  $targetfilename = 'scripts/autoCompleteEntries.js';
   
  //delete previous temp file, in case it was still lying around.
  @unlink ($tempfilename);
   
  //Load the dynamic page. The 'r' indicates that we only plan to read from this file.
  $dynpage = @fopen($srcurl, 'r');
   
  //Check for errors
  if (!$dynpage) {
         $output = '<p>Unable to load ' . $srcurl . '. Search tool update aborted!</p>';
         }
  else {
         //Read the contents into a variable. Specify that we're willing to read up to 1MB of data.
         $htmldata = fread($dynpage, 1024*1024);
         
         //Close connection to dynamic page
         fclose($dynpage);
         
         //Open the temp file (creating it in the process) in preparation to write to it.
         $tempfile = fopen($tempfilename, 'w');
         
         //Check for errors
         if (!tempfile) {
                $output = '<p>Unable to open temporary file ($tempfilename) for writing. Search tool update aborted!</p>';
                }
         else {
                //Write the data for the static page into the temporary file
                fwrite($tempfile, $htmldata);
                
                //Close the temp file, now that we're done writing to it.
                fclose($tempfile);
                
                //Copy the temp file on top of the static page
                $ok = copy($tempfilename, $targetfilename);
                
                //Finally, delete the temp file
                unlink($tempfilename);
                
                $output = '<p>Search tool successfully updated!</p>';
                }
         } //closes else of 'if (!$dynpage)'
[FONT=&quot][/FONT]
[FONT=&quot] [/FONT]
Now, the problem I have is that this seems to work fine on my local test server but, when I put it on my x10hosting site to show my friend, generate_search_list.php still works fine but update_search.php will only create an autoCompleteEntries.js file containing about three entries. If you like, compare the output of http://marrs.x10hosting.com/knowledgebase/generate_search_list.php to the contents of http://marrs.x10hosting.com/knowledgebase/scripts/autoCompleteEntries.js

Do you think it is timing out or something?
 
Last edited:

quantum1

New Member
Messages
68
Reaction score
0
Points
0
Does your mysql database on your x10hosting site have the same data as your test server?
 

DaveBC

New Member
Messages
13
Reaction score
0
Points
0
Thanks for replying.

It did initially, but the two have diverged as my friend has been trying out the x10hosting site and i've been adding new features to the test server site.

I don't remember it working on the x10hosting site when i first put it up anyway.

The database on the test server actually has a few more entries than the x10hosting database now, so i don't think its to do with that.

Do you think maybe the script update_search.php is closing the connection to the dynamic page generate_search_list.php before said dynamic page has finished generating its list?
 

quantum1

New Member
Messages
68
Reaction score
0
Points
0
can you verify that your autoCompleteEntries_temp.js temp file has all the entries in it before it is copied to the final file?
 

DaveBC

New Member
Messages
13
Reaction score
0
Points
0
I've now commented out the unlink () line so that the temp file isn't deleted and i can see what it looks like. And the autoCompleteEntries_temp.js file generated on the x10hosting server looks just the same as the autoCompleteEntries.js file generated.
 

DaveBC

New Member
Messages
13
Reaction score
0
Points
0
I think I've solved the problem now, by using a different approach! I used output buffering to do it, rather than copying the contents of a dynamically generated page and saving them as a .js file.

I got instructions and a php class from http://codeutopia.net/blog/2007/10/03/how-to-easily-redirect-php-output-to-a-file/ if anyone's interested.


Thanks for your help anyway,

David

PS Does anyone know why my CPanel says I'm using 4 SQL databases of my allowed 3? I've only set up 3! and there are only 3 listed in PHPmyadmin (not counting information_schema). If not, please close this thread ;)
 
Top