PHP table from JSON without names in array - php

I am having a problem with api Yandex.
I have to get data from api and make with php the table:
Source Users New Pages Bounce Goal1 Goal2
organic s1v1 s1v2 s1v3 s1v4 s1v5 s1v6
referral s2v1 s2v2 s2v3 s2v4 s2v5 s2v6
(none) s3v1 s3v2 s3v3 s3v4 s3v5 s3v6
After json_decode I have:
{"data":[
{"dimensions":[{"name":"organic"}],"metrics":[s1v1,s1v2,s1v3,s1v4,s1v5,s1v6]},
{"dimensions":[{"name":"referral"}],"metrics":[s2v1,s2v2,s2v3,s2v4,s2v5,s2v6]},
{"dimensions":[{"name":"(none)"}],"metrics":[s3v1,s3v2,s3v3,s3v4,s3v5,s3v6]},
]}
But I can't correctly parse it into a table. So far I have written code only for the first column Source, and then stuck:
<?php
$metrika_o = json_decode($metrika);
echo "<table>
<tr>
<td><strong>Source</strong></td>
<td><strong>Users</strong></td>
<td><strong>New</strong></td>
<td><strong>Pages</strong></td>
<td><strong>Bounce</strong></td>
<td><strong>Goal1</strong></td>
<td><strong>Goal2</strong></td>
</tr>";
foreach($metrika_o->data as $data)
foreach($data->dimensions as $source)
:
?>
<tr>
<td><?php echo $source->name?></td>
</tr>
<?php endforeach;
echo "</table>";
?>
The Source number is constantly changing, the set of columns is fixed.
Please help me to solve this task

You are on the right way:
<?php foreach($metrika_o->data as $data): ?>
<tr>
<td><?php echo $data->dimensions[0]->name; ?></td>
<?php foreach(explode(',',$data->metrics) as $col):
<td><?php echo $col; ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>

Related

Foreach loop Json array

I am learning to work with Json at the moment, I have figured out how to display data for Name and Craft in space in the moment.
I cannot figure out how to display number of people.
Is not correct, I am receiving error
foreach ($json_data['number'] as $key => $value) {
echo $value;
}
Also does not work
foreach($json_data as $key=>$value)
{
echo $key['number'];
}
// Read JSON file
$json = file_get_contents('http://api.open-notify.org/astros.json');
//Decode JSON
$json_data = json_decode($json,true);
HTML
<table>
<tr>
<th>Name</th>
<th>Craft</th>
</tr>
<?php foreach($json_data['people'] as $key=>$value): ?>
<tr>
<td><?php echo $value['name']; ?></td>
<td><?php echo $value['craft']; ?></td>
</tr>
<?php endforeach; ?>
</table>
I want to display number of people in the space by using foreach loop
Change your code to
<?php
// Read JSON file
$json = file_get_contents('http://api.open-notify.org/astros.json');
//Decode JSON
$json_data = json_decode($json,true);
$peopleCount = 0;
?>
<table>
<tr>
<th>Name</th>
<th>Craft</th>
</tr>
<?php foreach($json_data['people'] as $key=>$value):
$peopleCount++;
?>
<tr>
<td><?php echo $value['name']; ?></td>
<td><?php echo $value['craft']; ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php
echo "Total People count: ". $peopleCount;
explanation:
$peopleCount variable is holding the number of people.
At first, its values is 0.
When iterating over the array the $peopleCount's value is incrementing by 1 ($peopleCount++; is equal to $peopleCount = $peopleCount +1;)
PS:
Your code had missing PHP closing tag at line number 8. I fixed it.
foreach is used to loop through an array. In your JSON output number is not returned as an array, so you do not need to use foreach loop for displaying number. You can just display the Number using following code:
<?php echo $json_data['number']; ?>

Mysql returns duplicate in html table

I am trying to populate html table with data from mysql.But i am stuck on this part where each time i add some data it keeps repeating. On the picture below you can see that Test 1 repeat each time for every P20,P21,P24,P22,P23 and i needed to be one TEST1 for all of them. When i add Test 2 with value 19000 its making new P20 and all data come from Test 1 to Test 2. Can someone help me how to fix this.Any hint or suggestion wil be appreciated. Thank you all very much (Sorry for my bad english)
This is code that runs this
$sql = 'SELECT DISTINCT Pers.naam, Rol.funkcija,pdata.broj
FROM ids
left JOIN Pers ON ids.persid = Pers.id
left JOIN Rol ON ids.rolid = Rol.id
left JOIN pdata ON ids.pdataid = pdata.id
';
$query = $conn->prepare($sql);
$query->execute();
$testing = $query->fetchAll(PDO::FETCH_ASSOC);
?>
<table>
<tr>
<th>P Small <small>(NONE)</small></th>
<?php
foreach ($testing as $test):
?>
<th>
<?php
echo $test['naam'] . '<br />';
?>
</th>
<?php
endforeach;
?>
</tr>
<tr>
<th>TESTING LINES</th>
</tr>
<?php foreach ($testing as $test): ?>
<tr>
<td><?php echo $test['funkcija']; ?></td>
<?php endforeach; ?>
<?php
foreach ($testing as $test):
?>
<td><?php echo $test['broj']; ?></td>
<?php
endforeach;
?>
</tr>
</table>
I would like to have it like this
There is some refactoring required to get to the output you want.
You seem to be enumerating quite a lot of data from your SQL result, so there is some grouping required to make things easier.
To get the appropriate number to each "naam" and "funkcija" you can use array_filter which however could theoretically return several numbers in each case.
You'll also have to nest a couple foreach instead of running them after one another.
If I understand your data structure correctly, this should at least give you a good starting point for the output you want:
<?php
// ...
$testing = $query->fetchAll(PDO::FETCH_ASSOC);
$naams = array_unique(array_column($testing, "naam"));
$funkcijas = array_unique(array_column($testing, "funkcija"));
?>
<table>
<tr>
<th>P Small <small>(NONE)</small></th>
<?php foreach ($naams as $naam): ?>
<th>
<?php echo $naam; ?>
</th>
<?php endforeach; ?>
</tr>
<tr>
<th>TESTING LINES</th>
</tr>
<?php foreach ($funkcijas as $funkcija): ?>
<tr>
<td><?php echo $funkcija; ?></td>
<?php foreach ($naams as $naam): ?>
<?php
$data = array_filter(
$testing,
function ($v) use ($naam, $funkcija)
{
return $v["naam"] === $naam && $v["funkcija"] === $funkcija;
}
);
foreach ($data as $value): ?>
<td><?php echo $value["broj"]; ?></td>
<?php endforeach; ?>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>

how to concatinate two variables

I have table that contain data
<td><?php echo $users->aa_0?></td
<td><?php echo $users->aa_1?></td>
<td><?php echo $users->aa_2?></td>
<td><?php echo $users->aa_3?></td>
when i'm applying static value to above code, it is working
But when i'm changing it to dynamic the below code is not working...
kindly help me
<?php foreach ($get_users as $users) { ?>
<tr>
<td><?php echo $users->aa_.$i?></td>
<?php ?>
</tr>
<?php } ?>
Basically
$users->{"aa_$i"}
But as JJ said, you should use arrays, like
$users->aa[$i]

how to transfer value to another page with link?

Hello i'm stil learning, using Codeigniter can someone tell me, or give example code?
what i need is in Round Id we have 111 i want give it link and search database with value 111 how to do that? here the code i tried but still not right
<div class="row" id="ajaxdata">
<table border="1">
<tr>
<th>Round Id</th>
<th>Player Id</th>
<th>Bet Place</th>
<th>Total Bet</th>
<th>Win</th>
<th>Lose</th>
</tr>
<?php foreach ($tbl_bet_spot as $data) {?>
<tr>
<td><?php echo $data->round_id;?>
<td><?php echo $data->id;?></td>
<td><?php echo $data->bet;?></td>
<td><?php echo $data->total_bet;?></td>
<td><?php echo $data->win;?></td>
<td><?php echo $data->lose;?></td>
</tr>
<?php } ?>
</table>
</table>
</div>
controller
public function detail_round_id(){
$select = $_GET['select'];
$data['tbl_bet_spot'] = $this->login_model->selectRoundId_by_round($select)->result();
print_r ($data);
}
i just try with my code and it work now, but it's static in here
<td><?php echo $data->round_id;?>
how i can send this value <?php echo $data->round_id;?> properly into controller? thanks a lot.
Use this code
<td><?php echo $data->round_id;?></td>
controller
public function detail_round_id(){
$select = $this->uri->segment(3);
$data['tbl_bet_spot'] = $this->login_model->selectRoundId_by_round($select)->result();
print_r ($data);
}
Try this may help you,
In view make link like this,
<td><?php echo $data->round_id;?>
And in controller add parameter like this,
public function detail_round_id($id){
$data['tbl_bet_spot'] = $this->login_model->selectRoundId_by_round($id)->result();
print_r ($data);
}
view page you pass value like this
<?php echo $data->round_id;?>
In controller get value like this
$select=$this->uri->segment(4);
hope this will help

Display two arrays in foreach loop in Php?

I want to display data in single foreach loop. I have two tables dailystats and monthlystats both of them have same columns like calls,minutes,incomingcalls etc Im using Yii PHP Framework Here is my controller
public function actionViewStats(){
$model = new Company();
$id = $_GET['id'];
$modelMonthly = $model->getCompanyUsageMonthly($id);
$modelDaily = $model->getCompanyUsageDaily($id);
$this->renderPartial('/shared/_company_stats', array(
'modelDaily' => $modelDaily,
'modelMonthly'=>$modelMonthly
));
}
Here is my view of table.
<?PHP if(isset($modelMonthly) && sizeof($modelMonthly)!=0): ?>
<div class="ibox-content col-md-12 col-xs-12">
<div class="title col-md-2">
<h3>Current Usage</h3>
</div>
<div class="col-md-10">
<div class="col-md-12 col-xs-12">
<table class="table">
<thead>
<th>Minutes</th>
<th>Incoming Minutes</th>
<th>Calls</th>
<th>Incoming Calls</th>
</thead>
<tbody>
<?PHP
if(isset($modelMonthly)){
foreach($modelMonthly as $monthlystats){
?>
<tr>
<td><?php echo $monthlystats->minutes?></td>
<td><?PHP echo $monthlystats->incoming_minutes; ?></td>
<td><?PHP echo $monthlystats->calls; ?></td>
<td><?PHP echo $monthlystats->incoming_calls; ?></td>
</tr>
<?PHP } } ?>
</tbody>
</table>
</div>
</div>
</div>
<?PHP endif;?>
This is showing only monthly stats modelMonthly but i want to show daily stats modelDaily too in the same foreach loop.. How do i do that? I have tried array_combine etc but unable to do it. Searched on SOF but unable to find solution for it.
My code above shows only Monthly stats like this
But I want to show like this below. I have already made the table but im not able to use both modelDaily and modelMonthly in same foreach loop. I have tried different things but unable to do it..
<?PHP
if(isset($modelMonthly)){
foreach($modelMonthly as $usage){
?>
<tr>
<td><?php echo $usage->minutes?></td>
<td><?PHP echo $usage->incoming_minutes; ?></td>
<td><?PHP echo $usage->calls; ?></td>
<td><?PHP echo $usage->incoming_calls; ?></td>
</tr>
<?PHP } } ?>
If both your arrays have numerical indexes, you can use a regular for loop:
for ($i = 0; $i < max(count($modelMonthly), count($modelDaily)); $i) {
if (isset($modelDaily[$i]) {
// Add the daily columns
} else {
// Add as much empty columns
}
if (isset($modelMonthly[$i]) {
// Add the monthly columns
} else {
// Add as much empty columns
}
}
The next step is adding the required headers th in the thead, and adding the extra td inside the loop.
Alternatively, you can create both table independantly, and position them using CSS. It is not in the scope of this question, but this is what I would recommend.
You shoudl use this tecnique
foreach($modelMonthly as $index => $usage){
?>
<tr>
<td><?php echo $usage->minutes?></td>
<td><?PHP echo $usage->incoming_minutes; ?></td>
<td><?PHP echo $usage->calls; ?></td>
<td><?PHP echo $usage->incoming_calls; ?></td>
<td><?php if (isset($dailystats[$index]->minute)) echo $dailystats[$index]->minutes?></td>
<td><?PHP if (isset($dailystats[$index]->incoming_minutes)) echo $dailystats[$index]->incoming_minutes; ?></td>
<td><?PHP if (isset($dailystats[$index]->calls)) echo $dailystats[$index]->calls; ?></td>
<td><?PHP if (isset($dailystats[$index]->incoming_calls)) echo $dailystats[$index]->incoming_calls; ?></td>
</tr>
<?PHP } } ?>

Categories