I have one array that I am showing like this:
echo '<table>';
foreach ($rowData as $row => $tr) {
echo '<tr>';
foreach ($tr as $td)
echo '<td>' . $td .'</td>';
echo '</tr>';
}
echo '</table>';
The second and fourth columns are always the name of the column. The result is something like:
Name: John Locke - NickName: Locke
Age: 20 - Adress: Ok
See the pattern?
How can I put these array in my database?
As my database table structure is:
ID - Name - NickName - Age - Adress
I don't have a clue how to do that with the array i'm using..
--UPDATE
$table = $html->find('table', 0);
$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) {
foreach($cell->find('span') as $e){
$e->innertext = '';
}
// push the cell's text to the array
$flight[] = $cell->plaintext;
}
$rowData[] = $flight;
}
echo '<table>';
foreach ($rowData as $row => $tr) {
echo '<tr>';
echo '<td>' . $row . '</td>';
foreach ($tr as $td)
echo '<td>' . $td .'</td>';
echo '</tr>';
}
echo '</table>';
you can do that:
$insert = "";
foreach ($rowData as $row => $tr) {
$insert .= "('".$tr[0]."','".$tr[0]."','".$tr[0]."','".$tr[0]."'),";
}
$insert = substr($insert, 0, -1)
$sql = "Insert into YourTable values ".$insert;
As you already know what the array positions relate to, IE positions 0 through 3. You can simply iterate as you are doing now and compile a query instead.
Just drop this snippet into your code, ive knocked this up on the fly and its a bit hackish, probably a better way. Just look at what it does on your screen and see where if any it needs correcting.
$insert = '';
for ($i=0; $i<=3; $i++):
$insert .= "'" . $tr[$i] . "'";
if ($i !== 3):
$insert .= ',';
endif;
endfor;
echo $insert;
Once you are seeing what looks like a correct insert then you can (using safe methods) insert it into your database.
Related
I need to get a table with simple html dom parser, clean it (remove attr and spaces), then output it again.
My question is, how can i loop through with PHP and output the TH and TD in one sequence?
At the moment it will handle the TH as a TD but i like to have the TH set correctly.
<table>
<tbody>
<tr>
<th>Date1</th>
<th>Date2</th>
</tr>
<tr>
<td>01.01.2019</td>
<td>05.01.2019</td>
</tr>
</tbody>
</table>
require('simple_html_dom.php');
$html = file_get_html( "template.html" );
$table = $html->find('table', 0);
$rowData = array();
foreach($table->find('tr') as $row) {
$keeper = array();
foreach($row->find('td, th') as $cell) {
$keeper[] = $cell->plaintext;
}
$rowData[] = $keeper;
}
echo '<table">';
foreach ($rowData as $row => $tr) {
echo '<tr>';
foreach ($tr as $td)
echo '<td>' . $td .'</td>';
echo '</tr>';
}
echo '</table>';
I tried something with the foreach, but i think i need something else.
ty for your ideas.
greet;s
You can do it like this:
require('simple_html_dom.php');
$html = file_get_html( "template.html" );
$table = $html->find('table', 0);
$rowData = array();
foreach($table->find('tr') as $row) {
$keeper = array();
foreach($row->find('td, th') as $cell) {
$data = array();
$data['tag'] = $cell->tag; //stored Tag and Plain Text
$data['plaintext'] = $cell->plaintext;
$keeper[] = $data;
}
$rowData[] = $keeper;
}
echo '<table>';
foreach ($rowData as $row => $tr) {
echo '<tr>';
foreach ($tr as $td)
echo '<'.$td['tag'].'>' . $td['plaintext'] .'</'.$td['tag'].'>'; // Tag used
echo '</tr>';
}
echo '</table>';
You would need to store the type of cells, it would be enough to store them at row level as they should all be the same. Then when you rebuild the rows, use this type as the cell type to create...
foreach($table->find('tr') as $row) {
$keeper = array();
foreach($row->find('td, th') as $cell) {
$keeper[] = $cell->plaintext;
}
// Type is the 2nd & 3rd chars of html - <th>content</th> gives th
// Store type and cell data as two elements of the rowData
$rowData[] = ["type" => substr($cell,1,2), "cells" => $keeper];
}
echo '<table>';
foreach ($rowData as $row => $tr) {
echo '<tr>';
// Loop over the cells of the row
foreach ($tr["cells"] as $td)
// Output row type as the element type
echo "<$tr[type]>" . $td ."</$tr[type]>";
echo '</tr>';
}
echo '</table>';
I want to copy some table to my website, but only with few specific columns
Here is my code
<?php
require('simple_html_dom.php');
$html = file_get_html('http://pomorskifutbol.pl/liga.php?id=970');
$table = $html->find('table', 0);
$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>';
?>
whole table
And this prints whole table, I want to get only few columns, for example only "M.", "Drużyna", "M", "PKT"
How extract only this columns from this table?
You can change the entry:
foreach($row->find('td') as $cell) {
to
foreach($row->find('td') as $columnNumber => $cell) {
and make an if before the filling of the $flights array, e.g
//somewhere in code
$columnNumbers = [ 0, 2, 3,4, 7];
// int the foreach loop
if ( in_array( $columnNumber, $columnNumbers ) ) {
$flight[] = $cell->plaintext;
}
PS. Powodzenia :)
I have a MySQL database for a website am building, i have displayed contents of a table in the MySQL database on my page using the PHP code shown below. What i wanted to know was if there was a way to Merge rows of a column with the same value.
note: i don't want the changes to occur in the database but just on the html table displayed on the page
here is the code i used
$arr = array();
while($row = mysql_fetch_row($result)) {
if (empty($arr[$row[0]])) $arr[$row[0]] = array();
$arr[$row[0]] [$row[1]] [$row[2]] [$row[3]] [$row[4]] [$row[5]] [$row[6]][] = $row[7];
}
foreach ($arr as $key => $subordinatearr) {
echo '<tr>';
echo '<td rowspan="', count($subordinatearr), '">', $key, '</td>';
echo '<td>', $subordinatearr[0], '</td>';
echo '<td>', $subordinatearr[1], '</td>';
echo '<td>', $subordinatearr[2], '</td>';
echo '<td>', $subordinatearr[3], '</td>';
echo '<td>', $subordinatearr[4], '</td>';
echo '<td>', $subordinatearr[5], '</td>';
echo '<td>', $subordinatearr[6], '</td>';
if (count($subordinatearr) > 1) {
for ($i = 1; $i < count($subordinatearr); $i++) {
echo '</tr><tr><td>', $subordinatearr[$i], '</td>';
}
}
echo '</tr>';
}
Please change this script to
while($row = mysql_fetch_row($result)) {
echo "<tr>";
echo "<td>".$row['Location']."</td>";
echo "<td>".$row['Vessel ']."</td>";
echo "</tr>";
}
Store the result in an array.
$arr = array();
while($row = mysql_fetch_row($result)) {
if (empty($arr[$row[0]]) $arr[$row[0]] = array();
$arr[$row[0]][] = $row[1];
}
Then put the array using rowspan like as follows:
foreach ($arr as $key => $subordinatearr) {
echo '<tr>';
echo '<td rowspan="', count($subordinatearr), '">', $key, '</td>';
echo '<td>', $subordinatearr[0], '</td>';
if (count($subordinatearr) > 1) {
for ($i = 1; $i < count($subordinatearr); $i++) {
echo '</tr><tr><td>', $subordinatearr[$i], '</td>';
}
}
echo '</tr>';
}
If data is too big, this may cause an memory error.
I did not understand this part clearly - "Merge rows of a column with the same value."
If you don't want to show duplicate records of a column, you can do it in two ways -
Option 1
Keep the value of specific column in an array. Before displaying that column check if the value exists in the array using is_array(). If exists, then skip, otherwise show and add into the array.
Option 2
While making the query, use DISTINCT(col_name) to get only the unique values.
I have a MySQL query that returns data using PHP.
My problem is that I need to populate my html table (with 3 columns) with the data returned by the query but the data should be populated Columnwise.
Like first the first column should be populated .. then the second and finally the third one.
Also I would like to know that is it possible to do so for an unlimited set of data?
Following the screen shot of the desired layout
you can use while.
<table>
$sql=mysql_query("select * from table");
while($s=mysql_fetch_array($sql))
{
$x=$s["x"];
<tr>
<td ><?php echo $x; ?></td>
<td ><?php echo $y; ?></td>
<td ><?php echo $z; ?></td>
</tr>
}
</table>
guybennet's answer is correct but if you want it to work for an unlimited amount of columns you could do this: (I also threw in some column headers for readability)
echo '<table>';
$counter = 0;
$result = mysql_query("select * from table");
while($row = mysql_fetch_array($result)) {
$counter++;
if($counter == 1) {
echo '<tr>';
foreach($row as $key => $val) {
echo "<th>$key</th>";
}
echo '</tr>';
}
echo '<tr>';
foreach($row as $key => $val) {
echo "<td>$val</td>";
}
echo '</tr>';
}
echo '</table>';
Also of course you should use mysqli or PDO I'm just showing a quick example.
If you don't care about how it's organized, you can try something like this:
echo "<table><tr>";
$i=0;
while($row = mysql_fetch_array($sql))
{
echo "<td>".$row[0]."</td>\n";
$i++;
if($i==3)
{
echo "</tr>\n<tr>";
$i=0;
}
}
echo "</tr></table>";
Otherwise, I'd suggest putting it all into an array and then putting it into the table.
$data = array();
$result = mysql_query("SELECT * FROM your_table");
while ($row = mysql_fetch_array($result)) {
$data[] = $row;
}
$itemsAmount = count($data);
$ceilAmount = ($itemsAmount - $itemsAmount % 3) / 3;
$lastAmount = $itemsAmount % 3;
$firstArray = array_slice($data, 0, $itemsAmount);
$secondArray = array_slice($data, 0, $itemsAmount*2);
$thirdArray = array_slice($data, 0, $lastAmount);
$output = "<table>";
foreach ($data as $key => $value) {
$output .= "<tr>";
$output .= "<td>" . $firstArray[$key][0] . "</td>";
$output .= "<td>" . $secondArray[$key][0] . "</td>";
if (empty($thirdArray[$key])) {
$str = '';
} else {
$str = $thirdArray[$key][0];
}
$output .= "<td>" . $str . "</td>";
$output .= "</tr>";
}
$output .= "</table>";
echo $output;
You need to check all the results returned, then print them as you won't print in order:
First get an array with all the results
<?php
$sql= mysql_query('SELECT * FROM table');
$num= mysql_affected_rows($sql);
$items = array();
$i=0;
while($item=mysql_fetch_array($sql)){
$items[++$i]=$item['data'];
}
Now start printing
int $num_rows;
$num_rows=$num%3;
echo '<table>';
for ($i=0;$i<$num_rows;$i++){
echo '<tr>';
for ($j=0;$j<2,$j++){
$n=$i+1+($j*$num_rows);
if($items[$n]!==null)
echo '<td>'.$items[$n].'</td>';
else
echo '<td></td>';
}echo '</tr>';
}echo'</table>';
?>
I have an Array of Arrays and Want to create a Tabular data layout. This is not the exact code, as how their generated is quite the cluster (Coming from COBOL interaction) but this should give me enough to make it work if someone can think of a clean way to code this.
array(
Array(847.0, 97010, 11)
Array(847.0, 97012, 10)
Array(847.1, 97010, 08)
Array(847.1, 97012, 14)
)
So I want to put these into a Table that looks something like
97010 97012
847.0 11 10
847.1 08 14
The first 2 elements of the arrays will always be the two axis and the 3rd the contents of the table.
Any Suggestions? thanks!
$table = array();
$columns = array();
// copy the array (x, y, value) into a table
// keeping track of the unique column names as we go
foreach ($dataSet as $point) {
// provided sample data used floats, ensure it is a string
$x = strval($point[0]);
$y = strval($point[1]);
$data = $point[2];
if (!isset($table[$x])) {
$table[$x] = array();
}
$table[$x][$y] = $data;
// quick and dirty style 'unique on insert'
$columns[$y] = true;
}
// switch the column names from title => true to just titles
$columns = array_flip($columns);
// Display the table
echo '<table>';
// Header row
echo '<tr>';
echo '<th> </th>';
foreach ($columns as $columnTitle) {
echo '<th>' . $columnTitle . '</th>';
}
echo '</tr>';
// Bulk of the table
foreach ($table as $rowTitle => $row) {
echo '<tr>';
echo '<th>' . $rowTitle . '</th>';
foreach ($columns as $columnTitle => $junk) {
if (isset($row[$columnTitle]) {
echo '<td>' . $row[$columnTitle] . '</td>';
} else {
// Handle sparse tables gracefully.
echo '<td> </td>';
}
}
echo '</tr>';
}
echo '</table>';
From what I understood:
$arr[847.0][97010] = '11';
$arr[847.0][97012] = '10';
$arr[847.1][97010] = '08';
$arr[847.1][97012] = '14';
And you may create a table:
$result = "<table>";
foreach($arr as $row_key => $row) {
$result .= "<tr>";
foreach($row as $column_key => $data) {
$result .= "<td>".$data."</td>";
}
$result .= "</tr>";
}
$result .= "</table>";
echo $result;