74835

integrating a function

Question:

this is the follow-up to my question about getting a div's content. the second function is the one im having a hard time with. im pretty sure i can call a function from another one, but im not sure about placing them into oneanother like i did here. its obviously a silly attempt to make the code work, since it gives me an error:

<em>Blackline Frostbyte : in stock. : $139.99</em>

<em>Fatal error: Cannot redeclare get_string_between() (previously declared in /home/rambazar/public_html/cron.php:69) in /home/rambazar/public_html/cron.php on line 69</em>

as i see this, the code is partially ok, because it gets a products stock info and the price tag, but the code stops and i cant figure out where <em>get_string_between</em> is redeclared, as it is only called. please help me sorting this out, thanks!

<?php set_time_limit(1800); include("admin/include/db.php"); error_reporting(E_ALL); $res=mysql_query("select * from products"); while($row=mysql_fetch_array($res)) { $availability=getavailability($row['newegg_productid']); $price=getprice($row['newegg_productid']); echo $row['productname']." : ".$availability." : ".$price."<br />"; } function getavailability($itemId) { $url=trim("http://www.newegg.com/Product/Product.aspx?Item=".$itemId); $ch = curl_init(); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_VERBOSE, true); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 20); curl_setopt($ch, CURLOPT_AUTOREFERER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); $content = curl_exec ($ch); curl_close ($ch); $content=strtolower($content); $buff=$content; $isAvailable=false; $pos1=strpos($content,'<p class="note">')+16; if($pos1==16)return ""; $pos2=strpos($content,'

',$pos1); $availability= trim(substr($content,$pos1,($pos2-$pos1))); return strip_tags($availability); } function getprice($itemId) { function get_string_between($string, $start, $end) { $string = " ".$string; $ini = strpos($string,$start); if ($ini == 0) return ""; $ini += strlen($start); $len = strpos($string,$end,$ini) - $ini; return substr($string,$ini,$len); } $data = file_get_contents("http://www.newegg.com/Product/Product.aspx?Item=".$itemId); $pricediv = get_string_between($data, '<div class="current" id="singleFinalPrice"><span class="label">Now:', '</div'); $price = strip_tags($pricediv); return $price; } ?>

Answer1:

Take out the get_string_between() out of the getprice() function and you should be good to go:

function get_string_between($string, $start, $end) { $string = " ".$string; $ini = strpos($string,$start); if ($ini == 0) return ""; $ini += strlen($start); $len = strpos($string,$end,$ini) - $ini; return substr($string,$ini,$len); } function getprice($itemId) { $data = file_get_contents("http://www.newegg.com/Product/Product.aspx?Item=".$itemId); $pricediv = get_string_between($data, '<div class="current" id="singleFinalPrice"><span class="label">Now:', '</div'); $price = strip_tags($pricediv); return $price; }

Recommend

  • How does one use multiple modules in fortran without name clashes
  • Arduino - struct out of scope why?
  • Laravel subdomain 500 error
  • Multiple File Upload results in foreach() error
  • Unable to read PHP Mail Attachment
  • nginx: Map single static URL to a PHP file
  • PHP Help with regular expressions… error: Delimiter must not be alphanumeric
  • CodeIgniter: Allowed memory exhausted
  • Can't access index in folder when rewrite engine removes .php and .html
  • OAuth 2.0 - When should an access token be renewed with refresh token?
  • Swiftmailer email message returns error when attaching file
  • automatically update mysql field based on value of other field
  • Wordpress header external php file - change title?
  • Linear gradient not applying in Webkit with d3 generated SVG
  • Error Processing Request: Mage registry key “_singleton/inchoo_notes/feed_updates” already exists
  • Show records ordered with maximum price first in PHP & MySQL
  • How to change default stop edit behavior in jtable
  • What is this strange character in chrome's resource css viewer?
  • Time out Error in send mail
  • Find JSON nested nodes using multiple string values
  • Hector: how to query parts of a Composite Type
  • JSON encode and decode on PHP
  • Building Qt project for C++11 standard
  • XSLT foreach repeating nodes to flat
  • How to create a 2D image by rotating 1D vector of numbers around its center element?
  • Thread 1: EXC_BAD_ACCESS (code =1 address = 0x0)
  • converting text file into xml using php?
  • Could not find rake using whenever rails
  • How to rebase a series of branches?
  • Get one-time binding to work for ng-if
  • Volley JsonObjectRequest send headers in GET Request
  • Importing jscolor library in angular 2
  • Do I've to free mysql result after storing it?
  • Why winpcap requires both .lib and .dll to run?
  • A cron job substitute?
  • json Serialization in asp
  • How to set the response of a form post action to a iframe source?
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • unknown Exception android
  • Easiest way to encapsulate a HTML5 webpage into an android app?