php - How can I don't use global variables in this page? -
on php document, made function.
function getprices($url) { global $pricelist; // declare global . point of this. $src = file_get_contents_curl($url); $dom = new domdocument(); $selector = new domxpath($dom); $results = $selector->query('//table/tr/td/span'); foreach($results $node) { array_push($pricelist, $node->nodevalue); } }
and bottom of page, called several.
$pricelist = array(); getprices("http://finance.naver.com/item/sise_day.nhn?code=005930"); getprices("http://finance.naver.com/item/sise_day.nhn?code=005930&page=2"); getprices("http://finance.naver.com/item/sise_day.nhn?code=005930&page=3");
and display it.
echo $pricelist[1]; echo $pricelist[2]; echo $pricelist[3];
the problem i'm using cms kinds of joomla, wordpress, , not support using global variable don't know how make without using global. how can make it? need many pages scrapping, i'm afraid. if scrap 1 page,
return in function, ,
$pricelist = getprices("http://finance.naver.com/item/sise_day.nhn?code=$code");
but don't know many scrapping case. please me...
you return partial results function , merge them complete results array.
<?php $result = []; $result = array_merge($result, getsomevalues()); $result = array_merge($result, getsomevalues()); $result = array_merge($result, getsomevalues()); var_export($result); function getsomevalues() { static $i = 0; // returning partial result of 3 elements return [ $i++, $i++, $i++ ]; }
prints
array ( 0 => 0, 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7, 8 => 8, )
Comments
Post a Comment