I have an array of date, something like below
Array
(
[0] => stdClass Object
(
[question_id] => 64
[title] => Question1
[question_type_id] => 1
[settings] =>
[poll_id] => 5
[answer] => Array
(
[0] => stdClass Object
(
[answer_id] => 1
[poll_id] => 5
[question_id] => 64
[answer] => Answer1-1
[created_at] => 2018-07-08 17:36:15
)
[1] => stdClass Object
(
[answer_id] => 5
[poll_id] => 5
[question_id] => 64
[answer] => Answer1-2
[created_at] => 2018-07-08 19:27:33
)
)
)
[1] => stdClass Object
(
[question_id] => 65
[title] => Question2
[question_type_id] => 6
[settings] => ["više od 2km","manje od 2km"]
[poll_id] => 5
[answer] => Array
(
[0] => stdClass Object
(
[answer_id] => 2
[poll_id] => 5
[question_id] => 65
[answer] => Answer2-1
[created_at] => 2018-07-08 17:36:47
)
[1] => stdClass Object
(
[answer_id] => 6
[poll_id] => 5
[question_id] => 65
[answer] => Answer2-2
[created_at] => 2018-07-09 23:31:31
)
)
)
I need a table in browser to look like
enter image description here
Question1 Question2 Question3
Answer1-1 Answer2-1 Answer3-1
Answer1-2 Answer2-2 Answer3-2
This is my code:
<table class="table">
<thead>
<tr>
<?php foreach ($DATA['question'] as $question): ?>
<th><?php echo htmlspecialchars($question->title); ?></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<tr>
<?php foreach ($DATA['question'] as $question): ?>
<td><?php for ($i=0,$j=count((array)$question->answer);$i<$j;$i++) {
echo htmlspecialchars($question->answer[$i]->answer);
}?></td>
<?php endforeach; ?>
</tr>
</tbody>
My problem is that I can not place Answer1-2 in the next row of table.
Any idea?
Thanks a lot
There are a few options how you can do this.
Generate each of your intended columns as a single element and place that in a cell.
Populate an array in the way you wish to display them
Create a for(i=0;i<maxAmountOfAnswers;i++) wrapping another foreach (questions) and populate the table row by row.
Use DataTables or other libraries that can read a json.
Think about why you want this though; semantically tables are intended to display one kind of data. Is there a good reason why you wouldn't just show 1 table per question, or tables at all?
Thanks a lot for your answer! It is my solutionn.
<table class="table">
<thead>
<tr>
<?php foreach ($DATA['question'] as $question): ?>
<th><?php echo htmlspecialchars($question->title); ?></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<tr>
<?php foreach ($DATA['question'] as $question): ?>
<td><?php for ($i=0,$j=count((array)$question->answer);$i<$j;$i++) {
echo htmlspecialchars($question->answer[$i]->answer);
echo "<br />";
}?></td>
<?php endforeach; ?>
</tr>
</tbody>
</table>
And what is looks like.
enter image description here
Related
I have history list of product price(order by created_at) like this:
Array
(
[0] => stdClass Object
(
[id] => 1
[product_id] => 49
[price] => 14520000.0000
[created_at] => 1592501154
)
[1] => stdClass Object
(
[id] => 2
[product_id] => 49
[price] => 14000000.0000
[created_at] => 1592587554
)
[2] => stdClass Object
(
[id] => 4
[product_id] => 49
[price] => 14800000.0000
[created_at] => 1592673954
)
[3] => stdClass Object
(
[id] => 5
[product_id] => 49
[price] => 10000000.0000
[created_at] => 1592760354
)
[4] => stdClass Object
(
[id] => 6
[product_id] => 49
[price] => 14000000.0000
[created_at] => 1592846754
)
[5] => stdClass Object
(
[id] => 7
[product_id] => 49
[price] => 14000000.0000
[created_at] => 1592933154
)
[6] => stdClass Object
(
[id] => 8
[product_id] => 49
[price] => 14000000.0000
[created_at] => 1593019554
)
)
Now for show data in table I listed price using foreach method like this:
<?php foreach($product_prices_list as $product_price_list):?>
<tr>
<td><?= esc($product_price_list->created_at);?></td>
<td class="text-center"><?= esc(number_format($product_price_list->price));?></td>
<td class="text-center"></td> //show difference between of two price
</tr>
<?php endforeach;?>
I can see true output in table but I need to show difference between of two price in the third column like this picture:
how do can i show difference between of two price in my list?!
You just need to compare the current price property to the price from the previous object in the array?
Something like this should work:
<?php foreach($product_prices_list as $key => $product_price_list):?>
<tr>
<td><?= esc($product_price_list->created_at);?></td>
<td class="text-center"><?= esc(number_format($product_price_list->price));?></td>
<td class="text-center"><?= (!empty($product_prices_list[$key - 1])) ? $product_prices_list[$key + 1]->price - $product_price_list->price: 0; ?></td> //show difference between of two price
</tr>
<?php endforeach;?>
If you are running MySQL 8.0, you can compute this information directly in the database using window functions:
select
t.*,
price
- lag(price, 1, price) over(partition by product_id order by created_at)
as price_diff
from mytable t
This adds one more column to your resultset, that contains the difference between the current price and the previous price of the same product.
I have an array called $data and I would like to display a two column html table,
tableHeader on the left hand column.
tableData on the right hand column.
A print_r($data) displays the following
Array
(
[0] => Array
(
[tableHeader] => ID
[tableData] => 104
)
[1] => Array
(
[tableHeader] => Member Number
[tableData] => not available
)
[2] => Array
(
[tableHeader] => First Name
[tableData] => Peter
)
[3] => Array
(
[tableHeader] => Last Name
[tableData] => Keys
)
[4] => Array
(
[tableHeader] => Address
[tableData] => 17 main road
)
[5] => Array
(
[tableHeader] => Email
[tableData] => P3TER#HOTMAIL.CO.UK
)
[6] => Array
(
[tableHeader] => Post Code
[tableData] => LDN 1
)
[7] => Array
(
[tableHeader] => City
[tableData] => London
)
[8] => Array
(
[tableHeader] => Year Graduated
[tableData] => 0000-00-00
)
[9] => Array
(
[tableHeader] => Subject Studied
[tableData] => Comp
)
[10] => Array
(
[tableHeader] => Telephone Number
[tableData] => 123123
)
)
I have tried the following foreach loop but I keep receiving an error message;
Message: Undefined variable: value
<table class="table">
<thead>
<? foreach ($data as $value): ?>
<tr>
<th scope="col"><?php echo $value['tableHeader']; ?></th>
</tr>
<? endforeach; ?>
</thead>
<tbody>
<? foreach ($data as $value): ?>
<tr>
<th scope="col"><?php echo $value['tableData']; ?></th>
</tr>
<? endforeach; ?>
</tbody>
</table>
What am I doing wrong?
It looks like the short tags are not working, because when the php code inside of the foreach loop is executed, it does not recognise the $value variable, because it was never parsed by php.
I want print: Id, Nombre_del_paciente__c, Fecha_de_la_cita__c and Hora_de_la_cita__c
This is print_r($response); result:
Object (
[queryLocator] => [done] => 1 [records] => Array (
[0] => SObject Object (
[type] =>
Cita__c [fields] => stdClass Object
(
[Nombre_del_paciente__c] => 0030O000021cPBuQAM
[Fecha_de_la_cita__c] => 2017-11-28
[Hora_de_la_cita__c] => 15:30
)
[Id] => a000O00000tmZH6QAM
)
) [size] => 1
)
I do this and I can acces to ID value... but i cant acceso to other values:
<table>
<tr>
<th>ID </th>
<th>Nombre_del_paciente__c</th>
<th>Fecha_de_la_cita__c</th>
<th>Hora_de_la_cita__c</th>
</tr>
<?php
foreach ($response->records as $record) {
echo '<tr>
<td>'.$record->Id.'</td>
<td></td>
<td></td>
<td></td>
</tr>';
}
?>
</table>
Im try to do:
$record->type->Nombre_del_paciente__c
$record->Cita__c ->Nombre_del_paciente__c
$record->Cita__c['fields'] ->Nombre_del_paciente__c
but i cant acces to values
Your key 'type' is not a object array. you can try this-
$record->type['fields']->Nombre_del_paciente__c
$record->type['fields']->Fecha_de_la_cita__c
$record->type['fields']->Hora_de_la_cita__c
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.
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