I have an array of potentially 1000s of items that I need to feed to and API that can handle 10 at a time. So, can I do something like this?
$data = [ ... ];
$start = 0;
$step = 10;
foreach (api_function(array_slice($data, $start, $step))){
$start += $step;
}
Using Nigel Ren's answer the final code looks like this:
$results = BC_cust_query($qlist, true);
$start = 0;
$step = 10;
$stop = count($results);
while ($start < $stop){
print_r(array_slice($results, $start, $step));
$start += $step;
}
Where print_r() will be replace by the API call
You would need to keep repeating the call to the API for each slice, until there is no more data left (check the start against the length of the array). Assuming it returns blank, some thing like
$data = [ ... ];
$start = 0;
$step = 10;
$countData = count($data);
// Fetch block of data whilst some data to fetch.
while ($start < $countData &&
$dataBlock = api_function(array_slice($data, $start, $step))){
// Loop over this block
foreach ( $dataBlock as $dataItem) {
// Process item
}
// Move onto next block
$start += $step;
}
or use a for loop...
$data = [ ... ];
$step = 10;
$countData = count($data);
// Fetch block of data whilst some data to fetch.
for ($start = 0;$start < $countData; $start += $step) {
$dataBlock = api_function(array_slice($data, $start, $step));
// Loop over this block
foreach ( $dataBlock as $dataItem) {
// Process item
}
}
I need to generate a dynamic text in accordance with the number of data that comes from the database.
In a html List, I need to display the following text as example:
GROUP A
GROUP B
GROUP C
If the amount of records coming from the DB is only 1000, then only one item list is displayed. 1000 records equals 1 Item list.
The text must be dynamically changed as described above.
Each text in the list is displayed for 1000 records.
The code below statically displays 11 items
I've made a few tries on the code below but still to no avail.
This code is working, but it display only the static text.
$return .= "<div id='test_id'><ul>";
// $qty = 10424 / $limit = 1000
// $tabs = $qty / $limit = 10,424
$tabs = $qty/$limit;
for ($i=0; $i < $tabs ; $i++)
{
$start = $i * $limit + 1;
$end = ($i + 1) * $limit;
$color = 'classpA';
$message = '<small>GROUP A</small>';
if ($i < $tab_selected) {
$color = 'classB';
$message = 'RESERVED';
}
$active = $i == $tab_selected ? "active" : "";
$return .= "<li class='tab {$color} {$active}' tab-id='#tab-".$i."'>
<span class='available {$color}_text'><small>{$message}</small></span>
<a href='#' class='{$color}_text'>".$start." - ".min($end,$qty)."</a></li>";
}
$return .= "</ul></div>";
And this is that logic that i`m trying to apply
$qty = 18424;
$limit = 1000;
$tabs = ($qty / $limit);
$group = $tabs / $limit;
$alphabet = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
for($i = 0; $i < $group; $i++) {
echo "Group {$alphabet[$i]}".'<br>';
}
echo 'Qtd Group= '.$group.' Value Var Tabs= '.$tabs;
https://www.tehplayground.com/zyVzmaeYnTyyA4S9
How can I make the magic happen?
Your logic is fine, just the limit to the for loop seems to be miscalculated.
Right now, for the variable group = (qty/limit)/limit, you have an extra division by limit here.
Whatever your need maybe for that division, you can simply use the value of tabs in your for loop condition, or assign it to another temporary variable its up to you.
for($i = 0; $i < $tabs; $i++) {
echo "Group {$alphabet[$i]}".'<br>';
}
Cheers!
Maybe this could helps you
<?php
//$tabs = ($qty / $limit);
//$group = $tabs / $limit;
$alphabet = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
$return = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
$a = array_chunk($return, 3);
//$a = array_chunk($return, $limit);
$key = 0;
$group = array();
$r = count($a);
foreach($alphabet as $v)
{
if($key < $r)
{
if(!isset($group[$v]))
{
$group[$v] = array();
}
$group[$v] = $a[$key];
$key++;
}
}
print_r($group);
Here
https://www.tehplayground.com/bfmZOubIMdmBcjhu
I've written a script in PHP to get the tabular data from a webpage. When I execute my script I can get them in a single column. However, I wish to parse them as a list, as in how they look like in that webpage.
Website link
To be clearer:
My current output are like:
978
EMU
EUR
1
118.2078
36
Australija
AUD
1
73.1439
My expected output are like:
['978', 'EMU', 'EUR', '1', '118.2078']
['36', 'Australija', 'AUD', '1', '73.1439']
['124', 'Kanada', 'CAD', '1', '77.7325']
['156', 'Kina', 'CNY', '1', '14.6565']
['191', 'Hrvatska', 'HRK', '1', '15.9097']
This is my try so far:
<?php
$url = "http://www.nbs.rs/kursnaListaModul/srednjiKurs.faces?lang=lat";
$dom = new DomDocument;
$dom->loadHtmlFile($url);
$xpath = new DomXPath($dom);
$rowData = array();
foreach ($xpath->query('//tbody[#id="index:srednjiKursList:tbody_element"]//tr') as $node) {
foreach ($xpath->query('td', $node) as $cell) {
$rowData[] = $cell->nodeValue;
}
}
foreach($rowData as $rows){
echo $rows . "<br/>";
}
?>
You are adding each element one at a time to the output array, you probably wanted to build up a row at a time and output that...
$rowData = array();
foreach ($xpath->query('//tbody[#id="index:srednjiKursList:tbody_element"]//tr') as $node) {
$row = array();
foreach ($xpath->query('td', $node) as $cell) {
$row[] = $cell->nodeValue;
}
$rowData[] = $row;
}
foreach($rowData as $rows){
print_r($rows); // Format the data as needed
}
Try this.
$htmlContent = file_get_contents("http://www.nbs.rs/kursnaListaModul/srednjiKurs.faces?lang=lat");
$DOM = new DOMDocument();
$DOM->loadHTML($htmlContent);
$Header = $DOM->getElementsByTagName('th');
$Detail = $DOM->getElementsByTagName('td');
//#Get header name of the table
foreach($Header as $NodeHeader)
{
$aDataTableHeaderHTML[] = trim($NodeHeader->textContent);
}
//#Get row data/detail table without header name as key
$i = 0;
$j = 0;
foreach($Detail as $sNodeDetail)
{
$aDataTableDetailHTML[$j][] = trim($sNodeDetail->textContent);
$i = $i + 1;
$j = $i % count($aDataTableHeaderHTML) == 0 ? $j + 1 : $j;
}
//print_r($aDataTableDetailHTML)
//#Get row data/detail table with header name as key and outer array index as row number
for($i = 0; $i < count($aDataTableDetailHTML); $i++)
{
for($j = 0; $j < count($aDataTableHeaderHTML); $j++)
{
#$aTempData[$i][$aDataTableHeaderHTML[$j]] = $aDataTableDetailHTML[$i][$j];
}
}
$aDataTableDetailHTML = $aTempData; unset($aTempData);
print_r($aDataTableDetailHTML);
I want to add a child with a specified value which I get from my array. But my problem is that my array is bigger then my amount of XML products...
This is why I get this error message:
PHP Notice: Undefined offset: 1589 in C:\Users\Jan\PhpstormProjects\censored\test.php on line 63
PHP Fatal error: Uncaught Error: Call to a member function appendChild() on null in C:\Users\Jan\PhpstormProjects\censored\test.php:64
To check that I made two loops which say me that I have 1588 names and 1588 products, both loops say that. Thats logical!
$markerProduct = $root->getElementsByTagName("product");
for($i = $markerProduct->length - 1; $i >= 0; $i--){
$productCounter = $productCounter + 1;
}
$markerTitle = $root->getElementsByTagName("name");
for($i = 0; $i < $markerTitle->length; $i++){
$nameCounter = $nameCounter + 1;
}
But my array in which I store the specified value for each product is 1589 (0 - 1588) values big... And I don't know.
Can anyone help me and tell me the error?
$searches = ["Steam", "Uplay", "Xbox", "Nintendo", "PS3", "PS4", "PSN"];
function isContaining($searches, $titleTag, $urlTag, $productTag, $path){
$dom = new DOMDocument('1.0', 'utf-8');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->load($path);
$root = $dom->documentElement;
$markerTitle = $root->getElementsByTagName($titleTag);
$markerURL = $root->getElementsByTagName($urlTag);
$plat = array();
for($i = $markerTitle->length - 1; $i >= 0 ; $i--){
$title = $markerTitle->item($i)->textContent;
$url = $markerURL->item($i)->textContent;
$co = count($searches);
$productFound = false;
for($j = 0; $j < $co; $j++){
if(stripos($title, $searches[$j]) !== false){
if($j > 3){
array_push($plat, "PlayStation");
}else{
array_push($plat, $searches[$j]);
}
$productFound = true;
}elseif(stripos($url, $searches[$j]) !== false){
if($j > 3){
array_push($plat, "PlayStation");
}else{
array_push($plat, $searches[$j]);
}
$productFound = true;
}
}
if($productFound == false){
array_push($plat, "Nothing");
}
}
print_r($plat);
$c = count($plat);
echo $c;
for($i = $c - 1; $i >= 0; $i--){
$node = $dom->createElement('plat', $plat[$c]);
$dom->getElementsByTagName($productTag)->item($i)->appendChild($node);
}
$dom->saveXML();
$dom->save('data/gamesplanet2.xml');
}
Error is in line 63:
$node = $dom->createElement('plat', $plat[$c]);
Greetings and Thank You!
I extract values from an XML with PHP
<?php
$url = 'list.xml';
$xml = simplexml_load_file($url);
$entries = $xml->item;
$i = 0;
$total = 1;
foreach($entries as $entry){
$i++;
$number[$i] = $entry->total;
$total *= $number[$i];
}
echo $total;
?>
How can I build a total based on each $number extracted from the XML? Right now, my total is zero.
So for all loops together something like:
$total = $number[1] * $number[2] * $number[3] * $number[4] ....
This should work for you:
(You have to cast the return of simplexml_load_file() to double)
$url = "list.xml";
$xml = simplexml_load_file($url);
$entries = $xml->results->rate;
$count = 0;
$total = 1;
$number = array();
foreach($entries as $entry){
$count++;
$number[$count] = $entry->Bid;
$total *= (double)$number[$count];
}
echo "Total: " . $total;