Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have main div with three different classes: CLASS1; ClASS2 AND CLASS3. Now I want to use a single while loop in all classes to get three images from database and display.
First class is class1 then two div have same class class2 and then last div has class3. How to do it through loop, please help, Thanks :)
<div class="col-md-9 hidden-sm hidden-xs">
<div class="col-md-1"></div>
<div class="col-md-2">
<div class="ad1">
<img src="" alt="">
</div>
</div>
<div class="col-md-2">
<div class="ad2">
<img src="" alt="">
</div>
</div>
<div class="col-md-2">
<div class="ad2">
<img src="" alt="">
</div>
</div>
<div class="col-md-5">
<div class="ad3">
<img src="" alt="">
</div>
</div>
</div>
<?php
$query = "SELECT * FROM headerimages limit 4";
$result = mysqli_query($con, $query) or die(mysqli_error($query));
while ($row = mysqli_fetch_array($result)) { ?>
Answer updated to reflect changes in the Question
I can see roughly what you are trying to achieve, hope this helps you...
<div class="col-md-9 hidden-sm hidden-xs">
<div class="col-md-1"></div>
<?php
$query = "SELECT * FROM headerimages limit 4";
$result = mysqli_query($con, $query) or die(mysqli_error($query));
$class_number = 1;
$class_two_repeated = FALSE;
while ($row = mysqli_fetch_array($result)):
// This bit ensures the 2nd & 3rd divs have class 'ad2'
if ($class_number == 3 && !$class_two_repeated) {
$class_two_repeated = TRUE;
$class_number--;
}
?>
<div class="col-md-<?= $class_number == 3 ? 5 : 2 ?>">
<div class="ad<?= $class_number++ ?>">
<img src="/path/to/<?= $row['whatever_the_image_field_is_named'] ?>" alt="">
</div>
</div>
<?php
endwhile;
?>
</div>
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
This php while loop is intended to display information about products stored in MySql database. The while loop is supposed to display the ENTIRE PRODUCTS in the database in bootstrap cards. "Fabric Used" and "Tailor" for each product are stored in separate tables in the database. The tables are connected with foreign keys. Why did the loop stop after displaying only one product AND how do i fix this?
<div class="row" id="result">
<?php
$query ="SELECT * FROM products, tailor_details";
$result=$dbcon->query($query);
while($row=$result->fetch_assoc()){
?>
<div class="col-md-3 mb-2">
<div class="card-deck">
<div class="card border-secondary">
<img src="<?= $row['Picture'] ?>" class="card-img-top">
<div class="card-body">
<h6 class="text light bg-info text-center rounded p-1"><?= $row['Name']; ?></h6>
<h4 class="card-title text-danger">Price: <?= number_format($row['Price']); ?>/-
</h4>
<p>Weight: <?= $row['Weight']; ?><br>Tailor:
<?php
$tailor=$row['Tailor'];
$getTailor="SELECT biz_name
FROM tailor_details
WHERE user_id=$tailor";
$res=$dbcon->query($getTailor);
while($rw = $res->fetch_assoc()){
echo $row['biz_name'];
}
?>
<br>
Fabric Used:
<?php
$fabric=$row['Fabric'];
$getfabric="SELECT FabricName FROM fabrics WHERE FabricsId=$fabric";
$result=$dbcon->query($getfabric);
while($row = $result->fetch_assoc()){
echo $row['FabricName'];
}
?>
</p>
Customize
</div>
</div>
</div>
</div>
<?php
} // endwhile
?>
</div>
“I expected the while loop to display every single product and its details in bootstrap cards, just like seen in ecommerce sites; but the actual output I got is just one product and the loop stopped!.”
See database schema below:
products table
fabrics table
tailor_detals table
Screenshot of output
As a lot of peolpe told you in their comments, you don't need three queries, you can do it just with one query. I have changed your code to use only one query:
<div class="row" id="result">
<?php
$query ="SELECT picture,`Name`,price, biz_name,FabricName FROM products a join tailor_details b on a.Tailor=b.user_id join fabrics c on c.FabricsId=a.Fabric";
$result=$dbcon->query($query);
while($row=$result->fetch_assoc()){
?>
<div class="col-md-3 mb-2">
<div class="card-deck">
<div class="card border-secondary">
<img src="<?= $row['Picture'] ?>" class="card-img-top">
<div class="card-body">
<h6 class="text light bg-info text-center rounded p-1"><?= $row['Name']; ?></h6>
<h4 class="card-title text-danger">Price: <?= number_format($row['Price']); ?>/-
</h4>
<p>Weight: <?= $row['Weight']; ?><br>Tailor:
<?php
echo $row['biz_name'];
?>
<br>
Fabric Used:
<?php
echo $row['FabricName'];
?>
</p>
Customize
</div>
</div>
</div>
</div>
<?php
} // endwhile
?>
</div>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Here i am generating dynamic div i want to execute div first and second alternatively in PHP as dynamic values comes.. please help me related this.
here $count=$content_faq['number'];count of the result
<?php
foreach($content_faq['rows'] as $howit){
$count=$content_faq['number'];
$title=$howit->title;
$images=$howit->image;
$description=$howit->description;
for($i=0; $i < $count;$i++)
{
if($i % 2){ //remove quotes around 0
?>
<!-- div first -->
<div id="<?php echo rtrim($title); ?>" style="height: 400px; margin-top: 40px;">
<section >
<div class="container about-intro" >
<div class="col-lg-6">
<div class="image1_work">
<img class="img" height="380px" width="100%" src="<?=SITE_MEDIA?>images/howitwork/<?php echo $images?>" alt="image" >
</div>
</div>
<div class="col-lg-6">
<div class="" data-aos="fade-up">
<?php echo $description; ?>
</div>
</div>
</div>
</section><!-- closing of section is misplaced -->
</div>
<?php } else {?>
<!-- div Second -->
<div id="<?php echo rtrim($title); ?>" style="height: 400px; margin-top: 40px;">
<section >
<div class="container about-intro" >
<div class="col-lg-6">
<div class="" data-aos="fade-up">
<?php echo $description; ?>
</div>
</div>
<div class="col-lg-6">
<div class="image1_work">
<img class="img" height="380px" width="100%" src="<?=SITE_MEDIA?>images/howitwork/<?php echo $images?>" alt="image" >
</div>
</div>
</div>
</section>
</div>
<?php } } } ?>
My if and else condition not working in that case whats wrong there can anyone please help related this..
I want if id =1 then if part div execute, if id=2 cos its divisible and reminder is 0 then else part to be executed.. but i am not getting the same result what i am doing wrong here.
A job for the wonderful modulus operator!
Grab the array id with your foreach loop:
foreach ($content_faq['rows'] as $id=>$howit) {
And then use the modulus to alternate
if ($id % 2)
This will evaluate to 0 and 1 alternately, which we know are false and true.
i am using a main fours divs first div has class ad1, second and third div has class ad2 and then last div has class ad3...
i want to display 4 images from database
How to do it through while loop, please help, Thanks :)
here is html code and the php code....
<div class="col-md-9 hidden-sm hidden-xs">
<div class="col-md-1"></div>
<div class="col-md-2">
<div class="ad1">
<img src="" alt="">
</div>
</div>
<div class="col-md-2">
<div class="ad2">
<img src="" alt="">
</div>
</div>
<div class="col-md-2">
<div class="ad2">
<img src="" alt="">
</div>
</div>
<div class="col-md-5">
<div class="ad3">
<img src="" alt="">
</div>
</div>
</div>
php code is
<?php
$query = "SELECT * FROM headerimages limit 4";
$result = mysqli_query($con, $query) or die(mysqli_error($query));
while ($row = mysqli_fetch_array($result))
{?>
<?php
}
?>
First of all I recommend to use pdo database driver.
In your code the fast fix will be to assign result to array and simply echo it in the view.
$result = array();
while ($row = mysqli_fetch_array($result))
{
$result[]=$row['img'];
}
and in view echo it
<div class="col-md-9 hidden-sm hidden-xs">
<div class="col-md-1"></div>
<div class="col-md-2">
<div class="ad1">
<img src="<?=$result[0]?>" alt="">
</div>
</div>
<div class="col-md-2">
<div class="ad2">
<img src="<?=$result[1]?>" alt="">
</div>
</div>
<div class="col-md-2">
<div class="ad2">
<img src="<?=$result[2]?>" alt="">
</div>
</div>
<div class="col-md-5">
<div class="ad3">
<img src="<?=$result[3]?>" alt="">
</div>
</div>
</div>
I have this carousel that I want to make dynamic. To operate, the carousel must begin with a class
<div class="item active">
and must stay out of the while loop. After the fourth record extract, the class must become simply
<div class="item">
In summary:
0 to 4 -> <div class="item active">
5 to 8 --><div class="item">
9 to 12 --> <div class="item"> ....so on
How do I count the records extracted?
Thanks
<?php
$banner = "SELECT * FROM tbl_banner";
$result_b = dbQuery($banner);
// here --> <div class="item active"> or <div class="item">
while($row_b = dbFetchAssoc($result_b)) {
extract($row_b);
?>
<div class="col-md-3">
<div class="w-box inverse">
<div class="figure">
<img alt="" src="banner/<?php echo $img; ?>" class="img-responsive">
<div class="figcaption bg-2"></div>
<div class="figcaption-btn">
<i class="fa fa-plus-circle"></i> Zoom
<i class="fa fa-link"></i> View
</div>
</div>
<div class="row">
<div class="col-xs-9">
<h2><?php echo $img_title; ?></h2>
<small><?php echo $img_desc; ?></small>
</div>
</div>
</div>
</div>
<?php
}
?>
You can use num_rows to see how many rows were returned. Unfortunately from your question I can't tell whether you're using MySQL, MySQLi or PDO as it seems you pass all your queries into a class.
For MySQLi use the following:
$num = $result_b->num_rows;
SELECT COUNT(*) AS cnt FROM tbl_banner
OR
count( $res ); # in php
Also see: Which is fastest? SELECT SQL_CALC_FOUND_ROWS FROM `table`, or SELECT COUNT(*)
I don't understand....
I would like a result like this, explained in this way:
$query ="SELECT .....
<div class="row">
<div class="item active">
while {
<div1>...</div>
<div2>...</div>
<div3>...</div>
<div4>...</div>
} //end while
</div>
</div>
<div class="row">
<div class="item">
while {
<div4>...</div>
<div6>...</div>
<div7>...</div>
<div8>...</div>
} //end while
etc etc
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I have problem in my PHP code. I'm trying to make internet shop and Im now in product listing step. My code is so long, so I will show only substantial part.
$result = mysql_query("SELECT name, category, describtion, value FROM products") or die(mysql_error());
if(mysql_num_rows($result) % 2 == 0)
{
while($left = mysql_fetch_array($result, MYSQL_NUM) && $right = mysql_fetch_array($result, MYSQL_NUM))
{
echo '
<div class="sub_items">
<div class="sub_left">
<div class="sub_items_header">
<h1>'.$left[0].'</h1>
<h2>'.$left[1].'</h2>
</div>
<div class="sub_items_image">
<img src="images/'.$left[0].'.png" width="167" height="164" alt="'.$left[0].'" />
</div>
<div class="sub_items_text">
'.$left[2].'
</div>
<div class="sub_items_cartinfo">
<div class="price">
<h2>'.$left[3].'$</h2>
</div>
<div class="addtocart">
<span>Add to Cart</span>
</div>
<div class="clearthis"> </div>
</div>
<div class="clearthis"> </div>
</div>
<div class="sub_right">
<div class="sub_items_header">
<h1>'.$right[0].'</h1>
<h2>'.$right[1].'</h2>
</div>
<div class="sub_items_image">
<img src="images/'.$right[0].'.png" width="175" height="170" alt="'.$right[0].'" />
</div>
<div class="sub_items_text">
'.$right[2].'
</div>
<div class="sub_items_cartinfo">
<div class="price">
<h2>'.$right[3].'$</h2>
</div>
<div class="addtocart">
<span>Add to Cart</span>
</div>
<div class="clearthis"> </div>
</div>
<div class="clearthis"> </div>
</div>
<!-- End of Right Sub Item -->
<div class="clearthis"> </div>
</div>
<div class="h_divider"> </div>
';
}
In the while cycle I'm echoing data. Data is separated to left and right div. I'm not getting any error message, but I have bad output. Look.
The left side not loads, but right yes. Why? Anybody please help me.
= has a higher precedence thaN AND; use AND instead of && or parenthesize the assignments.
Instead of
$left = mysql_fetch_array($result, MYSQL_NUM) && $right = mysql_fetch_array($result, MYSQL_NUM)
either
$left = mysql_fetch_array($result, MYSQL_NUM) AND $right = mysql_fetch_array($result, MYSQL_NUM)
or
($left = mysql_fetch_array($result, MYSQL_NUM)) && ($right = mysql_fetch_array($result, MYSQL_NUM))