+ Reply to Thread
Results 1 to 6 of 6

Thread: Problem with file copy script

  1. #1
    DaveBC is offline x10Hosting Member DaveBC is an unknown quantity at this point
    Join Date
    Nov 2007
    Posts
    13

    Problem with file copy script

    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)'


    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/knowledg...earch_list.php to the contents of http://marrs.x10hosting.com/knowledg...leteEntries.js

    Do you think it is timing out or something?
    Last edited by DaveBC; 01-04-2009 at 04:18 PM.

  2. #2
    quantum1's Avatar
    quantum1 is offline x10Hosting Member quantum1 is an unknown quantity at this point
    Join Date
    Sep 2008
    Location
    near Nashville, TN
    Posts
    68

    Re: Problem with file copy script

    Does your mysql database on your x10hosting site have the same data as your test server?
    Two rules of development:
    1) Computers work for people; People do not work for computers
    2) Maintainability is all that matters.

  3. #3
    DaveBC is offline x10Hosting Member DaveBC is an unknown quantity at this point
    Join Date
    Nov 2007
    Posts
    13

    Re: Problem with file copy script

    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?

  4. #4
    quantum1's Avatar
    quantum1 is offline x10Hosting Member quantum1 is an unknown quantity at this point
    Join Date
    Sep 2008
    Location
    near Nashville, TN
    Posts
    68

    Re: Problem with file copy script

    can you verify that your autoCompleteEntries_temp.js temp file has all the entries in it before it is copied to the final file?
    Two rules of development:
    1) Computers work for people; People do not work for computers
    2) Maintainability is all that matters.

  5. #5
    DaveBC is offline x10Hosting Member DaveBC is an unknown quantity at this point
    Join Date
    Nov 2007
    Posts
    13

    Re: Problem with file copy script

    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.

  6. #6
    DaveBC is offline x10Hosting Member DaveBC is an unknown quantity at this point
    Join Date
    Nov 2007
    Posts
    13

    Re: Problem with file copy script

    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/0...put-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 ;)

+ Reply to Thread

Similar Threads

  1. It works! ...
    By bpakidz in forum Programming Help
    Replies: 4
    Last Post: 09-07-2008, 11:12 PM
  2. help!!
    By retro-bliss in forum Free Hosting
    Replies: 25
    Last Post: 12-07-2007, 01:12 PM
  3. Windows Sortcuts
    By Blazer9131 in forum Computers & Technology
    Replies: 3
    Last Post: 11-29-2007, 04:07 PM
  4. Replies: 0
    Last Post: 10-10-2007, 07:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers