PHP while numbers in each row - php

<?php
$result10=mysql_query("SELECT * FROM blog_articles WHERE fk_semikatagori_id = 9") or die(mysql_error());
?>
<?php
$index = 1;
while($row10 = mysql_fetch_array($result10)) {
if($index%2==0) {
echo "<span class=\"f\">";
echo $row10['english_navn'];
echo "</span><br />";
}
else {
echo "<p><span class=\"f\">";
echo $row10['english_navn'];
echo "</span></p><br />";
echo "<p>";
echo $row10['english_tekst'];
echo "</p><br />";
}
$index++;
}
?>
Any ideas how I can display the number for each row?
I want it to start from 1.
I cant display the id from Mysql because it doesnt start from 1.

You could for example use a for() loop instead of a while() loop,
for ($index = 0; $row10 = mysql_fetch_array($result10); $index++)
{
...
echo $index;
}
Then the counter would be echoed.

$index = 1;
while( $row = mysql_fetch_array($result) )
{
// display the number for each row
echo $index;
// other code...
// increment number
$index++;
}

Thanks everyone for the help, it worked.
Here is the code if anyone else can use it.
<?php
$index = 1;
for ($index2 = 1; $row10 = mysql_fetch_array($result10); $index2++)
{
if($index%2==0)
{
echo "<span class=\"f\">";
echo $row10['english_navn'];
echo $index2;
echo "</span><br />";
}
else
{
echo "<p><span class=\"f\">";
echo $row10['english_navn'];
echo "</span></p><br />";
echo "<p>";
echo $index2;
echo $row10['english_tekst'];
echo "</p><br />";
}
$index++;
}
?>

I think the best solution that you can get are the follow:
Make a for statement:
<?php
for($idx=1; $row = mysql_fetch_array($result10); $idx++) {
...
}
?>
as said before or use the follow:
<?php
$idx = 1;
while($row = mysql_fetch_array($result10)) {
...
$idx++;
}
?>
I would recommend you to make a like upgrade in your select statement to get a faster result (if you start get many rows as result):
from: SELECT * FROM blog_articles WHERE fk_semikatagori_id = 9
to:
SELECT
english_navn
, english_tekst
FROM blog_articles
WHERE
fk_semikatagori_id = 9
Because you are using just these 2 columns into your loop but you are making MySQL engine get all columns from this table.
I hope it help you.

Related

Count row and repeat the same rows within Loop

I'm working on mansory gallery here images fetching into the row first row contains three images and then for the second row contains two images and so on.
Now currently all images coming inside same row. I want to add in a loop I had tried but unable to achieve the result. For inspiration link
<?php
include('admin/config.php');
$result = mysqli_query($db, "SELECT * FROM gallery order by id desc");
// var_dump($result->num_rows);
while ($row = mysqli_fetch_array($result)) {
echo "<div class='gallery-items'>";
echo "<div class='mansory-item'>";
echo "<a href='admin/images/".$row['path']."' data-lightbox='gallery' class='ansa-thumb'>";
echo "<img src='admin/images/".$row['path']."' class='item-img img-1'>";
echo "</a>";
echo "</div>";
echo "</div>";
}
?>
Current output
Expected output
Can anyone suggest me how should i get this output.
Check below snippet,
$inc = 4;
$i = 1;
while ($row = mysqli_fetch_array($result)) {
if (empty($temp) || $inc != $temp) {
$temp = $inc;
if ($inc == 4) {
echo "<div class='gallery-grid'>";
}
echo "<div class='gallery-items'>";
}
if ($i <= $inc) {
// echo $i . '<>';
echo "<div class='mansory-item'>";
echo "<a href='admin/images/" . $row['username'] . "' data-lightbox='gallery' class='ansa-thumb'>";
echo "<img src='admin/images/" . $row['username'] . "' class='item-img img-1'>";
echo "</a>";
echo "</div>";
$i++;
}
if ($i == $inc) {
echo "</div>";
if ($i == 3) {
echo "</div>";
}
$i = 1;
$inc = ($inc == 4 ? 3 : 4);
}
}

PHP Variable table size with restricted number of columns

I'm trying to make an image preview in a table, just for checking the submitted files. The table can't have more than 3 cols, and the number of rows and cels is variable, because I skip the "not found" images in DB.
I did the code below, but couldn't solve the logic by myself. The table shows the same image for row, and jumps 2 results for the next one.
<?php
$dados = mysql_fetch_array (mysql_query("SELECT id,placa,usuario FROM dados WHERE id='".$_SESSION['novaOS']."'"));
$itens = mysql_result (mysql_query("SELECT itens_acessorios FROM vist_aval WHERE idp='".$_SESSION['novaOS']."'"),0);
$sqlFotos = "SELECT * FROM imagens WHERE idp='".$_SESSION['novaOS']."'";
$qrFotos = mysql_query($sqlFotos) or die();
$rowFotos = mysql_fetch_array($qrFotos) or die();
?>
(...)
<div>
<table>
<?php
$dir_img = "../uploads/fotos/";
for($f=2;$f<=33;){
$foto = $rowFotos[$f];
$td = '<td><img src="'.$dir_img.''.$foto.'" style="margin:0;width:100%;height:auto"></td>';
$tr = '<tr id="gridpreview"></tr>';
if ($foto != false){
for ($r=0;$r<=3;$r++){
if ($r>0){
echo $td;
$f++;
}
else{
echo $tr;
}
}
}
}
?>
</table>
</div>
In addiction, the message "Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\app\Finalizar.php on line 88" appears on loading. The line 88 refers to if ($foto != false).
That's not how you do a columnar output. It should be more like
$col = 0;
echo '<tr>';
while($row = ... fetch row from result ...) {
echo "<td>$row[whatever]</td>";
$col++;
if ($col > 2) {
echo '</tr><tr>';
$col = 0;
}
}
echo '</tr>';
You don't need two loops, just the one loop, which keeps repeating until there's more no results left in the DB.
Just to register the functional code:
<table>
<?php
$dir_img = "../uploads/fotos/";
$col=0;
$f=2;
echo "<tr>";
while ($f<=33){
$foto = $rowFotos[$f];
if ($foto != false){
if ($col>2){
echo "</tr><tr>";
$col = 0;
}
else{
echo "<td><img src='".$dir_img."".$foto."' style='margin:0;width:100%;height:auto'></td>";
$col++;
$f++;
}
}
else{
$f++;
}
}
echo '<td>';
?>
</table>

how to group the data having similar values in php?

I have a table in the database named 'transactions'. I want to list out the values from this table. The result should be displayed in HTML table format. The rows in the resulted table should be grouped as per the amount.There is a column named 'amount' in the 'transactions' table.
Here is my code:
$s1 = DB::table('transactions')
->select(DB::raw("GROUP_CONCAT(selected_seats SEPARATOR '') as selected_seats"),'userid','amount','show_id')
->where("show_id","=",$s)
->groupBy('userid')
->groupBy('amount')
->orderBy('userid','ASC')
->orderBy('amount', 'DESC')
->get();
I am retrieving the values from $s1 variable through loop.
Following is the output:
and i want the result like this:
Please click on the links given above. I am new to this forum so its not allowing me to add images in the question. Kindly help.
Here is the entire code:
if($s1 != null)
{
echo "<div class='panel-body'>";
echo "<table class='table table-responsive'>"; $c = 1;
echo "<th>Drama Title</th>";
echo "<th>Show Date</th>";
echo "<th>Seat booked</th>";
echo "<th>Amount</th>";
echo "<th>User</th>";
for($i=0;$i<count($s1);$i++)
{
echo "<tr><td>".$shows1[0]->show_title."</td>";
echo "<td>".$shows[0]->show_date."</td>";
echo "<td>";
echo $s1[$i]->selected_seats;
echo "</td>";
/*echo $s1[$i]->userid."<br/>";
echo $s1[$i]->amount."<br/>";*/
$transactions = DB::table('transactions')->where('userid',$s1[$i]->userid)->where('show_id',$s1[$i]->show_id)->get();
//var_dump($transactions);
$total_amount = 0;
//echo "<pre>Users<br/>"; var_dump($transactions);
foreach ($transactions as $transaction)
{
//echo "userid ".$s1[$i]->userid." "."show id: ".$transaction->show_id." "."<br/>";
// echo "amount: ".$transaction->amount." ";
$amount = $transaction->amount;
$total_amount = $total_amount + $amount; //echo $amount."<br/>";
$c = $c+1;
//echo "no. of seats:".$c;
$users = DB::table('users')->where('id',$s1[$i]->userid)->get();
}
echo "<td>".$total_amount."</td>";
//echo $s1[$i]->userid."<br/>";
//echo "<td>".$users[0]->name."</td>";
//echo "<pre>"; var_dump($users);
echo "</td>";
if(isset($users[0]))
{
//echo "values are set";
echo "<td>".$users[0]->name."</td></tr>";
}
else
{
//echo "null value";
continue;
}
}
/*echo $shows[0]->show_date."<br/>";
echo $shows[0]->show_title;*/
//echo "<pre>"; var_dump($s1);
//echo "<td>".$users[0]->name."</td>";
//echo "</tr>";
echo "</table>";
echo "</div>";
?>
}
else
{
echo "No records found";
}
Easiest way I think is calculate amounts in the loop you are printing data or readying data to send to a view. Since your data sorted by user_id, you always can change amount variable in the loop when user_id is changing. You can use count(explode($seat_variable)) to get number of seats in a single run of the loop. Look at the example code below.
$num_seat = 0; $amount = 0; $running_user_id = -1;
foreach ($row as $entry) {
...
if ($running_user_id != $entry['user_id']) { // check if the same user
$running_user_id = $entry['user_id'];
$num_seat = 0; $amount = 0; // resetting variable values for seperate user.
}
$num_seat += count(explode($entry['selected_seats']));
$amount += $entry['amount'];
...
}
And I assume you have missing data like emails in the query.
Code update after questioner added his code.
if($s1 != null) {
echo "<div class='panel-body'>";
echo "<table class='table table-responsive'>"; $c = 1;
echo "<tr>";
echo "<th>Drama Title</th>";
echo "<th>Show Date</th>";
echo "<th>Seat booked</th>";
echo "<th>Amount</th>";
// echo "<th>User</th>";
echo "</tr>";
$category = ""; $num_seats = 0;
for ($i=0; $i<count($s1); $i++) {
if ($category != $amount) {
if ($i > 0) { // print totals
echo "<tr>
<td colspan='3' align='right'>Total</td>
<td>".$num_seats."</td>
<td>".($num_seats * $amount)."</td>
</tr>";
echo "<tr><td colspan='5'> </td></tr>"; // empty row after totals printed
$num_seats = 0; //resetting number of seats per category
}
echo "<tr><td colspan='5'><h2>Category : ".$amount."</h2></td></tr>"; // printing category line in the given table
$category = $amount; // this logic is to prevent category line printing for each row
}
$transactions = DB::table('transactions')->where('userid',$s1[$i]->userid)->where('show_id',$s1[$i]->show_id)->get();
echo "<tr>";
$users = DB::table('users')->where('id', $s1[$i]->userid)->get();
// Check below values are correct or not
echo "<td>".$users[0]->name."</td>";
echo "<td>".$s1[$i]->userid."</td>";
echo "<td>".$s1[$i]->email."</td>"; // get user's email property here
echo "<td>".$s1[$i]->selected_seats."</td>";
$num_seats += count(explode(',', $s1[$i]->selected_seats));
echo "<td> </td></tr>"; // empty colomn for amount and the row end
}
echo "</table>";
echo "</div>";
}

mysqli/PHP - first two rows never show up(table with row data going across columns then moving down)

My website currently ignores the first two images you place into the database and then proceeds to add images going across 5 columns and then moving down to the next row.
Update: Now it shows 3 of the 4 images in the database. Skips one image.
<?php
$i = 1;
echo "<table>";
while ($row = $Recordset2->fetch_object()) {
if ($i == 1) {
echo "<tr>";
}
echo '<td><img src="'.$row_Recordset2['ImgSource'].'" width="100" height="100"></td>';
if ($i == 5) {
$i = 1;
echo "</tr>";
} else {
$i++;
}
}
echo "</table>";
?>
This is what my database looks like
http://i.stack.imgur.com/IFba8.jpg
This is what my website shows
http://i.stack.imgur.com/Wf7E1.jpg
Try this:
<?php
$i = 1;
echo "<table>";
while ($row = $Recordset2->fetch_object()) {
if ($i == 1) {
echo "<tr>";
}
echo '<td><img src="'.$row['ImgSource'].'" width="100" height="100"></td>';
if ($i == 5) {
$i = 1;
echo "</tr>";
} else {
$i++;
}
}
echo "</table>";
?>
Please try this.
<?php
$i = 1;
echo "<table>";
while ( $row = $Recordset2->fetch_object() ) {
if ($i == 1) {
echo "<tr>";
}
echo '<td><img src="'.$row_Recordset2['ImgSource'].'" width="100" height="100"></td>';
if ($i == 5) {
$i = 1;
echo "</tr>";
} else {
$i++;
}
}
echo "</table>";
?>

split images from array

i am getting links from a database and want to display the images but break line every 5 images. i can display images but need help, I'm sure its an if statement but don't know how to write it. i need to display
image1,image2,image3,image4,image5 break
image6,image7,image8,image9,image10 break
and so on, i have a total of 100 images
<?php
include('connect.php');
$query = "SELECT * FROM `image`";
$result = mysql_query($query);
$pics = array();
while($row = mysql_fetch_array($result)){
$pics[] = "\"".$row['src']."\"";
}
foreach($pics as $show){
echo "<img src=".$show.">";
}
?>
You can use another variable to count the entries
$count=0;
foreach($pics as $show){
echo "<img src=".$show.">";
if( $count % 5 == 0 ) echo "<br>";
$count++;
}
$count = 1;
foreach($pics as $show){
if($count < 6)
{
echo '<img src="'.$show.'">';
$count++;
}
else
{
echo '<br><img src="'.$show.'">';
$count = 1;
}
}

Categories