how i can access to this values? php - 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

Related

PHP Multidimensional Array to Two Column HTML Table

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.

Create spec HTML Table using PHP stdClass Object

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

How to display array data from MySql in laravel

So I a Manifest where you can add multiple customers, produce etc and it saves in the DB as an array
Now I'm making a page where you can view all the manifest info I want it to look something like this
if I try to do something like this {{ $manifest->customer_name }} i get a error htmlspecialchars() expects parameter 1 to be string, array given because it's an array I'm only having trouble with displaying the array data
Controller Code to get the correct manifest
public function view($id) {
$manifests = Manifest::where('id', $id)->where('user_id', Auth::user()->id)->firstOrFail();
return view('users.manifest.view', compact('manifests'));
}
view - usually I would just put {{ $manifets->customer_name }} but i can't because it an array
<table class="table table-striped">
<thead>
<tr>
<th width="15%">Customer</th>
<th width="15%">Produce</th>
<th width="15%">Task</th>
<th width="15%">Units</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="text" class="form-control" value="{{ $manifests->customer_name }}" />
</td>
</tr>
</tbody>
</table>
print_r
App\Manifest Object
(
[table:protected] => manifest
[fillable:protected] => Array
(
[0] => user_id
[1] => date
[2] => driver_name
[3] => truck_number
[4] => run_number
[5] => customer_name
[6] => produce
[7] => task
[8] => units
)
[casts:protected] => Array
(
[customer_name] => array
[produce] => array
[task] => array
[units] => array
)
[connection:protected] => mysql
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[id] => 8
[user_id] => 1
[date] => 2017-11-02
[driver_name] => Harry Oberlander
[truck_number] => 7989
[run_number] => 8
[customer_name] => ["evergreen","Surplus"]
[produce] => ["Apples","Meat"]
[task] => ["Pick Up","Delivery"]
[units] => ["3 skids","1"]
[created_at] => 2017-11-02 04:49:49
[updated_at] => 2017-11-02 04:49:49
)
[original:protected] => Array
(
[id] => 8
[user_id] => 1
[date] => 2017-11-02
[driver_name] => Harry Oberlander
[truck_number] => 7989
[run_number] => 8
[customer_name] => ["evergreen","Surplus"]
[produce] => ["Apples","Meat"]
[task] => ["Pick Up","Delivery"]
[units] => ["3 skids","1"]
[created_at] => 2017-11-02 04:49:49
[updated_at] => 2017-11-02 04:49:49
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[events:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
)
[touches:protected] => Array
(
)
[timestamps] => 1
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
)
here is the updated code
#foreach($manifests as $manifest)
<tr>
<td>
<input type="text" class="form-control" value="{{ $manifest['customer_name'] }}" />
</td>
<td>
<input type="text" class="form-control" value="{{ $manifest['produce'] }}" />
</td>
<td>
<input type="text" class="form-control" value="{{ $manifest['task'] }}" />
</td>
</tr>
#endforeach
The problem is that you are storing array (which is an object in PHP) in database.
This makes a form unrecognisable when you retrieve back the data.
You should serialise the array before storing it to database. It will save the data as a kind of JSON string.
serialize($array_var_name);
Further to get the array back you can use
unserialize($data_var);
Now you can use the array as usual array
You can do something like this:
#foreach($manifets as $manifestos)
#for ($counter = 0; $counter < sizeof($manifestos['customer_name']); $counter++)
<tr>
<td>
<input type="text" class="form-control" name="nameoffield" value="{{ $manifestos['customer_name'][$counter] }}" />
</td>
<td>
<input type="text" class="form-control" name="nameoffield" value="{{ $manifestos['otherfields'][$counter]}}" />
</td>
//more td here depending on the fields you want to display
</tr>
#endfor
#endforeach

How to customize json response?

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.

php mongodb, sort multidimensional array

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

Categories