combining two scripts: how to invoke a variable between them

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lin
Forum Commoner
Posts: 49
Joined: Tue Dec 07, 2010 1:53 pm

combining two scripts: how to invoke a variable between them

Post by lin »

This is just basic script, where i try to modify for the needs. I try to play with it.

i want to parse some data. The whole script has three parts

1. Fetching
2. parsing
3. storing

i want to put all into one script:




1st i need to have a connection to database lets say MySQL. I will suggest to use mysqli instead of mysql.
Well - okay i safe this db.php


Code: Select all

$host="localhost"; //database hostname
$username="******"; //database username
$password="******"; //database password
$database="******"; //database name
?>
Now i am going to take a new script and save this config.php

Code: Select all

<?php
require_once('db.php'); //call db.php
$connect=mysqli_connect($host,$username,$password); //connect to mysql through mysqli
if(!$connect){
die("Cannot connect to host, please try later."); //throw error if any problem
}
else
{
$select_db=mysqli_select_db($database); //select database
if(!$select_db){
die("Site Database is down at the moment, Please check later. We will be back shortly."); // error if cannot connect to database or db does not 

exist
}
}
?>

Now i have to take care for the script, that takes the files (note this is very basic - it is only a proof of concept.
In the real situation i will take cURL since cURL is much much nicer and more elegant and faster etc.

Code: Select all

<?php
require_once('config.php'); // call config.php for db connection
$content = file_get_contents("<-here the path to the file goes in-> Position XY! an URL is here ");

var_dump($content);

$pattern = '/<td>(.*?)<\/td>/si';
preg_match_all($pattern,$content,$matches);

foreach ($matches[1] as $match) {
    $match = strip_tags($match);
    $match = trim($match);
    var_dump($match);
$sql = mysqli_query("insert into tablename(contents) values ('$match')");
}

?>

Note: This is just basic script, where you can modify it for your taste and can play with it.

Question: If i have stored the URLs that i want to parse in a local file - how do i "call" them in the script.
How do i do the call to file where the URLs (there are more than 2500 URLs that have to be parsed) at the
following position: $content = file_get_contents("<-here the path to the file goes in-> Position XY! an URL is here ");


The folder with the URLs is stored in the same folder as the scripts reside!

Many thanks for all hints and for a starting point!

if i have to write more - or if you need more infos - or if i have to be more concrete, just let me know!


i love to hear from you!
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: combining two scripts: how to invoke a variable between

Post by Celauran »

If you're going to be reading and parsing this file frequently, I'd recommend inserting it into a database for future use. That aside, I'd opt for fopen() and fgets() rather than file_get_contents so that you can read the file into an array one line at a time. I think that would make life much easier when it comes time to parse.
lin
Forum Commoner
Posts: 49
Joined: Tue Dec 07, 2010 1:53 pm

Re: combining two scripts: how to invoke a variable between

Post by lin »

hello Celauran

many many thanks for the answer. Great to hear from you!
Celauran wrote:If you're going to be reading and parsing this file frequently, I'd recommend inserting it into a database for future use. That aside, I'd opt for fopen() and fgets() rather than file_get_contents so that you can read the file into an array one line at a time. I think that would make life much easier when it comes time to parse.
i like your tipps.

BTW - the database-solution came to my mind some days ago. Well i could store all URLs in a database and start a call
So that the URL will get inserted at the certain place. That is a very very interesting solution.

And i can run this again and again. Well i will try to get this solution done! I try to figure it out!

I come back and tell you how it runs...
untlil soooon

greetings lin :D
Post Reply