PHP - getting the first sum number only - php

There are 5 types of prices that have been summed-up in each div using MySQL query and works perfectly.
Then, I wish to get the sum of those 5 total values by giving them each a $_SESSION variable before it sum them all up. It only gets the first price but still sums them up perfectly.
(e.g $extra_price is 25 then if i $extra_price+$extra_price the $total can get 50)
Lets say if (e.g $extra_price is 25 and $decoprice is 10 then i sum $extra_price+$decoprice the $total only show 25) the 10 has been ignore.
Any idea how to get the sum perfectly ?
Below is my code:
<?php
$extra_price = $_SESSION['extra_price'];
$decoprice = $_SESSION['decoprice'];
$foodprice = $_SESSION['foodprice'];
$drinksprice = $_SESSION['drinksprice'];
$venueprice = $_SESSION['venueprice'];
$total = $extra_price+$decoprice+$foodprice+$drinksprice+$venueprice;
?>
<center><b>Total <?php echo $total ?></b></center>

U shouldnt use this syntax
$_SESSION['decoprice'] = $show_pextra['SUM(decoprice)'];
Try to name you SUM(decoprice) and use it to register the session.
select SUM(decoprice) as sum_decoprice from selectdeco where title = '$title'
$_SESSION['decoprice'] = $show_pextra['sum_decoprice '];

You don't need all those extra variables, you can just write it like this:
$total = 0;
$keys = array('extra_price', 'decoprice', 'foodprice', 'drinksprice', 'venueprice');
foreach ($keys AS $key)
{
$total += $_SESSION[$key];
}
Obviously, this is irrelevant, just an issue of style. In any case the + operator will sum things perfectly for you. That's what it does. It sums things. Perfectly.
But the problem is, nobody knows what's in your $_SESSION. Do you? Why don't you try logging it somewhere or at least doing:
print_r($_SESSION);
Then you will see that your session probably doesn't contain what you think it does.

Hey first of all you don't need session here. Simple declare total variable at top of your code and replace each session with total variable with plus. and remove extra variables.
Replace
$_SESSION['extra_price'] = $show_pextra['SUM(price)'];
With
$total += $show_pextra['SUM(price)'];
Do with all session.
Here is your full code:
<?php $total = 0;?>
<tr><!--extra-->
<td style="width:120px;border:2px solid #000;"> Extra Item:</td>
<td style="border:2px solid #000;">
<?php
$read_allextra = mysql_query("select * from selectextra where title = '$title'");
while($show_allextra = mysql_fetch_array($read_allextra))
{
?>
<b>♦ <?php echo $show_allextra['extraitem'] ?></b></br>
<?php
}
?></td>
<td style="width:120px;border:2px solid #000;">
<?php
$read_pextra = mysql_query("select SUM(price) from selectextra where title = '$title'");
while($show_pextra = mysql_fetch_array($read_pextra))
{
?>
<center><b>RM <?php echo $show_pextra['SUM(price)'] ?></b></center>
<?php
$total += $show_pextra['SUM(price)'];
}
?>
</td>
</tr>
<tr><!--deco-->
<td style="width:120px;border:2px solid #000;"> Decoration :</td>
<td style="border:2px solid #000;">
<?php
$read_alldeco = mysql_query("select * from selectdeco where title = '$title'");
while($show_alldeco = mysql_fetch_array($read_alldeco))
{
?>
<b>♦ <?php echo $show_alldeco['decoitem'] ?></b></br>
<?php
}
?>
</td>
<td style="width:120px;border:2px solid #000;">
<?php
$read_pdeco = mysql_query("select SUM(decoprice) from selectdeco where title = '$title'");
while($show_pdeco = mysql_fetch_array($read_pdeco))
{
?>
<center><b>RM <?php echo $show_pdeco['SUM(decoprice)'] ?></b></center>
<?php
$total += $show_pextra['SUM(decoprice)'];
}
?>
</td>
</tr>
<tr><!--food-->
<td style="width:120px;border:2px solid #000;"> Foods :</td>
<td style="border:2px solid #000;">
<?php
$read_allfood = mysql_query("select * from selectfood where title = '$title'");
while($show_allfood = mysql_fetch_array($read_allfood))
{
?>
<b>♦ <?php echo $show_allfood['fooditem'] ?></b></br>
<?php
}
?>
</td>
<td style="width:120px;border:2px solid #000;">
<?php
$read_pfood = mysql_query("select SUM(foodprice) from selectfood where title = '$title'");
while($show_pfood = mysql_fetch_array($read_pfood))
{
?>
<center><b>RM <?php echo $show_pfood['SUM(foodprice)'] ?></b></center>
<?php
$total += $show_pextra['SUM(foodprice)'];
}
?>
</td>
</tr>
<tr><!--drinks-->
<td style="width:120px;border:2px solid #000;"> Drinks :</td>
<td style="border:2px solid #000;">
<?php
$read_alldrinks = mysql_query("select * from selectdrinks where title = '$title'");
while($show_alldrinks = mysql_fetch_array($read_alldrinks))
{
?>
<b>♦ <?php echo $show_alldrinks['drinksitem'] ?></b></br>
<?php
}
?>
</td>
<td style="width:120px;border:2px solid #000;">
<?php
$read_pdrinks = mysql_query("select SUM(drinksprice) from selectdrinks where title = '$title'");
while($show_pdrinks = mysql_fetch_array($read_pdrinks))
{
?>
<center><b>RM <?php echo $show_pdrinks['SUM(drinksprice)'] ?></b></center>
<?php
$total += $show_pextra['SUM(drinksprice)'];
}
?>
</td>
</tr>
<tr><!--venue-->
<td style="width:120px;border:2px solid #000;"> Venue :</td>
<td style="border:2px solid #000;">
<?php
$read_allvenue = mysql_query("select * from selectvenue where title = '$title'");
while($show_allvenue = mysql_fetch_array($read_allvenue))
{
?>
<b>♦ <?php echo $show_allvenue['venuename'] ?></b></br>
<?php
}
?>
</td>
<td style="width:120px;border:2px solid #000;">
<?php
$read_pvenue = mysql_query("select venueprice from selectvenue where title = '$title'");
while($show_pvenue = mysql_fetch_array($read_pvenue))
{
?>
<center><b>RM <?php echo $show_pvenue['venueprice'] ?></b></center>
<?php
$total += $show_pextra['SUM(venueprice)'];
}
?>
</td>
</tr>
<tr>
<td style="border:2px solid #000;" colspan="2">
<center><input type="submit" class="button" style="width:300px;" value="Pay Now" name="send_payment"/></center></td>
<td style="width:120px;border:2px solid #000;">
<center><b>Total <?php echo $total ?></b></center>
</td>
</tr>

Related

Background color for the last second row of the table

I want red color as the background for the last second row of the table.
But it is not changing.
I tried:
$i = 0;
$total = 0;
$stmttr = $user_home->runQuery("SELECT * FROM invoice WHERE BRN = :inv"); // Your query
$stmttr->bindParam(':inv',$irn);
$allRows = $stmttr->rowCount(); // Count Total rows available in result
$tri = 0; // initiate variable to check <tr> status
while($rowc)
{
extract($rowc);
$i++;
?>
<tr <?php if(++$tri==$allRows) { ?> style="background-color: red;"<?php } ?> >
<td style="border-top:none !important; border-bottom:none !important;">
See the full code:
<?php
$i = 0;
$total = 0;
$stmttr = $user_home->runQuery("SELECT * FROM invoice WHERE BRN = :inv"); // Your query
$stmttr->bindParam(':inv',$irn);
$allRows = $stmttr->rowCount(); // Count Total rows available in result
$tri = 0; // initiate variable to check <tr> status
while($rowc)
{
extract($rowc);
$i++;
?>
<tr <?php if(++$tri==$allRows) { ?> style="background-color: red;"<?php } ?> >
<td style="border-top:none !important; border-bottom:none !important;">
<?php echo $i; ?>
</td>
<td style="border-top:none !important; border-bottom:none !important;">
<?php
$itm = $IRN;
$stmti = $user_home->runQuery('SELECT * FROM item WHERE IRN = :iinv ');
$stmti->bindParam(':iinv',$itm);
$stmti->execute();
$rowci = $stmti->fetch(PDO::FETCH_ASSOC);
echo $rowci['Name'];
$srv = $SRN;
$stmts = $user_home->runQuery('SELECT * FROM service WHERE SRN = :sinv ');
$stmts->bindParam(':sinv',$srv);
$stmts->execute();
$rowcs = $stmts->fetch(PDO::FETCH_ASSOC);
?>
(<?php echo $rowcs['Name']; ?>)
</td>
<td style="border-top:none !important; border-bottom:none !important;">
<?php echo $Quantity; ?>
</td>
<td style="border-top:none !important; border-bottom:none !important;">
<?php echo $Amnt; ?>
</td>
</tr>
The error is at: $allRows = $stmttr->rowCount(); // Count Total rows available in result
It is not counting the rows available in MySQL
The updated code:
$i = 0;
$total = 0;
$stmttr = $user_home->runQuery("SELECT COUNT(*) As rows FROM invoice WHERE BRN = :inv"); // Your query
$stmttr->bindParam(':inv',$irn);
$stmttr->execute();
$cresult = $stmttr->fetch(PDO::FETCH_ASSOC);
$tf= ($cresult['rows']);
while($rowc)
{
extract($rowc);
$i++;
?>
<tr <?php if($i == $tf) { ?> style="background-color: red;"<?php } ?> >
<td style="border-top:none !important; border-bottom:none !important;">
Hope it helps you. Good Luck!!
Maybe you have a problem on your loop. What are on variable $rowc?
Maybe you should add <table> before the first <tr> and </table> after
the last </tr>?
And CSS should be in a css file, better practice!
I don't have your DB, but this code is working, if it's what you want.
<?php
$tri = 0;
$nbrows=10;
?>
<table>
<?php
while ($tri<$nbrows) { ?>
<tr <?php if(++$tri==$nbrows) { echo "style=\"background-color: red;\"";} ?> >
<td style="border-top:none !important; border-bottom:none !important;">Your content </td>
<td style="border-top:none !important; border-bottom:none !important;">Content 2 </td>
</tr>
<?php
}
?>
</table>

How to enter a word in looping <td> in php

I have table data which is taking data from foreach and looping through a for loop.(looping 3 times with different values from foreach)
I want to know that how can I put a word in last <td> ?
my code;
<?php
foreach($halls_all_array AS $row){?>
<td nowrap class="auto-style34" style="height: 30px; width: 20%" align="left">
time1-<?php echo format_time($row[$start]['time'])?>
</td>
<?php } ?>
foreach($halls_all_array AS $row){
for($j=$start;$j<$limit;$j++){ ?>
<td nowrap class="auto-style34" style="height: 30px; width: 20%" align="left"> time1-<?php echo format_time($row[$j]['time'])
if($j==($limit-1)){ echo "word";}
?>
</td>
<?php
}
}
?>
View
<?php foreach(... ) { ?>
<td>loop td</td>
<?php } ; ?>
Use :
if($j == ($limit - 1)) {
// last <td> here. You can add the text here.
}
EDIT
<?php
$count = count($halls_all_array);
$i = 0;
foreach($halls_all_array AS $row){
?>
<td nowrap class="auto-style34" style="height: 30px; width: 20%" align="left">time1-<?php echo format_time($row[$start]['time'])?>
<?php
if(++$i === $count){
//echo 'WORD TO PRINT';
}
?>
</td>
<?php
}
?>
Try the following
// first of all check $halls_all_array and $start are defined or not
<?php
$count=count($halls_all_array);
$i=1;
foreach($halls_all_array AS $row){
echo'<td nowrap class="auto-style34" style="height: 30px; width: 20%" align="left">time1-'.format_time($row[$start]['time']);
if($i == $count){
//echo 'WORD TO PRINT';
}
echo'</td>';
$i++;
}
?>
Try this code
<?php
$length = count($halls_all_array); // TAKE THE LENGTH OF ARRAY
$i = 1;
foreach($halls_all_array as $row){ ?>
<td nowrap class="auto-style34" style="height: 30px; width: 20%" align="left">
<?php if($i < $length){ ?>
time1- <?php echo format_time($row[$start]['time']);?>
<?php }else{
echo "YOUR WORD..."; // Last Looping..
} ?>
</td>
<?php
$i++; // INCREMENT THE COUNTER
} ?>
first get the last key from array
end($halls_all_array); // move the internal pointer to the end of the array
$last_key = key($halls_all_array);// fetches the key of the element pointed to by the internal pointer
try this code:
<?php
/*get the last key*/
end($halls_all_array);
$last_key = key($halls_all_array);
foreach($halls_all_array as $key => $row){?>
<td nowrap class="auto-style34" style="height: 30px; width: 20%" align="left">
time1-<?php echo format_time($row[$start]['time'])?>
</td>
<?php if ($last_key == $key): ?>
<td><?php echo $row ?></td>
<?php endif ?>
<?php } ?>
Please write code like this:
<?php
$counter = 0; // start a counter
$total_count = count($halls_all_array); //total Counts
foreach($halls_all_array AS $row){?>
<td nowrap class="auto-style34" style="height: 30px; width: 20%" align="left">
time1-<?php echo format_time($row[$start]['time'])?>
<?php if($total_count==$counter) echo 'word'; ?> // you condition for last and word
</td>
<?php
$counter++;
}
?>

Viewing queried data from mysql database in a table

I am attempting to update the code for my web page's search function, right now it is not returning anything. I have been working with it for a little while and not getting anything out of it.
This is the HTML search code:
<form method="post" action="words_results1.php">
<table align="center">
<tr>
<td>Keyword</td>
<td><input type="text" name="Keyword" /></td>
</tr>
<tr>
<td>Author</td>
<td><input type="text" name="Author" /></td>
</tr>
<tr>
<td valign=bottom>Words Posted<BR />on or before</td>
<td valign=top>
<table>
<tr>
<td width="33%">Day</td>
<td width="33%">Month</td>
<td width="34%">Year</td>
</tr>
<tr>
<td>
<select name=Day>
<?php
echo '<option></option>';
for($count = 1; $count <= 31; ++$count)
{
echo "<option>$count</option>";
}
?>
</select>
</td>
<td>
<select name=Month>
<?php
echo '<option></option>';
for($count = 1; $count <= 12; $count++)
{
echo "<option value=$count>".date("M", mktime(0,0,0,$count,1, 2000))."</option>";
}
?>
</select>
</td>
<td>
<select name=Year>
<?php
echo '<option></option>';
for($count = date("Y"); $count >= 1997; $count--)
{
echo "<option>$count</option>";
}
?>
</select>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan=2 align=center>
<BR />
<input type="submit" value="Search" />
<input type="submit" name="cancel" value="Cancel" />
</td>
</tr>
</table>
</form>
PHP
<?php
if(isset($_POST['cancel']))
{
echo("index.html");
exit;
}
$qry_string = "SELECT * FROM Words";
$search = "";
if(!empty($Keyword))
{
$End_String = "(Word LIKE '%$Keyword%' OR Title LIKE '%$Keyword%')";
$search .="&Keyword=$Keyword";
}
if(!empty($Author))
{
if(isset($End_String))
{
$End_String .= " AND (Author LIKE '%$Author%')";
}
else
{
$End_String = "(Author LIKE '%$Author%')";
}
$search .="&Author=$Author";
}
if(!empty($Day))
{
if(isset($End_String))
{
$End_String .= " AND (DAYOFMONTH(Date_Created) = '$Day')";
}
else
{
$End_String = "(DAYOFMONTH(Date_Created) = '$Day')";
}
$search .="&Day=$Day";
}
if(!empty($Month))
{
if(isset($End_String))
{
$End_String .= "AND (MONTH(Date_Created) = '$Month')";
}
else
{
$End_String = "(MONTH(Date_Created) = '$Month')";
}
$search .="&Month=$Month";
}
if(!empty($Year))
{
if(isset($End_String))
{
$End_String .= " AND (YEAR(Date_Created) = '$Year')";
}
else
{
$End_String = "(YEAR(Date_Created) = '$Year')";
}
$search .="&Year=$Year";
}
if (!isset($offset)) $offset=0;
if(isset($End_String))
{
$qry_string = $qry_string." WHERE ".$End_String . " ORDER BY Date_Created DESC LIMIT $offset,101";
}
else
{
$qry_string = $qry_string." ORDER BY Date_Created DESC LIMIT $offset,101";
}
// echo $qry_string . "<P><HR><P>";
$result = mysql_query($qry_string);
echo mysql_error();
?>
This last bit is the code that forms the table, I have an assumption that the problem lies here but honestly am not sure at this point
<table style="margin: 5px 15px; 5px 20px;" align="center" bgcolor="#666666" border="0" cellpadding="3" cellspacing="1">
<tbody><tr style="background: #04C1DE; font-family: Verdana; font-weight: bold; font-size: 18px;">
<td style="width: 50%; padding: 5px;">
Word
</td>
<td style="width: 20%; padding: 5px;">
Author
</td>
<td style="width: 10%; padding: 5px;">
Date
</td>
<td>Category</td>
<td>Active?</td>
<td> </td>
<td> </td>
</tr>
</tbody>
</tr>
<?php
$count = 1;
$bgc = 0;
while($row = mysql_fetch_array($sql))
{
if ($count > 100) break;
echo '<tr style="background: ';
if ($bgc==0) echo "#FFFFFF";
else echo "#CFEBFD";
$bgc == 0?$bgc=1:$bgc=0;
echo ';">';
echo "<td><a href=../../words/display_word.php?ID=$row[ID]>$row[Title]</a></td>";
echo "<td>$row[Author]</td><td>$row[Display_Date]</td><td>$row[category]</td>";
if($row[active])
{
echo "<td>YES</td>";
}
else
{
echo "<td>NO</td>";
}
echo "<td>$row[link_count]</td>";
if($row[Title] != "")
{
echo "<td><a href=words_edit.html?ID=$row[ID]>Edit</a></td></tr>";
}
else
{
echo "</tr>";
}
$count++;
}
?>
It seems,you are not collecting the value of
$Keyword=$_POST['Keyword'];
and add ,closing table tag to display the results in the table format correctly.

How do I display a query in PHP table

My code
<?php
include('ConnectToDb.php');
$query = "SELECT * FROM News WHERE NewsFlag = 1 ORDER BY PostDate DESC";
$arrCount = -1;
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$ID=$row['ID'];
$PostDate = $row['PostDate'];
$NewsHeader = stripslashes($row['NewsHeader'])
;
$NewsStart = stripslashes($row['NewsStart'])
;
echo "<hr>";
echo "<div>". date('j F Y',strtotime($PostDate)). "</div>";
echo "<p>";
$news_id = strval(sprintf("%1$04d",$ID));
$array = scanImageFolder("newsImages/newsThumbs",$news_id);
if(count($array)>0) {
echo "<img src='". $array[0]. "' alt='' />";
}
echo "<h2 style='text-align:center'><u><a href='latestnews_full.php?ID=$ID'>". $NewsHeader. "</a></u></h2>";
echo "<div style='text-align:left'><h3>";
echo $NewsStart. " ......<a href='latestnews_full.php?ID=$ID'>(more)</a><br />";
echo "<div style='text-align:center'>";
echo "</div>";
echo "</h3></div>";
}
?>
displays my data nicely on four lines with date at the top, then a picture, title and then description.
However, I want to display the data as a table like this
<table style="width: 100%">
<tr>
<td colspan="2">postDate here</td>
</tr>
<tr>
<td rowspan="2">picture here</td>
<td>newsHeader here</td>
</tr>
<tr>
<td>newsStart here</td>
</tr>
</table>
I'm not sure how to echo the table cells correctly and all of my attempts so far have resulted in a white page. Could anyone please enlighten me?
I'd suggest you to make your logic separated from your presentable part. Close the PHP tag once you are ready fetching the results, assigning var's etc, then:
<table style="width: 100%">
<?php
//yourcode
//...
//...
$NewsStart = stripslashes($row['NewsStart']);
$news_id = strval(sprintf("%1$04d",$ID));
$array = scanImageFolder("newsImages/newsThumbs",$news_id);
?>
<tr>
<td colspan="2"><?= date('j F Y',strtotime($PostDate)) ?></td>
</tr>
<tr>
<?php
if(count($array)>0) {
?>
<td rowspan="2"><img src='<?= $array[0] ?>' alt='' /></td>
<?php } ?>
<td><?= $NewsHeader ?></td>
</tr>
<tr>
<td><?= $NewsStart ?> </td>
</tr>
<?php } ?>
</table>
However, it's again not so clear, and I would suggest using a template engine. If you want I can post a code with assigning vars to Smarty and output your presentation in Smarty template

PHP echo tables

Overview
I have some data stored in MySql database related to Products. I am trying to retrieve this data and display it on a page using HTML table.
The PHP and MySql has gone well and all the data is retrieved but it is displayed in a very messy manner.
Here is what I have as a layout:
What I am aiming to achieve is to further divide the results table add more columns rows to make the data more readable
Something like this;
The code: PHP, MySQL & HTML:
<?php
session_start();
include('connect_mysql.php');
$product_name = 'product_name';
$product_qua = 'product_qua';
$product_price = 'product_price';
$product_image = 'product_image';
$product_des = 'product_des';
$sql = mysql_query("SELECT * FROM products");
echo "<table id='display'>";
while($rows = mysql_fetch_array($sql))
{
echo"<br>";
echo"<tr><td>";
echo"$rows[$product_name]<br></td>";
echo"<td><img src=$rows[$product_image] height='200px' width='200px'><br></td>";
echo"<td>Avalible: $rows[$product_qua]<br></td>";
echo"<td>Price: $rows[$product_price]<br></td>";
echo"<td>Description: $rows[$product_des]<br></td>";
echo"</tr>";
}
echo "</table>";
?>
CSS responsible for this part:
#display{
float:left;
border: 5px solid black;
margin-left:100px;
}
just add some padding or a border to the table cells:
table#display td{
border: 1px solid black;
padding:0 8px;
}
Edit: What you could do as well:
<table id='display'>
<?php while($rows = mysql_fetch_array($sql)): ?>
<!-- <br> <- why a break? it's not in the correct spot anyway -->
<tr><td>
<?php echo $rows[$product_name]; ?><br>
</td>
<td> - </td>
<td><img src="<?php echo $rows[$product_image]; ?>" height='200px' width='200px'><br></td>
<td> - </td>
<td>Avalible: <?php echo $rows[$product_qua]; ?><br></td>
<td> - </td>
<td>Price: <?php echo $rows[$product_price]; ?><br></td>
<td> - </td>
<td>Description: <?php echo $rows[$product_des]; ?><br></td>
</tr>
<?php endwhile; ?>
</table>
Tip: I prefer to use the while/endwhile; approach rather than using brackets when displaying data to the user.
First of all don't echo so much HTML using PHP, instead do it like this
<?php
session_start();
include('connect_mysql.php');
$product_name = 'product_name';
$product_qua = 'product_qua';
$product_price = 'product_price';
$product_image = 'product_image';
$product_des = 'product_des';
$sql = mysql_query("SELECT * FROM products"); ?>
<table id='display'>
<?php
while($rows = mysql_fetch_array($sql)) {
?>
<tr><td><?php echo $rows[$product_name]; ?></td>
<!-- And so on-->
<?php
}
?>
</table>
Secondly by seeing your inmage, it seems like you need a border for your table so use
table, td {
border: 1px solid #000000;
}
add the following css to apply border on your table cells:
#display td{ border: 2px solid red; }
and optionally add to your #display { :
border-collapse: collapse;

Categories