Mysql fetch assoc with PHP MYSQL in a table format - php

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;
?>

Related

JSON TO HTML ROW USING PHP

I need to convert JSON into a list using PHP, Tried code below but cannot make it work
$json=file_get_contents("http://feeds.mse.mk/service/FreeMSEFeeds.svc/ticker/JSON/8BA941D0-D6E6-44BD-8D8B-47FDB7A563FA");
$data = json_decode($json);
if (count($data->stand)) {
// Open the table
echo "<table>";
// Cycle through the array
foreach ($data->stand as $idx => $stand) {
// Output a row
echo "<tr>";
echo "<td>$stand->AvgPrice</td>";
echo "<td>$stand->Description </td>";
echo "</tr>";
}
// Close the table
echo "</table>";
}
And I want to show list as here (not as a table):
http://prntscr.com/no1479
your all code is right but you can use stand class that is wrong your class is GetTickerJSONResult and so change the class stand to GetTickerJSONResult.
try this modified code..
<?PHP
$set =json_decode($json);
if (count($set->GetTickerJSONResult)) {
echo "<table>";
foreach ($set->GetTickerJSONResult as $idx => $stand) {
echo "<tr>";
echo "<td>$stand->AvgPrice</td>";
echo "<td>$stand->Description </td>";
echo "</tr>";
}
echo "</table>";
}
?>
It does not work because in $data->stand there is nothing.

Finding the sum of specific multidimensional array php

<?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){
}

Foreach associative array table

**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>';
}

While loop inside for loop not working while fetching mysql rows

<?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

Map array and display it?

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>&nbsp</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>

Categories