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>
Related
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>";
}
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>";
?>
<?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.
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;
}
}
I'm trying to display all the information in a msSQL table in an HTML table and have up with something that is not so great.
<table border="1">
<?
echo "<tr>";
for ($i = 0; $i < mssql_num_fields($result); ++$i){
echo "<th>" .$column_names[$i] . "</th>";
}
echo "</tr>";
$num_rows = mssql_num_rows($result);
for ($i = 0; $i < $num_rows; ++$i){
echo "<tr>";
foreach ($column_names as $key => $val){
$result_row = mssql_query("SELECT * FROM username WHERE id = '$i'");
$row = mssql_fetch_assoc($result_row);
echo "<td>";
echo $row[$val];
echo "</td>";
}
echo "</td>";
}
?>
</table>
This works. The first part prints out the column names successfully, but as for the rest:
1) I think it is sort of cumbersome to make a query for every time through the loop
2) it doesn't really work because the ids of the rows go much higher than the number of rows in the table, as some ids aren't used.
It seems like I should be able just make one query and pull everything from the database at one go, then build my HTML table from that, but I can't figure out how to access it row by row where I could go $row[next row][shifted value from $column_names. How can improve this query?
function table_cell($item, $header=false) {
if (!$item) return '';
$elemname = ($header) ? 'th' : 'td';
$escitem = htmlspecialchars($item, ENT_NOQUOTES, 'UTF-8');
return "<{$elemname}>{$escitem}</{$elemname}>";
}
function table_header_cell($item) {
return table_cell($item, true);
}
function table_row($items, $header=false) {
$func = ($header) ? 'table_header_cell' : 'table_cell';
return "<tr>\n\t".implode("\n\t", array_map($func, $items))."\n</tr>\n";
}
function echo_result_as_table($result) {
if ($result && $row = mssql_fetch_assoc($result)) {
$columnnames = array_keys($row);
echo "<table>\n", table_row($columnnames, true), "\n";
do {
echo table_row($row), "\n";
} while ($row = mssql_fetch_assoc($result));
echo "</table>\n";
}
}
$result = mssql_query("SELECT * FROM username");
echo_result_as_table($result);
if ($result) mssql_free_result($result);
If you have an array of associative arrays instead of a raw mssql result handle, you can use a function like this to produce a table string:
function array_to_table($arrayofassoc) {
if (!$arrayofassoc) return '';
$tablestr = '<table>';
$tablestr .= table_row(array_keys($arrayofassoc[0]), true);
$tablestr .= implode('', array_map('table_row', $arrayofassoc));
$tablestr .= '</table>';
return $tablestr;
}
try this:
while($row = mysql_fetch_results($result)){
echo '<td>'.$row['column_name'].'</td>';
}
with 'column_name' being the name of the column in mysql.
but some will say..."wait, but PDO"