Create looping table with variable from result - php

I have pulled some information from a site API and it is stored in $result. I would like to create a table with this information that loops x number of times (constant). Here is how I currently have it repeating by hand and it works. I just imagine there must be a better/easier way to do this then manually repeating it each time.
<tr>
<td><?php echo strtoupper($result['weapons']['0']['stat']['id']); ?></td>
<td><?php echo round($result['weapons']['0']['extra']['accuracy'],2); ?>%</td>
<td><?php echo round($result['weapons']['0']['extra']['kpm'],2); ?></td>
<td><?php echo number_format($result['weapons']['0']['stat']['shots']); ?></td>
<td><?php echo number_format($result['weapons']['0']['stat']['hits']); ?></td>
<td><?php echo number_format($result['weapons']['0']['stat']['kills']); ?></td>
<td><?php echo number_format($result['weapons']['0']['stat']['hs']); ?></td>
<td><?php echo round($result['weapons']['0']['extra']['hkp'],2); ?>%</td>
</tr>
<tr>
<td><?php echo strtoupper($result['weapons']['1']['stat']['id']); ?></td>
<td><?php echo round($result['weapons']['1']['extra']['accuracy'],2); ?>%</td>
<td><?php echo round($result['weapons']['1']['extra']['kpm'],2); ?></td>
<td><?php echo number_format($result['weapons']['1']['stat']['shots']); ?></td>
<td><?php echo number_format($result['weapons']['1']['stat']['hits']); ?></td>
<td><?php echo number_format($result['weapons']['1']['stat']['kills']); ?></td>
<td><?php echo number_format($result['weapons']['1']['stat']['hs']); ?></td>
<td><?php echo round($result['weapons']['1']['extra']['hkp'],2); ?>%</td>
</tr>
I just can't wrap my head around how I would loop this? I have searched and looked at many articles, I just can't seem to get anything to work.

If you'd like to run through every result the easiest way would be to use foreach
e.g.
foreach($result['weapons'] as $tr){ ?>
<tr>
<td><?= strtoupper($tr['stat']['id']) ?></td>
<td><?= round($tr['extra']['accuracy'],2) ?>%</td>
//... etc.
</tr>
<?php } //...
If you'd prefer to define the amount of loops just use "for"
for ($i = 0; $i <= 10; $i++) { ?>
<tr>
<td><?= strtoupper($result['weapons'][$i]['stat']['id']) ?></td>
<td><?= round($result['weapons'][$i]['extra']['accuracy'],2) ?>%</td>
<td><?= round($result['weapons'][$i]['extra']['kpm'],2) ?></td>
//.... etc.
</tr>
<?php }

it's very easy to use foreach loop lets assume you got your value in variable $result_api then loop through this array
$i=0;
foreach($result_api as $result)
{ ?>
<tr>
<td><?php echo strtoupper($result['weapons'][$i]['stat']['id']); ?></td>
<td><?php echo round($result['weapons'][$i]['extra']['accuracy'],2); ?>%</td>
<td><?php echo round($result['weapons'][$i]['extra']['kpm'],2); ?></td>
<td><?php echo number_format($result['weapons'][$i]['stat']['shots']); ?></td>
<td><?php echo number_format($result['weapons'][$i]['stat']['hits']); ?></td>
<td><?php echo number_format($result['weapons']['0']['stat']['kills']); ?></td>
<td><?php echo number_format($result['weapons'][$i]['stat']['hs']); ?></td>
<td><?php echo round($result['weapons'][$i]['extra']['hkp'],2); ?>%</td>
</tr>
<?php
$i++;
}

Related

Achieve dynamic ranking structure for sql data on a web page

I have a SQL data displayed on a web page and I want to create a dynamic ranking structure for that data. The data will be updated as the new user logs in and updates his data and structure has to update their ranks based on their scores. Like, there are around 20 rows for each user and all the fields of one row should be compared with all the other users' rows and do it for all the rows and update the ranking system.
I have tried checking SQL ranking functions but they were limited to one row, do not know if there are any other options to try them out or there is a completely different way around.
<tr>
<th>Kanto</th>
<th>Johto</th>
<th>Hoenn</th>
<th>Sinnoh</th>
<th>Collector</th>
<th>Scientist</th>
<th>Backpacker</th>
<th>Battle Girl</th>
<th>Berry Master</th>
<th>Gym Leader</th>
<th>Idol</th>
<th>Great League Veteran</th>
<th>Ultra League Veteran</th>
<th>Master League Veteran</th>
<th>Jogger</th>
<th>Breeder</th>
<th>Pikachu Fan</th>
<th>Champion</th>
<th>Battle Legend</th>
<th>Pokemon Ranger</th>
<th>Gentleman</th>
<th>Pilot</th>
<th>Fisherman</th>
<th>Ace Trainer</th>
<th>Youngster</th>
<th>Cameraman</th>
</tr>
<?php
$result=$con->query("SELECT * from user") or die($con->error());
$i=0;
while($row = mysqli_fetch_array($result)){
?>
<tr>
<td><?php echo $row['kanto']; ?></td>
<td><?php echo $row['johto']; ?></td>
<td><?php echo $row['hoenn']; ?></td>
<td><?php echo $row['sinnoh']; ?></td>
<td><?php echo $row['collector']; ?></td>
<td><?php echo $row['scientist']; ?></td>
<td><?php echo $row['backpacker']; ?></td>
<td><?php echo $row['battle_girl'];; ?></td>
<td><?php echo $row['berry_master']; ?></td>
<td><?php echo $row['gym_leader']; ?></td>
<td><?php echo $row['idol']; ?></td>
<td><?php echo $row['great_league_veteran']; ?></td>
<td><?php echo $row['ultra_league_veteran']; ?></td>
<td><?php echo $row['master_league_veteran'];?></td>
<td><?php echo $row['jogger']; ?></td>
<td><?php echo $row['breeder']; ?></td>
<td><?php echo $row['pikachu_fan']; ?></td>
<td><?php echo $row['champion']; ?></td>
<td><?php echo $row['battle_legend']; ?></td>
<td><?php echo $row['pokemon_ranger']; ?></td>
<td><?php echo $row['gentleman']; ?></td>
<td><?php echo $row['pilot']; ?></td>
<td><?php echo $row['fisherman']; ?></td>
<td><?php echo $row['ace_trainer']; ?></td>
<td><?php echo $row['youngster']; ?></td>
<td><?php echo $row['cameraman']; ?></td>
</tr>
<?php
$i++;
} ?>
</table>
So, above is my table showing data. All the fields are numbers and I want the data to be structured dynamically considering all the values from all the rows.
Thanks in advance.

Do While with IF

i just want data will show if ['stt']==1
Here is the code :
<?php do { ?>
<tr>
<td><?php echo $row_daftaruser['id']; ?></td>
<td><?php echo $row_daftaruser['nama']; ?></td>
<td><?php echo $row_daftaruser['email']; ?></td>
<td><?php echo $row_daftaruser['username']; ?></td>
<td><?php echo $row_daftaruser['password']; ?></td>
<td><?php echo $row_daftaruser['alamat']; ?></td>
<td><?php echo $row_daftaruser['tgl_lahir']; ?></td>
<td><?php echo $row_daftaruser['stt']; ?></td>
</tr>
<?php } while ($row_daftaruser = mysql_fetch_assoc($daftaruser)); ?>
Can somebody help me?
you could try this:
<?php do { ?>
<?php if($row_daftaruser['stt'] == 1): ?>
<tr>
<td><?php echo $row_daftaruser['id']; ?></td>
<td><?php echo $row_daftaruser['nama']; ?></td>
<td><?php echo $row_daftaruser['email']; ?></td>
<td><?php echo $row_daftaruser['username']; ?></td>
<td><?php echo $row_daftaruser['password']; ?></td>
<td><?php echo $row_daftaruser['alamat']; ?></td>
<td><?php echo $row_daftaruser['tgl_lahir']; ?></td>
<td><?php echo $row_daftaruser['stt']; ?></td>
</tr>
<?php endif ?>
<?php } while ($row_daftaruser = mysqli_fetch_assoc($daftaruser)); ?>
This code will only display values if stt is equal to 1.
Also, use mysqli not mysql because it mysql is obsolete.
Why do not you write the query this way?
SELECT * FROM TABLE WHERE stt = 1;

Loop php values from an echo in HTML

I have a little issue when I try to to loop my php values in HTML. So far this is what I tried but I have not excpected result.
If I remove the loop I only get the first entry. I would like to echo all the possibles entries from my research.
This is my code ( from an SQL request).
<html>
<table>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Number</th>
<th>Adress</th>
<th>link</th>
<!--<th class="glyphicon glyphicon-pencil"></th>-->
</tr>
<tr>
<?php $rowG = oci_fetch_array($stid, OCI_RETURN_NULLS);?>
<?php foreach($array as $rowG=>$value): ?> <tr>
<td><?php echo $rowG[2]; ?></td>
<td><?php echo $rowG[1]; ?></td>
<td><?php echo $rowG[0]; ?></td>
<td><?php echo $rowG[3]?></td>
<td><?php echo "<a href='./consultation.php?Login=$rowG[2]'> Link </a>" ; ?></td>
<?php endforeach;}} ?>
</tr>
</table>
</html>
Do you know where I made my mistake ?
Thank you for your help
Edit : Finally I managed to do it by using a do{}while loop.
Thank you all for your help
RFlow
It's hard to guess what you are trying to do or even what actually happens, since I don't know what is assigned to $rowG, so I tried to hack meaning out of this from the code's errors and came up with that :
<?php
while ($rowG = oci_fetch_array($stid, OCI_RETURN_NULLS)) {
?>
<tr>
<td><?php echo $rowG[2]; ?></td>
<td><?php echo $rowG[1]; ?></td>
<td><?php echo $rowG[0]; ?></td>
<td><?php echo $rowG[3]; ?></td>
<td><?php echo ' Link '; ?></td>
</tr>
<?php
}
?>
If it doesn't work with you, you'll have to provide the informations that should have been included in your question since the begining.
This is basic iteration over query results:
while ($rowG = oci_fetch_array($stid, OCI_RETURN_NULLS)) {?>
<tr>
<td><?php echo $rowG[2]; ?></td>
<td><?php echo $rowG[1]; ?></td>
<td><?php echo $rowG[0]; ?></td>
<td><?php echo $rowG[3]?></td>
<td><?php echo "<a href='./consultation.php?Login=$rowG[2]'> Link </a>" ; ?></td>
</tr>
<?php
}
<?php foreach($array as $rowG=>$value): ?> <tr>
<td><?php echo $rowG[2]; ?></td>
<td><?php echo $rowG[1]; ?></td>
<td><?php echo $rowG[0]; ?></td>
<td><?php echo $rowG[3]?></td>
<td><?php echo "<a href='./consultation.php?Login=$rowG[2]'> Link </a>" ; ?></td>
<?php endforeach;}} ?>
Would be :
<?php foreach($rowG as $arr): ?> <tr>
<td><?php echo $arr[2]; ?></td>
<td><?php echo $arr[1]; ?></td>
<td><?php echo $arr[0]; ?></td>
<td><?php echo $arr[3]?></td>
<td><?php echo "<a href='./consultation.php?Login=$arr[2]'> Link </a>" ; ?></td>
<?php endforeach;}} ?>
Note the use of $arr instead of $rowG.
In the original code $array is not used.

Looping array sum per row (array[0] + array[1] = $a) then $a + array[2]) php mysql

I have a problem with my code.
This rule for my array loop:
I have tried like this :
$line_1=mysql_query("SELECT * FROM loading_plan WHERE line='1' ORDER BY id_load asc");
$total_hr_21 = Array();
while ($l1=mysql_fetch_array($line_1)){
$total_hr[] = $l1[total_hr];
}
This code just describes the array number, but I don't have any idea how to sum this "$l1[total_hr];" per row like in my rule.
[EDIT]
I was implement answer from Karthick Kumar to my coding, that just using array_sum($total_hr_21); and it's work for my rule. so this is my full code :
<?php
error_reporting(0);
include "../config/koneksi.php";
include "../config/library.php";
$today = date("Y-m-d");
$line_1=mysql_query("SELECT * FROM loading_plan WHERE line='1' ORDER BY id_loading asc");
$total_hr_21 = Array();
while ($l1=mysql_fetch_array($line_1)){
$est_time_wobal = $l1[qty_wo] / $l1[uph];
$est_time_wofk = $l1[bal_fk] / $l1[uph];
$total_hr = $est_time_wobal + $est_time_wofk + $l1[setup_time];
$kit_to_ship = ((abs(strtotime ($l1[ship_date]) - strtotime ($l1[susbstore_date])))/(60*60*24));
$balance_days = ((abs(strtotime ($l1[ship_date]) - strtotime ($tgl_sekarang)))/(60*60*24));
$q_days = ((abs(strtotime ($tgl_sekarang) - strtotime ($l1[susbstore_date])))/(60*60*24));
$total_hr_21[] = $total_hr;
?>
<tr>
<td><?php echo "Line#".$l1[line]; ?></td>
<td><?php echo $l1[no_run]; ?></td>
<td><?php echo $l1[susbstore_date]; ?></td>
<td><?php echo $l1[no_prog]; ?></td>
<td><?php echo $l1[product_number]; ?></td>
<td><?php echo $l1[product_desc]; ?></td>
<td><?php echo $l1[wo]; ?></td>
<td><?php echo $l1[qty_wo]; ?></td>
<td><?php echo $l1[bal_wo]; ?></td>
<td><?php echo $l1[bal_fk]; ?></td>
<td><?php echo $l1[uph]; ?></td>
<td><?php echo round($est_time_wobal,2); ?></td>
<td><?php echo round($est_time_wofk,2); ?></td>
<td><?php echo $l1[setup_time]; ?></td>
<td><?php echo round($total_hr,2); ?></td>
<td><?php echo $l1[type_solder]; ?></td>
<td><?php echo $l1[ship_date]; ?></td>
<td><?php echo $kit_to_ship; ?></td>
<td><?php echo $q_days; ?></td>
<td><?php echo array_sum($total_hr_21); ?></td>
<td><?php echo " "; ?></td>
<td><?php echo " "; ?></td>
</tr>
<?php } ?>
Thanks, For help :)

How to convert xml file to php array? [duplicate]

This question already has answers here:
How to convert XML into array in PHP?
(12 answers)
Closed 7 years ago.
I've got strange xml file like this http://xxx.pl/tmp/tabela.xml, and I don't know how to convert it into an array. I tried something like this:
<?php
$get = file_get_contents('http://xxx.pl/tmp/tabela.xml');
$arr = simplexml_load_string($get);
$data = $arr -> druzyna;
?>
<table>
<tr>
<th>pozycja</th>
<th>pkt</th>
<th>mecze</th>
<th>zwyciestwa</th>
<th>porazki</th>
<th>wygrane w domu</th>
<th>przegrane w domu</th>
<th>wygrane na wyjezdzie</th>
<th>przegrane na wyjezdzie </th>
<th>kosze zdobyte</th>
<th>kosze stracone</th>
<th>stosunek zdobytych punktów do straconych punktów</th>
<th>pelna nazwa klubu</th>
<th>logo klubu</th>
</tr>
<?php foreach($data as $row) : ?>
<tr>
<td><?php echo $row->pozycja ?></td>
<td><?php echo $row->pkt ?></td>
<td><?php echo $row->mecze ?></td>
<td><?php echo $row->zwyciestwa ?></td>
<td><?php echo $row->porazki ?></td>
<td><?php echo $row->wygrane_dom ?></td>
<td><?php echo $row->przegrane_dom ?></td>
<td><?php echo $row->wygrane_wyjazd ?></td>
<td><?php echo $row->przegrane_wyjazd ?></td>
<td><?php echo $row->kosze_zdobyte ?></td>
<td><?php echo $row->kosze_stracone ?></td>
<td><?php echo $row->stosunek_zdob_strac ?></td>
<td><?php echo $row->pelna_nazwa ?></td>
<td><?php echo $row->logo; ?></td>
</tr>
<?php endforeach;?>
</table>
but it's not working.
I read something about json solution, but i don't know how to use it, however I met first time xml format like that.
I think that this topic How to convert xml into array in php? isn't solution of my problem, because i've got another xml file..
simplexml_load_string returns an SimpleXMLElement object. You can query the object (search your data) by using the xpath() function.
Because of how your XML file is set up, the values are set as attributes.
So you need to retrieve the attributes before you can read them.
<?php
$get = file_get_contents('http://xxx.pl/tmp/tabela.xml');
$arr = simplexml_load_string($get);
$data = $arr->xpath('tabela/druzyna');
?>
<table>
<tr>
<th>pozycja</th>
<th>pkt</th>
<th>mecze</th>
<th>zwyciestwa</th>
<th>porazki</th>
<th>wygrane w domu</th>
<th>przegrane w domu</th>
<th>wygrane na wyjezdzie</th>
<th>przegrane na wyjezdzie </th>
<th>kosze zdobyte</th>
<th>kosze stracone</th>
<th>stosunek zdobytych punktów do straconych punktów</th>
<th>pelna nazwa klubu</th>
<th>logo klubu</th>
</tr>
<?php foreach($data as $row) : ?>
<?php
$row = $row->attributes();
?>
<tr>
<td><?php echo $row->pozycja ?></td>
<td><?php echo $row->pkt ?></td>
<td><?php echo $row->mecze ?></td>
<td><?php echo $row->zwyciestwa ?></td>
<td><?php echo $row->porazki ?></td>
<td><?php echo $row->wygrane_dom ?></td>
<td><?php echo $row->przegrane_dom ?></td>
<td><?php echo $row->wygrane_wyjazd ?></td>
<td><?php echo $row->przegrane_wyjazd ?></td>
<td><?php echo $row->kosze_zdobyte ?></td>
<td><?php echo $row->kosze_stracone ?></td>
<td><?php echo $row->stosunek_zdob_strac ?></td>
<td><?php echo $row->pelna_nazwa ?></td>
<td><?php echo $row->logo; ?></td>
</tr>
<?php endforeach;?>
</table>
It seems like a normal xml, please try this
$url = "http://example.com/tmp/tabela.xml";
$xml = file_get_contents($url);
$xml = simplexml_load_string($xml);
echo '<pre>';
print_r($xml);

Categories