php FOR not work - php

this my code. It create xml file from mysql..
my problem:
for($i=0; $i<count($str_exp1); $i++) // HERE
{
$str_exp2 = explode(",", $str_exp1[$i]);
$newnode->setAttribute("lat", $str_exp2[0]);
$newnode->setAttribute("lng", $str_exp2[1]);
}
for not show the all data... it only show me latest one data.. i cant find where is there problem..
P.S. Sorry for my english
0
$doc = new DOMDocument("1.0");
$node = $doc->createElement("marker");
$parnode = $doc->appendchild($node);
$result = mysql_query("SELECT * FROM usersline");
if(mysql_num_rows($result)>0)
{
header("Content-type: text/xml");
while ($mar = mysql_fetch_array($result))
{
$node = $doc->createElement("line");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("id_line", $mar['id_line']);
$newnode->setAttribute("color", $mar['colour']);
$newnode->setAttribute("width", $mar['width']);
$node = $doc->createElement("point");
$newnode = $parnode->appendChild($node);
$str_exp1 = explode(";", $mar['coordinats']);
for($i=0; $i<count($str_exp1); $i++) // HERE
{
$str_exp2 = explode(",", $str_exp1[$i]);
$newnode->setAttribute("lat", $str_exp2[0]);
$newnode->setAttribute("lng", $str_exp2[1]);
}
}
$xmlfile = $doc->saveXML();
echo $xmlfile;
}
else
{
echo "<p>Ëèíèé íå îáíàðóæåíî!</p>";
}

Your problem is that you set multiple values to the same node. So you are always overwriting the attribute values with the latest lat/long value.
Instead you need to add a new element per each lat/long pair because XML elements do not have duplicate attributes.
Some example code based on your question, as you can see I introduce some functions to keep things more modular:
$result = $db->query("SELECT * FROM usersline");
if (!$result || !count($result)) {
echo "<p>Ëèíèé íå îáíàðóæåíî!</p>";
return;
}
$doc = new DOMDocument("1.0");
$doc->loadXML('<marker/>');
$marker = $doc->documentElement;
foreach ($result as $mar) {
$line = $doc->createElement('line');
$attributes = array_map_array(['id_line', 'colour' => 'color', 'width'], $mar);
element_add_attributes($line, $attributes);
foreach (coordinates_to_array($mar['coordinats']) as $latlong) {
$point = $doc->createElement('point');
element_add_attributes($point, $latlong);
$line->appendChild($point);
}
$marker->appendChild($line);
}
header("Content-type: text/xml");
echo $doc->saveXML();
function element_add_attributes(DOMElement $element, array $attributes)
{
foreach ($attributes as $name => $value) {
if (!is_string($name)) continue;
$element->setAttribute($name, $value);
}
}
function array_map_array(array $map, array $array)
{
$result = array();
foreach ($map as $alias => $name) {
$source = is_string($alias) ? $alias : $name;
$result[$name] = $array[$source];
}
return $result;
}
function coordinates_to_array($coordinates)
{
$result = array();
$coordinatePairs = explode(";", $coordinates);
foreach ($coordinatePairs as $coordinatePair) {
list($pair['lat'], $pair['lng']) = explode(',', $coordinatePair, 2) + ['', ''];
$result[] = $pair;
}
return $result;
}
I hope this example is helpful and shows you some ways how you can put a problem apart so that your code becomes more easy and more stable.
To make use of $db->query(...) first define a class that has the query method:
class DB {
public function query($sql) {
$dbhandle = mysql_query($sql);
$result = array();
while ($mar = mysql_fetch_array($dbhandle))
$result[] = $mar
;
return $result;
}
}
Then instantiate it:
$db = new DB();
You can then use the code above for that part.
For the problem with the PHP 5.4 array notation for example in this line:
$attributes = array_map_array(['id_line', 'colour' => 'color', 'width'], $mar);
First of all extract the array out of it:
$mapping = ['id_line', 'colour' => 'color', 'width'];
$attributes = array_map_array($mapping, $mar);
Then define the array with the array( and ) notation instead of [ and ]:
$mapping = array('id_line', 'colour' => 'color', 'width');
$attributes = array_map_array($mapping, $mar);
Do so as well in other places, e.g.
['', '']
becomes
array('', '')
and similar.

Replace your code with this:
$str_exp1 = explode(";", $mar['coordinats']);
$newnode->setAttribute("lat", $str_exp1[0]);
$newnode->setAttribute("lng", $str_exp1[1]);

Related

output and call array from class function (rollingcurl)

Excuse my English, please.
I use Rollingcurl to crawl various pages.
Rollingcurl: https://github.com/LionsAd/rolling-curl
My class:
<?php
class Imdb
{
private $release;
public function __construct()
{
$this->release = "";
}
// SEARCH
public static function most_popular($response, $info)
{
$doc = new DOMDocument();
libxml_use_internal_errors(true); //disable libxml errors
if (!empty($response)) {
//if any html is actually returned
$doc->loadHTML($response);
libxml_clear_errors(); //remove errors for yucky html
$xpath = new DOMXPath($doc);
//get all the h2's with an id
$row = $xpath->query("//div[contains(#class, 'lister-item-image') and contains(#class, 'float-left')]/a/#href");
$nexts = $xpath->query("//a[contains(#class, 'lister-page-next') and contains(#class, 'next-page')]");
$names = $xpath->query('//img[#class="loadlate"]');
// NEXT URL - ONE TIME
$Count = 0;
$next_url = "";
foreach ($nexts as $next) {
$Count++;
if ($Count == 1) {
/*echo "Next URL: " . $next->getAttribute('href') . "<br/>";*/
$next_link = $next->getAttribute('href');
}
}
// RELEASE NAME
$rls_name = "";
foreach ($names as $name) {
$rls_name .= $name->getAttribute('alt');
}
// IMDB TT0000000 RLEASE
if ($row->length > 0) {
$link = "";
foreach ($row as $row) {
$tt_info .= #get_match('/tt\\d{7}/is', $doc->saveHtml($row), 0);
}
}
}
$array = array(
$next_link,
$rls_name,
$tt_info,
);
return ($array);
}
}
Output/Return:
$array = array(
$next_link,
$rls_name,
$tt_info,
);
return ($array);
Call:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
function get_match($regex, $content, $pos = 1)
{
/* do your job */
preg_match($regex, $content, $matches);
/* return our result */
return $matches[intval($pos)];
}
require "RollingCurl.php";
require "imdb_class.php";
$imdb = new Imdb;
if (isset($_GET['action']) || isset($_POST['action'])) {
$action = (isset($_GET['action'])) ? $_GET['action'] : $_POST['action'];
} else {
$action = "";
}
echo " 2222<br /><br />";
if ($action == "most_popular") {
$popular = '&num_votes=1000,&production_status=released&groups=top_1000&sort=moviemeter,asc&count=40&start=1';
if (isset($_GET['date'])) {
$link = "https://www.imdb.com/search/title?title_type=feature,tv_movie&release_date=,".$_GET['date'].$popular;
} else {
$link = "https://www.imdb.com/search/title?title_type=feature,tv_movie&release_date=,2018".$popular;
}
$urls = array($link);
$rc = new RollingCurl([$imdb, 'most_popular']); //[$imdb, 'most_popular']
$rc->window_size = 20;
foreach ($urls as $url) {
$request = new RollingCurlRequest($url);
$rc->add($request);
}
$stream = $rc->execute();
}
If I output everything as "echo" in the class, everything is also displayed. However, I want to call everything individually.
If I now try to output it like this, it doesn't work.
$stream[0]
$stream[1]
$stream[3]
Does anyone have any idea how this might work?
Thank you very much in advance.
RollingCurl doesn't do anything with the return value of the callback, and doesn't return it to the caller. $rc->execute() just returns true when there's a callback function. If you want to save anything, you need to do it in the callback function itself.
You should make most_popular a non-static function, and give it a property $results that you initialize to [] in the constructor.. Then it can do:
$this->results[] = $array;
After you do
$rc->execute();
you can do:
foreach ($imdb->results as $result) {
echo "Release name: $result[1]<br>TT Info: $result[2]<br>";
}
It would be better if you put the data you extracted from the document in arrays rather than concatenated strings, e.g.
$this->$rls_names = [];
foreach ($names as $name) {
$this->$rls_names[] = $name->getAttribute('alt');
}
$this->$tt_infos = [];
foreach ($rows as $row) {
$this->$tt_infos[] = #get_match('/tt\\d{7}/is', $doc->saveHtml($row), 0);
}
$this->next_link = $next[0]->getAttribute('href'); // no need for a loop to get the first element of an array

Magento get Layered Filter from Collection Programmatically

I am building custom webservices for the Mobile app,
I want to load filter according to product collection programmatically.
Currently i am getting filter on entire category but when we apply any filter it will gave same filter option again.
Below is the code which i have used for category filter
$layer = Mage::getModel("catalog/layer");
$_category = Mage::getModel("catalog/category")->load($category_id);
$layer->setCurrentCategory($_category);
$attributes = $layer->getFilterableAttributes();
/* custom for filters
$collection = Mage::getResourceModel('catalog/product_attribute_collection');
$collection->setItemObjectClass('catalog/resource_eav_attribute');
$collection->setAttributeSetFilter(array(4));
$collection->addStoreLabel(Mage::app()->getStore()->getId());
$collection->setOrder('position', 'ASC');
$collection->addFieldToFilter('additional_table.is_filterable', array('gt' => 0));
$attributes = $collection->load();
*/
// print_r($attributes->getData());exit();
$attributeCollection =array();
$i=0;
$attributeCollection = array();
foreach ($attributes as $attribute) {
if($attribute->getAttributeCode() == 'price') {
$filterBlockName = 'catalog/layer_filter_price';
}elseif($attribute->getBackendType() == 'decimal'){
$filterBlockName = 'catalog/layer_filter_decimal';
}else{
$filterBlockName = 'catalog/layer_filter_attribute';
}
$result = Mage::app()->getLayout()->createBlock($filterBlockName)->setLayer($layer)->setAttributeModel($attribute)->init();
$attributeCollection[$i]['Code'] = $attribute->getAttributeCode();
$attributeCollection[$i]['Label'] = $attribute->getStoreLabel();
$j=0;
$attributeOptionCollection =array();
foreach($result->getItems() as $option) {
if($attribute->getAttributeCode()=='price'){
$attributeOptionCollection[$j]['Label'] = strip_tags($option->getLabel());
}else{
$attributeOptionCollection[$j]['Label'] = $option->getLabel();
}
$attributeOptionCollection[$j]['Value'] = $option->getValue();
$attributeOptionCollection[$j]['Type'] = $option->getFrontend();
$j++;
}
$attributeCollection[$i]['Options'] = $attributeOptionCollection;
$i++;
}
// print_r($attributeCollection);exit();
// echo "<pre>";
$counter = 0;
$availableSortOptions = $_category->getavailablesortbyoptions();
foreach ($availableSortOptions as $key=>$options) {
$optinsList[$counter]['Code'] = $key;
$optinsList[$counter]['Label'] =$options;
$counter++;
// print_r($options);
}
$information['Filters'] = $attributeCollection;
Please suggest how i proceed
I used this for a particular Category - hope this helps
//require necessary files
require_once('../app/Mage.php');
$categoryID = $_POST['categoryID'];
//necessary initialization
Mage::app();
$websiteId = Mage::app()->getWebsite()->getId();
$store = Mage::app()->getStore();
try{
$json = array('status' => true);
$json['data'] = array();
$layer = Mage::getModel("catalog/layer");
$category = Mage::getModel("catalog/category")->load($categoryID); // 3rd Category
$layer->setCurrentCategory($category);
$attributes = $layer->getFilterableAttributes();
foreach ($attributes as $attribute) {
$filterBlockName = 'catalog/layer_filter_attribute';
$result = Mage::app()->getLayout()->createBlock($filterBlockName)->setLayer($layer)->setAttributeModel($attribute)->init();
foreach($result->getItems() as $option) {
$count[] = array('attribute_name' => $option->getLabel(),'attribute_value' => $option->getValue());
}
if($count!=null){
$json['data'][] = array('name'=>ucfirst($attribute->getAttributeCode()),'count'=>$count);
}
unset($count);
}
}
catch (Exception $e) {
$json = array('status' => false, 'message' => $e->getMessage());
}
echo json_encode($json);

Decoding mixture of array and stdObject to Database

I am trying to decode a collection of json files to a mysql database and return the decoded values to a datatable for presentation. I have one table called ec2_instances, and want to send to that table an array of values which are located at cfi configuration which works fine. but have now added a new column called aws account id which is on object rather than an array I have updated the model to include the new column but I am struggling
<?php
function from_camel_case($input)
{
preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
$ret = $matches[0];
foreach ($ret as &$match)
{
$match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
}
return implode('_', $ret);
}
$resource_types = array();
$resource_types['AWS::EC2::Instance'] = 'EC2Instance';
$resource_types['AWS::EC2::NetworkInterface'] = 'EC2NetworkInterface';
$resource_types['AWS::EC2::VPC'] = 'VPC';
$resource_types['AWS::EC2::Volume'] = 'Volume';
$resource_types['AWS::EC2::SecurityGroup'] = 'EC2SecurityGroup';
$resource_types['AWS::EC2::Subnet'] = 'Subnet';
$resource_types['AWS::EC2::RouteTable'] = 'RouteTable';
$resource_types['AWS::EC2::EIP'] = 'EIP';
$resource_types['AWS::EC2::NetworkAcl'] = 'NetworkAcl';
$resource_types['AWS::EC2::InternetGateway'] = 'InternetGateway';
$accounts = DB::table('aws_account')->get();
$account_id = array($accounts);
$account_id_exists = array_add($account_id, 'key', 'value');
foreach(glob('../app/views/*.json') as $filename)
{
//echo $filename;
$data = file_get_contents($filename);
if($data!=null)
{
$decoded=json_decode($data,true);
if(isset($decoded["Message"]))
{
//echo "found message<br>";
$message= json_decode($decoded["Message"]);
if(isset($message->configurationItem))
{
// echo"found cfi<br>";
$insert_array = array();
$cfi = $message->configurationItem;
switch ($cfi->configurationItemStatus)
{
case "ResourceDiscovered":
//echo"found Resource Discovered<br>";
if (array_key_exists($cfi->resourceType,$resource_types))
{
//var_dump($cfi->resourceType);
$resource = new $resource_types[$cfi->resourceType];
foreach ($cfi->configuration as $key => $value)
{
if (in_array($key,$resource->fields))
{
$insert_array[from_camel_case($key)] = $value;
}
}
if (array_key_exists($cfi->awsAccountId,$resource_types))
{
$resource = new $resource_types[$cfi->awsAccountId];
foreach ($cfi->awsAccountId as $key => $value)
{
if (in_array($key,$resource->fields))
{
$insert_array[from_camel_case($key)] = $value;
}
}
$resource->populate($insert_array);
if (!$resource->checkExists())
{
$resource->save();

Unexpected output from xpath screen scraping

Despite the xpath being correct (to the best of my knowledge), this code is still outputting strangely.
By this I mean that lots of was_price and now_price values are not being scraped from the page and so are returning as £.
Any idea what's wrong?
Here's the site I'm scraping from.
Code:
function scrape($list_url, $shop_name, $photo_location, $photo_url_root, $product_location, $product_url_root, $was_price_location, $now_price_location, $gender, $country, mysqli $con)
{
$html = file_get_contents($list_url);
$doc = new DOMDocument();
libxml_use_internal_errors(TRUE);
if(!empty($html))
{
$doc->loadHTML($html);
libxml_clear_errors(); // remove errors for yucky html
$xpath = new DOMXPath($doc);
/* FIND LINK TO PRODUCT PAGE */
$products = array();
$row = $xpath->query($product_location);
/* Create an array containing products */
if ($row->length > 0)
{
foreach ($row as $location)
{
$product_urls[] = $product_url_root . $location->getAttribute('href');
}
}
else { echo "product location is wrong<br>";}
$imgs = $xpath->query($photo_location);
/* Create an array containing the image links */
if ($imgs->length > 0)
{
foreach ($imgs as $img)
{
$photo_url[] = $photo_url_root . $img->getAttribute('src');
}
}
else { echo "photo location is wrong<br>";}
$was = $xpath->query($was_price_location);
/* Create an array containing the was price */
if ($was->length > 0)
{
foreach ($was as $price)
{
$stripped = preg_replace("/[^0-9,.]/", "", $price->nodeValue);
$was_price[] = "£".$stripped;
}
}
else { echo "was price location is wrong<br>";}
$now = $xpath->query($now_price_location);
/* Create an array containing the sale price */
if ($now->length > 0)
{
foreach ($now as $price)
{
$stripped = preg_replace("/[^0-9,.]/", "", $price->nodeValue);
$now_price[] = "£".$stripped;
}
}
else { echo "now price location is wrong<br>";}
$result = array();
/* Create an associative array containing all the above values */
foreach ($product_urls as $i => $product_url)
{
$result[] = array(
'product_url' => $product_url,
'shop_name' => $shop_name,
'photo_url' => $photo_url[$i],
'was_price' => $was_price[$i],
'now_price' => $now_price[$i]
);
}
echo json_encode($result);
}
else
{
echo "this is empty";
}
}
$list_url = "http://www.asos.com/Women/Sale/70-Off-Sale/Cat/pgecategory.aspx?cid=16903&pge=0&pgesize=1002&sort=-1";
$shop_name = "ASOS";
$photo_location = "//ul[#id='items']/li/div[#class='categoryImageDiv']/*[1]/img";
$photo_url_root = "";
$product_location = "//ul[#id='items']/li/div[#class='categoryImageDiv']/*[1]";
$product_url_root = "http://www.asos.com";
$was_price_location = "//ul[#id='items']/li/div[#class='productprice']/span[#class='price' or #class='recRP rrp']"; // leave recRP rrp
$now_price_location = "//ul[#id='items']/li/div[#class='productprice']/span[#class='prevPrice previousprice' or #class='price outlet-current-price']"; // leave outlet-current-price
$gender = "f";
$country = "UK";
scrape($list_url, $shop_name, $photo_location, $photo_url_root, $product_location, $product_url_root, $was_price_location, $now_price_location, $gender, $country, $con);
I was counting the number of matches per site, and it looks like that there are 1563 hits for your was_price and only 1440 for your now_price. This tells me that either your Xpath isn't working in 100% of the cases or that some of the articles only have one price.
So you have to make sure that all of our XPath expressions return the same amount of results, so that: products = new_price = old_price = images

SimpleXML node with spaces and equal signs in the node name

I have an xml document that contains spaces and equal signs in the node names. I'm trying to use SimpleXML to extract the data from those nodes but it returns blank no matter what I try.
A sample of the xml document
<code><away>
<radio>url.here</radio>
<live bitrate="1">url.here</live>
<live bitrate="0">url.here</live>
</away></code>
I have tried using bothecho "<td>".$node->away->{'live bitrate="1"'}."</td>";
echo "<td>".$node->away->{'live'}->{'bitrate="1"'}."</td>";
Here is a function I use to convert the SimpleXML object to an array:
public function simpleXMLToArray($xml,
$flattenValues=true,
$flattenAttributes = true,
$flattenChildren=true,
$valueKey='#value',
$attributesKey='#attributes',
$childrenKey='#children'){
$return = array();
if(!($xml instanceof SimpleXMLElement)){return $return;}
$name = $xml->getName();
$_value = trim((string)$xml);
if(strlen($_value)==0){$_value = null;};
if($_value!==null){
if(!$flattenValues){$return[$valueKey] = $_value;}
else{$return = $_value;}
}
$children = array();
$first = true;
foreach($xml->children() as $elementName => $child){
$value = $this->simpleXMLToArray($child, $flattenValues, $flattenAttributes, $flattenChildren, $valueKey, $attributesKey, $childrenKey);
if(isset($children[$elementName])){
if($first){
$temp = $children[$elementName];
unset($children[$elementName]);
$children[$elementName][] = $temp;
$first=false;
}
$children[$elementName][] = $value;
}
else{
$children[$elementName] = $value;
}
}
if(count($children)>0){
if(!$flattenChildren){$return[$childrenKey] = $children;}
else{$return = array_merge($return,$children);}
}
$attributes = array();
foreach($xml->attributes() as $name=>$value){
$attributes[$name] = trim($value);
}
if(count($attributes)>0){
if(!$flattenAttributes){$return[$attributesKey] = $attributes;}
else{$return = array_merge($return, $attributes);}
}
return $return;
}

Categories