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;
}
}
Related
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>
I want to warp every two row with a class. So I saw some guideline from here and googling and found foreach array_chunk to do this. And I tried as below which can't display any result. Without foreach its work well. Were is my wrong here please?
In the above picture that I want to do; my every two category warp with a class. And add a divider after each top category.
Here is my tried:
echo '<ul class="dropdown-menu dropdown-menu-large row">';
$sql = "SELECT id,name FROM main_cata ORDER BY id ASC";
$execute = $dbh->query("$sql");
$rowcount = $execute->num_rows ;
$row = $dbh->query($sql) ;
while ($row = $execute->fetch_assoc()) {
foreach (array_chunk($row, 2, true) as $array){
echo'<li class="col-sm-3">';
foreach($array as $rows){
echo '<ul><li class="dropdown-header">'.$rows->name.'</li>';
$sql2 = "SELECT id,name,page FROM catagory WHERE m_cata = '".$rows->id."' ORDER BY id ASC";
$execute2 = $dbh->query("$sql2");
$rowcount2 = $execute2->num_rows ;
$row = $dbh->query($sql) ;
while ($row = $execute2->fetch_assoc()) {
$cid = $row['id'];
$cname = $row['name'];
$page = $row['page'];
echo '<li>'.$cname.'</li>';
}
echo '<li class="divider"></li></ul>';
}
echo '</li>';
}
}
echo '</ul>';
while ($row = $execute->fetch_assoc()) {
fetch_assoc returns 1 row (id, name) from the query, then you are using the result and spliting in 2 (https://secure.php.net/manual/pt_BR/mysqli-result.fetch-assoc.php), What you can do is, make a counter instead of the array chunk that resets every time it reachs 2.
foreach (array_chunk($row, 2, true) as $array){
The content of $row is, for example, array('id' => 1, 'name' => 'Category 1').
foreach($array as $rows){
You are iterating again without needing to.
A simple version:
counter = 0;
echo "<ul>";
while (row = execute->fetch_assoc()) {
if (counter == 0) {
echo "init li";
}
echo "subinit li";
get subcategories
while (row2 = execute2->fetch_assoc()) {
print content
}
echo "subend li";
if (counter == 2) {
counter == 0;
echo "end li";
} else {
counter++;
echo "divider";
}
}
echo "</ul>";
<?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'm trying to make this take 6 times, and then roll over into another row. I'm sure there's a better way of going about this, and that's probably why mine doesn't work.
However, when I use the foreach() it displays nothing, and when I use the while() the page breaks completely. The headers don't send, and php_error doesn't catch it.
All the queries work just fine, it's the display that's causing issues.
Perhaps you guys can help?
public static function DisplayInv($user) {
$user = users::lookup($user);
$sql = mysql_query("SELECT * from `inventory` where `userid`='{$user['id']}'");
$limit = 6;
$count = 0;
$fetch = mysql_fetch_array($sql) or die('Error: '.mysql_error());
while($count <= 7) {
print "<div class='row-fluid show-grid'>";
foreach($fetch as $items) {
$getItem = self::ItemInfo($items['itemid']);
print "<span class='span2'><b>{$getItem['name']}</b><br />{$getItem['description']}<br /><b>Power: {$getItem['power']}</b></span>";
$count++;
}
/* while($items = mysql_fetch_array($sql)) {
$getItem = self::ItemInfo($items['itemid']);
print "<span class='span2'><b>{$getItem['name']}</b><br />{$getItem['description']}<br /><b>Power: {$getItem['power']}</b></span>";
$count++;
}*/
print "</div>";
if($count == 6) {
$count = 0;
}
}
}
I guess you are looking for something like this?
public static function DisplayInv($user) {
$user = users::lookup($user);
$sql = mysql_query("SELECT * from `inventory` where `userid`='{$user['id']}'");
$limit = 6;
$count = 0;
print "<div class='row-fluid show-grid'>";
while($items = mysql_fetch_array($sql)) {
$getItem = self::ItemInfo($items['itemid']);
print "<span class='span2'><b>{$getItem['name']}</b><br />{$getItem['description']}<br /><b>Power: {$getItem['power']}</b></span>";
if($count == $limit){
print "</div><div class='row-fluid show-grid'>";
$count = 0;
}else{
$count++;
}
}
print "</div>";
}
let's say that i have this simplyfied code:
$sql = dbquery("SELECT * FROM videos WHERE views > 4 ORDER BY id DESC LIMIT 0,10 ");
while($row = mysql_fetch_array($sql)){
$url = $row["url"];
$title = $row["title"];
$list ='<div><a href="'.$url.'" >'.$title.'</a></div>';
$ad ='<div>something here</div>';
}
echo $list;
instead to display a list of 10 divs, i want to echo 5 divs from $list, echo $ad then echo the rest of the $list
How can i do this?
Later edit :
First problem solved thanks to Michael.
Now, i have a problem with my template, and i don't know how can i add to every X number of $list divs, class="nomar"?
You can use a counter $i
$sql = dbquery("SELECT * FROM videos WHERE views > 4 ORDER BY id DESC LIMIT 0,10 ");
$i = 1;
// Use mysql_fetch_assoc() rather than mysql_fetch_array()!
while($row = mysql_fetch_assoc($sql)){
$url = $row["url"];
$title = $row["title"];
// Change the list class on a certain number...
if ($i == 3) {
$list_class = "normal";
}
else $list_class = "some-other-class";
// Incorporate the new class
$list ='<div class="' . $list_class . '"><a href="'.$url.'" >'.$title.'</a></div>';
// Output $list
echo $list;
// Increment your counter
$i++;
// Output $ad when you reach 5
// This only happens once. Afterward, $list continues to print.
if ($i == 5) {
$ad ='<div>something here</div>';
echo $ad;
}
}