I have an array which is populated from a MySQL query:
if ($result) {
$foundResult = true;
foreach ($result as $row) {
array_push($searchResultAccount,$row->Account);
array_push($searchResultUsername,$row->Username);
array_push($searchResultPassword,$row->Password);
array_push($searchResultCreated,$row->Created);
array_push($searchResultStrength,$row->Strength);
}
} else {
array_push($error,"No results found");
}
Further down in my page, I want to create a table and populate the bootstrap table with the results from the array. However, each cell in the table prints all sets of results for that given field. I suspect I have a problem with my loop logic. I tried changing from a foreach to a for loop but still no luck:
<tbody>
<?php for ($x = 0; $x < sizeof($result); $x++) { ?>
<tr>
<td><?php for ($i = 0; $i < sizeof($searchResultAccount); $i++){echo($searchResultAccount[$i]);}?></td>
<td><?php for ($i = 0; $i < sizeof($searchResultUsername); $i++){echo($searchResultUsername[$i]);}?></td>
<td><?php for ($i = 0; $i < sizeof($searchResultPassword); $i++){echo($searchResultPassword[$i]);}?></td>
<td><?php for ($i = 0; $i < sizeof($searchResultCreated); $i++){echo($searchResultCreated[$i]);}?></td>
<td><p>ExpiresHere</p></td>
<td><?php for ($i = 0; $i < sizeof($searchResultStrength); $i++){echo($searchResultStrength[$i]);}?></td>
</tr>
<?php } ?>
</tbody>
The outcome looks like below:
I have 2 results in the array, and as you can see they're printing together in each cell. Any ideas on where I've went wrong ?
Edit: Full PHP code as from the query string below:
$sql = "SELECT * FROM Accounts WHERE User_ID = :userid AND Account = :account";
$stmt = $pdo->prepare($sql);
$stmt->bindValue(':userid',$userid);
$stmt->bindValue(':account',$account);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_CLASS);
if($result) {
$foundResult = true;
foreach($result as $row) {
array_push($searchResultAccount,$row->Account);
array_push($searchResultUsername,$row->Username);
array_push($searchResultPassword,$row->Password);
array_push($searchResultCreated,$row->Created);
array_push($searchResultStrength,$row->Strength);
}
} else {
array_push($error,"No results found");
}
Work like this:
foreach($result as $row) {
?>
<tr>
<td><?php echo $row->Account; ?></td>
<td><?php echo $row->Username; ?></td>
<td><?php echo $row->Password; ?></td>
<td><?php echo $row->Created; ?></td>
<td><?php echo $row->Strength; ?></td>
</tr>
<?php
}
with this approach you create table row for each result row
You have to remove your for loops inside of td elements
foreach($result as $row) {
<tr>
<td><?php echo $row->Account;?></td>
<td><?php echo $row->Username;?></td>
<td><?php echo $row->Password;?></td>
<td><?php echo $row->Created;?></td>
<td><?php echo $row->Strength;?></td>
</tr>
}
The problem is occurring because of lines like this.
for($i = 0; $i < sizeof($searchResultAccount); $i++){echo($searchResultAccount[$i]);}
I'm "guessing" that the problem is occurring because this is the length of the rows you want to print, not the count of data you want the print on the table.
Try changing the rows from:
<?php for($i = 0; $i < sizeof($searchResultAccount); $i++){echo($searchResultAccount[$i]);}?>
into
<td><?php echo($searchResultAccount[$x]) ; } ?></td>
You are now looping trough each result in each tablerow (tr). With this code, you are only focussing the result that belongs to the tr.
<tbody>
<?php for($x = 0; $x < sizeof($result); $x++) { ?>
<tr>
<td><?php echo($searchResultAccount[$x]);?></td>
<td><?php echo($searchResultUsername[$x]);?></td>
<td><?php echo($searchResultPassword[$x]);?></td>
<td><?php echo($searchResultCreated[$x]);?></td>
<td><p>ExpiresHere</p></td>
<td><?php echo($searchResultStrength[$x]);?></td>
</tr>
<?php } ?>
</tbody>
Related
Here's my code sir:
<?php
session_start();
include 'include/db_config.php';
$result = $dbs->prepare("SELECT * FROM service_info ORDER BY id DESC");
/*$result->bindParam(':id', $_SESSION['id']);*/
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
$result2 = $dbs->prepare("SELECT *FROM customer_info ORDER BY id DESC");
$result2->execute();
for($j=0; $row2 = $result2->fetch();$j++){
?>
<tr class="record">
<td><?php echo $row2['firstname'];?></td>
<td><?php echo $row['no_guest']; ?></td>
<td><?php echo $row['type_service']; ?></td>
<td><?php echo $row['datepicker']; ?></td>
<td><?php echo $row['t_time']; ?></td>
<td> delete </td>
</tr>
<?php
}
}
?>
i didnt use a LEFT JOIN for displaying data's from 2 tables. i just want to do it in my own way . But my problem is it duplicates my data . before i inserted my second query its just 2 data's and now its 4 already. i just cant figure it out where is the duplication occurs.
Someone help me out please . Thanks in Advance.
You display data in second for, which is inside first for. So you get result count of customer_info table length*service_info length results. You should save info in arrays and then use only one for cycle. Eg.:
<?php
session_start();
include 'include/db_config.php';
$services = array();
$customers = array();
$result = $dbs->prepare("SELECT * FROM service_info ORDER BY id DESC");
/*$result->bindParam(':id', $_SESSION['id']);*/
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
$services[] = $row;
}
$result2 = $dbs->prepare("SELECT *FROM customer_info ORDER BY id DESC");
$result2->execute();
for($j=0; $row2 = $result2->fetch();$j++){
$customers[] = $row2;
}
foreach($services as $key => $service) {
?>
<tr class="record">
<td><?php echo $customers[$key]['firstname'];?></td>
<td><?php echo $service['no_guest']; ?></td>
<td><?php echo $service['type_service']; ?></td>
<td><?php echo $service['datepicker']; ?></td>
<td><?php echo $service['t_time']; ?></td>
<td> delete </td>
</tr>
<?php
}
?>
Btw, i suggest you use different approach, like joins
In phpMyAdmin I have a simple query:
SELECT * FROM `recent` WHERE datediff(now(), `timestamp`) > 1
BUT when I try to do this in my clear_recent.php:
<?php $result = $conn->query("SELECT * FROM `recent` WHERE datediff(now(), `timestamp`) > 1"); ?>
<?php foreach ($result->fetch_assoc() as $row): ?>
<?php while($row = $result->fetch_assoc()) { ?>
<tr>
<td><?php echo($title = $row["id"]); ?></td>
<td><?php echo($title = $row["pid"]); ?></td>
<td><?php echo($title = $row["user_id"]); ?></td>
<td><?php echo($title = $row["timestamp"]); ?></td>
</tr>
<?php } ?>
<?php endforeach; ?>
I get an error:
Invalid argument supplied for foreach() in /database/chron/clear_recent.php
Cannot for the life of my figure out what's wrong!!!! Please help!
You should check if the query returns any row. try the following code.
<?php
$result = $conn->query("SELECT * FROM `recent` WHERE datediff(now(), `timestamp`) > 1");
$rows = $result->fetch_assoc();
//check if some rows available.
if (count($rows) > 0) {
foreach ($rows as $row) {
while ($row = $rows) {
?>
<tr>
<td><?php echo($title = $row["id"]); ?></td>
<td><?php echo($title = $row["pid"]); ?></td>
<td><?php echo($title = $row["user_id"]); ?></td>
<td><?php echo($title = $row["timestamp"]); ?></td>
</tr>
<?php
}
}
}
?>
additional question: why you use while loop under the foreach loop? is it right?
You are getting this error because no rows return from this query.
there are multiple column in my table ..
col1 col2 col3 price
500 700 100 10
501 700 100 20
502 700 100 30
503 700 100 10
4 70
I need to get count of col1 and display its sum.
and also display sum of price column...
But the main thing is i need to display this in last row....after all data...how can select this and echo in last row...
plz help me...
I need to echo exactly as i put the data in above table...
I need sql query and also need help to echo only sum of this two column in last row only......
SELECT *,IFNULL(col1,'SUM'), count(*) as count FROM coupon_entry WHERE Group By col1 WITH ROLLUP
<?php if (is_array($data)) { foreach($data as $row){ ?>
<tr>
<td><?php echo htmlspecialchars($row['col1']); ?></td>
<td><?php echo htmlspecialchars($row['col2']); ?></td>
<td><?php echo htmlspecialchars($row['col3']); ?></td>
<td><?php echo htmlspecialchars($row['price']); ?></td>
</tr>
<?php } }?>
One solution is to calculate the sum in PHP with variables :
<?php if (is_array($data)) {
$totalCol1 = 0;
$totalPrice = 0;
foreach($data as $row){
$totalCol1 += $row['col1'];
$totalPrice += $row['price'];
?>
<tr>
<td><?php echo htmlspecialchars($row['col1']); ?></td>
<td><?php echo htmlspecialchars($row['col2']); ?></td>
<td><?php echo htmlspecialchars($row['col3']); ?></td>
<td><?php echo htmlspecialchars($row['price']); ?></td>
</tr>
<?php }
<tr>
<td><?php echo $totalCol1;?></td>
<td></td>
<td></td>
<td><?php echo $totalPrice;?></td>
</tr>
}?>
Either you have to do 2 separate queries or else you have to do your calculations in PHP - either one is fairly simple although the PHP solution will probably be slightly (negligibly?) faster.
double-query:
$r = query('SELECT *
FROM yada-yada');
while ($row = fetch($r)) {
echo "<td>$row[col1]</td>\n";
echo "<td>$row[col2]</td>\n";
echo "<td>$row[col3]</td>\n";
echo "<td>$row[price]</td>\n";
}
$r2 = query('SELECT COUNT(*) as cnt, sum(col1) as sum_col1,
sum(price) as sum_price
FROM yada-yada...');
if ($row = fetch($r2)) {
echo "<td>$row['cnt']</td><td>$row['sum_col1']</td>...$row['sum_price']...\n";
}
calculate in PHP:
$cnt = $sum_col1 = $sum_price = 0;
$r = query('SELECT *
FROM yada-yada');
while ($row = fetch($r)) {
echo "<td>$row[col1]</td>\n";
echo "<td>$row[col2]</td>\n";
echo "<td>$row[col3]</td>\n";
echo "<td>$row[price]</td>\n";
$cnt++;
$sum_col1 += $row['col1'];
$sum_price += $row['price'];
}
echo "<td>$cnt</td><td>$sum_col1</td>...$sum_price...\n";
Below is my sql query to get count and sum of column.
SELECT COUNT(coupon) As Total,SUM(Price) AS TotalPrice FROM coupon_entry
And put this in seprate row below table...
<?php
foreach($tot as $tota)
?>
<tr style="background-color:#33CCFF;">
<td colspan="2" style="text-align:right; font-weight:bold;">Total Coupon</td>
<td><?php echo $tota['Total'];?></td>
<td style="text-align:right; font-weight:bold;">Total Amount in Rs</td>
<td><?php echo $tota['TotalPrice'];?></td>
</tr>
<?php }?>
I want to recapitulate report that grouped by month. But the results of sum is incorrect. It's just sum data in one month and show in all rows.
<?php
$query = mysql_query("SELECT * FROM barang GROUP BY arsip");
$i = 1;
while ($data = mysql_fetch_assoc($query)) {
?>
<tr class="<?php if ($i % 2 == 0) { echo "odd"; } else { echo "even"; } ?>">
<td><?php echo $i; ?></td>
<td><?php echo $data['arsip']; ?></td>
<td><?php
$Masuk= mysql_query("SELECT SUM(barang_in) AS masuk FROM barang GROUP BY arsip");
if($Masuk){
$data = mysql_fetch_assoc($Masuk);
echo $data['masuk'];
}
?></td>
<td><?php
$keluar= mysql_query("SELECT SUM(bijih_out+htm_out+pth_out) AS keluar FROM barang GROUP BY arsip");
if($keluar){
$data = mysql_fetch_assoc($keluar);
echo $data['keluar'];
}?></td>
<td><?php
$efisiensi= mysql_query("SELECT SUM((bijih_out+htm_out+pth_out)-barang_in) AS efisiensi FROM barang GROUP BY arsip");
if($efisiensi){
$data = mysql_fetch_assoc($efisiensi);
echo $data['efisiensi'];
}
?></td>
<td><?php
$persen= mysql_query("SELECT SUM(barang_in/(bijih_out+htm_out+pth_out))*1 AS persen FROM barang GROUP BY arsip");
$simbol = "%";
if($persen){
$data = mysql_fetch_assoc($persen);
echo number_format($data['persen'],2); echo $simbol;
}
?></td>
<?php
$i++;
}
?>
so that's my code. what's wrong with that? There are some columns for displaying income items, outcome items, efficiency ( difference of outcome and income items ) and percent of efficiency.
finally I found out the right code. Thankyou for #RandomSeed who helps me. This is my final code and it works!
<?php
$query = mysql_query("SELECT arsip, SUM(barang_in) AS income, SUM(bijih_out+htm_out+pth_out) AS outcome, SUM((bijih_out+htm_out+pth_out
)-barang_in) AS efficiency, SUM((barang_in / ( bijih_out + htm_out + pth_out))*100) AS percent FROM barang GROUP BY arsip");
$i = 1;
while ($data = mysql_fetch_assoc($query)) {
?>
<tr class="<?php if ($i % 2 == 0) { echo "odd"; } else { echo "even"; } ?>">
<td><?php echo $i; ?></td>
<td><?php echo $data['arsip']; ?></td>
<td><?php echo $data['income']; ?></td>
<td><?php echo $data['outcome']; ?></td>
<td><?php echo $data['efficiency']; ?></td>
<td><?php $simbol = "%"; echo number_format($data['percent'],0); echo $simbol; ?></td>
<?php
$i++;
}
?>
I want to make a table, and then for each 6 rows should have a tr, and then the rows are inside td.
So example:
<tr>
<td><?php echo $show[id]; ?></td> // 1
<td><?php echo $show[id]; ?></td> // 2
<td><?php echo $show[id]; ?></td> // 3
<td><?php echo $show[id]; ?></td> // 4
<td><?php echo $show[id]; ?></td> // 5
<td><?php echo $show[id]; ?></td> // 6
</tr> <tr> // start new tr after 6 rows
...repeat the tds
How can i do something like this?
I have tried myself doing
<tr>
<?php
while ($show == mysql_fetch_array($query)){ ?>
<td><?php echo $show[id]; ?></td>
<?php } ?>
</tr>
But as you can see this just inserts everything in one tr..
Thank you
<tr>
<?php
$c = 0; // Our counter
$n = 6; // Each Nth iteration would be a new table row
while ($show = mysql_fetch_array($query))
{
if($c % $n == 0 && $c != 0) // If $c is divisible by $n...
{
// New table row
echo '</tr><tr>';
}
$c++;
?>
<td><?php echo $show[id]; ?></td>
<?php
} ?>
</tr>
Related links:
Modulus operator (%) on php.net
Count the number of lines if the modulo 6 is null then echo the </tr><tr>
<tr>
<?php
$i=0;
while ($show == mysql_fetch_array($query)){ ?>
<td><?php echo $show[id]; ?></td>
<?php if(++$i%6 == 0) echo '</tr><tr>'; ?>
<?php } ?>
</tr>
You can do:
<tr>
<?php
$count = 0;
while ($show = mysql_fetch_array($query)){
if($count == 6) {
$count = 0;
echo "</tr> <tr>";
}
echo "<td>".$show[id]."</td>";
$count++;
}
</tr>
You’re accidentally using the comparison operator == instead of an assignment operator =.
And to always put six cells into each row, I’d do this:
$perRow = 6;
$counter = 0;
echo '<tr>';
while ($show = mysql_fetch_array($query)) {
if ($counter % $perRow === 0 && $counter !== 0) {
echo '</tr><tr>';
}
echo '<td>', $show['id'], '</td>';
$counter++;
}
while ($counter++ % $perRow !== 0) {
echo '<td></td>';
}
echo '</tr>';
This will ensure that each row is properly filled with six cells.
<?php
$countRows = 0;
while ($show == mysql_fetch_array($query)){
if($countRows == 0) echo '<tr>';
?>
<td><?php echo $show[id]; ?></td>
<?php
$countRows++;
if($countRows == 6){
$countRows = 0;
echo '</tr>';
}
?>
<?php } ?>
<?php if($countRows < 6) echo '</tr>'; ?>