$result = mysql_query("SELECT * FROM productlist1 order by pdesc ASC");
while($row = mysql_fetch_array($result))
$sold=$row['psold'];
$left=$row['pleft'];
$all=$left + $sold;
{
echo '<tr>';
echo '<td>'.$row['pcode'].'</td>';
echo '<td>'.$row['pdesc'].'</td>';
echo '<td>'.$row['date'].'</td>';
echo '<td>'.$row['time'].'</td>';
echo '<td><div align="center">'.$row['psold'].'</div></td>';
echo '<td><div align="center">'.$row['pleft'].'</div></td>';
echo '<td><div align="center">'.$row['pprice'].'</div></td>';
echo '<td><div align="center">'.$all.'</div></td>';
i want to add the pleft and psold, the $all doesn't work at all... what would i do with this code?
You have a syntax error. You need to place following 3 lines inside brackets:
$sold=$row['psold'];
$left=$row['pleft'];
$all=$left + $sold;
like:
while($row = mysql_fetch_array($result))
{
$sold=$row['psold'];
$left=$row['pleft'];
$all=$left + $sold;
echo '<tr>';
....
You have to put opening braces for while loop just after the while statement like.
while($row = mysql_fetch_array($result))
{
$sold=$row['psold'];
$left=$row['pleft'];
$all=$left + $sold;
...............
Put your addition inside while loop,
because outside while loop it doesn't work, because it does not get proper value
So , try like this
<?php
$result = mysql_query("SELECT * FROM productlist1 order by pdesc ASC");
while($row = mysql_fetch_array($result))
{
$all=$row['psold'] + $row['pleft'];
echo '<tr>';
echo '<td>'.$row['pcode'].'</td>';
echo '<td>'.$row['pdesc'].'</td>';
echo '<td>'.$row['date'].'</td>';
echo '<td>'.$row['time'].'</td>';
echo '<td><div align="center">'.$row['psold'].'</div></td>';
echo '<td><div align="center">'.$row['pleft'].'</div></td>';
echo '<td><div align="center">'.$row['pprice'].'</div></td>';
echo '<td><div align="center">'.$all.'</div></td>';
Try this.
$result = mysql_query("SELECT * FROM productlist1 order by pdesc ASC");
while($row = mysql_fetch_array($result))
{
$sold=$row['psold'];
$left=$row['pleft'];
$all=$left + $sold;
echo '<tr>';
echo '<td>'.$row['pcode'].'</td>';
echo '<td>'.$row['pdesc'].'</td>';
echo '<td>'.$row['date'].'</td>';
echo '<td>'.$row['time'].'</td>';
echo '<td><div align="center">'.$row['psold'].'</div></td>';
echo '<td><div align="center">'.$row['pleft'].'</div></td>';
echo '<td><div align="center">'.$row['pprice'].'</div></td>';
echo '<td><div align="center">'.$all.'</div></td>';
echo '</tr>';
}
Try parsing the values to integer like
$all=intval($left)+intval($sold);
Yeah one more thing your while is misplaced!!
Related
I'm trying to list multiple record from MySQL Database using PHP but when grouping it returns only one record from database, below is my PHP code I'm using.
<?php
include "connection.php";
$query = "select * from lectureupload GROUP BY submittedOn";
$result=mysqli_query($conn, $query);
while ($row = mysqli_fetch_array($result)) {
echo '<table class="table">';
echo '<thead>';
echo '<td><strong>ID</strong></td>';
echo '<td><strong>Title</strong></td>';
echo '<td><strong>Semester</strong></td>';
echo '<td><strong>Teacher</strong></td>';
echo '<td><strong>Lecture Link</strong></td>';
echo '<td><strong>Submitted On</strong></td>';
echo '</thead>';
echo '<div class="section-header">';
echo '<br>';
echo '<h3>'.$row['submittedOn'].'</h3>';
echo '</div>';
echo '<tr >';
echo '<td>'.$row['id'].'</td>';
echo '<td>'.$row['title'].'</td>';
echo '<td>'.$row['semester'].'</td>';
echo '<td>'.$row['teacherName'].'</td>';
echo '<td>'.$row['lectureLink'].'</td>';
echo '<td>'.$row['submittedOn'].'</td>';
echo '</tr>';
echo '</table>';
}
mysqli_close($conn);
?>
The current Result it gives me is somewhat like that,
My Database table is this.
Solution to this problem will be highly appreciated.
Regards.
You can't use GROUP BY clause to retrieve all records from that table. GROUP BY will always return one row per GROUP BY CONDITIONAL_COLUMN. I will suggest you below modification:
<?php
include "connection.php";
//ORDER BY submittedOn will make sure the records retrieved in ordered as per submittedOn.
$query = "select * from lectureupload ORDER BY submittedOn";
$result = mysqli_query($conn, $query);
$submitted_on_keys = array();
while ($row = mysqli_fetch_array($result)) {
if (!in_array($row['submittedOn'], $submitted_on_keys)) {
if (count($submitted_on_keys) > 0) {
//It means we have already created <table> which needs to be closed.
echo '</table>';
}
$submitted_on_keys[] = $row['submittedOn'];
echo '<table class="table">';
echo '<thead>';
echo '<td><strong>ID</strong></td>';
echo '<td><strong>Title</strong></td>';
echo '<td><strong>Semester</strong></td>';
echo '<td><strong>Teacher</strong></td>';
echo '<td><strong>Lecture Link</strong></td>';
echo '<td><strong>Submitted On</strong></td>';
echo '</thead>';
echo '<div class="section-header">';
echo '<br>';
echo '<h3>' . $row['submittedOn'] . '</h3>';
echo '</div>';
}
echo '<tr >';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['title'] . '</td>';
echo '<td>' . $row['semester'] . '</td>';
echo '<td>' . $row['teacherName'] . '</td>';
echo '<td>' . $row['lectureLink'] . '</td>';
echo '<td>' . $row['submittedOn'] . '</td>';
echo '</tr>';
}
if (count($submitted_on_keys) > 0) {
//There is last <table> which needs to be closed.
echo '</table>';
}
mysqli_close($conn);
?>
You should group it in your PHP code rather than your SQL.
Something like this:
$lectures = [];
$result = mysqli_query($conn, "select * from lectureupload");
while ($row = mysqli_fetch_array($result)) {
$lectures[$row['submittedOn']][] = $row;
}
foreach ($lectures as $submittedOn => $rows) {
echo '<table class="table">';
echo '<thead>';
echo '<td><strong>ID</strong></td>';
echo '<td><strong>Title</strong></td>';
echo '<td><strong>Semester</strong></td>';
echo '<td><strong>Teacher</strong></td>';
echo '<td><strong>Lecture Link</strong></td>';
echo '<td><strong>Submitted On</strong></td>';
echo '</thead>';
echo '<div class="section-header">';
echo '<br>';
echo '<h3>'.$submittedOn.'</h3>';
echo '</div>';
foreach ($rows as $row) {
echo '<tr >';
echo '<td>'.$row['id'].'</td>';
echo '<td>'.$row['title'].'</td>';
echo '<td>'.$row['semester'].'</td>';
echo '<td>'.$row['teacherName'].'</td>';
echo '<td>'.$row['lectureLink'].'</td>';
echo '<td>'.$row['submittedOn'].'</td>';
echo '</tr>';
}
echo '</table>';
}
I use this but only one will display...
please help me I need to display same name in a table..
<?php
$id=$_SESSION['SESS_MEMBER_ID'];
$result = mysql_query("SELECT * FROM user_reservation WHERE id = '$id' ");
while($row = mysql_fetch_array($result)){
echo '<tr class="record" id="'.$row['status'].'">';
echo '<td style="border-left: 1px solid #C1DAD7;">'.$row['confirmation'].'</td>';
echo '<td>'.$row['firstname'].' '.$row['lastname'].'</td>';
echo '<td><div align="right">'.$row['email'].'</div></td>';
echo '<td><div align="left">';
echo '</div></td>';
echo '<td><div align="right">'.$row['status'].'</div></td>';
echo '<td><div align="center">delete</div></td>';
echo '</tr>';
}
?>
Use session_start() at the top
<?php
session_start()
$id=$_SESSION['SESS_MEMBER_ID'];
$result = mysql_query("SELECT * FROM user_reservation WHERE id = '$id' ");
while($row = mysql_fetch_array($result)){
echo '<tr class="record" id="'.$row['status'].'">';
echo '<td style="border-left: 1px solid #C1DAD7;">'.$row['confirmation'].'</td>';
echo '<td>'.$row['firstname'].' '.$row['lastname'].'</td>';
echo '<td><div align="right">'.$row['email'].'</div></td>';
echo '<td><div align="left">';
echo '</div></td>';
echo '<td><div align="right">'.$row['status'].'</div></td>';
echo '<td><div align="center">delete</div></td>';
echo '</tr>';
}
?>
Im creating a basic website that will show 10 different tv programmes.
I have the 10 different programmes stored in the database. Im able to retriev the 10 programmes but they all appear in a column.
I was wondering if theres a way to have then appear 5 in a row?
I have tried basic CSS but i cant seem to get it working
Here is the code i have so far:
<?php
$results = $mysqli->query("SELECT * FROM programmes ORDER BY ProgrammeName ASC");
if ($results) {
while($obj = $results->fetch_object())
{
echo '<br>';
echo '<div class="tvProgs">';
echo '<form method="post" id = "books" action="cart_update.php">';
echo '<div class="progImage"><img src="images/'.$obj->Image.'"></div>';
echo '<div class="progTitle"><h3>'.$obj->ProgrammeName.'</h3>';
echo '</form>';
echo '</div>';
}
}
?>
I was wondering if theres a way to achive that i want or will i have to try something else?
anything will help.
Thanks!
Try to put them in a table:
<?php
$results = $mysqli->query("SELECT * FROM programmes ORDER BY ProgrammeName ASC");
if ($results) {
$i=0;
echo '<table><tr>';
while($obj = $results->fetch_object())
{
echo '<td>';
echo '<div class="tvProgs">';
echo '<form method="post" id = "books" action="cart_update.php">';
echo '<div class="progImage"><img src="images/'.$obj->Image.'"></div>';
echo '<div class="progTitle"><h3>'.$obj->ProgrammeName.'</h3>';
echo '</form>';
echo '</div>';
echo '</td>';
$i++;
if ($i == 5) {
echo '</tr><tr>';
}
}
echo '</tr></table>';
}
?>
You can start from:
$i=0;
echo '<br>';
while($obj = $results->fetch_object())
{
echo '<div class="tvProgs">';
echo '<form method="post" id = "books" action="cart_update.php">';
echo '<div class="progImage"><img src="images/'.$obj->Image.'"></div>';
echo '<div class="progTitle"><h3>'.$obj->ProgrammeName.'</h3>';
echo '</form>';
echo '</div>';
if (($i++) == 5) { echo '<br>'; $i=0; }
}
UPDATE CSS
.tvProgs {
float:left;
width:200px;
display:block;
}
This will put them in a table 5 in each row just like you asked for.
<?php
$results = $mysqli->query("SELECT * FROM programmes ORDER BY ProgrammeName ASC");
if ($results) {
$i = 0;
echo '<table>';
while($obj = $results->fetch_object())
{
if ($i == 0) {
echo '<tr>';
}
echo '<td>';
echo '<div class="tvProgs">';
echo '<form method="post" id = "books" action="cart_update.php">';
echo '<div class="progImage"><img src="images/'.$obj->Image.'"></div>';
echo '<div class="progTitle"><h3>'.$obj->ProgrammeName.'</h3>';
echo '</form>';
echo '</div>';
echo '</tr>';
$i++;
if ($i == 5) {
echo '</tr>';
$i = 0;
}
}
if ($i != 0) {
echo '</tr>';
}
echo '</table>';
}
?>
I'm trying to style the output of each echo.
Ideally I'd like to use <span class=""> </span> for each echo, but I'm not too sure how to achieve this.
$result = mysql_query("SELECT * FROM Blog");
while($row = mysql_fetch_array($result))
{
echo $row['Date'];
echo $row['Title'];
echo $row['Message'];
echo "<img src='".$row['Image']."'/>";
}
mysql_close($con);
$result = mysql_query("SELECT * FROM Blog");
while($row = mysql_fetch_array($result))
{
echo "<span class=\"myclass\">$row['Date']</span>";
echo "<span class=\"myclass\">$row['Title']</span>";
echo "<span class=\"myclass\">$row['Message']</span>";
echo "<img src='".$row['Image']."'/>";
}
mysql_close($con);
or, much nicer, in a table:
$result = mysql_query("SELECT * FROM Blog");
echo "<table>"
while($row = mysql_fetch_array($result)) {
echo "<tr>"
echo "<td>$row['Date']</td>";
echo "<td>$row['Title']</td>";
echo "<td>$row['Message']</td>";
echo "<td><img src='".$row['Image']."'/></td>";
echo "</tr>"
}
echo "</table>"
mysql_close($con);
You then can style each row and column with a class.
Try this:
$prepend = "<span class=''>";
$append = "</span>";
$result = mysql_query("SELECT * FROM Blog");
while($row = mysql_fetch_array($result))
{
echo $prepend.$row['Date'].$append;
echo $prepend.$row['Title'].$append;
echo $prepend. $row['Message'].$append;
echo $prepend."<img src='".$row['Image']."'/>".$append;
}
mysql_close($con);
I'd create a function that does this:
function decorated_echo($text) {
echo '<span class="myclass">' . $text . '</span>';
}
This way, you don't have to repeat this everytime you want this behaviour.
You are guessing right, just add required html in the echo:
echo '<span class="yourclass"><img src="'.$row['Image'].'" /></span>';
or you can just put inline style if no css file is loaded:
echo '<span style="color:red;"><img src="'.$row['Image'].'" /></span>';
Hi I have been using this code to fetch data from mysql database and displaying it in column format i.e
a
b
c
What i want is to show 4 names per row i.e
a b c d
e f g....
Can anybody help me in this regard
Thankyou.
Here is my code
<?php
$result = mysql_query("SELECT * FROM Persons");
echo "<table border='1'>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Name'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
Use modulus operator %
$i = 0;
while($row = mysql_fetch_array($result))
{
$i++;
if ($i % 4 == 0) {
echo "<tr>";
}
echo "<td>" . $row['Name'] . "</td>";
if ($i % 4 == 0) {
echo "</tr>";
}
}
As a bonus, you've got syntax error in your code. Look at this line
echo "<table border='1'>;
which
should be
echo "<table border='1'>";
$cnt = 0;
while($row = mysql_fetch_array($result))
{
if($cnt%4==0) echo "<tr>";
echo "<td>" . $row['Name'] . "</td>";
$cnt++;
}
<?php
$con = mysql_connect("localhost","user","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM Persons");
?>
<table border='1'>
<tr>
<?php
$count=1;
while($row = mysql_fetch_array($result))
{
if($count%4==0)
{ ?>
</tr><tr>
<?php }
<td><?php echo $row['Name']; ?></td>
$count++;
}
?>
</tr>
</table>
<?php
mysql_close($con);
?>