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:
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:
To run this script and save the output as autoCompleteEntries.js, a site administrator actually runs the following script (update_search.php):
[FONT="][/FONT]
[FONT="] [/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?
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="] [/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: