Board index   FAQ   Search  
Register  Login
Board index PHP PHP Scripts

Pull CSV values into array, insert sequentially into string

Links for php scripts

Moderators: macek, egami, gesf

Pull CSV values into array, insert sequentially into string

Postby tambo » Sun Sep 30, 2012 12:16 pm

Hi there,

I have written a PHP script that takes copies of external webpages and stores them on a local fileserver.

The URLs of the webpages always follow the same pattern, in that they have the same domain prefix and the same arguments as a suffix... it's just the bit in the middle that changes (the 'widgetid').

Here it is, in all it's glory...

(don't laugh... it took me four hours to write after copious reading of w3schools... haha)

Code: Select all
<?php

/* widget url variables */
$wyoprefix = 'http://www.example.com/widget/view?widgetId=';
$wyosuffix = '&geoTypeId=1&geoIds=0001&useComp=true&format=html';

$widgets_list = array(

"WIDGET_0000001",
"WIDGET_0000002"

);

$widgets_count = count($widgets_list);

for ($i = 0; $i < $widgets_count; $i++) {
    $fullstring = $wyoprefix . $widgets_list[$i] . $wyosuffix;
   
    /* saved files location on server */
   $destination = 'tables/' . $widgets_list[$i] . '.html';
   
    file_put_contents($destination, file_get_contents($fullstring));
}

?>

It works well enough and I intend to automate it to run once a week. However, as the number of instances in the array is likely to grow substantially, I would prefer to keep the array values in a separate CSV file for ease of maintenance and addition.

I've spent the last few hours trying to use different techniques that I've seen listed (SplFileObject and others), but I can't make it work.

Can anyone give me a few pointers?

Thanks,
tambo
tambo
New php-forum User
New php-forum User
 
Posts: 2
Joined: Sun Sep 30, 2012 11:27 am

Re: Pull CSV values into array, insert sequentially into str

Postby seandisanti » Thu Oct 11, 2012 1:13 pm

are you manually populating $widgets_list or is that coming from a site or being generated based on already saved pages? are you doing a full grab every time, or just new additions? sorry, i want to help but need to understand how you want it to work before i can offer any relevant assistance.
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Pull CSV values into array, insert sequentially into str

Postby tambo » Wed Oct 24, 2012 2:08 am

Sorry for the slow reply. Been busy on other projects.

To answer your question, the $widgets_list array is currently being manually populated as and when I see a widget that I want to take a copy of. So, for example, if I saw a widget that I liked and wanted to store a local copy of, I would add "WIDGET_0000003" (or whatever the widget ID was) to the array in the code I gave above, save the file, then run it.

That would then add a local copy of the new widget to the fileserver and also update all the local copies of the other widgets listed in the array.

All I'd like to do is hold the widget IDs in a CSV file, so that they can be manually added to without accessing the PHP file in an editor.

Does that clarify?

Thanks in advance to yourself or anyone else who can spare the time to guide me with this.
tambo
New php-forum User
New php-forum User
 
Posts: 2
Joined: Sun Sep 30, 2012 11:27 am

Re: Pull CSV values into array, insert sequentially into str

Postby seandisanti » Wed Oct 24, 2012 8:01 am

no problem, in the re-reading it seems pretty straightforward. will add sample code to this post in just a few minutes

***edit***
Try this.

Code: Select all
    <?php

    /* widget url variables */
    $wyoprefix = 'http://www.example.com/widget/view?widgetId=';
    $wyosuffix = '&geoTypeId=1&geoIds=0001&useComp=true&format=html';
   $file=fopen('sample.txt','rb'); //open file for reading
   $contents=fread($file, filesize($file));//read entire file
   $widgets_list = explode(',',$contents);//split on commas and create array
    $widgets_count = count($widgets_list);

    for ($i = 0; $i < $widgets_count; $i++) {
        $fullstring = $wyoprefix . $widgets_list[$i] . $wyosuffix;
       
        /* saved files location on server */
       $destination = 'tables/' . $widgets_list[$i] . '.html';
       
        file_put_contents($destination, file_get_contents($fullstring));
    }

    ?>

seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm


Return to PHP Scripts

Who is online

Users browsing this forum: Majestic-12 [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron