I currently can parse a html table using simplehtmldom. The problem I have is that the program prints the entire table in one block.
How would I print row by row?
How could I limit the rows to just the ones with times? (see http://www.masjid-umar.org/downloads/timetable_apr.htm)
Below is the code I am currently using:
<?php
include('simple_html_dom.php');
$dom = file_get_html('http://www.masjid-umar.org/downloads/timetable_apr.htm');
$table = $dom->find('table',0);
$rows = $table->children(0)->children();
foreach($rows as $row)
foreach($row->children() as $column) {{
if(!empty($column->innertext)) {
echo $column->innertext . '<br />' . PHP_EOL;
}
}
}
The following is printed http://pastebin.com/cAMECf9f
You could store the individual cells in a multi-dimensional array and loop through that to output it, but if you just want the table, you can skip the loops and do something like:
$table = $dom->find('table',0);
echo $table->save();
Just search for the times:
foreach($dom->find('tr') as $tr){
if(!preg_match('/\d+\.\d+/', $tr->text())) continue;
echo $tr->text() . "\n";
}
Related
I found the following in the bootstrap documentation:
Create equal-width columns that span multiple rows by inserting a .w-100 where you want the columns to break to a new line every 3rd column. - https://getbootstrap.com/docs/4.0/layout/grid/#equal-width-multi-row
Is there a better way to do this?
echo "<div class=\"row\">";
$rows = [];
$rows = ["apples", "oranges", "bananas", "pears", "grapes", "watermelons"];
foreach ($rows as $value) {
echo "<div class=\"col-md-4\">" . $value . "</div>";
}
echo "</div>"; //End of row
I am attempting to echo a sting containing a variable already stored in a variable. Essentially I am building a class that can dynamically build tables based on a changing number of columns. I need get my db field names into the foreach loop before the foreach loop that has my db results, and then iterate over them in the loop. The problem is I have to store them in a loop prior to the db results loop; which is not recognizing it as a variable and just giving me a plain text '$row['myVar']'.
How can I get this to recognize the variable in the second loop?
$sqlVarNames = explode(', ', $sqlVar);
foreach ($sqlVarNames as $columnVar) {
$finalColumnVars .= '<td>\'. $row[\''.$columnVar.'\'] .\'</td>';
}
and then into my second loop
foreach ($sqlResult as $row) {
echo '<tr>';
echo $finalColumnVars;
echo '</tr>';
}
I tried all sorts of escape sequences on my $finalColumnVars and can't seem to get it to output the variable instead on plain text.
Here is what I get with the above code
<td>'. $row['client_name'] .'</td>
Is this possible?
$sqlVarNames = explode(', ', $sqlVar);
foreach ($sqlVarNames as $columnVar) {
$finalColumnVars .= '<td>'.$row[$columnVar].'</td>';
}
here is how you can merge these two loop and get the desired.
$sqlVarNames = explode(', ', $sqlVar);
foreach ($sqlResult as $row) {
echo '<tr>';
foreach ($sqlVarNames as $columnVar) {
echo '<td>'. $row[$columnVar] .'</td>';
}
echo '</tr>';
}
Is there a clean way (no hardcoding) in which I can dump all the contents of a database directly to HTML using PHP?
I don't want to run queries for every table and step through the results, outputting them. I need this for testing purposes, so the tables aren't that big.
Any hints?
I want this done directly in my php file, where the rest of the test takes place, so that I may compare with the sample. I need to do this automatically, so can't really use tools like PHPMyAdmin.
Something like this ought to work:
<?php
function dump_mysql_results($mysql_table){
$query = mysql_query("SELECT * FROM `$table` WHERE 1",[your connection]) or die(mysql_error());
if (!mysql_num_rows($query)){die("No rows in $table");}
while($r=mysql_fetch_array($query)){
if (!isset($html)){
$keys = array_keys($r);
$html = "<tr>";
foreach($keys as $key){
$html .= "<th>$key</th>";
}
$html .= "</tr>";
}
$html .= "<tr>";
foreach($r as $value){
$html .= "<td>$value</td>";
}
$html .= "</tr>";
}
return "<table>".$html."</table>";
}
//ADDING a loop to dump the whole db:
$tables = mysql_list_tables ( 'database name',$link_identifier) or die(mysql_error());
while($r=mysql_fetch_array($tables)){
echo dump_mysql_results($r[0]);
}
?>
Use SHOW TABLES to get the list of tables, then iterate through them normally to select all the rows and display in HTML.
See http://dev.mysql.com/doc/refman/5.5/en/show-tables.html
What about mysqldump ?
<?php
exec('mysqldump --user=DBuser --password=DBpass --host=localhost --compact --xml DBname > file.xml');
?>
Then use simpleXML to convert xml to HTML
Currently, $selection outputs the following: MIN(Bale_ID), MIN(Incoming_Moisture) which is exactly what it should be outputting (they're names from another table). However, when I put $selection into the mysql_query $data1, it seems to just be reading the last value (MIN(Incoming_Moisture)) and only displays the results for that. How do I get the query to read the entire array of elements in $selection? Thank you!!
while ($row1 = mysql_fetch_array($fieldnames1)) {
$fields = $row1['fields1'];
$explode = explode(',',$fields);
if ($row1) {
for ($i=0; $i<$minrows; $i++) {
if ($i<$minrows-1){
$comma = ", ";
}
else {
$comma = "";
}
//$selection = "MIN(".$explode[$i].")".$comma;
//echo $selection;
$data1 = mysql_query("SELECT MIN(".$explode[$i].")".$comma." from data WHERE (fchmitimestamp LIKE CONCAT(#year,'%',#month,'%',#day,'_________'))");
$all1 = mysql_num_fields($data1); //return # of columns; for some reason is returning "1" right now.
while ($row2 = mysql_fetch_array($data1)) {
for ($col=0; $col<$all1; $col++) {
echo $all1;
echo "<td>Min: " . $row2[$col] . "</td>";
}
echo "</tr>";
}
}
}
}
echo "</table>";
Look at the order of operations in your code:
loop {
... fetch data ...
... assign results to $data1 ...
}
Nowhere in your loop do you output or save the results you've got in $data1, so each iteration of the loop overwrites the results of the previous iteration - in other words, only the LAST iteration's results will be stored.
you are running the query once per for loop cycle (1 field at a time) and since first ones yields in SQL error because of the trailin comma, these will not be echoed except the last one.
-- notice the error in first query
SELECT MIN(Bale_ID), from data WHERE (fchmitimestamp LIKE CONCAT(#year,'%',#month,'%',#day,'_________'))
SELECT MIN(Incoming_Moisture) from data WHERE (fchmitimestamp LIKE CONCAT(#year,'%',#month,'%',#day,'_________'))
use var_dump($selection) instead of echo $selection to see yourself
php newbie here..I need some PHP help ideas/examples on how to import data from a delimited text file and map them into html tables. The data should populate and be mapped under its proper header. There are instances also where each record doesn't have all the values and if no data, then we can leave it null (See sample records). I would also create a table row entry for each record.
For example, the input/source file has these entries: (they are prefixed by a number to represent the header in the html table. So data from "1-MyServer" is "server4.mra.dev.pp1" and should be under table header "Server" for example. There are instances also where the record doesn't have all the values (1-7) (See below):
1-MyServer=server4.mra.dev.pp1;2-MyLogdate=Wed Aug 11 2010;3-MyDataset=dbip.pp1;4-MyStartTime=01:00:03;5-MyDuration=00:36:09;6-MySize=41.54 GB;7-MyStatus=Succeeded;
1-MyServer=server9.mra.dev.kul;2-MyLogdate=Wed Aug 11 2010;3-MyDataset=gls202.kul_lvm;5-MyDuration=06:20:33;7-MyStatus=Succeeded;
1-MyServer=server9.mra.dev.kul;2-MyLogdate=Wed Aug 11 2010;3-MyDataset=gls101.aie_lvm;4-MyStartTime=01:00:02;
Here is a copy of my html table that I would need it to map it too:
(Also, I would not have to populate record for "2-MyLogdate" into a header)
<table id="stats">
tr>
<th>Server</th>
<th>Set</th>
<th>Start</th>
<th>Duration</th>
<th>Size</th>
<th>Status</th>
</tr>
<tr>
<td>server4.mel.dev.sp1</td>
<td>dbip.sp1</td>
<td>01:00:03</td>
<td>00:36:09</td>
<td>41.54 GB</td>
<td>Succeeded</td>
</tr>
</table>
So what I really need is a system to map these appropriately.
How would I write this in php?? Thanks!
It's all about finding the patterns in your files. In your example, it's rather easy:
[number]-[column name]=[value];
The main problem I see is that you have redundant information: the number of the column, and the columns themselves, which are repeated for every row. You can parse them away, though. It depends on what you expect from your program: will the columns order always be the same? Will there always be the same columns? How should you react to unknown columns?
Here's a quick example of what you could do, using regular expressions. This example assumes that column names are all the same, and that you want to display them all, and that they'll always be in the same order. In other words, I'm doing it the easy way.
$data = array();
$lines = file("my/log/file.log");
// this will parse every line into an associative array, discarding the header number
foreach ($lines as $line)
{
// populate $matches with arrays where indices are as follows:
// 0: the whole string (0-Foo=bar;)
// 1: the column number (0)
// 2: the column name (Foo)
// 3: the value (Bar)
preg_match_all("/([0-9])-([^=]+)=([^;]+);/", $line, $matches, PREG_SET_ORDER);
$lineData = array();
foreach ($matches as $information)
$lineData[$information[2]] = $information[3];
$data[] = $lineData;
}
$keys = array_keys($data[0]); // you can also set this yourself
// for instance, $keys = array('MyServer', 'MyDataset'); would only display those two columns
echo '<table><tr>';
foreach ($keys as $column)
echo '<th>' . $column . '</th>';
echo '</tr>';
foreach ($data as $row)
{
echo '<tr>';
foreach ($keys as $column)
echo '<td>' . (isset($row[$column]) ? $row[$column] : '') . '</td>';
echo '</tr>';
}
echo '</table>';
something like this
$logarr = file("log.txt");
foreach ($logarr as $s) {
$s = str_replace(";","&",$s);
$a = array();
parse_str($s,$a);
echo "<tr>\n";
if (isset($a['1-MyServer'])) echo $a['1-MyServer']; else echo " "
if (isset($a['2-MyLogdate'])) echo $a['2-MyLogdate']; else echo " "
// and so on
echo "</tr>\n";
}