Dynamic div change alternatively in PHP [closed] - php

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.

Related

database values not displaying in carousal in codeigniter php

i have a website in which i used carousel sliders to display images and its values, I have 3 carousel in my homepage, 3 of them have same code, however the first carousel is not displaying name and price like the 2nd and 3rd carousel:
as you can see in the image
the code for this carousel is below:
<section class="carousel_sec">
<div class="container">
<div class="row ">
<h3 class="carousel_heading">Trending Birthday Cakes <button class="pull-right"><span>VIEW ALL</span><!-- <span class="visible-xs">>></span> --></button></h3>
<hr>
<div class="carou">
<div class="demo">
<ul id="landing_products1">
<?php foreach($trending_products1 as $key=>$val) { ?>
<li>
<div class="item">
<div class="pad15 content_div">
<div class="carou_thumb">
<img class="st_img" src="<?php echo ADMIN_IMG.'cakes/'.$val['image'][0]['cake_image'];?>" alt="<?php echo $val['cake_name']; ?>" />
<img class="nd_img" src="<?php echo ADMIN_IMG.'cakes/'.$val['image'][0]['cake_image'];?>" alt="<?php echo $val['cake_name']; ?>" />
<p>
<?php echo $val['cake_name']; ?>
</p>
<?php
$btp_price=0;$offer_price=0;
$btp_price=$val['price'][0]['btp_price'];
$offer_price=$val['price'][0]['offer_price'];
?>
<div class="price text-center" style="width:100%;float:left;">
<span class="amount" style="color:#f2136e;font-size:14px;">
<?php echo PRICE_UNIT;?><?php echo $offer_price;?>
</span> (
<s> <?php echo $btp_price;?></s>)
<span> </span>
<br>
</div>
<!--<br><span><?php echo PRICE_UNIT;100; ?></span></p>-->
</div>
</div>
</div>
</li>
<?php } ?>
</ul>
</div>
</div>
</div>
</div>
</section>
<?php } ?>
while inspecting in browser the value is showing, the live URL is enter link description here
can anyone please tell me what could be wrong here, thanks in advance
You'll need to tweak the css to reveal the content that is hidden.
To push you in the direction, see the improvement with:
#landing_products1 {
height: 330px;
margin-top: -65px;
}
I don't know how responsive you need to be with your UI, so let's call this a temporary fix. The first carousel's dimensions don't perfectly match the others, so I'll leave the fine tuning to you.
It also appears that you have some js and css issues to mop up.

PHP While loop stopped after displaying only one product in MySql database [closed]

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>

bootstrap 4 cards with php foreach loop

Iam trying to get my web page to display all of my shop items in bootstrap 4 cards. 3 wide by however many deep ( i may add pagination another day )
I've got a php foreach loop which populates bootstrap4 cards perfectly. The trouble is they display vertically ( one on top of the other ) . ive tried class= columns which works on dummy divs but not when i integrate with my for each loop.
I ve tried everything regarding bootstrap docs but cant get the cards to display 3 wide and however many deep ( the foreach and the items control this. )
Should i be even using 'cards' or use something else. thx for your time
<div class="container">
<!-- $result = my php code using x-path to get results from xml query goes here. -->
<?php
foreach ( $result as $elements){
?>
<div class="row-fluid ">
<div class="col-sm-4 ">
<div class="card-columns-fluid">
<div class="card bg-light" style = "width: 22rem; " >
<img class="card-img-top" src=" <?php echo $elements->pictures->picture[2]->filename ; ?> " alt="Card image cap">
<div class="card-body">
<h5 class="card-title"><b><?php echo $elements->advert_heading ?></b></h5>
<p class="card-text"><b><?php echo $elements->price_text ?></b></p>
<p class="card-text"><?php echo $elements->bullet1 ?></p>
<p class="card-text"><?php echo $elements->bullet2 ?></p>
Full Details
</div></div></div></div>
<?php
}
}
?>
</div>
</div> <!--container close div -->
As stated, the .row-fluid should be outside of the loop :
<div class="container">
<div class="row-fluid ">
<!-- my php code which uses x-path to get results from xml query. -->
<?php foreach ( $result as $elements) : ?>
<div class="col-sm-4 ">
<div class="card-columns-fluid">
<div class="card bg-light" style = "width: 22rem; " >
<img class="card-img-top" src=" <?php echo $elements->pictures->picture[2]->filename ; ?> " alt="Card image cap">
<div class="card-body">
<h5 class="card-title"><b><?php echo $elements->advert_heading ?></b></h5>
<p class="card-text"><b><?php echo $elements->price_text ?></b></p>
<p class="card-text"><?php echo $elements->bullet1 ?></p>
<p class="card-text"><?php echo $elements->bullet2 ?></p>
Full Details
</div>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
</div> <!--container div -->
You need to move your <div class="row-fluid "> class outside of the foreach loop, otherwise it will create a new row for each class.
Also, as a comment has mentioned, you need to close all your divs correctly.

a single php while one three different classes [closed]

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>

Using PHP if and else statements to search multiple values in table

I am trying to get the following to happen:
if "field_ListingID" = h100 then show div x
or if "field_ListingID" = h200 then show same div x
Basically, within the same table1, I have 2 values "h100 "& "h200". I am able to pull just one value fine, but how do I pull if red or if blue.
Here is the code I have currently that is working for just one value. I tried placing the same php code for "h100" under the else statement, but that generated errors:
<?php if ($row["field_ListingID"]=="h100"){ ?>
<div class="featured-property">
<a href="#">
<div class="inner-logo"><img src="images/inner-logo.png" alt="LOGO" width="136" height="41" /></div>
<div class="bg-txt">
<div class="star-icon"></div>
<div class="txt"><p>Featured <span>Property</span></p></div>
</div>
</a>
</div>
<?php } else { ?>
<div style="margin-top: 20px; min-height:55px;"> </div>
<?php }?>
Thanks.
Here try this, I added the or operator (||) to your if statement:
<?php if ($row["field_ListingID"]=="h100" || $row["field_ListingID"]=="h200" ){ ?>
<div class="featured-property">
<a href="#">
<div class="inner-logo"><img src="images/inner-logo.png" alt="LOGO" width="136" height="41" /></div>
<div class="bg-txt">
<div class="star-icon"></div>
<div class="txt"><p>Featured <span>Property</span></p></div>
</div>
</a>
</div>
<?php } else { ?>
<div style="margin-top: 20px; min-height:55px;"> </div>
<?php }?>

Categories