Currently I have a PHP 3 level array. How do I display it in a table form? Using print_r I could display out the full array but I need to beautify it to show in a table form. Is it possible?
Sample of the array to be inserted is as shown in another posting: PHP foreach with Nested Array?
So... each level of the array should be an embedded table?
<table>
<?php // FIRST LEVEL
foreach ($myArray as $first_level): ?>
<tr>
<td>The header of the first level, here's some data <?php echo $first_level['some_data']; ?></td>
</tr>
<tr>
<td>
<table>
<?php // SECOND LEVEL
foreach($first_level['second_level'] as $second_level): ?>
<tr>
<td><?php echo $second_level['some_data']; ?></td>
</tr>
<?php endforeach; ?>
</table>
</td>
</tr>
<?php endforeach; ?>
</table>
..and keep repeating the pattern
So many ways to do it, even more so since you didn't provide a template for the output format ....
Let's assume each element $e of the input array $src stands for one row in the table.
Then $e[0] is the string element (one, two, three) and $e[1] is the corresponding array 1,2,3, 4,5,6 or 7,8,9.
Let's put $e[0] into <th>...</th> elements.
foreach( $src as $e ) {
echo '<th>', $e[0], '</th>';
}
and then wrap each element in $e[1] in <td>....</td>.
foreach( $src as $e ) {
echo '<th>', $e[0], '</th>';
foreach($e[1] as $v) {
echo '<td>', $v, '</td>';
}
}
now wrap that into another <tr>...</tr> and you are done
foreach( $src as $e ) {
echo '<tr>';
echo '<th>', $e[0], '</th>';
foreach($e[1] as $v) {
echo '<td>', $v, '</td>';
}
echo "</tr>\r\n";
}
the same thing a bit shorter (see http://docs.php.net/join)
<?php
$src = getData();
foreach( $src as $e ) {
echo '<tr><th>', $e[0], '</th><td>', join('</td><td>', $e[1]), "</td></tr>\n";
}
function getData() {
return array(
array( 'one', array(1,2,3) ),
array( 'two', array(4,5,6) ),
array( 'three', array(7,8,9) )
);
}
the output is
<tr><th>one</th><td>1</td><td>2</td><td>3</td></tr>
<tr><th>two</th><td>4</td><td>5</td><td>6</td></tr>
<tr><th>three</th><td>7</td><td>8</td><td>9</td></tr>
see also: http://docs.php.net/htmlspecialchars
Related
I have an array of result after searching parameters,i want to print that result.while printing the result using foreach loop,i need to check the condition that ,if purchase dates are same, they should be printed in same row.How i can implement this?please help me.Thanks in advance
here is my code: ( i have get the result in controller and then i append the result in view)
foreach($res as $key => $row)
{
$i++;
$billfirst='';
$billend= $row['bill_date'];
if( $billfirst != $billend){
echo "<tr>
<td class='txt'> ".$i."</td>
<td class='txt'> ".$row['store']."</td>
<td class='txt'> ".$row['cust']."</td>
<td class='txt'> ".$row['name']."</td>
<td class='txt'> ".$row['net_amount']."</td>
<td class='txt'> ".$row['bill_date']."</td>
</tr>" ;
}
else {
echo "<tr><td class='txt'> ".$row['name']."</td>
<td class='txt'> ".$row['net_amount']."</td>
<td class='txt'> ".$row['bill_date']."</td>
</tr>";
}
$billfirst == $billend;
echo "</tbody></table>";
}
You need to make additional array to group your rows, example:
$arRows = array();
$billPrevious = "empty";
// save new grouped array
foreach($res as $key => $row) {
$arRows[$row['bill_date']][] = $row;
}
// show groups - in the way you want to group it visually in table
foreach ($arRows as $key => $arGroup) {
echo '<tr>';
$arName = '';
$arStore = '';
foreach ($arGroup as $keyInGroup => $row) {
$arName[] = $row['name'];
$arStore[] = $row['store'];
}
echo '<td>'.implode('<br>', $arName).'</td>';
echo '<td>'.implode('<br>', $arStore).'</td>';
echo '</tr>';
}
I am trying to create an array from this data, but I donĀ“t get it. I tried with the array_merge function, but the array doesn't construct correctly. This is my code, I want to create an array with the different fields of the table.
<?php
require('extractorhtml/simple_html_dom.php');
$dom = new DOMDocument();
//load the html
$html = $dom->loadHTMLFile("http:");
//discard white space
$dom->preserveWhiteSpace = false;
//the table by its tag name
$tables = $dom->getElementsByTagName('table');
//get all rows from the table
$rows = $tables->item(0)->getElementsByTagName('tr');
echo '<input type="text" id="search" placeholder="find" />';
echo '<table id="example" class="table table-bordered table-striped display">';
echo '<thead>';
echo '<tr>';
echo '<th>Date</th>';
echo '<th>Hour</th>';
echo '<th>Competition</th>';
echo '<th>Event</th>';
echo '<th>Chanel</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
// loop over the table rows
foreach ($rows as $row)
{
// get each column by tag name
$cols = $row->getElementsByTagName('td');
// echo the values
echo '<tr>';
echo '<td>'.$cols->item(0)->nodeValue.'</td>';
echo '<td>'.$cols->item(1)->nodeValue.'</td>';
echo '<td>'.$cols->item(3)->nodeValue.'</td>';
echo '<td class="text-primary">'.$cols->item(4)->nodeValue.'</td>';
echo '<td>'.$cols->item(5)->nodeValue.'</td>';
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
?>
You don't need to merge arrays, you just need to push onto a new array to create a 2-dimensional array.
$new_array = array();
foreach ($rows as $row)
{
// get each column by tag name
$cols = $row->getElementsByTagName('td');
// echo the values
echo '<tr>';
echo '<td>'.$cols->item(0)->nodeValue.'</td>';
echo '<td>'.$cols->item(1)->nodeValue.'</td>';
echo '<td>'.$cols->item(3)->nodeValue.'</td>';
echo '<td class="text-primary">'.$cols->item(4)->nodeValue.'</td>';
echo '<td>'.$cols->item(5)->nodeValue.'</td>';
echo '</tr>';
$new_array[] = array(
'date' => $cols->item(0)->nodeValue,
'hour' => $cols->item(1)->nodeValue,
'competition' => $cols->item(3)->nodeValue,
'channel' => $cols->item(5)->nodeValue
);
}
Based on your <th> values, you know which columns contain which values, so it looks like you'd just need to modify the code inside your foreach loop to append the values to an array rather than generating new HTML with them.
foreach ($rows as $row)
{
// get each column by tag name
$cols = $row->getElementsByTagName('td');
$array['date'] = $cols->item(0)->nodeValue;
$array['hour'] = $cols->item(1)->nodeValue;
$array['competition'] = $cols->item(3)->nodeValue;
$array['event'] = $cols->item(4)->nodeValue;
$array['chanel'] = $cols->item(5)->nodeValue;
$result[] = $array;
}
After this loop, $result will be an array of arrays containing the values from the <td>s, where each inner array represents one <tr>.
**Edit: We got there in the end, Thanks guys! It was the HTML tables confusing me.
<tr>
<td><?php echo $row['owner_firstname'];?></td>
<td><?php echo $row['owner_surname'];}?></td>
</tr>**
I am trying to put some information into a table and I don't want to do it using this method...
<tr>
<td><?php echo $rows[0]['owner_firstname'] ; ?></td>
<td><?php echo $rows[0]['owner_surname'] ; ?></td>
<td><?php echo $rows[0]['owner_contantno'] ; ?></td>
</tr>
I want to use a foreach loop but I am struggling to get it working, Each person from the database is [0],[1],[2] etc in my array.
Here is a print_r of my dataset
Array
(
[0] => Array
(
[ID] => LEI12345
[owner_firstname] => Shanel
[owner_surname] => **********
[owner_contantno] => *******
[owner_address] => ********
[band_firstname] => Nathan
[band_lastname] => **********
[band_disability] => *******
[band_emergencycontact] => ********
[band_description] => ************
)
)
$data = $yourDataSet; // your data set here
// check data
if($data) {
foreach($data as $val) {
$str = "";
$str = "<tr>";
$str .= "<td>" . $val['owner_firstname'] . "</td>";
$str .= "<td>" . $val['owner_surname'] . "</td>";
// add other td here if there's more
// end of tr
$str .= "</tr>";
echo $str;
}
}
try this one, i hope this one would help
from your for each you can grab results like this
get your results to an array
$rows = mysql_fetch_array($query);
foreach($rows as $key=> $row){
if(is_array($row))
foreach($row as $id => $val)
echo '<td>'.$val.'</td>';
}
So I want to show in an 'echo' only the people that day which the situation is 'Yes', so let's assume today is only 'John' from my array list that have the situation 'Yes'.
$people = array('John', 'Greg', 'Mike', 'James', 'Jason');
$situation = array('Yes');
$html = str_get_html('<table class="mytab">
<tr>
<th>Name</th>
<th>Situation</th>
</tr>
<tr>
<td>
John
</td>
<td class="s">
<strong>Yes</strong>
</td>
</tr>
<tr>
<td>
Allan
</td>
<td class="s">
<strong>No</strong>
</td>
</tr>
<tr>
<td>
James
</td>
<td class="s">
<strong>No</strong>
</td>
</tr>
</table>');
$table = $html->find('table', 0);
$rowData = array();
foreach($table->find('tr') as $row) {
// initialize array to store the cell data from each row
$content= array();
foreach($row->find('td') as $cell) {
// push the cell's text to the array
$content[] = $cell->plaintext;
}
$rowData[] = $content;
}
echo '<table>';
foreach ($rowData as $row => $tr) {
echo '<tr>';
foreach ($tr as $td)
echo '<td>' . $td .'</td>';
echo '</tr>';
}
echo '</table>';
The code above is going to show:
John Yes
Allan No
James No
It's driving me insane I can't figure it out!!
How can I search in the <td>s and get only the people 'Yes' that day?
Assuming your $html is a valid XML string (meaning each tag closes properly, and has a parent tag, and no text outside tags), do this:
$html="<table>..";
$xml = new SimpleXmlElement($html);
$result=array();
foreach($xml->tr as $tr){
//you have your td, see what it has
$name='';
foreach($tr->td as $td) {
if(isset($td->a))
$name=$td->a;
if(isset($td->strong) && $td->strong=='Yes'){
$result[]=strval($name);
}
}
}
print_r($result); //this will have array with name of those where YES
Array
(
[0] => John
)
EDIT: , if you have a well-known XML format of the html, no need to go and check every td. Here I remove nested loop, and reducing complexity from O(n2) to O(n).
$result=array();
foreach($xml->tr as $tr){
//just in case see if a tag is there
if(isset($tr->td[0]->a))
$name = $tr->td[0]->a;
$td2 = $tr->td[1];
if(isset($td2->strong) && $td2->strong=='Yes'){
$result[]=strval($name);
}
}
print_r($result);
Hi there this is a bit hard to word so I'm trying my best to explain what is happening. Pretty much I have a form where I had a selection box. I had to fill it with the following.
<?
$PROVINCES = array("--" => "---Please Select Provinces---",
"nf"=>"Newfoundland",
"pe"=>"PrinceEdwardIsland",
"nb"=>"New Brunswick",
"ns"=>"Nova Scotia",
"qc"=>"Quebec",
"on"=>"Ontario",
"mb"=>"Manitoba",
"sk"=>"Saskatchewan",
"ab"=>"Alberta",
"bc"=>"British Columbia",
"nt"=>"Northwest Territories");?>
So I did that:
<select name = "province[]" multiple size = "12" <?if ($_SERVER['REQUEST_METHOD'] == 'POST'){if (isset($errorList['province']))
{
echo "class=\"error\"";
}}?>>
<?php foreach($PROVINCES as $key => $value) { ?>
<option value="<?php echo $key ?>"<?= (in_array($key, $_POST['province'] ) )?'selected':'';?>><?php echo $value?></option>
<?php }?>
</select>
Now the next step was to make a table of all the values in $_POST
but the problem is since the values are nf, pe, nb etc it will write those in the table and not PrinceEdwardIsland, New Brunswick, Nova Scotia.
echo '<table border="1" style="width:100%">';
foreach($_POST as $name => $out)
{
echo '<tr>';
echo '<td>';
echo '<strong>';
echo strtoupper($name);
echo '</strong>';
echo '</td>';
echo '<td>';
if (is_array($out))
{
count($_POST);
$arrayOutput = implode(", ", $out);
echo $arrayOutput;
}
else if (strlen($out) <= 0)
{
echo "---None supplied---";
}
else
{
echo $out;
}
echo '</td>';
echo '</tr>';
}
echo '</table>';
But as you can see from here we have multiple different $name from $_POST when we call it in our foreach loop.
And as you can see when I do is_array($out) I implode the array and split it out by ","'s I only need to get the full names for provinces, I had a checkbox for Status and it only has to display what the value in the checkbox was. I'm trying to figure out how I can get the $key of $PROVINCES to replace the imploded values in $_POST['province']
Hopefully I explained it well enough for people to understand.