foreach($key_doc_count as $item) {
mb_language('Japanese');
$product = $item["key"];
$product_url = 'https://search.rakuten.co.jp/search/mall/'.urlencode($product) . '/';
$source = file_get_contents($product_url);
$source = mb_convert_encoding($source, 'utf8', 'auto');
$rakuten_search_html = str_get_html($source);
$count=0;
foreach ($rakuten_search_html->find('img._verticallyaligned') as $item_image) {
if(strlen($item_image->alt > 2))
{
$ss['image_url'] = $item_image->src;
$ss['title'] = $item_image->alt;
$items_kk[] = $ss;
$count++;
if($count <5)
{
break;
}
}
}
$new_item["term"] = $item["key"];
$new_item["current_count"] = $item["doc_count"];
$new_item["results"] = $terms_kk;
$new_word_array[] = $new_item;
}
var_dump($new_word_array);
I am trying to insert the url and title of the product in an array names $ss and then assign that array to $new_term["result"] .
But its not working
The error was HTTP ERROR 500
The variables $items_kk and $new_word_array seems to have not been initialized outside of the foreach loop. Try to init them as an empty array just before the loop :
$new_word_array = [];
foreach($key_doc_count as $item) {
mb_language('Japanese');
$product = $item["key"];
$product_url = 'https://search.rakuten.co.jp/search/mall/'.urlencode($product) . '/';
$source = file_get_contents($product_url);
$source = mb_convert_encoding($source, 'utf8', 'auto');
$rakuten_search_html = str_get_html($source);
$count=0;
$items_kk = [];
foreach ($rakuten_search_html->find('img._verticallyaligned') as $item_image) {
if(strlen($item_image->alt > 2))
{
$ss['image_url'] = $item_image->src;
$ss['title'] = $item_image->alt;
$items_kk[] = $ss;
$count++;
if($count <5)
{
break;
}
}
}
$new_item["term"] = $item["key"];
$new_item["current_count"] = $item["doc_count"];
$new_item["results"] = $terms_kk;
$new_word_array[] = $new_item;
}
var_dump($new_word_array);
Also, you're setting $items_kk and assigning $terms_kk to the new array. It may be an error ?
Related
i'm setting up a new server, and want to scrape some information from a website
this is my code i tried to scrape pages one by one but i only get 2 of pages
$result = array();
function scrapingAnimelist($url, $page)
{
$res = array();
$urlParsed = $url . "&page=" . $page;
$html = file_get_html($urlParsed);
$pageData = array();
foreach ($html->find('div[class=body]') as $item) {
$metaData = array();
$metaData['title'] = $item->find('h2[class=title]', 0)->innertext;
$metaData['img'] = $item->find('img[class=img]', 0)->src;
$metaData['url'] = $item->find('a', 0)->href;
array_push($pageData, $metaData);
}
$res[$page] = $pageData;
if (sizeof($pageData) == 20) {
$page++;
$res[$page] = scrapingAnimelist($url, $page);
}
global $result;
$result = $res;
return $pageData;
}
i expect the output of json object with only 2 arrays ( page datas ) to be 3 in link : https://anime-list2.cf/anime-search?s=mag
Your $result is not set on the second run
yout should make it like this
$result = array();
function scrapingAnimelist($url, $page) {
global $result;
$urlParsed = $url . "&page=" . $page;
$html = file_get_html($urlParsed);
$pageData = array();
foreach ($html->find('div[class=body]') as $item) {
$metaData = array();
$metaData['title'] = $item->find('h2[class=title]', 0)->innertext;
$metaData['img'] = $item->find('img[class=img]', 0)->src;
$metaData['url'] = $item->find('a', 0)->href;
array_push($pageData, $metaData);
}
$result[$page] = $pageData;
if (sizeof($pageData) == 20) {
return scrapingAnimelist($url, $page + 1);
}
return $result;
}
My spreadsheet will always have column B,C,D,E,F,G row 3 = address, name, phone, department, etc.. The data from the cells beneath (some empty some populated) 1234 x street, 1234 y street, 555-5555, HR, etc. So if My array could look like this:
[1] =>array(
['address1'] =>'1234 x street'
['name1'] =>'1234 y street'
['phone1'] =>'555-5555'
...etc
['department1'] =>'HR'
[2] =>array(
['address2'] =>'1234 x street'
['name2'] =>'1234 y street'
['phone2'] =>'555-5555'
...etc
['department2'] =>'HR'
My current code is:
<SNIP>
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($inputFileName);
$objWorksheet = $objPHPExcel->getActiveSheet();
if($header){
$highestRow = $objWorksheet->getHighestRow();
$highestColumn = $objWorksheet->getHighestColumn();
$headingsArray = $objWorksheet->rangeToArray('A1:'.$highestColumn.'1',null, true, true, true);
$headingsArray = $headingsArray[1];
$r = -1;
$namedDataArray = array();
for ($row = 2; $row <= $highestRow; ++$row) {
$dataRow = $objWorksheet->rangeToArray('A'.$row.':'.$highestColumn.$row,null, true, true, true);
if ((isset($dataRow[$row]['A'])) && ($dataRow[$row]['A'] > '')) {
++$r;
foreach($headingsArray as $columnKey => $columnHeading) {
$namedDataArray[$r][$columnHeading] = $dataRow[$row][$columnKey];
}
}
}
}
else{
$namedDataArray = $objWorksheet->toArray(null,true,true,true);
}
Research suggests I can use one of the following methods however I need help putting it all together:
$column = 'IV';
$columnIndex = PHPExcel_Cell::columnIndexFromString($column);
$adjustment = -2;
$currentColumn = 'BZ';
$columnIndex = PHPExcel_Cell::columnIndexFromString($currentColumn);
$adjustedColumnIndex = $columnIndex + $adjustment;
$adjustedColumn = PHPExcel_Cell::stringFromColumnIndex($adjustedColumnIndex - 1);
I ended up not using any of the native classes for the sorting piece, lowered overhead by just writing it myself as follows:
<?php
//require 'FirePHPCore/fb.php'; This section was just to use FirePHP so I could see the JSON output without using html.
//ob_start('ob_gzhandler');
//FB::info('Hello, FirePHP');
//FB::log('Log message');
//FB::info('Info message');
//FB::warn('Warn message');
//FB::error('Error message');
require_once dirname(__FILE__) . '/classes/PHPExcel.php';
// Include PHPExcel_IOFactory
include 'classes/PHPExcel/IOFactory.php';
$inputFileName = './uploads/fw.xls';
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);/** Identify the type of $inputFileName **/
$objReader = PHPExcel_IOFactory::createReader($inputFileType);/** Create a new Reader of the type that has been identified **/
$objReader->setReadDataOnly(true); /** Set read type to read cell data only **/
$objPHPExcel = $objReader->load($inputFileName);/** Load $inputFileName to a PHPExcel Object **/
$objWorksheet = $objPHPExcel->getActiveSheet();//Get worksheet and built array with first row as header
// stuff all tabs into their own array
$sheetNames = $objPHPExcel->getSheetNames();
foreach ($sheetNames as $sheet) {
$sheet2 = preg_replace("/\s+/","_",$sheet);
//print "$sheet: $sheet2<br>\n";
${$sheet2} = $objPHPExcel->getSheetByName($sheet);
}
// print_r($Network_Security);
//exit;
// parse each tab, tack onto end of $prejson array
$prejson = [];
// parse network security
//use class to find data range
$highestRow = $Network_Security->getHighestRow();
$range = $Network_Security->calculateWorksheetDimension();
// manually narrow range columns
$range = preg_replace("/A/","B",$range);
$range = preg_replace("/L/","K",$range);
//create array of data rows
$rows = $Network_Security->rangeToArray($range);
$active = 0;
//loop through each row
foreach ($rows as $row) {
if (preg_match("/Do not edit the data/",$row[0])) { $active = 2; continue; }
if (preg_match("/^Line/",$row[0]) && $active == 0) { $active = 1; } //show header
if ($row[0] == "") { $active = 0; continue; }
if ($active == "1") {
//key array
array_unshift($row,"netsec");
// stuff to prejson
array_push($prejson,$row);
}
}
// end netsec
// parse network translation
$highestRow = $Network_Translation->getHighestRow();
$range = $Network_Translation->calculateWorksheetDimension();
$range = preg_replace("/A/","B",$range);
$range = preg_replace("/J/","G",$range);
$rows = $Network_Translation->rangeToArray($range);
$active = 0;
foreach ($rows as $row) {
if (preg_match("/^Source/",$row[0]) && $active == 0) { $active = 1; } //show header
if (preg_match("/Do not edit the data/",$row[0])) { $active = 2; continue; }
if ($row[0] == "") { $active = 0; continue; }
if ($active == "1") {
//key array
array_unshift($row,"nettrans");
// stuff to prejson
array_push($prejson,$row);
}
}
// end nettrans
// parse routing
$highestRow = $Routing->getHighestRow();
$range = $Routing->calculateWorksheetDimension();
$range = preg_replace("/A/","B",$range);
$range = preg_replace("/H/","G",$range);
$rows = $Routing->rangeToArray($range);
$active = 0;
foreach ($rows as $row) {
if (preg_match("/Do not edit the data/",$row[0])) { $active = 2; continue; }
//if (preg_match("/^Add/",$row[0]) && $active == 0) { $active = 1; continue; } //hide header
if (preg_match("/^Add/",$row[0]) && $active == 0) { $active = 1; } //show header
if ($row[0] == "") { $active = 0; continue; }
if ($active == "1") {
// key array
array_unshift($row,"rtg");
// stuff to prejson
array_push($prejson,$row);
}
}
// end routing
//print "<pre>";
//var_dump($prejson);
$json = json_encode($prejson);
//$json_string = prettyPrint($json);
$json_string = json_encode($json,JSON_PRETTY_PRINT); //remove ,JSON_PRETTY_PRINT
print $json;
//print "</pre>";
function prettyPrint( $json )
{
$result = '';
$level = 0;
$in_quotes = false;
$in_escape = false;
$ends_line_level = NULL;
$json_length = strlen( $json );
for( $i = 0; $i < $json_length; $i++ ) {
$char = $json[$i];
$new_line_level = NULL;
$post = "";
if( $ends_line_level !== NULL ) {
$new_line_level = $ends_line_level;
$ends_line_level = NULL;
}
if ( $in_escape ) {
$in_escape = false;
} else if( $char === '"' ) {
$in_quotes = !$in_quotes;
} else if( ! $in_quotes ) {
switch( $char ) {
case '}': case ']':
$level--;
$ends_line_level = NULL;
$new_line_level = $level;
break;
case '{': case '[':
$level++;
case ',':
$ends_line_level = $level;
break;
case ':':
$post = " ";
break;
case " ": case "\t": case "\n": case "\r":
$char = "";
$ends_line_level = $new_line_level;
$new_line_level = NULL;
break;
}
} else if ( $char === '\\' ) {
$in_escape = true;
}
if( $new_line_level !== NULL ) {
$result .= "\n".str_repeat( "\t", $new_line_level );
}
$result .= $char.$post;
}
return $result;
}
?>
Now I just need to get the hook so that when dropzone.js completes it will autopopulate the .js form.
How can I access the contents of $value[$i] which is an array. No luck using foreach in the form below.
The idea is to loop through $contentArray and display one item from each sub-array on every iteration.
$addsContent = $Adds->selectAdds(10);
$sharedArticlesContent = $SharedContent->getSharedContent($topic_selected, $filter_selected);
$blogPostsContent = $BlogPosts->getRecentBlogPostsByTopic("business");
$contentArray = array(
$sharedArticlesContent,
$addsContent ,
$blogPostsContent
);
foreach($contentArray as $value)
{
if(count($value)>$maxLength)
{
$maxLength = count($value);
}
}
for($i=0; $i<$maxLength; $i++)
{
foreach($contentArray as $value)
{
if(isset($value[$i]))
{
if($value==$sharedArticlesContent){
$data = $value[$i];
foreach($sharedArticlesContent as $data){
$post_id = $data['id'];
$uploaded_by = $data['uploaded_by'];
$text = $data['text'];
$image = $data['image'];
require 'template1.php';
}
}elseif($value==$addsContent){
//template2
}else{
//template3
}
}
}
}
You're dealing with an associative array here, you can access it like that:
<?php
$addsContent = $Adds->selectAdds(10);
$sharedArticlesContent = $SharedContent->getSharedContent($topic_selected, $filter_selected);
$blogPostsContent = $BlogPosts->getRecentBlogPostsByTopic("business");
$contentArray = array(
$sharedArticlesContent,
$addsContent ,
$blogPostsContent
);
foreach($contentArray as $value)
{
if(count($value)>$maxLength)
{
$maxLength = count($value);
}
}
for($i=0; $i<$maxLength; $i++)
{
foreach($contentArray as $value)
{
if(isset($value[$i]))
{
if($value==$sharedArticlesContent)
{
$post_id = $value[$i]['id'];
$uploaded_by = $value[$i]['uploaded_by'];
$text = $value[$i]['text'];
$image = $value[$i]['image'];
require 'template1.php';
}
elseif($value==$addsContent)
{
//template2
}
else
{
//template3
}
}
}
}
You don't need the foreach. $data is an associative array, you don't need to loop through it.
if($value==$sharedArticlesContent){
$data = $value[$i];
$post_id = $data['id'];
$uploaded_by = $data['uploaded_by'];
$text = $data['text'];
$image = $data['image'];
require 'template1.php';
}
I have a problem with shuffling and slicing array.
I have this code:
$maxDisplayItem = $this->_getFlexformConfig('max_item_to_display', 'product_setting');
$selectedProducts = array_slice($selectedProducts, 0, $maxDisplayItem);
foreach ($selectedProducts as $_id) {
shuffle($products);
foreach ($products as $_product) {
....
}
}
My code limiting the number of displayed item but didn't shuffle it at all.
When I change the order of actions:
shuffle($selectedProducts);
foreach ($selectedProducts as $_id) {
$maxDisplayItem = $this->_getFlexformConfig('max_item_to_display', 'product_setting');
$products = array_slice($products, 0, $maxDisplayItem);
foreach ($products as $_product) {
....
}
}
the code shuffling and slicing results but only first (e.g. 3 results) from whole array who has 50 items.
Could anyone help me with this?
here is the whole function:
function displayProductList()
{
// Store View
$store = $this->getStoreViewCode();
$selectedProducts = $this->getSelectedProducts();
$products = $this->_products->getProductsFromDb($selectedProducts, $store, $this->getProductsStoragePid());
// Load Template File
$templateHtml = $this->cObj->fileResource( $this->_getConfig('templateProductList') );
$productListHtml = $this->cObj->getSubpart($templateHtml, '###PRODUCT_LIST###');
$productHtml = $this->cObj->getSubpart($productListHtml, '###PRODUCT_ITEM###');
$subPartContent = ''; $item = 0; $items = count($products); $even = true; $line = '';
shuffle($selectedProducts);
foreach ($selectedProducts as $_id) {
$maxDisplayItem = $this->_getFlexformConfig('max_item_to_display', 'product_setting');
$products = array_slice($products, 0, $maxDisplayItem);
foreach ($products as $_product) {
if ($_id === $_product['product_id']) {
$markers = $this->_products->getProductMarkers($_product);
// Even/Odd CSS Class Determination
if ($even === true) {
$line = 'even';
$even = false;
} else {
$line = 'odd';
$even = true;
}
// Class Determination First/Last
if ($item == 0) {
$markers['###EVENODD###'] = $line . ' ' . 'first';
} else if ($item == $items-1) {
$markers['###EVENODD###'] = $line . ' ' . 'last';
} else {
$markers['###EVENODD###'] = $line;
}
// Check if the product has an image
$imageHtml = '<p>'.$this->pi_getLL('template_label_no_image_available').'</p>';
if ($markers['###DETAIL_IMAGE###'] != 'no_selection') {
$imageHtml = $this->cObj->getSubpart($productHtml, '###PRODUCT_IMAGE###');
}
$p = $this->cObj->substituteSubpart($productHtml, '###PRODUCT_IMAGE###', $imageHtml);
$subPartContent .= $this->cObj->substituteMarkerArray($p, $markers);
$item++;
}
}
}
return $this->cObj->substituteSubpart($productListHtml, '###PRODUCT_ITEM###', $subPartContent);
}
function displayProductList(){
// Store View
$store = $this->getStoreViewCode();
$selectedProducts = $this->getSelectedProducts();
$products = $this->_products->getProductsFromDb($selectedProducts, $store, $this->getProductsStoragePid());
// Load Template File
$templateHtml = $this->cObj->fileResource( $this->_getConfig('templateProductList') );
$productListHtml = $this->cObj->getSubpart($templateHtml, '###PRODUCT_LIST###');
$productHtml = $this->cObj->getSubpart($productListHtml, '###PRODUCT_ITEM###');
$subPartContent = ''; $item = 0; $items = count($products); $even = true; $line = '';
$maxDisplayItem = $this->_getFlexformConfig('max_item_to_display', 'product_setting');
$products = array_slice($products, 0, $maxDisplayItem);
shuffle($products);
foreach ($products as $_product) {
$markers = $this->_products->getProductMarkers($_product);
// Even/Odd CSS Class Determination
if ($even === true) {
$line = 'even';
$even = false;
} else {
$line = 'odd';
$even = true;
}
// Class Determination First/Last
if ($item == 0) {
$markers['###EVENODD###'] = $line . ' ' . 'first';
} else if ($item == $items-1) {
$markers['###EVENODD###'] = $line . ' ' . 'last';
} else {
$markers['###EVENODD###'] = $line;
}
// Check if the product has an image
$imageHtml = '<p>'.$this->pi_getLL('template_label_no_image_available').'</p>';
if ($markers['###DETAIL_IMAGE###'] != 'no_selection') {
$imageHtml = $this->cObj->getSubpart($productHtml, '###PRODUCT_IMAGE###');
}
$p = $this->cObj->substituteSubpart($productHtml, '###PRODUCT_IMAGE###', $imageHtml);
$subPartContent .= $this->cObj->substituteMarkerArray($p, $markers);
$item++;
}
return $this->cObj->substituteSubpart($productListHtml, '###PRODUCT_ITEM###', $subPartContent);
}
I have two pages, one is the script itself, and the other the page that calls it. I can display my feed fine, but I need to display it in reverse order. (oldest first). I have tried the two asort and arsort functions, but I cant get them to work.
Here is the code:
$RSS_Content = array();
function RSS_Tags($item, $type)
{
$y = array();
$tnl = $item->getElementsByTagName("title");
$tnl = $tnl->item(0);
$title = $tnl->firstChild->data;
$tnl = $item->getElementsByTagName("link");
$tnl = $tnl->item(0);
$link = $tnl->firstChild->data;
//$tnl = $item->getElementsByTagName("description");
// $tnl = $tnl->item(0);
// $description = $tnl->firstChild->data;
$y["title"] = $title;
$y["link"] = $link;
//$y["description"] = $description;
$y["type"] = $type;
return $y;
}
function RSS_Channel($channel)
{
global $RSS_Content;
$items = $channel->getElementsByTagName("item");
// Processing channel
$y = RSS_Tags($channel, 0); // get description of channel, type 0
array_push($RSS_Content, $y);
// Processing articles
foreach($items as $item)
{
$y = RSS_Tags($item, 1); // get description of article, type 1
array_push($RSS_Content, $y);
}
}
function RSS_Retrieve($url)
{
global $RSS_Content;
$doc = new DOMDocument();
$doc->load($url);
$channels = $doc->getElementsByTagName("channel");
$RSS_Content = array();
foreach($channels as $channel)
{
RSS_Channel($channel);
}
}
function RSS_RetrieveLinks($url)
{
global $RSS_Content;
$doc = new DOMDocument();
$doc->load($url);
$channels = $doc->getElementsByTagName("channel");
$RSS_Content = array();
foreach($channels as $channel)
{
$items = $channel->getElementsByTagName("item");
foreach($items as $item)
{
$y = RSS_Tags($item, 1); // get description of article, type 1
array_push($RSS_Content, $y);
}
}
}
function RSS_Links($url, $size)
{
global $RSS_Content;
$page = "<ul>";
RSS_RetrieveLinks($url);
if($size > 0)
$recents = array_slice($RSS_Content, 0, $size);
foreach($recents as $article)
{
$type = $article["type"];
if($type == 0) continue;
$title = $article["title"];
$link = $article["link"];
$page .= "<li>$title</li>\n";
}
$page .="</ul>\n";
return $page;
}
function RSS_Display($url, $size)
{
global $RSS_Content;
asort($RSS_Content);
$opened = false;
$page = "";
RSS_Retrieve($url);
if($size > 0)
$recents = array_slice($RSS_Content, 0, $size);
foreach($recents as $article)
{
$type = $article["type"];
if($type == 0)
{
if($opened == true)
{
$page .="</ul>\n";
$opened = false;
}
$page .="<b>";
}
else
{
if($opened == false)
{
$page .= "<ul>\n";
$opened = true;
}
}
$title = $article["title"];
$link = $article["link"];
// $description = $article["description"];
$page .= "<p>$title";
// if($description != false)
{
//$page .= "<br>$description";
}
$page .= "</p>\n";
if($type==0)
{
$page .="</b><br />";
}
}
if($opened == true)
{
$page .="</ul>\n";
}
return $page."\n";
}
Then on the second page, I have this:
$url = "feedurlhere.xml";
echo RSS_Links($url, 10);
?>
</div>
Thanks to Scriptol, the answer is:
change all instances of array_push to array_switch
Cheers