How to show data in html table using PHP - html

This is my code the table appears up with the header and everything but the row with the td tag data shows nothing at all
echo "<p></p>";
//display name of the page and some random text
echo "<h2>".$pagename."</h2>";
if($_POST['h_prodid'] > 0){
$newprodid = $_POST['h_prodid'];
$reququantity =$_POST['quantity'];
$_SESSION['basket'][$newprodid]=$reququantity;
echo "<p>Your basket has been updated</p>";
} else {
echo "<p>Existing basket</p>";
}
echo "<table border = '1'>
<tr><th>Product Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Subtotal</th>
</tr>";
echo "<td>".$reququantity."</td>";
echo "</table>";

The reason your data is NOT displaying in the html is because it is NOT in a tr ( table row ) - simply a floating table cell
if( $_SERVER['REQUEST_METHOD']=='POST' ){
echo "<h2>".$pagename."</h2>";
if( isset( $_POST['h_prodid'],$_POST['quantity'] ) ){
if( $_POST['h_prodid'] > 0 ){
$newprodid = $_POST['h_prodid'];
$reququantity =$_POST['quantity'];
$_SESSION['basket'][$newprodid]=$reququantity;
echo "<p>Your basket has been updated</p>";
}else{
echo "<p>Existing basket</p>";
}
echo "
<table border = '1'>
<tr>
<th>Product Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Subtotal</th>
</tr>
<tr>
<td> </td>
<td> </td>
<td>{$reququantity}</td>
<td> </td>
</tr>
</table>";
}
}

Related

SQL fetch in tables

First of all. I apologize for the poor English. I'm also sorry for the novice level of this question.
I would like to put my SQL results in a table, and add a new row for every result.
if(mysql_num_rows($raw_results) > 0){
while($results = mysql_fetch_array($raw_results)){
echo "<p><b>".$results['FNAME']."</b> " .$results['LNAME']." ".$results['AGE']."</p>";
}
}
So I would like [FNAME] in a own TD, [LNAME] in it's on TD and the same with [AGE].
Thank you in advance!
Just put <tr> around each row of data, <td> around each column, and put everything inside <table>.
if (mysql_num_rows($raw_results) > 0) {
echo "<table><tr><th>First Name</th><th>LAst Name</th><th>Age</th></tr>";
while ($results = mysql_fetch_assoc($raw_results) {
echo "<tr><td>{$results['FNAME']}</td><td>{$results['LNAME']}</td><td>{$results['AGE']}</td></tr>";
}
echo "</table>";
}
use this code :
<table>
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<?php
if(mysql_num_rows($raw_results) > 0){
while($results = mysql_fetch_array($raw_results)){
echo "<tr><td>" . $results['FNAME'] . "</td><td>" . $results['LNAME'] . "</td><td>" .$results['AGE']."</td></tr>";
}
}
?>
</tbody>
</table>

Highlight a row in table html which has a value exceed the avg

I have a table which is looks like this.
What I want to do is, I want to Highlight (Yellow COLOR) which row has USAGE PER 1K Higher than the AVERAGE. The result should looks like this.
Here is my code to make a row been hightlight when exceed value of average, but I cannot change the value if($row['duh']>'0.77') to the if($row['duh']>'$avg') because it will hightlight all the rows.
$no = 1;
while ($row = mysqli_fetch_array($result))
{
$month1=strtotime($row['month']);
$month=date('m-Y',$month1);
$store_name=$row['store_name'];
$netamt1=$row['netamt'];
$netamt11=number_format($netamt1,2);
$usage1=$row['monusage'];
$usage11=number_format($usage1,2);
$answer=$usage1/$netamt1*1000;
$answer1=number_format($answer,2);
$total+=$answer1;
$avg=$total/$no;
$duh = number_format($row['duh'],2);
echo "<tr>";
echo "<td>".$no."</td>";
echo "<td>".$store_name."</td>";
echo "<td>".$date2111."</td>";
echo "<td>".$netamt11."</td>";
echo "<td>".$usage11."</td>";
if($row['duh']>'0.77') {// 0.77 i cannot change to $avg, because it will hightlight all the rows become yellow
echo "<td style='background-color: #FFFF00;'>".$duh."</td>";}
else {
echo "<td>".$duh."</td>";
}
echo "</tr>";
$no++;
}?>
</tbody>
<tfoot>
<tr>
<th colspan="5">TOTAL</th>
<th><?=($total)?></th>
</tr>
<tr>
<th colspan="5">AVERAGE</th>
<th><?=number_format($avg,4)?></th>
</tr>
</tfoot>
You can add avegage as another column in query result and compare the values against it, e.g.:
SELECT no, outlet, date, sales, usage, usage_per_1k, AVG(usage_per_1k) AS avg
FROM table
WHERE condition;
AND then, add the following to highlight:
if($row['duh']>$row['avg']) {
hightlight all the rows become yellow
echo "<td style='background-color: #FFFF00;'>".$duh."</td>";
}
After keep trying, now i figure out how to hightlight a row depending on the condition average, if Higher than average, the row will Yellow, but I make it RED now. If Lower than average, the color will not change. Im using script in <body> tag, and add class in <tr> tag, then do function.
$no = 1;
while ($row = mysqli_fetch_array($result))
{
$month1=strtotime($row['month']);
$month=date('m-Y',$month1);
$store_name=$row['store_name'];
$netamt1=$row['netamt'];
$netamt11=number_format($netamt1,2);
$usage1=$row['monusage'];
$usage11=number_format($usage1,2);
$answer=$usage1/$netamt1*1000;
$answer1=number_format($answer,2);
$duh = number_format($row['duh'],2);
$total+=$duh;
$avg=$total/$no;
echo "<tr class='y_n';>";
echo "<td>".$no."</td>";
echo "<td>".$store_name."</td>";
echo "<td>".$date2111."</td>";
echo "<td>".$netamt11."</td>";
echo "<td>".$usage11."</td>";
$no++;
echo "<td>".$duh."</td>";
echo "</tr>";
} ?>
<script type="text/javascript">
$('#table_id tr.y_n td').each(function() {
if ($(this).text() >=<?=number_format($avg,2)?>) {
$(this).parent().css('background-color', '#fc9c9c');
}
else
$(this).parent().css('background-color', '#e5f5ff');
});
</script>
</tbody>
<tfoot>
<tr>
<th colspan="5">TOTAL</th>
<th><?=($total)?></th>
</tr>
<tr>
<th colspan="5">AVERAGE</th>
<th><?=number_format($avg,4)?></th></tr>
<tr>
<th colspan="6"><center><?php echo $actionstatus?></center></th>
</tr>
</tfoot>
</table>
Here is the Result :

Creating table layout

I have created a php file called search.php. I want to show the result on a table
I want a table structure like this.
__________________________
| |________________|
| |________________|
| |________________|
|________|________________|
|________________|
|________________|
On the left side I want to place a picture which will be obtained from database and to the right I will place information about the student photo.
Could you write a code. Can we separate photo and the information column by a vertical line only.(if yes,HOW ?? )
<table>
<tr>
<td rowspan="4" style="border-right: 1px solid black">
<img src"whatever.png" alt="" />img
</td>
<td>1</td>
</tr>
<tr>
<td>2</td>
</tr>
<tr>
<td>3</td>
</tr>
<tr>
<td>4</td>
</tr>
<tr>
<td rowspan="2">
</td>
<td>5</td>
</tr>
<tr>
<td>6</td>
</tr>
</table>
but really: learn html an get used to the boxed model, that is: use divs
<?php
$sql = "Select FROM * `yourtablename` where `id` = 'theidofthestudent'";
if(!mysql_query($sql)){
die(mysql_error());
} else {
echo "<table border="1px" style="text-align:center;" align="justify">";
echo "<th>Photo of student</th>";
echo "<th>Name of the student</th>";
echo "<th>Student Detials</th>";
while($row = mysql_fetch_array($sql)){
echo "<tr>";
echo "<td style="float:left;">' . $row['student_pic'] . "</td>";
echo '<td style="float:right;">' . $row['student_name'] . "</td>";
echo '<td style="float:right;">' . $row['student_details'] . "</td>";
echo "</tr>"
} echo "</table>";
}
?>

PHP Array doesn't show all the data from database

I have a very basic and simple script that should display records from my database. The problem: it doesn't show all the records. I've tried it even with the most simple mysql
($sql="SELECT * FROM $tbl_name";) but still some records are missing (mostly the first of the list that isn't shown).
So here is my code (it's all on 1 page):
<?php
$host="localhost";
$username="***";
$password="***";
$db_name="***";
$tbl_name="***";
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name WHERE rowNameOne >= 0.01 AND rowNameTwo='2013'";
if ($_GET['sort'] == 'one')
{
$sql .= " ORDER BY one ASC";
}
elseif ($_GET['sort'] == 'two')
{
$sql .= " ORDER BY two ASC";
}
elseif ($_GET['sort'] == 'three')
{
$sql .= " ORDER BY three ASC";
}
elseif($_GET['sort'] == 'four')
{
$sql .= " ORDER BY four ASC";
}
elseif($_GET['sort'] == 'five')
{
$sql .= " ORDER BY five ASC";
}
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>
<body onload="parent.alertsize(document.body.scrollHeight);">
<br />
<table cellspacing="0" cellpadding="0" align="center">
<tr>
<td valign="top" colspan="5">
<font>Titel</font>
</td>
<tr>
<td>Titel one</td>
<td>Titel two</td>
<td>Titel three</td>
<td>Titel four</td>
<td>Titel five</td>
</tr>
<tr>
<td colspan="5" class="noBorder">
<?php
while($rows=mysql_fetch_array($result)){
?>
<a href="pageName.php?id=<? echo $rows['id']; ?>" >
<table width="100%">
<tr>
<td><? echo $rows['rowNameOne']; ?></td>
<td><? echo $rows['rowNameTwo']; ?></td>
<td><? echo $rows['rowNameThree']; ?></td>
<td><? echo $rows['rowNameFour']; ?></td>
<td><? echo $rows['rowNameFive']; ?></td>
</tr>
</table>
<input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>">
<?php
}
?>
</a>
</td>
</tr>
</table>
It's a very basic code, easy as can be I would say, but still it's missing records, not displaying everything that's in the database. What am I doing wrong?
Thanks for the help!
Before you start the loop, you do this:
$rows=mysql_fetch_array($result);
Then the loop condition is:
while($rows=mysql_fetch_array($result)){
So the first result is never shown. I would advice to remove the first statement, since you're not using its results between that statement and the loop.
On a related note, please consider moving to PDO or mysqli.

table with an url image inside header

I have a sortable table in html and looking to put an add.png inside each table header so if a user wants to add another url - they just click the add.png and redirects them to the addurl.php (if they click outside it will still sort the table just like normal). Right now it sorting when I click on the .png and/or outside it. If this something that can't be done, I've also thought about trying to add a final row with [add another site] but have no idea how to work it into the $j++ so it appears at the final row.
what it looks like
[PHP snippet]
<table class="datatable sortable selectable full">
<thead>
<tr class="theader">
<th width="100%">
<b><li><img src="images/logos/googlebuzz-2-icon.png" height="18" border="0" />Google <img src="img/icons/add.png" height="18" border="0" align="right"/></li></b>
</th>
<th>
<b>Coins</b>
</th>
<th>
<b>CPC</b>
</th>
</tr>
</thead>
<tbody>
<?
$site2 = mysql_query("SELECT * FROM `sites` WHERE `user`='{$data->login}' ORDER BY `cpc` DESC");
for($j=1; $site = mysql_fetch_object($site2); $j++)
{
?>
<tr>
<td>
<? if($site->banned == 1){ ?><font color="red"><? }else{ ?><font color="green"><? } echo($site->title); ?></font><span class="edit"></span>
</td>
<td align="right">
<? if($site->points <= 10){ ?><font color="red"><? }else{ ?><font color="green"><? } echo($site->points); ?></font><span class="add"></span>
</td>
<td>
<? echo $site->cpc;?>
</td>
</tr>
<?}?>
</tbody>
</table>
First of all, instead of a for loop, you should use while:
while($site = mysql_fetch_object($site2))
If you want to add a final row, just add it outside the iteration, before </tbody>:
<tbody>
<? while($site = mysql_fetch_object($site2)): ?>
<tr>
<td>
<? if($site->banned == 1){ ?><font color="red"><? }else{ ?><font color="green"><? } echo($site->title); ?></font><span class="edit"></span>
</td>
<td align="right">
<? if($site->points <= 10){ ?><font color="red"><? }else{ ?><font color="green"><? } echo($site->points); ?></font><span class="add"></span>
</td>
<td>
<? echo $site->cpc;?>
</td>
</tr>
<? endwhile; ?>
<tr>
<td colspan="3">
[add another site]
</td>
</tr>
</tbody>
It seems you are using a javascript to do the sorting. I don't know if that can be changed so your add button in the header works.
You can check if it's the final row by checking the actual row against all rows:
$site2 = mysql_query("SELECT * FROM `sites` WHERE `user`='{$data->login}' ORDER BY `cpc` DESC");
$rows = mysql_num_rows($site2);
$i = 0;
And add this in your for loop:
$i++;
if($rows == $i) {
// do something only at the final row
}