php include - how to make variables available where included?
I am trying to include a file which is scraping all my data from varrious websites, however its not working. Heres my code.
firstly, the scraping php file. named scrapedata.php
<?php
//Include the Simple HTML DOM to use its functions, used by other following scripts.
//navigate to the content of variable html and save it in price data variable
//get the whole html of the webpage into variable html
include 'simple_html_dom.php';
$html = file_get_html('http://www.play.com/Electronics/Electronics/4-/16230382/New-Apple-iPod-Touch-8GB-4th-Gen/Product.html?');
$price_data = $html->find('h6[id=bodycontent_0_middlecontent_1_ctl00_ctl00_product_ctl00__overview_ctl00__dataControl__price__headerSix',0)->plaintext;
//Amazon.co.uk scrape
$amazon_html = file_get_html('http://www.amazon.co.uk/New-Apple-iPod-touch-Generation/dp/B0040GIZTI/ref=br_lf_m_1000333483_1_1_img?ie=UTF8&s=electronics&pf_rd_p=229345967&pf_rd_s=center-3&pf_rd_t=1401&pf_rd_i=1000333483&pf_rd_m=A3P5ROKL5A1OLE&pf_rd_r=1ZW9HJW2KN2C2MTRJH60');
$amazon_pd = $amazon_html->find('b[class=priceLarge]',0)->innertext;
libxml_use_internal_errors(true);
$dom = new DOMDocument();
$dom->loadHTML($amazon_html);
$xpath = new DOMXpath($dom);
$expr = "/html/body/div[@id='divsinglecolumnminwidth']/form[@id='handleBuy']/table[3]/tr[3]/td/div/span";
$nodes = $xpath->query($expr); // returns DOMNodeList object
// you can check length property i.e. $nodes->length
//echo $nodes->item(0)->nodeValue; // get first DOMNode object and its value
$stock_data = $nodes->item(0)->nodeValue;
if ( $stock_data == "In stock." ) {
$stockyesno = "Yes";
} else {
$stockyesno = "No";
}
//Currys scrape
$currys_html = file_get_html('http://www.currys.co.uk/gbuk/apple-new-ipod-touch-8gb-4th-generation-07677427-pdt.html');
$currys_pd = $currys_html->find('p[class=prd-amount]',0)->plaintext;
$currys_stk = $currys_html->find('/html/body/div/div/div[2]/div/div/div[2]/div/ul[2]/li/span')->plaintext;
//span[class=icon icon-check]',0);
echo $currys_stk;
if ( $currys_stk == "Available for home delivery" ) {
$currys_stockyesno = "Yes";
} else {
$currys_stockyesno = "No";
}
//Argos scrape
$argos_html = file_get_html('http://www.argos.co.uk/static/Product/partNumber/9282197/Trail/searchtext%3EIPOD+TOUCH.htm');
$argos_pd = $argos_html->find('span[class=actualprice]',0)->plaintext;
//Ebuyer scrape
$ebuyer_html = file_get_html('http://www.ebuyer.com/product/237805');
$ebuyer_pd = $ebuyer_html->find('span[class=now]',0)->plaintext;
//PcWorld scrape
$pcworld_html = file_get_html('http://www.pcworld.co.uk/gbuk/apple-new-ipod-touch-8gb-4th-generation-07677427-pdt.html');
$pcworld_pd = $pcworld_html->find('p[class=prd-amount]',0)->plaintext;
?>
and then my page where its included, to then which its meant to access the data in the variables from the included file.
<?php include 'scrapedata.php';?>
<!-- MYSQL DATABASE CODE -->
<?php
$db_host = 'localhost';
$db_user = 'admin';
$db_pwd = '1admin';
$database = 'stock_checker';
$table = 'price_stock';
if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to database");
if (!mysql_select_db($database))
die("Can't select database");
?>
<!-- MYSQL DATABASE CODE END-->
//Insert the scraped data into the database.
mysql_query("INSERT INTO price_stock (retailer,price) VALUES('Play.com', '$play_pd' )")
or die(mysql_error());
mysql_query("INSERT INTO price_stock (retailer,price,stock) VALUES('Amazon', '$amazon_pd', '$stockyesno' )")
or die(mysql_error());
mysql_query("INSERT INTO price_stock (retailer,price,stock) VALUES('Currys', '$currys_pd', '$currys_stk' )")
or die(mysql_error());
mysql_query("INSERT INTO price_stock (retailer,price) VALUES('Argos', '$argos_pd' )")
or die(mysql_error());
mysql_query("INSERT INTO price_stock (retailer,price) VALUES('eBuyer', '$ebuyer_pd' )")
or die(mysql_error());
mysql_query("INSERT INTO price_stock (retailer,price) VALUES('PCWorld', '$pcworld_pd' )")
or die(mysql_error());
?>
<!-- MYSQL DATABASE % TABLE CREATION CODE -->
<?php
// sending query
$result = mysql_query("SELECT * FROM {$table}");
if (!$result) {
die("Query to show fields from table failed");
}
$fields_num = mysql_num_fields($result);
echo "<table width='650px'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<td><b>{$field->name}</b></td>";
}
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "<开发者_Python百科;tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
}
mysql_free_result($result);
?>
Have I done this correctly ? I don't know if they would automattically be included, or they have to be made GLOBAL ?
This is a very bad idea.
You should be using functions; passing and returning values. Include files at the beginning of the script, call functions when needed. Do not put any freestanding (non-function) code in files you include.
(By the way, the next step is OOP and autoloaders.)
And in case you're wondering why this is a very bad idea: I've looked over your code 5 times now (though with no in-depth analysis) and still haven't figured out what variables you want to share between the two files. If I went line-by-line, I'd find it, but I don't want to go line-by-line. Neither do you, in 3 months, when you're updating the code. Make our job easier.
--
From a point of view, an object is a collection of functions and the state they share; so it's the third step here: no functions -> some functions -> functions grouped together in classes. Don't even know if it's any good, but PHP has this to say about OOP. Autoloading is the mechanism PHP uses to load classes on-demand, usually saving you from includes.
They don't need to be imported explicitly.
Try this example:
<?php
// a.php
$myVar = 123;
and
<?php
// b.php
include 'a.php'
echo isset($myVar)?'Included':'Not included';
Related to your posted code: I don't see where you add data to the database. Shouldn't you do that as well?
It's a bad idea to use DOM Parsing for such requests. Amazon released a simple API - read here: http://aws.amazon.com/de/
PHP variables in the included file will appear in the main file by the same name.
But in your code I don't see variables being reused at all? The scrape generates information in variables, then your main file reads an unrelated database table. The two operations need to relate somehow.
精彩评论