Simple html dom parser table to array - php

I'm trying to parse the arrivals table from here [1] and put in into an array to be able to format it and put it into a table.
I did some research here and there, I've got some code from other questions, but I can't make the array and table look as I'd like.
Anyone can help me out?
<?php
require('simple_html_dom.php');
$html = file_get_html('http://flightplan.romatsa.ro/init/fpl/flightslr/LRCL/');
$table = $html->find('table', 3);
foreach($table->find('tr') as $row) {
// initialize array to store the cell data from each row
$rowData = array();
foreach($row->find('td') as $cell) {
// push the cell's text to the array
$rowData[] = $cell->innertext;
}
echo "<table>";
echo "<td>";
echo $rowData[0]. " ";
echo "</td>";
echo "<td>";
echo $rowData[1]. " ";
echo "</td>";
echo "<td>";
echo $rowData[2]. " ";
echo "</td>";
echo "<td>";
echo $rowData[3]. " ";
echo "</td>";
echo "<td>";
echo $rowData[4]. " ";
echo "</td>";
echo "<td>";
echo $rowData[5]. " ";
echo "</td>";
echo "<td>";
echo $rowData[6]. " ";
echo "</td>";
echo "<td>";
echo $rowData[7]. " ";
echo "</td>";
echo "<td>";
echo $rowData[8]. " ";
echo "</td>";
echo "</table>";
}
?>

Maybe try putting each row into an array and then each cell into another array. Hopefully, that will do what you want.
require('simple_html_dom.php');
$html = file_get_html('http://flightplan.romatsa.ro/init/fpl/flightslr/LRCL/');
$table = $html->find('table', 3);
$rowData = array();
foreach($table->find('tr') as $row) {
// initialize array to store the cell data from each row
$flight = array();
foreach($row->find('td') as $cell) {
// push the cell's text to the array
$flight[] = $cell->plaintext;
}
$rowData[] = $flight;
}
echo '<table>';
foreach ($rowData as $row => $tr) {
echo '<tr>';
foreach ($tr as $td)
echo '<td>' . $td .'</td>';
echo '</tr>';
}
echo '</table>';
Note: this solution requires the simple_html_dom.php library. Get it here!

Related

Donation list - code issue? There is something wrong in the code

When people donate more than once in the page, it only shows their recent donation and not their total donations (in the donations list). For example if I donate $5 now and I will donate $10 later, It will only show $10 and not $5. That's the website: gesher-jds.org/giving-tuesday
How do I fix it?
<div class="donors_info">
<div class="totalenteries"></div>
<?php
$valueid = 2727;
//Get donors based on forms IDs (can be single or multiple forms)
if(function_exists('give_get_payments')) {
$args = array(
'give_forms' => $valueid,
'number' => -1,
);
$donations = give_get_payments( $args );
echo "<table id='dtab'>";
echo "<thead>";
echo "<th align='center'>" . 'Name' . "</th>";
echo "<th align='center'>" . 'Amount' . "</th>";
echo "</thead>";
echo "<tbody>";
$donors = array();
foreach($donations as $donation) {
//Now get donor information from this donation ("customer" aka "donor")
$customer_id = give_get_payment_customer_id( $donation->ID );
$customers = new Give_Customer( $customer_id );
$nameis = $customers->name;
if (in_array($nameis, $donors)) {} else {
$amt = get_post_meta($donation->ID,'_give_payment_total',true) + 0;
echo "<tr class='select'>";
echo "<td align='left'>" . $nameis . "</td>";
echo "<td align='center'>" . $purchaseis = "$" . $amt . "</td>";
echo "</tr>";
$donors[] = $nameis;
}
}
echo "</tbody>";
echo "</table>";
}
?>
</div>
$amt = get_post_meta($donation->ID,'_give_payment_total',true)
"true" returns only the first value for the specified meta key & "false" will give you an array of meta-values. You can iterate the returned array with foreach.
https://developer.wordpress.org/reference/functions/get_post_meta/
edit:
Yes, you need to change it to false and then you get an array of values. Now you only get a single value.
then iterate the array:
$amt = get_post_meta($donation->ID,'_give_payment_total',false);
foreach ($amt as as $key => $value) {
echo "<tr class='select'>";
echo "<td align='left'>" . $nameis . "</td>";
echo "<td align='center'>" . $purchaseis = "$" . $value . "</td>";
echo "</tr>";
}
Code is unchecked, but this ist the way to go. Some information about foreach:
https://www.php.net/manual/de/control-structures.foreach.php
edit 2:
If you want to show all donations in one row:
<div class="donors_info">
<div class="totalenteries"></div>
<?php
$valueid = 2727;
//Get donors based on forms IDs (can be single or multiple forms)
if(function_exists('give_get_payments')) {
$args = array(
'give_forms' => $valueid,
'number' => -1,
);
$donations = give_get_payments( $args );
echo "<table id='dtab'>";
echo "<thead>";
echo "<th align='center'>" . 'Name' . "</th>";
echo "<th align='center'>" . 'Amount' . "</th>";
echo "</thead>";
echo "<tbody>";
$donors = array();
$dtmp=array(); //tmp array
foreach($donations as $donation) {
//Now get donor information from this donation ("customer" aka "donor")
$customer_id = give_get_payment_customer_id( $donation->ID );
$customers = new Give_Customer( $customer_id );
$nameis = $customers->name;
$amt = get_post_meta($donation->ID,'_give_payment_total',false);
$donationAll = 0;
foreach ($amt as $key => $value) {
$donationAll = $donationAll + $value;
}
//Write a tmp array with name as key and donation as value
if(array_key_exists($nameis,$dtmp)) {
//name already exists -> add donation value
$dtmp[$nameis] = $dtmp[$nameis] + $donationAll;
} else {
//name is not in the array -> new array: key name -> donation
$dtmp[$nameis] = $donationAll;
}
$donors[] = $nameis;
}
foreach ($dtmp as $key => $value) {
//iterate the tmp array
echo "<tr class='select'>";
echo "<td align='left'>" . $key . "</td>";
echo "<td align='center'>" . $purchaseis = "$" . $value . "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
}
?>
</div>
Code is unchecked.
Regards Tom

Single php array to html table

The idea is to print an html table from this array:
$arr = ['1','2','3','4,'5,'6','7','8','9'];
I expect my table to be something like:
1 2 3
4 5 6
7 8 9
I tried a lot but I couldn't find an idea to do this.
My idea was to break each three element but I need something smarter.
You can use array-chunk like this:
$arr = ['1','2','3','4','5','6','7','8','9'];
echo "<table>";
foreach(array_chunk($arr, 3) as $row) {
echo "<tr>";
foreach($row as $cell) {
echo "<td>$cell</td>";
}
echo "</tr>";
}
echo "</table>";
$arr = ['1','2','3','4','5','6','7','8','9'];
$from=0; //index from of arr
$number=3; //number cell per row
echo "<table border='1'>";
while($row=array_slice($arr,$from,$number)){
echo "<tr>";
foreach($row as $cell) {
echo "<td>$cell</td>";
}
echo "</tr>";
$from+=$number;
}
echo "</table>";
<?php
$arr = ['1','2','3','4','5','6','7','8','9'];
print "<table>\n";
foreach(array_chunk($arr, 3) as $row) {
print "<tr>";
foreach($row as $col) {
print "<td>";
print $col;
print "</td>";
}
print "</tr>\n";
}
print "</table>";
?>

why does foreach skip 2 rows from the query result?

For 10 rows in the query it only returns 8 rows but i get the fields right:
For Query data which returns less than 2 rows I get an error.
//create table to display all data
echo "<table border="1"> ";
echo "<tr>";
$row = mysqli_fetch_assoc($result);
foreach($row as $key => $value)
{
echo "<th>$key</th>";
}
echo "</tr>";
while (($row = $result->fetch_assoc()) !== null)
{
$output = array();
$i=1;
echo "<tr>";
foreach ($row as $columnName => $columnValue)
{
$output[] = $columnName."=>". $columnValue;
echo "<td>".$columnValue."</td>";
}
echo "</tr>";
}
echo "</table>";
Edit: Thanks to #Michael Berkowski for his comments on the question.
Here is a modified version of your code that should work:
//create table to display all data
echo "<table border=1 >";
echo "<tr>";
//echo "<th> ## </th>";
$row = $result->fetch_assoc(); // stick to the object-oriented form. It is cleaner.
foreach ($row as $key => $value)
{
echo "<th>$key</th>";
}
echo "</tr>";
do
{
$output = array();
echo "<tr>";
foreach ($row as $columnName => $columnValue)
{
$output[$columnName] = $columnValue; // this is neater.
echo "<td>" . $columnValue . "</td>";
}
echo "</tr>";
} while ($row = $result->fetch_assoc());
echo "</table>";
You can use your first foreach() loop to print the keys and then use a do-while() loop to get your desired output.
Additional reading:
PHP do-while loop
You need to use
mysqli_fetch_assoc($result);

php fun codeing

hey I make a simple php table using nested for loop ... and it will be like this...
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
but the problem is , i can not print this value using a loop inside the column .. so what will be the solution ??? please
my code :
echo "<table border=1>\n";
for($row=1;$row<=3;$row++)
{
echo "<tr>";
for($col=1;$col<=5;$col++)
{
echo "<td>";
echo "MY PROBLEM HERE...I cant print column numbers \n";
echo "</td>";
}
echo "</tr>";
}
echo "</table> \n";
echo "<table border=1>\n";
for($row=1;$row<=3;$row++)
{
echo "<tr>";
for($col=1;$col<=5;$col++)
{
echo "<td>";
//echo "MY PROBLEM HERE...I cant print column numbers \n";
echo $col + ($row - 1) * 5;
echo "</td>";
}
echo "</tr>";
}
echo "</table> \n";
To save several loops:
$rows = 3;
$cols = 5;
$table = '<table border="1">';
for($i=1;$i<=$rows;$i++){
$table .= '<tr><td>'.implode('</td><td>', range($cols*$i-$cols+1,$cols*$i)).'</td></tr>';
}
$table .= '</table>';
echo $table;
It's not $col + $row * 5 it has to be $row - 1
<?php
echo "<table border=1>\n";
for($row=1;$row<=3;$row++)
{
echo "<tr>";
for($col=1;$col<=5;$col++)
{
echo "<td>";
echo $col + ($row-1) * 5;
echo "</td>";
}
echo "</tr>";
}
echo "</table> \n";
?>

Print query with loops in it and condition

I have a foreach function which permits me to print fields in a database,
I want to do possible that if $row[value]="photo" don't just print the $row[value] but another thing.
Can I do it?
The code is this:
while($row = mysql_fetch_array($result)) {
echo "<tr>";
foreach($checked1 as $key => $value){
echo "<td>" . $row[$value] . "</td>";
}
I need to have a condition that if $row[value]=photo to echo another thing not: echo "<td>" . $row[$photo] . "</td>";
Any example?
Since i could not get what to print for if($row['value'] == photo) check, so add that code yourself in the code written below.
while($row = mysql_fetch_array($result)){
echo "<tr>";
foreach($checked1 as $key => $value){
if($value == 'photo'){
echo "<td><img src = '".$row[$value]."'></td>";
}else{
echo "<td>" . $row[$value] . "</td>";
}
}
}
This doesn't need to be complex.
foreach ($result as $row) {
if ($row['value'] === 'photo') {
// print "another thing"
} else {
// print "something"
}
}
Sure, just include an if statement with that. Your code will be similar to the code below:
$arr = array("one", "two", "three");
foreach ($arr as $value) {
if($value == "one")
echo "Not gonna print one.";
else
echo "Value: $value<br />\n";
}
Using your code, you can see if the value is not photo, echo it.
while($row = mysql_fetch_array($result)) {
echo "<tr>";
foreach($checked1 as $key => $value){
if($row[value] == "photo") {
echo "some other thing";
} else {
echo "<td>" . $row[$value] . "</td>";
}
}
}
If you want to print img src if the field is "photo" field (key) then
Try this :
while($row = mysql_fetch_array($result)){
echo "<tr>";
foreach($checked1 as $key => $value){
if($key == 'photo'){
echo "<td><img src = '".$row[$value]."'></td>";
}else{
echo "<td>" . $row[$value] . "</td>";
}
}
}

Categories