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);
Related
data i am fetching from infusion using tag id and dsfind() function. and that data i have to divide into 3 pages. (data used be unique)
<?php
require("isdk.php");
$app = new iSDK;
if ($app->cfgCon("infusionid"))
{
$returnFields = array('Id','Email');
$contacts = $app->dsFind('Contact',14,0,'Groups',7406,$returnFields);
echo '<pre>', print_r($contacts), '</pre>';
}
?>
I want to print this series
<tr>
<td><?php echo $contacts[0]['Id'] ?></td>
<td><?php echo $contacts[0]['Email']; ?></td>
</tr>
<tr>
<td><?php echo $contacts[4]['Id'] ?></td>
<td><?php echo $contacts[4]['Email']; ?></td>
</tr>
<tr>
<td><?php echo $contacts[7]['Id'] ?></td>
<td><?php echo $contacts[7]['Email']; ?></td>
</tr>
<tr>
<td><?php echo $contacts[11]['Id'] ?></td>
<td><?php echo $contacts[11]['Email']; ?></td>
</tr>
You can use foreach() function e.g:
echo "<table>";
foreach ( $contacts as $var) {
echo "<tr><td>ID: ", $var['Id']."</td>";
echo "<td>Email: ", $var['Email']."</td></tr>";
}
echo "</table>";
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;
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.
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 :)
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++;
}