As I can make an ordered descending by the query field bytes each IP. This is my array contains other fields as refered or other browsers ....
[_id] => MongoId Object (
[$id] => 528e6004b0a4191f698b4567
)
[FECHA] =>.........
[IP] => Array (
[0] => Array (
[Ip] => 172.17.10.
[conexiones] => 71
[bytes] => 122.75 KB
[media] => 1770.338
)
[1] => Array (
[Ip] => 192.168.6.145
[conexiones] => 79
[bytes] => 692.51 KB
[media] => 8976.3164
)
[2] => Array (
[Ip] => 172.17.108.3
[conexiones] => 2
[bytes] => 4.48 KB
[media] => 2294.5
)
[3] => Array (
[Ip] => 192.168.200.50
[conexiones] => 123
[bytes] => 6.10 MB
[media] => 52004.699186992
...............
This is my code:
$Filtro="IP";
$c = $collection->find(array('FECHA' => $Fecha), array($Filtro));
$cont = 1;
while ($c->hasNext()) {
$metodo = $c->getNext();
foreach ($metodo[$Filtro] as $f) {
?>
<tr>
<td><?= $cont++ ?></td>
<td><?= $f[$Filtro] ?></td>
<td><?= $f['conexiones'] ?></td>
<td><?= $f['bytes'] ?></td>
<td><?= $f['media'] ?></td></tr>
<?
}
}
Just shows me all data but unsorted. thanks
Related
I am very new to PHP (coming from JS) so am having trouble debugging this issue.
I have imported a .csv to render to a html table and am trying to retrieve a column so I can order the table. The table contains 5 headings (Date, TransactionNumber, CustomerNumber, Reference, Amount). I can retrieve all of the values except the Date column. That just returns an empty value.
<?php
$rows = array_map('str_getcsv', file('BankTransactions.csv'));
$header = array_shift($rows);
$csv = array();
foreach ($rows as $row) {
$csv[] = array_combine($header, $row);
}
print_r($csv[0]['Date']);
print_r("\n")
?>
// .csv
Date,TransactionNumber,CustomerNumber,Reference,Amount
2016-12-10 1:54PM,NUF5V6PT3U,5156,Purchase at JB HiFi,-2498
2016-12-4 4:54AM,J82964EFPS,2347,Refund,5424
2016-12-12 5:25PM,ZUFBQGCKTK,5683,Fee Refund,254
2016-12-01 12:00AM,XHNCASYLBR,2347,Purchase at Coles,-8873
2016-11-23 10:34PM,S98EBHDWG3,3423,Wages,198700
2017-09-23 9:34AM,MPNQYKVJ3G,4657,Purchase at Chemist Warehouse,-584
2015-11-23 10:34PM,74CQKEGSHB,2173,Refund,-3514
2015-11-23 10:35PM,WPTJMNVH4U,4527,Purchase at Hungry Monkey,45245
2017-01-01 12:00AM,U6BD3M75FD,7577,Interest,2778
// Array received from `.csv`
Array
(
[0] => Array
(
[Date] => 2016-12-10 1:54PM
[TransactionNumber] => NUF5V6PT3U
[CustomerNumber] => 5156
[Reference] => Purchase at JB HiFi
[Amount] => -2498
)
[1] => Array
(
[Date] => 2016-12-4 4:54AM
[TransactionNumber] => J82964EFPS
[CustomerNumber] => 2347
[Reference] => Refund
[Amount] => 5424
)
[2] => Array
(
[Date] => 2016-12-12 5:25PM
[TransactionNumber] => ZUFBQGCKTK
[CustomerNumber] => 5683
[Reference] => Fee Refund
[Amount] => 254
)
[3] => Array
(
[Date] => 2016-12-01 12:00AM
[TransactionNumber] => XHNCASYLBR
[CustomerNumber] => 2347
[Reference] => Purchase at Coles
[Amount] => -8873
)
[4] => Array
(
[Date] => 2016-11-23 10:34PM
[TransactionNumber] => S98EBHDWG3
[CustomerNumber] => 3423
[Reference] => Wages
[Amount] => 198700
)
[5] => Array
(
[Date] => 2017-09-23 9:34AM
[TransactionNumber] => MPNQYKVJ3G
[CustomerNumber] => 4657
[Reference] => Purchase at Chemist Warehouse
[Amount] => -584
)
[6] => Array
(
[Date] => 2015-11-23 10:34PM
[TransactionNumber] => 74CQKEGSHB
[CustomerNumber] => 2173
[Reference] => Refund
[Amount] => -3514
)
[7] => Array
(
[Date] => 2015-11-23 10:35PM
[TransactionNumber] => WPTJMNVH4U
[CustomerNumber] => 4527
[Reference] => Purchase at Hungry Monkey
[Amount] => 45245
)
[8] => Array
(
[Date] => 2017-01-01 12:00AM
[TransactionNumber] => U6BD3M75FD
[CustomerNumber] => 7577
[Reference] => Interest
[Amount] => 2778
)
)
Your CSV file is probably encoded in UTF-8 with a BOM, Byte Order Mark.
PHP does (still) not handle BOMs properly when reading files, so the very first line you read from the file will still contain it at the start, and therefor the first column value will also still contain it. So your key inside the $csv array is not actually Date, but [BOM]Date, but you don’t see that in the print_r output (a BOM isn’t actually “visible” per se.)
See How to remove multiple UTF-8 BOM sequences or https://gist.github.com/chrisguitarguy/6096271 for suggestions how the BOM can be removed.
You need to fetch the transaction for each field. Here: https://www.php.net/manual/en/function.fgetcsv.php
I am using the exact same CSV file and made a solution for an interview.
The CSV file's can be tested on the below link:
https://paduademo.azurewebsites.net/
<?php foreach ($transactions as $transaction) : ?>
<tr>
<td><?php echo $transaction['Date']; ?></td>
<td><?php echo $transaction['TransactionNumber']; ?></td>
<td><?php echo ($transaction['Valid'])? 'Yes': 'No'; ?></td>
<td><?php echo $transaction['CustomerNumber']; ?></td>
<td><?php echo $transaction['Reference']; ?></td>
<td><span class="<?php echo ($transaction['type'] == 'Credit')? 'text-danger': 'text-success'; ?>"><?php echo (($transaction['type'] == 'Credit')? '-': '') . '$' . $transaction['Amount']; ?></span></td>
</tr>
<?php endforeach; ?>
The screenshot is below:
I request a JSON query and get the output, I want to parse that output and show it in tabular form also want to insert it into database.
I performed following php code to get array representation of JSON data.
echo '<pre>';
print_r( json_decode( $result ) );
echo '</pre>';
and I get following output:
stdClass Object
(
[request] => stdClass Object
(
[Target] => Affiliate_Report
[Format] => json
[Service] => HasOffers
[Version] => 3
[Method] => getConversions
[api_key] =>
[NetworkId] =>
[limit] => 2
[fields] => Array
(
[0] => Offer.name
[1] => Browser.display_name
[2] => Stat.payout
[3] => Stat.sale_amount
[4] => Stat.status
[5] => Stat.datetime
[6] => Stat.ip
[7] => Stat.ad_id
[8] => Stat.affiliate_info1
)
)
[response] => stdClass Object
(
[status] => 1
[httpStatus] => 200
[data] => stdClass Object
(
[page] => 1
[current] => 2
[count] => 81
[pageCount] => 41
[data] => Array
(
[0] => stdClass Object
(
[Offer] => stdClass Object
(
[name] => Myntra (CPS)
)
[Browser] => stdClass Object
(
[display_name] => Firefox
)
[Stat] => stdClass Object
(
[payout] => 150.00000
[sale_amount] => 0.00000
[status] => approved
[datetime] => 2014-05-20 22:20:05
[ip] => 27.0.50.82
[ad_id] => 102fa12e74df6018e502d8e152adb2
[affiliate_info1] =>
)
)
[1] => stdClass Object
(
[Offer] => stdClass Object
(
[name] => Myntra (CPS)
)
[Browser] => stdClass Object
(
[display_name] => Firefox
)
[Stat] => stdClass Object
(
[payout] => 150.00000
[sale_amount] => 53.00000
[status] => rejected
[datetime] => 2014-03-30 13:14:50
[ip] => 27.0.51.145
[ad_id] => 102be1d682ac9b2e9ee8e14dd1aeca
[affiliate_info1] =>
)
)
)
[dbSource] => branddb
)
[errors] => Array
(
)
[errorMessage] =>
)
)
I want to display above data into tabular form.
Code used by me:
$result = file_get_contents($base);
$obj = json_decode($result, true);
<?php foreach ($obj['response'] as $licenseElement) :?>
<tr>
<td><?php echo $licenseElement->data->Offer->name; ?></td>
<td><?php echo $licenseElement->Stat->payout; ?></td>
<td><?php echo $licenseElement->Stat->sale_amount; ?></td>
<td><?php echo $licenseElement->Stat->datetime; ?></td>
</tr>
<?php endforeach; ?>
This code returns me an error Trying to get property of non-object everywhere at echo syntax.
Please help me to parse the above json output and display it in proper tabular format.
you are iterating over wrong object, you need to loop over $obj->response->data->data object to get what you are seeking
<?php foreach ($obj->response->data->data as $licenseElement) :?>
<tr>
<td><?php echo $licenseElement->Offer->name; ?></td>
<td><?php echo $licenseElement->Stat->payout; ?></td>
<td><?php echo $licenseElement->Stat->sale_amount; ?></td>
<td><?php echo $licenseElement->Stat->datetime; ?></td>
</tr>
<?php endforeach; ?>
You are not getting to the right level in the object hierarchy. Try with
foreach($obj->response->data->data as $licenseElement) { ... }
$result = file_get_contents($base);
$obj = json_decode($result, true);
<?php foreach ($obj['response']->data->data as $licenseElement) :?>
<tr>
<td><?php echo $licenseElement->data->Offer->name; ?></td>
<td><?php echo $licenseElement->Stat->payout; ?></td>
<td><?php echo $licenseElement->Stat->sale_amount; ?></td>
<td><?php echo $licenseElement->Stat->datetime; ?></td>
</tr>
<?php endforeach; ?>
This code should work.
What you are trying to do is that you have considered the response as an array but the print_r functions lets us know wether your data is an array or an object.
It says StdClass Object thats why we need the "->" sign to refer to it.
All i have changed is the foreach($obj['response']->data->data as $licenseElement)
We also need to check the levels , here we are 3 levels in. :)
Hope this helps.
Im trying to display some data im receiving from the cloudstack API. I can display some of the data I want when I use the JSON as objects, with the following code;
stdClass Object
(
[listvirtualmachinesresponse] => stdClass Object
(
[count] => 2
[virtualmachine] => Array
(
[0] => stdClass Object
(
[id] => bab03c9b-94e0-429f-906c-8853de306e9a
[name] => LinuxL1
[displayname] => LinuxL1
[account] => admin
[domainid] => a3121682-837b-11e3-964c-001372f94016
[domain] => ROOT
[created] => 2014-05-08T13:43:01+0200
[state] => Running
[haenable] =>
[zoneid] => 7a3ee2f0-540a-4232-8264-50a47a91001d
[zonename] => Amsterdam
[hostid] => 024b9ca2-4076-488e-ad13-86dfb7f2b766
[hostname] => ptvirt6
[templateid] => cf6abb42-574f-4840-886a-8662c5914166
[templatename] => CentOS 6.5
[templatedisplaytext] => good
[passwordenabled] =>
[isoid] => cf6abb42-574f-4840-886a-8662c5914166
[isoname] => CentOS 6.5
[isodisplaytext] => good
[serviceofferingid] => 4fc30c59-4600-44ed-b0bb-7fc48e795d90
[serviceofferingname] => L1
[cpunumber] => 1
[cpuspeed] => 1000
[memory] => 1024
[cpuused] => 0.02%
[networkkbsread] => 55
[networkkbswrite] => 1
[diskkbsread] => 1000680
[diskkbswrite] => 1301501
[diskioread] => 77192
[diskiowrite] => 8884
[guestosid] => a92508f4-837b-11e3-964c-001372f94016
[rootdeviceid] => 0
[rootdevicetype] => ROOT
[securitygroup] => Array
(
[0] => stdClass Object
(
[id] => edfe848c-837b-11e3-964c-001372f94016
[name] => default
[description] => Default Security Group
[account] => admin
[ingressrule] => Array
(
)
[egressrule] => Array
(
)
[tags] => Array
(
)
)
)
[nic] => Array
(
[0] => stdClass Object
(
[id] => bfaeb76d-7b5a-46b1-aaa7-7dd15e86bf2e
[networkid] => 83007a42-e4b7-4ea2-aa21- 20938ea3bc56
[networkname] => defaultGuestNetwork
[netmask] => 111.111.111.111
[gateway] => 111.111.111.111
[ipaddress] => 111.111.111.111
[isolationuri] => ec2://untagged
[broadcasturi] => vlan://untagged
[traffictype] => Guest
[type] => Shared
[isdefault] => 1
[macaddress] => 06:a5:24:00:00:0a
)
)
[hypervisor] => KVM
[instancename] => i-2-9-VM
[tags] => Array
(
)
[affinitygroup] => Array
(
)
[displayvm] => 1
[isdynamicallyscalable] =>
)
I can read the name / state from the list with a foreach loop
<?php
$api_content = file_get_contents("http://127.0.0.1:5950/client/api?command=listVirtualMachines&domainid=1&account=admin&response=json");
$api_data = json_decode($api_content);
$ap_machine = $api_data->listvirtualmachinesresponse->virtualmachine;
echo "<pre>";
print_r($api_data);
echo "</pre>";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<table border="1" width="300">
<tr>
<td>Naam</td>
<td>Status</td>
<td>Ip Address</td>
<td>Health</td>
</tr>
<?php
echo "<tr>";
foreach($ap_machine as $item){
echo "<td>".$item->name."</td>";
echo "<td>".$item->state."</td>";
echo "<td>".$item->nic->ipaddress."</td>";
if($item->state == 'Running'){
echo '<td><img src="/600px-Approve_icon.svg.png" height="50" width="50"> </td>';
}
else{
echo '<td><img src="/fail.png" height="25" width="25"></td>';
}
echo "</tr>";
}
?>
I've read online to make an associative array from the json list, I did that aswell and didnt get a result.
In your PHP code you are accessing nice key as a normal element of json:
echo "<td>".$item->nic->ipaddress."</td>";
But, from your var_dump output I can see its of array type.
So, to access elements of nice array, you'l have to access it by specifying array index.
echo "<td>".$item->nic[0]->ipaddress."</td>";
Look dude
change
$api_data = json_decode($api_content);
to
$api_data = json_decode($api_content, TRUE);
this makes it an associative array
check http://www.php.net/manual/en/function.json-decode.php
I'am making a stock control system for sugar, I need to print and compare values, just have a problem when I want to printf a valor of a array multidimencional example, this is my array:
Array
(
[0] => Array
(
[Azucar] => Array
(
[id] => 1
[nombre] => 21
[produccion] => verano.
[cantidad] => 10032.32
[fecha] =>
)
[grupo] => Array
(
[id] => 2
[categoria] => azucar
[subcategoria] => ingenio
[created] => 2008-02-13 18:34:56
)
[AzucarExistencia] => Array
(
[0] => Array
(
[id] => 1
[cantidad] => 3250
[cantidadtotal] => 325.12
[sacarosa] => 32
[refinada] => 956
)
[1] => Array
(
[id] => 2
[cantidad] => 4000
[cantidadtotal] => 564
[sacarosa] => 12
[refinada] => 780
)
[2] => Array
(
[id] => 3
[cantidad] => 4564
[cantidadtotal] => 654.32
[sacarosa] => 5451
[refinada] => 45
)
[3] => Array
(
[id] => 4
[cantidad] => 3244
[cantidadtotal] => 21.1
[sacarosa] => 123
[refinada] => 123.45
)
)
)
[1] => Array
(
[Azucar] => Array
(
[id] => 2
[nombre] => 32
[produccion] => verano.
[cantidad] => 9032.32
[fecha] =>
)
[grupo] => Array
(
[id] => 3
[categoria] => azucar
[subcategoria] => ingenio
[created] => 2008-02-13 18:34:56
)
[AzucarExistencia] => Array
(
[0] => Array
(
[id] => 6
[cantidad] => 3250
[cantidadtotal] => 325.12
[sacarosa] => 32
[refinada] => 956
)
[1] => Array
(
[id] => 7
[cantidad] => 4000
[cantidadtotal] => 564
[sacarosa] => 12
[refinada] => 780
)
[2] => Array
(
[id] => 8
[cantidad] => 4564
[cantidadtotal] => 654.32
[sacarosa] => 5451
[refinada] => 45
)
[3] => Array
(
[id] => 9
[cantidad] => 3244
[cantidadtotal] => 21.1
[sacarosa] => 123
[refinada] => 123.45
)
[4] => Array
(
[id] => 10
[cantidad] => 4564
[cantidadtotal] => 654.32
[sacarosa] => 5451
[refinada] => 45
)
[5] => Array
(
[id] => 11
[cantidad] => 3244
[cantidadtotal] => 21.1
[sacarosa] => 123
[refinada] => 123.45
)
)
)
)
I need to compare all the values within AzucarExistencia, the first foreach is so:
<?php foreach ($azucares as $azucar): ?>
<tr>
<td style="text-align: center;"><?php echo $azucar['Azucar']['username']; ?></td>
<td style="text-align: center;"><?php echo $azucar['Azucar']['nombre']; ?></td>
<td style="text-align: center;"><?php echo $azucar['Azucar']['apellido']; ?></td>
<td style="text-align: center;"><?php echo $azucar['Azucar']['email']; ?></td>
<td style="text-align: center;"><?php echo $azucar['Grupo']['categoria']; ?></td>
<td style="text-align: center;"><?php echo $azucar['Grupo']['subcategoria']; ?></td>
</tr>
<?php endforeach; ?>
<?php unset($azucar); ?>
but then needed to compare values within AzucarExistencia, try that:
<?php foreach ($azucares as $azucar): ?>
<tr>
<td style="text-align: center;"><?php echo $azucar['Azucar']['username']; ?></td>
<td style="text-align: center;"><?php echo $azucar['Azucar']['nombre']; ?></td>
<td style="text-align: center;"><?php echo $azucar['Azucar']['apellido']; ?></td>
<td style="text-align: center;"><?php echo $azucar['Azucar']['email']; ?></td>
<td style="text-align: center;"><?php echo $azucar['Grupo']['categoria']; ?></td>
<td style="text-align: center;"><?php echo $azucar['Grupo']['subcategoria']; ?></td>
<?php foreach ($azucar as $azucarexistencia): ?>
<td style="text-align: center;"><?php
if ($azucarexistencia['AzucarExistencia']['cantidad']<1500) {
echo 'peligro';
}
?></td>
</tr>
<?php endforeach; ?>
<?php unset($azucar); ?>
now the error is
Undefined index: azucarexistencia
or
Undefined index: AzucarExistencia
checked with debugkit the values ,come okay it's just make a good foreach, I try but i dont now how.
model
<?php
/**
*
*/
class Azucar extends AppModel
{
public $name ='User';
public $useTable = 'users';
public $primaryKey = 'id';
public $belongsTo = 'Grupo';
public $hasOne = 'Perfil';
public $hasMany = array('Deposito','AzucarExistencia');
}
?>
controller
<?php
/**
*
*/
class AzucaresController extends AppController
{
public $components = array('Session','RequestHandler');
public function index()
{
$this->loadModel('Azucar');
$this->paginate = array(
'conditions' => array('Grupo.categoria' => 'Azucar'),
'limit' => 25
);
$this->set('azucares', $this->paginate('Azucar'));
}
public function logout() {
$this->redirect($this->Auth->logout());
}
}
?>
Replace the loop as so
<?php foreach ($azucar['AzucarExistencia'] as $azucarexistencia): ?>
<td style="text-align: center;"><?php
if ($azucarexistencia['cantidad']<1500) {
echo 'peligro';
}
?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
What you were doing wrong was you were trying to loop through all indexes of $azucar so it would loop through $azucar["Azucar"] $azucar["grupo"] $azucar["AzucarExistencia"]
but only $azucar["AzucarExistencia"] needs to be looped through
<?php
foreach ($azucares as $azucar){
foreach ($azucar['AzucarExistencia'] as $azucarexistencia){
if ($azucarexistencia['cantidad']<1500) {
echo 'peligro';
}
}
}
?>
I would like to show some text under specific category. For that I would like to get category id. I printed category array in top.phtml(custom design) as shown below;
<?php foreach ($this->getStoreCategories() as $_category): ?>
print_r($_category);
The above print_r gives result as below.How can I parse the below object to get entity_id(here its value is 1013) or name(here it is 'winning looks').
Mage_Catalog_Model_Category Object
(
[_eventPrefix:protected] => catalog_category
[_eventObject:protected] => category
[_cacheTag:protected] => catalog_category
[_useFlatResource:protected] => 1
[_designAttributes:Mage_Catalog_Model_Category:private] => Array
(
[0] => custom_design
[1] => custom_design_from
[2] => custom_design_to
[3] => page_layout
[4] => custom_layout_update
[5] => custom_apply_to_products
)
[_treeModel:protected] =>
[_defaultValues:protected] => Array
(
)
[_storeValuesFlags:protected] => Array
(
)
[_lockedAttributes:protected] => Array
(
)
[_isDeleteable:protected] => 1
[_isReadonly:protected] =>
[_resourceName:protected] => catalog/category_flat
[_resource:protected] =>
[_resourceCollectionName:protected] => catalog/category_flat_collection
[_dataSaveAllowed:protected] => 1
[_isObjectNew:protected] =>
[_data:protected] => Array
(
[entity_id] => 1013
[name] => Winning Looks
[path] => 1/2/1013
[is_active] => 1
[is_anchor] => 1
[request_path] => winning-looks.html
[id] => 1013
)
[_hasDataChanges:protected] => 1
[_origData:protected] =>
[_idFieldName:protected] => entity_id
[_isDeleted:protected] =>
[_oldFieldsMap:protected] => Array
(
)
[_syncFieldsMap:protected] => Array
(
)
)
you can get with <?php echo $category->getEntityId()?>
<?php foreach ($this->getStoreCategories() as $_category):
print_r($_category);
echo $_category->getEntityId()
endforeach;
?>
hope this will work for you.
Try this:
if ($_category->getId() == 1013){ //or $_category->getEntityId();
//your magic code here
}