format and output all the data from a database table - php

I'm trying to make an page where all the things in the table in the database are displayed on the page. But in a nice format like something ebay where it shows the picture on the left and the title in the middle. Whereas I just have a list.
I can only seem to find tutorials which just echo out all the information which I don't think I can style around to make it look like what I want. I also tried using a variable to store it in, but I only get the last thing in my database showing up.
Here is some of my code:
$query = mysql_query("SELECT * FROM BoatsForRent") or die("Error Searching");
$count = mysql_num_rows($query);
if ($count == 0) {
$output = 'No results found';
} else {
while ($row = mysql_fetch_array($query))
{
$id = $row['boatId'];
$title = $row['title'];
$type = $row['type'];
$address = $row['address'];
$city = $row['city'];
$postcode = $row['postcode'];
$rooms = $row['rooms'];
$decks = $row['decks'];
$price = $row['price'];
$title = $row['title'];
echo "$title<br>";
}
}

To style the output of the table, you can do some CSS like this:
strong {background: #0ff; text-align: right; display: inline-block; width: 50px;}
span {display: inline-block;}
And you need to give your HTML as:
echo "<strong>Title</strong> <span>$title</span><br>";
Preview
Fiddle: http://jsfiddle.net/praveenscience/WBh52/

Use divs, spans or tables to display your data in any way you wish to. Here is an e.g.
<table boder="0" cellspacing="5" cellpadding="5">
<tr>
<td>Boat ID</td>
<td>Title</td>
<td>Type</td>
<td>Address</td>
<td>City</td>
<td>Post Code</td>
<td>Rooms</td>
<td>Decks</td>
<td>Price</td>
</tr>
<?php
$id = $row['boatId'];
$title = $row['title'];
$type = $row['type'];
$address = $row['address'];
$city = $row['city'];
$postcode = $row['postcode'];
$rooms = $row['rooms'];
$decks = $row['decks'];
$price = $row['price'];
$count =mysql_num_rows($query);
if($count == 0){
$output = 'No results found';
} else {
while ($row = mysql_fetch_array($query))
{
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['title']; ?></td>
<td><?php echo $row['type']; ?></td>
<td><?php echo $row['address']; ?></td>
<td><?php echo $row['city']; ?></td>
<td><?php echo $row['postcode']; ?></td>
<td><?php echo $row['rooms']; ?></td>
<td><?php echo $row['decks']; ?></td>
<td><?php echo $row['price']; ?></td>
</tr>
<?php
}
?>
</table>
Alternatively you can put complete table structure inside echo ''. You can now use css either external or inline as per you wish.

You need to put the data into html tags. Here are just some very basic examples. Then you need to use CSS to add styles.
$query = mysql_query("SELECT * FROM BoatsForRent") or die("Error Searching");
$count = mysql_num_rows($query);
if ($count == 0) {
$output = 'No results found';
} else {
while ($row = mysql_fetch_array($query))
{
$id = $row['boatId'];
$title = $row['title'];
$type = $row['type'];
$address = $row['address'];
$city = $row['city'];
$postcode = $row['postcode'];
$rooms = $row['rooms'];
$decks = $row['decks'];
$price = $row['price'];
$title = $row['title'];
echo "<h1>{$title}</h1>";
echo "<h2>{$type}</h2>";
echo "<h3>{$address}, {$city} {$postcode}</h3>";
echo "<p>This boat has {$rooms} rooms and {$decks} decks!</p>";
echo "<p>The selling price is ${$price}</p>";
}
}

The code you have regarded should be in your head section then the output of while should be changed to arrays:
...
while ($row = mysql_fetch_array($query))
{
$id[] = $row['boatId'];
$title[] = $row['title'];
$type[] = $row['type'];
$address[] = $row['address'];
$city[] = $row['city'];
$postcode[] = $row['postcode'];
$rooms[] = $row['rooms'];
$decks[] = $row['decks'];
$price[] = $row['price'];
$title[] = $row['title'];
}
...
In the body section of your page choose good design for a grid or table and use for loop to populate the data:
<table>
<tr>
<td>ID</td>
<td>Title</td>
....
<?php for ($i =0; $i < count($id); $i++){ ?>
<tr>
<td><?php echo $id[$i];?></td>
<td><?php echo $title[$i];?></td>
....
<?php } ?>
</table>

Related

echo first row of data into page title

I have a table of contacts and a search form the returns the contact searched for. All work fine as it should. But is it possible to show an instance of the first row only in a separate div or span at top of page? I have seen this done in MS Access, is it possible in php? Here is my search query but I have no idea how to echo or show the first row only into a title or heading div.
$sql = "SELECT * FROM people";
if( isset($_GET['search']) ){
$name = mysqli_real_escape_string($con, htmlspecialchars($_GET['search']));
$name2 = mysqli_real_escape_string($con, htmlspecialchars($_GET['search2']));
$sql = "SELECT * FROM people WHERE first_name ='$name' AND last_name= '$name2'";
}
$result = $con->query($sql);
?>
<?php
while($row = $result->fetch_assoc()){
?>
<tr>
<td><?php echo $row['people_id']; ?></td>
<td><?php echo $row['first_name']; ?></td>
<td><?php echo $row['last_name']; ?></td>
<td><?php echo $row['address']; ?></td>
<td><?php echo $row['contact']; ?></td>
</tr>
}
I just would do it like this (updated answer):
$tableData = [];
$divData = "";
while ($row = $result->fetch_assoc()) {
// empty tableData --> first row
if (empty($tableData)) {
// do your special for the first row here (div)
$divData = "hello world! ".$row['first_name']." ".$row['last_name'];
}
$tableData[] ="<tr>
<td>".$row['people_id']."</td>
<td>".$row['first_name']."</td>
<td>".$row['last_name']."</td>
<td>".$row['address']."</td>
<td>".$row['contact']."</td>
</tr>";
}
echo "<div>".$divData."</div>";
echo "<table>";
echo implode("\n", $tableData);
echo "</table>";

PHP query not returning all results

I'm trying to display a table of dates, albums and reviews made by users. The date and review show up with no problem, but at the moment my query is only displaying the first CD Title in the list.
Table 'cdreview' has columns 'CDID', 'userID', 'reviewDate', 'reviewText'.
Table 'cd' has 'CDTitle', so I've used a natural JOIN to link the 2 tables by the CDID, but I can't display the correct CDTitle.
Any help would be extremely grateful.
<?php
require_once 'database_conn.php';
$userid = $_SESSION['userSession'];
$sql = "SELECT * FROM cdreview JOIN cd WHERE '$userid'=cdreview.userID ORDER BY reviewDate ASC";
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
$row = mysqli_fetch_assoc($result);
$date = $row['reviewDate'];
$album = $row['CDTitle'];
$review = $row['reviewText'];
$cdid = $row['CDID'];
?>
<tr align='center'>
<td><?php echo $date;?></td>
<td><?php echo $album;?></td>
<td><?php echo $review;?></td>
<td><a href="edit_review.php?id=<?php echo $cdid;?>">Edit</td>
<td><a href="album.php?id=<?php echo $cdid;?>">Delete</td>
</tr>
</table>
You have to iterate through results :
while ($row = mysqli_fetch_assoc($result) ){
$date = $row['reviewDate'];
$album = $row['CDTitle'];
$review = $row['reviewText'];
$cdid = $row['CDID'];
// print stuff
}
mysqli_fetch_assoc is returning a result set.
You have to loop through this result set and handle each result separately.
while ($row = mysqli_fetch_assoc($result)) {
$date = $row['reviewDate'];
$album = $row['CDTitle'];
$review = $row['reviewText'];
$cdid = $row['CDID'];
?>
<tr align='center'>
<td><?php echo $date; ?></td>
<td><?php echo $album; ?></td>
<td><?php echo $review; ?></td>
<td><a href="edit_review.php?id=<?php echo $cdid; ?>">Edit</td>
<td><a href="album.php?id=<?php echo $cdid; ?>">Delete</td>
</tr>
<?php
}

getting value of array check box and textbox in table

i'm a beginner in php and i'm hoping that somebody here can help me in this.
i have a table that has a check box to select an item and a text box to indicate the quantity the person wants to borrow. i was wondering how i can retrieve both these data and then save them in my database.
here is a part of my code:
<td width="30">
<input id="optionsCheckbox" class="uniform_on" name="selector[]" type="checkbox" value="<?php echo $id; ?>">
</td>
<td><?php echo $row['item_code']; ?></td>
<td><?php echo $row['item_name']; ?></td>
<td align="center">
<img class="img-rounded" src="<?php echo $row['item_image'];?>" border="0" onMouseOver="showtrail('<?php echo $row['item_image'];?>','<?php echo $row['item_code'].": ".$row['item_name'];?> ',200,5)" onMouseOut="hidetrail()"></a></td>
<td><?php echo $row['item_quantity'] - $row['item_consumption']; ?> <?php echo $row['unit']; ?></td>
<td><input type="text" name="consume[]" pattern="[0-9]{1,4}"/></td>
here's what i have so far:
$id=$_POST['selector'];
$consume= $_POST['consume'];
$N = count($id);
for($i=0; $i < $N; $i++)
{
$query = $conn->query("select * from item where item_id ='$id[$i]'")or die(mysql_error());
$row = $query->fetch();
$code = $row['item_code'];
$name = $row['item_name'];
echo $code; echo $name;
echo $id;
}
$x = count($consume);
for($y=0; $y < $x; $y++)
{echo $consume;
}
the echos are just for checking if the data goes through. i'm just making sure they do before i make a query for table insertion.
what i'm trying to do is to post them on another page, display them in a table (a la shopping cart) and then make the borrower fill out a form with his details.
ok, using gul's answer below, this is what i did:
$id = $_POST['selector'];
$consume = $_POST['consume'];
//array var for getting the values
$ids = ''; $consumes='';
//loop the posted array
for($i=0;$i<count($id);$i++)
{
$ids .= $id[$i].',';
$consumes.="'".$consume[$i]."',";
}
//remove the last comma
$ids = substr($ids,0,-1);
$consumes = substr($consumes,0,-1);
$query = $conn->query("select * from item where item_id IN($ids)")or die(mysql_error());
//table element
echo "<table border='1' style='border-collapse:
collapse;border-color: silver;'>";
echo "<tr style='font-weight: bold;'>";
echo "<td width='150' align='center'>Item Code</td>";
echo "<td width='150' align='center'>Item Name</td>";
echo "<td width='150' align='center'>Quantity</td>";
echo "</tr>";
//get data in table
$row = $query->fetch();
$code = $row['item_code'];
$name = $row['item_name'];
$table ="";
$table.="<tr>";
$table.="<td>".$code."</td>";
$table.="<td>".$name."</td>";
$table.="<td>".$consumes."</td>";
$table.="</tr>";
$table.="</table>";
//echo the table
echo $table;
however, when i select multiple items, only the last one shows up, but the consumes show up like this:
consume shows in one cell with single quotes and comma. help?
Try like this:
//first get the post
$id = $_POST['selector'];
$consume = $_POST['consume'];
//array var for getting the values
$ids = ''; $consumes='';
//loop the posted array
for($i=0;$i<count($id);$i++)
{
$ids .= $id[$i].',';
//$consumes.="'".$consume[$i]."',";
}
//remove the last comma
$ids = substr($ids,0,-1);
$query = $conn->query("select * from item where item_id IN($ids))or die(mysql_error());
//table element
$table = "<table border='1'>";
$table.="<tr>";
$table.="<th>Code</th><th>Name</th>";
$table.="</tr>";
//get data in table
$row = $query->fetch();
$code = $row['item_code'];
$name = $row['item_name'];
$table.="<tr>";
$table.="<td>".$code."</td>";
$table.="<td>".$name."</td>";
$table.="</tr>";
$table.="</table>";
//echo the table
echo $table;
ok, so i tried this and it worked. thanks to gul for helping me.
$id = $_POST['selector'];
$consume = $_POST['consume'];
//array var for getting the values
$data = array();
//loop the posted array
for($i=0;$i<count($id);$i++)
{
$row = array('selector'=>$id[$i],'consume'=>$consume[$i]);
//push in array
array_push($data,$row);
$query = $conn->query("select * from item where item_id ='$id[$i]'")or die(mysql_error());
$row = $query->fetch();
$code = $row['item_code'];
$name = $row['item_name'];
while ($row = $query->fetch()) {
$id = $row['id'];
}
?>
<tr>
<td><?php echo $code; ?></td>
<td><?php echo $name; ?></td>
<td><?php echo $consume[$i]; ?></td>
<td></td>
</tr>
<?php } ?>
</tbody>
</table>

Displaying field wise records from mysql by php?

Hi i have table in mysql like this:
to show this by php like this:
now first i get and print all gradeid on top by this
$qry = "select * from grademaster";
$result= mysql_query($qry,$link);
$nro=mysql_num_rows($result);
while($row=mysql_fetch_array($result))
{
$gradeid = $row['gradeid'];
echo "<th> $gradeid </th>";
$grdid[] = $gradeid; ////////takes all grades in array
}
and then getting all details from the below table
and but i can't able to show records like on top how can i show this plz help
Just copy and paste it instead of your code, then run script in browser.
You'll see the table. Prettify it via CSS and you are done.
I've tested it on my local server and it seems to work.
$qry = "select * from grademaster";
$result = mysql_query($qry, $link);
$nro = mysql_num_rows($result);
$table = array();
$rowNum = 0;
while ($row = mysql_fetch_array($result)) {
$table[$rowNum]['ordid'] = $row['ordid'];
$table[$rowNum]['orddate'] = date('m/d/Y', strtotime($row['orddate']));
$table[$rowNum]['1001'] = intval($row['gradeid']) == 1001 ? $row['ordqty'] : '';
$table[$rowNum]['1002'] = intval($row['gradeid']) == 1002 ? $row['ordqty'] : '';
$table[$rowNum]['1003'] = intval($row['gradeid']) == 1003 ? $row['ordqty'] : '';
$table[$rowNum]['1004'] = intval($row['gradeid']) == 1004 ? $row['ordqty'] : '';
$table[$rowNum]['1005'] = intval($row['gradeid']) == 1005 ? $row['ordqty'] : '';
$rowNum++;
}
?>
<table>
<tr>
<th>ordid</th>
<th>orddate</th>
<th>1001</th>
<th>1002</th>
<th>1003</th>
<th>1004</th>
<th>1005</th>
</tr>
<?php foreach ($table as $row): ?>
<tr>
<td><?php echo $row['ordid']; ?></td>
<td><?php echo $row['orddate']; ?></td>
<td><?php echo $row['1001']; ?></td>
<td><?php echo $row['1002']; ?></td>
<td><?php echo $row['1003']; ?></td>
<td><?php echo $row['1004']; ?></td>
<td><?php echo $row['1005']; ?></td>
</tr>
<?php endforeach; ?>
</table>

How to make 4 items in a row using while loop?

I'm trying to display my products from the database. I'm trying to split it using the if 4%=0, but I can't get it to display 4 items in a row.
Here is my codes:
<table>
<tr>
<?php
include "connect_to_mysql.php";
$split = 0;
$display_all = mysql_query("SELECT * FROM products
ORDER BY FIELD(category, 'ipad','iphone','others')");
while($row=mysql_fetch_array($display_all)) {
$id = $row['id'];
$product_code = $row['product_code'];
$title = $row['title'];
$price = $row['price'];
$split++;
if ($split%4==0){
echo '</tr><tr>';
}
?>
<td>
<table class="normal_text" align="center">
<tr>
<td><a href="product.php?product_id=<?php echo $id?>">
<img width="200px" height="133px" src="products/<?php echo $product_code?>.jpg" />
</a></td>
</tr>
<tr>
<td align="center" style="font-weight:bold"><?php echo $title;?></td>
</tr>
<tr>
<td align="center">$<?php echo $price;?></td>
</tr>
</table>
</td>
<?php
}
?>
</tr>
</table>
You've got the PHP logic before, rather than inside your HTML table output.
Try reorganizing like this:
<?php
include "connect_to_mysql.php";
$split = 0;
$display_all = mysql_query("SELECT * FROM products
ORDER BY FIELD(category, 'ipad','iphone','others')");
?>
<table class="normal_text" align="center">
<tr>
<?php
while($row=mysql_fetch_array($display_all)) {
$id = $row['id'];
$product_code = $row['product_code'];
$title = $row['title'];
$price = $row['price'];
// output product details -- note use of quotes to include PHP vars
$rowHTML = "<td><a href='product.php?product_id=$id'>";
$rowHTML .= "<img width='200px' height='133px' src='products/$product_code.jpg' />";
$rowHTML .= "</a><br/>";
$rowHTML .= "<strong>$title</strong>";
$rowHTML .= "$price</td>";
echo $rowHTML;
$split++;
if ($split%4==0){
echo '</tr><tr>';
}
}
?>
</tr>
</table>
while($row=mysql_fetch_assoc($display_all)) {
$id = $row['id'];
$product_code = $row['product_code'];
$title = $row['title'];
$price = $row['price'];
if ($split % 4 === 0) echo "\n<tr>";
?>
// ONLY <td> CODE </td> here. NO <tr> and NO </table>
<?php
if ($split % 4 === 3) echo "</tr>";
$split++;
}
?>
You are starting and ending at same time and then putting table for each.
You want to start <tr> when %4==0 and end </tr> when %4==3.
Rather than use modulus (%), just add a second variable called $cols, assign it to the amount of columns you want, and compare it. For example:
$rows = 1;
$cols = 4;
$display_all = mysql_query("SELECT * FROM products ORDER BY FIELD(category, 'ipad','iphone','others')");
while($row=mysql_fetch_array($display_all)) {
$id = $row['id'];
$product_code = $row['product_code'];
$title = $row['title'];
$price = $row['price'];
$split++;
if ($rows==$cols){
echo '</tr><tr>';
$rows = 1;
}

Categories