I have:
$l = array(
array("A"=>0.1,"B"=>1,"C"=>1,"D"=>1),
array("A"=>0.1,"B"=>1,"C"=>0,"D"=>2),
);
$h = array('h1','h2');
1-How Can I map(l,h)to this?
$result= $array(
'h1'=> array("A"=>0.1,"B"=>1,"C"=>1,"D"=>1),
'h1'=> array("A"=>0.1,"B"=>1,"C"=>0,"D"=>2),
);
2- So I I can display(present html table)
-------------------
| A | B | C | D
-------------------
h1 |
-------------------
h2 |
--------------------
My trying to output:
<table>
<tr><td>A</td><td>B</td><td>C</td><td>D</td></tr>
foreach($result as $key=>$value){
<tr>
<tr>
}
<table>
Anybody can help me?
Mapping the array as you propose is easy:
$mapped = array_combine($h, $l);
Then:
// Print the top "headers" row
$columns = array_keys(reset($l));
echo '<table><tr><td> </td>';
foreach ($columns as $column) {
echo '<td>'.$column.'</td>';
}
echo '</tr>';
// Print each data row
foreach ($mapped as $key => $row) {
echo '<tr><td>'.$key.'</td>';
foreach ($row as $cell) {
echo '<td>'.$cell.'</td>';
}
echo '</tr>';
}
// Done!
echo '</table>';
$result = array_combine($h, $l);
This works bc there are implied numeric indexes for the array elements, as you can see if you var_dump($h) or var_dump($l)
<table>
<?php
echo "<tr>";
echo "<td> </td>";
foreach(array_keys($l[0]) as $letter)
echo "<td>$letter</td>"; //A,B,C,D
echo "</tr>";
foreach($result as $h_key=>$innerArr)
{
echo "<tr><td>$hkey</td>"; //h1,h2
foreach($innerArr as $key=>$val)
echo "<td>$val</td>"; //0.1, 1, etc.
echo "</tr>";
}
?>
</table>
Related
<?php
$bookrec=array(
'book1'=>array('callno'=>123005,'price'=>number_format(1380,2),'desc'=>'Attack on Titan Anthology'),
'book2'=>array('callno'=>123006,'price'=>number_format(844,2),'desc'=>'Binge'),
'book3'=>array('callno'=>123004,'price'=>number_format(598,2),'desc'=>'A Work in Progress'),
'book4'=>array('callno'=>123003,'price'=>number_format(668,2),'desc'=>'The Amazing Book is Not on Fire: The World of Dan and Phil'),
'book5'=>array('callno'=>123002,'price'=>number_format(760,2),'desc'=>'Children of Eden: A Novel')
);
$sumbook=array();
echo "<table>";
echo "<tr><b>
<td>Call Number</td><td>Price</td><td>Book Title</td>
</b></tr>";
while(list($booknum,$rec)=each($bookrec)){
echo "<tr>";
foreach($rec as $data){
echo "<td>". $data."<br></td>";
foreach($data as $k => $val){
if(array_key_exists($val,$sumbook))
$sumbook[$val]['price']=$sumbook[$val]['price']+$data['price'];
else if($k == 'desc' && $k == 'callno')
$sumbook[$val] = $data;
}
}
}
echo "<br></tr>";
echo "</table>";
echo "TOTAL AMOUNT: Php ".$sumbook;?>
I am finishing this code for next week. Also, I am a student and just beginning in PHP. My problem is that I am trying to output the sum of ['price'] in the given array but it gives me this error:
Invalid argument supplied for foreach
I also tried various ways on solving this problem but I don't really get it. Please help. :) Thanks!!
<?php
$bookrec=array(
'book1'=>array('callno'=>123005,'price'=>number_format(1380,2),'desc'=>'Attack on Titan Anthology'),
'book2'=>array('callno'=>123006,'price'=>number_format(844,2),'desc'=>'Binge'),
'book3'=>array('callno'=>123004,'price'=>number_format(598,2),'desc'=>'A Work in Progress'),
'book4'=>array('callno'=>123003,'price'=>number_format(668,2),'desc'=>'The Amazing Book is Not on Fire: The World of Dan and Phil'),
'book5'=>array('callno'=>123002,'price'=>number_format(760,2),'desc'=>'Children of Eden: A Novel')
);
$sumbook=array();
echo "<table>";
echo "<tr><b>
<td>Call Number</td><td>Price</td><td>Book Title</td>
</b></tr>";
foreach($bookrec as $key1=>$rec )
{
echo "<tr>";
foreach($rec as $key2=>$data)
{
echo "<td>". $data."<br></td>";
if($key2="price")
{
$total+=$data;
}
}
echo "</tr><br>";
}
echo "<br></tr>";
echo "</table>";
echo "TOTAL AMOUNT: Php ".$sumbook;?>
I think you have lots of looping error just follow this code and also understand the how to iterate the array using foreach
$sumbook=array();
$total =0;
echo "<table>";
echo "<tr><b>
<td>Call Number</td><td>Price</td><td>Book Title</td>
</b></tr>";
foreach($bookrec as $key1=>$rec )
{
echo "<tr>";
foreach($rec as $key2=>$data)
{
echo "<td>". $data."<br></td>";
if($key2="price")
{
$total+=$data;
}
}
echo "</tr><br>";
}
echo "</table>";
echo "TOTAL AMOUNT: Php ".$total;
?>
Your first foreach loop had a wrong variable name with the one you initialized as your array.
foreach($bookrec as $data){
}
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>.
<?php
echo '<table>';
for($i=1;$i<=12;$i++)
{
echo '<tr>';
while($row5=mysql_fetch_array($result5))
{
if($row5[3]=='monthly')
echo '<td>'.$row5[3].'</td>';
else if($row5[3]=='quarterly')
echo '<td rowspan="3">'.$row5[3].'</td>';
else if($row5[3]=='halfyearly')
echo '<td rowspan=""="6">'.$row5[3].'</td>';
else
echo '<td rowspan="12">'.$row5[3].'</td>';
}
echo '</tr>';
}
echo '</table>';
?>
This code is printing only one row instead of 12 rows. Please help me. I am doing this for managing student fees. I am stuck at the logic.
Create an array with sql result before :
$data = array();
while( $row5 = mysql_fetch_array($result5) )
$data[] = $row5;
Then replace this : while($row5=mysql_fetch_array($result5))
foreach ( $data as $row5 ) {
if($row5[3]=='monthly')
echo '<td>'.$row5[3].'</td>';
// ...
}
PS : Use mysqli_* instead of mysql_* which is deprecated
I have a code like this.
Code:
<?php
$book_query = mysql_query("select * from book_master')");
while($book_query_fetch = mysql_fetch_assoc($book_query)){
echo "<pre>";
print_r($book_query_fetch);
echo "</pre>"
}
?>
Output:
Array
(
[Book_Name] => Book1
[Book_ID] => 123
)
Array
(
[Book_Name] => Book2
[Book_ID] => 124
)
Expected Output: (in a table)
Book Name Book_ID
Book1 123
Book2 124
How can I achieve this?
EDIT:
The header part is a dynamic load. so i need the table header also in a loop
I don't know where you stuck doing that, but you can do below,
echo "<table>";
$i = 0;
while($row = mysql_fetch_assoc($book_query))
{
if($i == 0){
$columns = array_keys($row);
echo "<th>";
foreach($columns as $column){
echo "<td> $column</td>";
}
echo "</th>";
}
echo'<tr>';
echo '<td>'.$row['Book_Name'].'</td>';
echo '<td>'.$row['Book_ID'].'</td>';
echo '</tr>';
$i++;
}
echo "</table>";
Waring: Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
Try this-
<?php
$book_query = mysql_query("select * from book_master')");
echo "<table>";
echo"<tr><td>Book Name</td><td>Book_ID</td></tr>";
while($book_query_fetch = mysql_fetch_assoc($book_query)){
echo"<tr><td>".$book_query_fetch['Book_Name']."</td><td>".$book_query_fetch['Book_ID']."</td></tr>";
}
echo "</table>";
?>
Along with Rikesh's code, Use the array_keys() function. this will fetch all the keys of the subset array.
So you can fetch the keys also dynamically.
Hope this helps you.
you can do this
<table>
<?php
$book_query = mysql_query("select * from book_master')");
$book_query_fetch = mysql_fetch_assoc($book_query); ?>
<th>
<td><?php echo $book_query_fetch['book_name']; ?></td>
<td><?php echo $book_query_fetch['Book_ID']; ?> </td>
</th>
<?php while($book_query_fetch){ ?>
<tr>
<td><?php echo $book_query_fetch['Book_Name']; ?></td>
<td><?php echo $book_query_fetch['Book_ID']; ?></td>
</tr>
<?php } ?>
</table>
Try this
while($row = mysql_fetch_assoc($book_query))
{
echo'<tr><th>'.
$columns = array_keys($row);
foreach($columns as $column){
echo "<td> $column</td>";
}
.'</th></tr><tr>';
echo '<td>'.$row['Book_Name'].'</td>';
echo '<td>'.$row['Book_ID'].'</td>';
echo '</tr>';
}
echo "</table>";
//try this
<?php
$book_query = mysql_query("select * from book_master')");
while($book_query_fetch = mysql_fetch_assoc($book_query)){
echo "<pre>";
$a="<table><tr>";
foreach ($book_query_fetch as $key=>$value){
$a="<th>".$key."</th>"
}
exit;
}
$a="</tr>"
while($book_query_fetch = mysql_fetch_assoc($book_query)){
$a="<tr>";
foreach ($book_query_fetch as $key=>$value){
$a="<td>".$value."</td>"
}
$a="</tr>";
}
$a="</table>"
echo $a;
?>
Hi all i'm not very experienced with programming so this is probably easy to achieve.
I am pulling data from a mysql table using php i want to display the output like so:
$row[1] $row[2] Line break
$row[3] $row[4] Line break
$row[5] $row[6] Line break
And so on
Any help would be appreciated thanks
$user_table = select_table( "users", "user_name ASC" );
$rows = array( );
while( $row = mysqli_fetch_assoc( $user_table ) ) {
$rows[] = $row;
}
$user_groups = array_chunk( $rows, 2 );
foreach ( $user_groups as $user_group ) {
echo "<tr>";
foreach( $user_group as $row ) {
echo "<td class=\"user-box\">{$row["user_name"]}</td>";
}
echo "</tr>";
}
This is my working code. Thanks Tim
Loop through your results and start counting again after 2.
here's an example using a simple array and a table so you can see how it works
<?php
$results = array('one','two','three','four');
echo '<table border="1">';
foreach(array_chunk($results,2) as $row) {
echo '<tr>';
foreach($row as $value) {
echo '<td>'.$value.'</td>';
}
echo '</tr>';
}
echo '</table>';