Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 22nd, 2012  |  syntax: None  |  size: 4.49 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. php include - how to make variables available where included?
  2. <?php
  3.  
  4. //Include the Simple HTML DOM to use its functions, used by other following scripts.
  5. //navigate to the content of variable html and save it in price data variable
  6. //get the whole html of the webpage into variable html
  7. include 'simple_html_dom.php';
  8. $html = file_get_html('http://www.play.com/Electronics/Electronics/4-/16230382/New-Apple-iPod-Touch-8GB-4th-Gen/Product.html?');
  9. $price_data = $html->find('h6[id=bodycontent_0_middlecontent_1_ctl00_ctl00_product_ctl00__overview_ctl00__dataControl__price__headerSix',0)->plaintext;
  10.  
  11.  
  12. //Amazon.co.uk scrape
  13. $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');
  14. $amazon_pd = $amazon_html->find('b[class=priceLarge]',0)->innertext;
  15.  
  16. libxml_use_internal_errors(true);
  17.  
  18. $dom = new DOMDocument();
  19. $dom->loadHTML($amazon_html);
  20.  
  21. $xpath = new DOMXpath($dom);
  22.  
  23. $expr = "/html/body/div[@id='divsinglecolumnminwidth']/form[@id='handleBuy']/table[3]/tr[3]/td/div/span";
  24. $nodes = $xpath->query($expr); // returns DOMNodeList object
  25. // you can check length property i.e. $nodes->length
  26.  
  27. //echo $nodes->item(0)->nodeValue; // get first DOMNode object and its value
  28. $stock_data = $nodes->item(0)->nodeValue;
  29.  
  30. if ( $stock_data == "In stock." ) {
  31.     $stockyesno = "Yes";
  32. } else {
  33.     $stockyesno = "No";
  34. }
  35.  
  36. //Currys scrape
  37. $currys_html = file_get_html('http://www.currys.co.uk/gbuk/apple-new-ipod-touch-8gb-4th-generation-07677427-pdt.html');
  38. $currys_pd = $currys_html->find('p[class=prd-amount]',0)->plaintext;
  39. $currys_stk = $currys_html->find('/html/body/div/div/div[2]/div/div/div[2]/div/ul[2]/li/span')->plaintext;
  40. //span[class=icon icon-check]',0);
  41.  
  42. echo $currys_stk;
  43.  
  44. if ( $currys_stk == "Available for home delivery" ) {
  45.     $currys_stockyesno = "Yes";
  46. } else {
  47.     $currys_stockyesno = "No";
  48. }
  49.  
  50. //Argos scrape
  51. $argos_html = file_get_html('http://www.argos.co.uk/static/Product/partNumber/9282197/Trail/searchtext%3EIPOD+TOUCH.htm');
  52. $argos_pd = $argos_html->find('span[class=actualprice]',0)->plaintext;
  53.  
  54. //Ebuyer scrape
  55. $ebuyer_html = file_get_html('http://www.ebuyer.com/product/237805');
  56. $ebuyer_pd = $ebuyer_html->find('span[class=now]',0)->plaintext;
  57.  
  58. //PcWorld scrape
  59. $pcworld_html = file_get_html('http://www.pcworld.co.uk/gbuk/apple-new-ipod-touch-8gb-4th-generation-07677427-pdt.html');
  60. $pcworld_pd = $pcworld_html->find('p[class=prd-amount]',0)->plaintext;
  61. ?>
  62.        
  63. <?php include 'scrapedata.php';?>    
  64.  
  65.  
  66.    <!-- MYSQL DATABASE CODE -->
  67.   <?php
  68. $db_host = 'localhost';
  69. $db_user = 'admin';
  70. $db_pwd = '1admin';
  71.  
  72. $database = 'stock_checker';
  73. $table = 'price_stock';
  74.  
  75. if (!mysql_connect($db_host, $db_user, $db_pwd))
  76.     die("Can't connect to database");
  77.  
  78. if (!mysql_select_db($database))
  79.     die("Can't select database");
  80.  
  81. ?>
  82. <!-- MYSQL DATABASE CODE END-->
  83.        
  84. //Insert the scraped data into the database.
  85. mysql_query("INSERT INTO price_stock (retailer,price) VALUES('Play.com', '$play_pd' )")
  86. or die(mysql_error());
  87. mysql_query("INSERT INTO price_stock (retailer,price,stock) VALUES('Amazon', '$amazon_pd', '$stockyesno' )")
  88. or die(mysql_error());
  89.  mysql_query("INSERT INTO price_stock (retailer,price,stock) VALUES('Currys', '$currys_pd', '$currys_stk' )")
  90. or die(mysql_error());
  91. mysql_query("INSERT INTO price_stock (retailer,price) VALUES('Argos', '$argos_pd' )")
  92. or die(mysql_error());
  93. mysql_query("INSERT INTO price_stock (retailer,price) VALUES('eBuyer', '$ebuyer_pd' )")
  94. or die(mysql_error());
  95. mysql_query("INSERT INTO price_stock (retailer,price) VALUES('PCWorld', '$pcworld_pd' )")
  96. or die(mysql_error());
  97.        
  98. <!-- MYSQL DATABASE % TABLE CREATION CODE -->
  99. <?php
  100. // sending query
  101. $result = mysql_query("SELECT * FROM {$table}");
  102. if (!$result) {
  103.     die("Query to show fields from table failed");
  104. }
  105.  
  106. $fields_num = mysql_num_fields($result);
  107.  
  108.  
  109. echo "<table width='650px'><tr>";
  110.  
  111. // printing table headers
  112. for($i=0; $i<$fields_num; $i++)
  113. {
  114.     $field = mysql_fetch_field($result);
  115.     echo "<td><b>{$field->name}</b></td>";
  116. }
  117. echo "</tr>n";
  118. // printing table rows
  119. while($row = mysql_fetch_row($result))
  120. {
  121.     echo "<tr>";
  122.  
  123.     // $row is array... foreach( .. ) puts every element
  124.     // of $row to $cell variable
  125.     foreach($row as $cell)
  126.         echo "<td>$cell</td>";
  127.  
  128.     echo "</tr>n";
  129. }
  130. mysql_free_result($result);
  131.  
  132.  
  133. ?>
  134.        
  135. <?php
  136. // a.php
  137. $myVar = 123;
  138.        
  139. <?php
  140. // b.php
  141. include 'a.php'
  142. echo isset($myVar)?'Included':'Not included';