I'm trying to do some loops in loop using mysql but it seems that there not interpreted
it returns only the first result then nothing even If I have many records in my database, It should return everything
below is my code that display the results
while ($cr = mysql_fetch_assoc($result)):
$i = 0;
++$i;
?>
<div style="width:200px" class="fif"><table style="width:100%">
<tr class="fbgreybox">
<td colspan="3" style="text-align: center;font-weight: bold">
<img src="images/calendar_2.png"> Séance du : <?php echo date('d-m-Y', strtotime($cr['record'])); ?>
</td>
</tr>
<?php
$sql = "SELECT `exercise` FROM `workouts` WHERE `record` = '{$cr['record']}' AND `user`= {$_SESSION['userid']} GROUP BY `exercise`";
$result = mysql_query($sql);
while ($exo = mysql_fetch_assoc($result)) :
?>
<tr class="fbinfobox">
<td colspan="3" style="text-align: left">
<img src="images/Sport-dumbbell.png"> ////<?php echo exerciseName($exo['exercise']); ?>
</td>
</tr>
<tr>
<td style="text-align: center;font-weight: bold">Séries</td>
<td style="text-align: center;font-weight: bold">Reps</td>
<td style="text-align: center;font-weight: bold">Poids</td>
</tr>
<?php
$rqt = "SELECT `set_number`, `reps`, `weight` FROM `workouts` WHERE `exercise` = {$exo['exercise']} AND `record` = '{$cr['record']}' AND `user` = {$_SESSION['userid']} ORDER BY `set_number`";
$result2 = mysql_query($rqt);
while ($detail = mysql_fetch_assoc($result2)):
?>
<tr>
<td>
Série ////<?php echo $detail['set_number']; ?>
</td>
<td>
<?php echo $detail['reps']; ?>
</td>
<td>
<?php echo $detail['weight']; ?>
</td>
</tr>
<?php
endwhile;
endwhile;
?>
</table>
</div>
<?php
endwhile;
there is not mistakes in the code so I really do not understand why it is not interpreted.
anykind of help will be much appreciated.
You are using $result in both mysql_fetch_assoc() function. AVOID THAT.
In inner loop you can use another variable:
$sql = "SELECT `exercise` FROM `workouts` WHERE `record` = '{$cr['record']}' AND `user`= {$_SESSION['userid']} GROUP BY `exercise`";
$workoutsResult = mysql_query($sql);
while ($exo = mysql_fetch_assoc($workoutsResult )) :
If I were you, I would first loop through the first query and put it in an array.
Then you loop through this array, and search for the inner queries and also put in arrays inside the other array.
Then, you loop through them to build your table.
It'll make your code cleaner, and will avoid problems like the one you're having, the one #Suresh pointed out.
Related
In my admin panel i have a view of orders of day, individually is ok, but i want to count and sum the values (count number of orders) and (sum value from orders).
$pedido->valor_pedido (is the individual price from order)
My code is like this.
if(count($pedidos) > 0){ foreach($pedidos as $pedido){ $totalHoje += $valorCompra; ?>
<tr onclick="window.location='<?=base_url();?>index.php/adm/pedidos/alterar/<?=$pedido->id;?>'" style="cursor:pointer" target='_blank'>
<td width="50">
<?=$pedido->id;?>
</td>
<td class="pedidonome">
<?=strtolower($pedido->nome ? $pedido->nome : $pedido->razao_social);?>
</td>
<td>
<?php echo getPagamento($pedido->pagamento);?>
</td>
<td>R$
<?=$pedido->frete;?>
</td>
<td>R$
<?=$pedido->valor_pedido;?>
</td>
<td><span class="<?=$pedido->situacao;?>"><?=$pedido->situacao;?></span></td>
</tr>
If i understand you the right way you need to get a sum of orders, that's how you can do this:
<?php if(count($pedidos) > 0): ?>
<?php $sum = 0.00; ?>
<?php foreach($pedidos as $pedido): ?>
<tr onclick="window.location='<?=base_url();?>index.php/adm/pedidos/alterar/<?=$pedido->id;?>'" style="cursor:pointer" target='_blank'>
<?php $sum += $pedido->valor_pedido; ?>
<td width="50">
<?=$pedido->id;?>
</td>
<td class="pedidonome">
<?=strtolower($pedido->nome ? $pedido->nome : $pedido->razao_social);?>
</td>
<td>
<?php echo getPagamento($pedido->pagamento);?>
</td>
<td>R$
<?=$pedido->frete;?>
</td>
<td>R$
<?=$pedido->valor_pedido;?>
</td>
<td><span class="<?=$pedido->situacao;?>"><?=$pedido->situacao;?></span></td>
</tr>
...................
<?php endforeach; ?>
<?= 'Total: ' . $sum ?>
<?php endif; ?> //end if statement
<?php
$con = mysqli_connect("localhost","username","pwd","db name);
$result = mysqli_query($con,"select count(1) FROM tablename");
$row = mysqli_fetch_array($result);
$total = $row[0];
echo "Total rows: " . $total;
?>
I have a mysql table with orders. I am trying to loop through the orders table and select individual client orders according to their user id and display the orders on their client accounts. However, my code below just prints the first row and repeats it endlessly jaming my browser every time.
what is wrong with this and how to i solve it
<?php
$records = $conn->prepare('SELECT * FROM orders WHERE user_id= :id');
$records-> bindParam(':id', $_SESSION['user_id']);
$records->execute();
$results=$records->fetch(PDO::FETCH_ASSOC);
$i=0;
while($i<=$results):
$i++;
?>
<h3>Your Orders</h3>
<table >
<tr >
<th>Order Number</th><th>Academic Level</th><th>Order Details</th>Manage Order</th>
</tr>
<tr>
<td>#SJ<?=$results['id']; ?> </td><td><?=$results['academic_level']; ?></td><td ><?=$results['details']; ?></td>
</tr>
</table>
<?php
endwhile;
?>
Remove all $i related code. Just move your fetch statement to while condition, like the following:
while( $results=$records->fetch(PDO::FETCH_ASSOC))
You need to include the html code in the while loop because you want to display all of the orders.
I'm using this method, try this out.
Start while in first php section
<?php
$username = $_SESSION['username'];
$sql = $db->prepare("SELECT * FROM tableName WHERE username = '$username' ");
$sql->execute();
while($results=$sql->fetch(PDO::FETCH_ASSOC)){
?>
After that the html section coming:
<h3>Your Orders</h3>
<table >
<tr >
<th>Order Number</th><th>Academic Level</th><th>Order Details</th>Manage Order</th>
</tr>
<tr>
<td><?php echo $results['id']; ?> </td><td><?php echo $results['academic_level']; ?></td><td ><?php echo $results['details']; ?></td>
</tr>
</table>
and here the ending
<?php
}
?>
Trying to figure out how to make it so results stop displaying as one monolithic row but are instead each on their own line. Not having much luck thus far.
$result = mysql_query("SELECT *, ROUND(score) AS performance FROM images ORDER BY ROUND(score) DESC LIMIT 11,50");
while($row = mysql_fetch_object($result)) {
$top_ratings2[] = (object) $row;
}
<? foreach($top_ratings2 as $key => $image) : ?>
<td valign="top">
<b><?=$image->name?></b>
<b>Score:</b> <?=$image->score?>
<b>Wins:</b> <?=$image->wins?>
</td>
<? endforeach ?>
try using a table
<?php
$result = mysql_query("SELECT *, ROUND(score) AS performance FROM images ORDER BY ROUND(score) DESC LIMIT 11,50");
while ($row = mysql_fetch_object($result)) {
$top_ratings2[] = (object)$row;
}
?>
<table>
<thead>
<tr>
<td>Name</td>
<td>Score</td>
<td>Wins</td>
</tr>
</thead>
<tbody>
<?php foreach ($top_ratings2 as $key => $image) : ?>
<tr>
<td valign="top">
<?= $image->name ?>
</td>
<td valign="top">
<?= $image->score ?>
</td>
<td valign="top">
<?= $image->wins ?>
</td>
</tr>
<?php endforeach ?>
</tbody>
</table>
My script basically loads data from my database and I have to <br> somewhere but it wont work because its a huge table. So I need to format this table so every 10 rows there is a <br>.
<table align="center" cellspacing="0" cellpadding="0">
<?php
$date = mysql_query("SELECT DISTINCT `date` FROM `matches`");
while($daterow = mysql_fetch_assoc($date)){
echo ' <tr>
<td style="width:60px; background-color:#000000;"><font color="#FFFFFF" style="font-size:11px">'.$daterow['date'].'</font></td>
</tr>
';
$query = mysql_query("SELECT hour, team1, team2, goalsteam1, goalsteam2, competition FROM `matches` WHERE `date`='". $daterow['date'] ."'");
while($row = mysql_fetch_array($query)){
$teamtest = mysql_query("SELECT teamname FROM `teams` WHERE `team`='".$row['team1']."'");
$teamtestrow = mysql_fetch_assoc($teamtest);
$hour = substr($row['hour'],0,5);
echo '
<tr class="teamtable">
<td style="width:60px; font-size:11px;">'.$hour.'</td>
<td style="width:145px; font-size:11px;">'.$teamrow['teamtest'].'</td>
<td style="width:15px; font-size:11px;">'.$row['goalsteam1'].'</td>
<td style="width:15px; font-size:11px;">'.$row['goalsteam2'].'</td>
<td style="width:145px; font-size:11px;">'.$row['team2'].'</td>
<td style="width:120; font-size:11px;">'.$row['competition'].'</td>
</tr>';
}
}
?>
</table>
Or just <br> before every date. When I tried it will just leave a huge blank space up but still no space between the date and matches.
Not too sure if CSS can handle this. And unclear on where you expect a break to occur based on the code you are sharing. But in general you would use the modulus operator in PHP would be a good start. Knowing that maybe this would work for you:
<table align="center" cellspacing="0" cellpadding="0">
<?php
$date = mysql_query("SELECT DISTINCT `date` FROM `matches`");
$counter = 0;
while($daterow = mysql_fetch_assoc($date)){
echo ' <tr>
<td style="width:60px; background-color:#000000;"><font color="#FFFFFF" style="font-size:11px">'.$daterow['date'].'</font></td>
</tr>
';
$query = mysql_query("SELECT hour, team1, team2, goalsteam1, goalsteam2, competition FROM `matches` WHERE `date`='". $daterow['date'] ."'");
while($row = mysql_fetch_array($query)){
$teamtest = mysql_query("SELECT teamname FROM `teams` WHERE `team`='".$row['team1']."'");
$teamtestrow = mysql_fetch_assoc($teamtest);
$hour = substr($row['hour'],0,5);
echo '
<tr class="teamtable">
<td style="width:60px; font-size:11px;">'.$hour.'</td>
<td style="width:145px; font-size:11px;">'.$teamrow['teamtest'].'</td>
<td style="width:15px; font-size:11px;">'.$row['goalsteam1'].'</td>
<td style="width:15px; font-size:11px;">'.$row['goalsteam2'].'</td>
<td style="width:145px; font-size:11px;">'.$row['team2'].'</td>
<td style="width:120; font-size:11px;">'.$row['competition'].'</td>
</tr>';
$counter++;
// Do a modulus check.
if ($counter % 10 == 0) {
echo "<tr><td colspan="6"> </td></tr>";
}
}
}
?>
</table>
The key chunk of code is this:
$counter++;
// Do a modulus check.
if ($counter % 10 == 0) {
echo "<tr><td colspan="6"> </td></tr>";
}
On each loop $counter is incremented by 1. And using the modulus operator in the if() statement we check for every 10th item & then do something. In this case insert a row with an in it.
i'm facing problem in getting the apprpriate values for some cells ....In the first page i'm getting the appropriate result..but in the next pages the values for product information,total price and profit from order all have value 0 but all the other fields are containing correct information.
Five records are shown per page.. The code is given below:
<?include"dbconnect.php"?>
session_start();
?>
<?include "adminhead.php"?>
<?include "adminleftnav.php"?>
<div id="seasonal">
<table width="635" border="0" align="left" bgcolor="#CCCCCC" >
<tr>
<td>
<table width="635" border="0" align="left" >
<tr>
<td width="52" bgcolor="#E0EBED">Order Number</td>
<td width="150" bgcolor="#E0EBED">Product Information</td>
<td width="100" bgcolor="#E0EBED">Total Price(TK)</td>
<td width="90" bgcolor="#E0EBED">Order Date</td>
<td width="80" bgcolor="#E0EBED">Payment Status</td>
<td width="100" bgcolor="#E0EBED">Profit from order(TK)</td>
</tr>
</table>
</td>
</tr>
<?php
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1)*5 ;
$sql = "Select distinct order_no,date,payment_status from product_order_info order by order_no ASC LIMIT $start_from,5";
$query_for_order1 = mysql_query ($sql);
?>
<tr>
<td>
<table id="foo" border="1" bgcolor="#f4eddb" cellspacing="2" cellborder="2" width=635>
<? while($row_for_my_order1=mysql_fetch_assoc($query_for_order1))
{
?>
<tr>
<td width=52>
<?echo $row_for_my_order1['order_no'];?>
</td>
<td width=156>
<?
$sql1 = "Select * from product_order_info where order_no= '$row_for_my_order1[order_no]' order by order_no ASC LIMIT $start_from,5";
$query_for_order = mysql_query($sql1);
while($row_for_my_order=mysql_fetch_assoc($query_for_order))
{
$query_for_product_details=mysql_query("select * from product where product_no='$row_for_my_order[product_id]' ");
$row_for_product_details=mysql_fetch_array($query_for_product_details);
?>
<br>
<?echo $row_for_product_details['product_name'];
?>
<br>
Quantity
<?echo $row_for_my_order['quantity'];
}
?>
</td>
<td width=106>
<?
$total=0;
$sql1 = "Select * from product_order_info where order_no= '$row_for_my_order1[order_no]' order by order_no ASC LIMIT $start_from,5";
$query_for_order = mysql_query($sql1);
while($row_for_my_order2=mysql_fetch_assoc($query_for_order))
{
$query_for_product_details=mysql_query("select * from product where product_no='$row_for_my_order2[product_id]' ");
$row_for_product_details=mysql_fetch_array($query_for_product_details);
$total = $total+ $row_for_product_details['sell_price'] * $row_for_my_order2['quantity'];
}
echo $total;
?>
</td>
<td width=90>
<?echo $row_for_my_order1['date'];?>
</td>
<td>
<?echo $row_for_my_order1['payment_status'];?>
</td>
<td width=100>
<? $total=0;
$sql1 = "Select * from product_order_info where order_no= '$row_for_my_order1[order_no]' order by order_no ASC LIMIT $start_from,5";
$query_for_order = mysql_query($sql1);
while($row_for_my_order3=mysql_fetch_assoc($query_for_order))
{
$total=$total+$row_for_my_order3['profit'];
}
echo $total; ?>
</td>
</tr>
<?
}
?>
</table>
</div>
<?php
$sql = "SELECT DISTINCT COUNT(order_no) FROM product_order_info";
$rs_result = mysql_query($sql);
$row = mysql_fetch_row($rs_result);
$total_records = $row[0];
$total_pages = ceil($total_records / 5);
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='view_order.php?page=".$i."'>".$i."</a> ";
};
?>
<?include "footer1.php"?>
Please help me in this regard...
You should delete LIMIT $start_from,5 from every query except the first one. As Dagon pointed out, you should consider the possibility to use a single query, to reduce the number of queries to the SQL server.