i put limit in this code but how can i calculate the totalmin in this page?
here is my code
<?php
$id = $_GET['id'];
$sql = "SELECT * FROM time WHERE id = $id ORDER BY id DESC LIMIT 15";
$result = mysql_query($sql);
?>
<table border="0" style="width:50%">
<tr>
<th>Time in</th>
<th>Time Out</th>
</tr>
<?php
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td><center>".$row['totalmin']."</center></td>";
echo "</tr>";
}
</table>
mysql_close();
i want to know how to total all the mins in 15 row with php code
Something like this may do the trick, if you want to show sum only
But you should NOT use mysql_ functions, and this code is not sequre from SQL Injects
$id = $_GET['id'];
$sql = "SELECT * FROM time WHERE id = $id ORDER BY id DESC LIMIT 15";
$result = mysql_query($sql);
$sum = 0;
$num = 0;
?>
<table border="0" style="width:50%">
<tr>
<th>Time in</th>
<th>Time Out</th>
</tr>
<?php
while($row = mysql_fetch_array($result)){
$num++;
$sum += $row['totalmin'];
echo "<tr>";
echo "<td><center>".$row['totalmin']."</center></td>";
echo "</tr>";
if($num == 15){
echo "<tr>";
echo "<td><center>".$sum."</center></td>";
echo "</tr>";
}
}
?>
</table>
<?php
mysql_close();
You need to store $totalmin into your while loop and update it with new value if $row['totalmin'] is less then $totalmin.
Use something like this:
$totalmin = null;
while($row = mysql_fetch_array($result)) {
if (is_null($totalmin) || $row['totalmin'] < $totalmin) {
$totalmin = $row['totalmin'];
}
echo "<tr>";
echo "<td><center>" . $totalmin . "</center></td>";
echo "</tr>";
}
Sum up the minutes as you loop through the result:
$sum = 0;
while($row = mysql_fetch_array($result)){
$sum += floatval($row['totalmin']); //or intval()
echo "<tr>";
echo "<td><center>".$row['totalmin']."</center></td>";
echo "</tr>";
}
echo $sum;
make sure you close your while loop
Related
I have a table 'tblexam' which contains State,city,candidate.I want to fetch the data from this table using where clause.I am able to fetch the data but i want to add the sum of Candidate at the last row(As Shown In Picture) how can i do that .
$sql="SELECT *
FROM tblexam
WHERE state='UP'";
$cnt=1;
if($query->rowCount() > 0)
{
foreach($results as $result) {
$cnt=$cnt+1; ?>
<tr class="odd gradeX">
<td class="center"><?php echo htmlentities($cnt);?></td>
<td class="left"align="left"><?php echo htmlentities($result->state);?></td>
<td class="center" align="left"><?php echo htmlentities($result->city);?></td>
<td class="center"align="left"><?php echo htmlentities($result->candidate);?></td>
<?php } ?>
<td class="center"align="left"><?php echo htmlentities($result->Total);?></td>
</tbody>
</table>
Add total while iterating through rows and display it outside the loop, Sample code is given below
$sql = "SELECT * FROM tblexam WHERE city='UP'";
$result = $conn->query($sql);
$numRows = $result->num_rows;
if ($numRows> 0) {
$total = 0;
echo '<table border="1" cellpadding="5" cellspacing="0">';
echo '<tr>';
echo '<th>state</th>';
echo '<th>city</th>';
echo '<th>candidate</th>';
echo '</tr>';
while($row = $result->fetch_assoc()) {
$total+=$row['candidate'];
echo '<tr>';
echo '<td>'.$row['city'].'</td>';
echo '<td>'.$row['state'].'</td>';
echo '<td>'.$row['candidate'].'</td>';
echo '</tr>';
}
echo '<tr>';
echo '<td>Total</td>';
echo '<td> </td>';
echo '<td>'.$total.'</td>';
echo '</tr>';
echo '<table>';
}
Before you start looping the data you can create a variable which you set to 0, inside the loop you can add the result's total to this variable, after the loop, the variable will contain the grand total:
$total = 0;
foreach($results as $result) {
$total += (int)$result->Total;
...
}
// $total = 1425
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>
I need to select some data from mysql and echo them into a table,
I have 20 entries which I want to echo them into 5by4 table I can select them like this:
<?php
$sql = "SELECT player FROM `prize` WHERE inviter='$player'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
?>
<table style="width: 100%;border:1px">
<tr> <td class="auto-style3">
<?php
while($row = $result->fetch_assoc()) {
?>
<?php echo "<br>". $row["player"].""; ?>
<?php
}}
?>
</td> </tr> </table>
it gives me something like this:
but I want it like this:
Can anyone help?
try it like this, using $count to count your item in array. and after 5 items have been echoed, then you put <tr> to echo in new row
<?php
$count=0;
echo "<table>";
while($row = $result->fetch_assoc()) {
if($count==0) {
echo "<tr>";
}
$count++;
echo "<td>".$row["player"]."</td>";
if($count==5) {
echo "</tr>";
$count=0;
}
}
echo "</table>";
?>
I changed your code to add a counter and close the tag every five records, this is the result:
<?php
$sql = "SELECT player FROM `prize` WHERE inviter='$player'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
?>
<table style="width: 100%;border:1px">
<tr>
<?php
$counter=0;
while($row = $result->fetch_assoc()) {
if($counter == 4){
echo '</tr><tr>';
$counter=0;
}
?>
<td><?php echo $row["player"].""; ?></td>
<?php
$counter++;
}}
?>
</tr> </table>
I am using an HTML table to display data from a MySQL table using PHP. I need it so once the table has 10 columns, it will move on to the next row.
<?php
$result = mysqli_query($con,"SELECT * FROM table");
echo '<table width="100%" border="1px"><tr width="100%">';
while($row = mysqli_fetch_array($result))
{
?>
<td width="10%"><?php echo $row['Name']; ?></td>
<?php
}
echo "</tr></table>";
mysqli_close($con);
?>
How can this be done?
Untested but something like this should work or get you started in a good direction:
<?php
$result = mysqli_query($con,"SELECT * FROM table");
echo '<table width="100%" border="1px"><tr width="100%">';
$x=0;
while($row = mysqli_fetch_array($result))
{
if($x==0){
echo "<tr>\n";
}elseif($x%10){
echo"</tr><tr>\n";
}
?>
<td width="10%"><?php echo $row['Name']; ?></td>
<?php
$x++;
}
echo "</tr></table>";
mysqli_close($con);
?>
Add a counter to your loop starting at one.
Each time through the loop if the remainder after dividing the counter value by 10 is 1
add the <tr>. If the remainder is 0 then add a </tr> Then after the loop a </tr> if the remainder is not evenly divisible by 10.
<?php
echo '<table width="100%" border="1px"><tr width="100%">';
$i = 0;
while($row = mysqli_fetch_array($result))
{
$i++;
?>
<?php if ($i%10 ==1): ?><tr><?php endif; ?>
<td width="10%"><?php echo $row['Name']; ?></td>
<?php if ($i%10 ==0): ?></tr><?php endif; ?>
<?php
}
if ($i%10 != 0) echo "</tr>";
echo "</tr></table>";
Using modulo (%)
After each 10th cell, if a new cell is added, the current row is closed and a new row is opened first, before outputting the cell.
<?php
$result = mysqli_query($con,"SELECT * FROM table");
echo '<table width="100%" border="1px"><tr>';
$cell = 0;
while($row = mysqli_fetch_array($result))
{
if ($cell++ % 10 == 0 && $cell > 1)
{
?>
</tr><tr>
<?php
}
?>
<td width="10%"><?php echo $row['Name']; ?></td>
<?php
}
echo "</tr></table>";
mysqli_close($con);
?>
The extra condition && $cell > 1 seems to be a little odd, but without it, you will get an empty row to start with. Eliminating it by putting ++ before $cell will cause the first row to be 9 cells instead of 10. Putting $cell > 0 && in front of the modulo will cause cell never to be incremented, because the first part of the expression is always false. Moving the if to execute it after outputting the cell, would cause the risk of ending with an empty row. It could be solved using a do..while loop, but you'd have to check up front if you have one row at least.
Long story short: use the code above. :)
Using a simple counter and reset it after each row
I think it's even more readable without the modulo, though you'd have to initialize $cell to -1 to prevent the first row to be 9 cells. Nevertheless, I think this is cleaner:
<?php
$result = mysqli_query($con,"SELECT * FROM table");
echo '<table width="100%" border="1px"><tr>';
$cell = -1;
while($row = mysqli_fetch_array($result))
{
if (++$cell == 10)
{
$cell = 0;
?>
</tr><tr>
<?php
}
?>
<td width="10%"><?php echo $row['Name']; ?></td>
<?php
}
echo "</tr></table>";
mysqli_close($con);
?>
<table>
<tr>
<?php
$endRow = 0;
$columns = 10; // number of columns
$hloopRow1 = 0;
do {
if($endRow == 0 && $hloopRow1++ != 0) echo "<tr>";
?>
<td>
<?php echo $row['Name']; ?>
</td>
<?php $endRow++; if($endRow >= $columns) { ?>
</tr>
<?php $endRow = 0; }
} while ($row = mysql_fetch_assoc($result));
if($endRow != 0) {
while ($endRow < $columns) {
echo("<td> </td>");
$endRow++;
}
echo("</tr>");
}
?>
</table>
This should work fine. Hope this helps.
I'm trying to get an id from the users database so that I can link to about.php?id=$id1 and I'm using 2 while loops to do that. I end up getting what I want, about.php?id=$id1 but there are duplicate entries...
Here's the code.
<?php
require("connect.php");
require("header.php");
$max = 5; //amount of articles per page. change to what to want
$p = $_GET['p'];
if(empty($p))
{
$p = 1;
}
$limits = ($p - 1) * $max;
//view the news article!
$id = isset($_GET['id']) ? $_GET['id'] : false;
if($id && is_numeric($id)){
$id = $_GET['id'];
$sql = mysql_query("SELECT * FROM blogdata WHERE id = '$id'");
while($r = mysql_fetch_array($sql))
{
echo $total;
$id = $r['id'];
$date = $r['date'];
$title = $r['title'];
$content = $r['content'];
$email = $r['author_email'];
$cat = $r['category'];
$author = $r['author'];
$query1 = mysql_query("SELECT * FROM users");
while ($row1 = mysql_fetch_array($query1)){
$id1 = $row1['id'];
echo "<center>
<table border='0' width='100%' cellspacing='10'>
<tr>
<td width='20%' valign='top'><div class='title'>$title</div>
<div class='info'><i>$date</i><br />
By <a href='$id1'>$author</a><br />
$cat</div>
</td>
<td width='80%' valign='top'>";
echo nl2br($content);
echo "</td>
</tr>
</table>
<hr />
<a href='index'>← Rewind.</a>
</center>";
}
}
}else{
//view all the news articles in rows
$sql = mysql_query("SELECT * FROM blogdata ORDER BY id DESC LIMIT ".$limits.",$max") or die(mysql_error());
//the total rows in the table
$totalres = mysql_result(mysql_query("SELECT COUNT(id) AS tot FROM blogdata"),0);
//the total number of pages (calculated result), math stuff...
$totalpages = ceil($totalres / $max);
while($r = mysql_fetch_array($sql))
{
$id = $r['id'];
$date = $r['date'];
$title = $r['title'];
$content = $r['content'];
$email = $r['author_email'];
$cat = $r['category'];
$author = $r['author'];
$query1 = mysql_query("SELECT * FROM users");
while ($row1 = mysql_fetch_array($query1)){
$id1 = $row1['id'];
echo "<center>
<table border='0' width='100%' cellspacing='10'>
<tr>
<td width='20%' valign='top'><div class='title'>$title</div>
<div class='info'><i>$date</i><br />
By <a href='$id1'>$author</a><br />
$cat</div>
</td>
<td width='80%' valign='top'>";
echo nl2br($content);
echo "</td>
</tr>
</table>
<hr />
</center>";
}
}
//close up the table
echo "</tr></table><center>";
$page = $_GET['p'];
for($i = 1; $i <= $totalpages; $i++)
{
if ( $page == $i ) {
echo "<b>$i</b>";
} else {
echo "<a href='?p=$i'>$i</a>";
}
if ($i < $totalpages)
echo " <font color=\"#666\">•</font> ";
}
}
echo "<br />";
require("footer.php");
?>
Couldn't fully get your question, but does SELECT DISTINCT column_name FROM table help you? It selects only distinct rows from a table.