I have that
<?php
$new=get_records("tbl_item","status=1 AND idshop='{$idshop}' AND special","id DESC", $startRow.",".$pageSize, " ");
$dem=1;
while($row_new=mysql_fetch_assoc($new)){
?>
<div class=""row>
<div class="col-3">
</div>
</div>
<?php } ?>
I want when this code run, it'll display 1 row with 4 col-3 until the end of the loop.
I want output like this
<div class=""row>
<div class="col-3">
</div>
<div class="col-3">
</div>
<div class="col-3">
</div>
<div class="col-3">
</div>
</div>
<div class=""row>
<div class="col-3">
</div>
<div class="col-3">
</div>
<div class="col-3">
</div>
<div class="col-3">
</div>
</div>
Please help me! Thanks all!
This should work:
<?php
$new=get_records("tbl_item","status=1 AND idshop='{$idshop}' AND special","id DESC", $startRow.",".$pageSize, " ");
$i=0;
while($row_new=mysql_fetch_assoc($new)){
if($i % 4 == 0) {
?>
<div class="row">
<?php
}
?>
<div class="col-3">
</div>
<?php
if($i % 4 == 3) {
?>
</div>
<?php
}
?>
<?php
$i++;
}
$i--;
if ($i % 4 != 3) {
?>
</div>
<?php
}
?>
The last if is in case the total number of records weren't a multiple of 4, then we need to close the div tag.
And, not to mention, you should definitely use MySQLi. Why should I use MySQLi?
try this bro:
update, if count rows less then 4:
$new = get_records("tbl_item", "status=1 AND idshop='{$idshop}' AND special", "id DESC", $startRow . "," . $pageSize, " ");
$dem = 1;
$x = 0;
$count = count($new);
while ($row_new = mysql_fetch_assoc($new)) {
$x++;
if ($x==1 || $x % 4 == 1) { ?>
<div class="row">
<?php } ?>
<div class="col-3">
<?= "x: $x" ?>
</div>
<?php if ($x % 4 == 0 || $x == $count) { ?>
</div>
<?php
}
}
Try this:
<?php
$new=get_records("tbl_item","status=1 AND idshop='{$idshop}' AND special","id DESC", $startRow.",".$pageSize, " ");
$dem=1;
while($row_new=mysql_fetch_assoc($new)){
?>
<div class="row">
<?php for($i = 1; $i <= 4; $i++){?>
<div class="col-3">
</div>
<?php } ?>
</div>
<?php } ?>
Related
I have coded this image slider that moves the image and it's details using the cursor.
I want to fetch data from database and display 4 books images with their details in each row I have tried this code but all the data have displayed in one row only.
Also if this does not take much of your time I want to make only 7 rows per page (28 books) and add numbers at the bottom of the page to move to the next 7 rows.
this is my try :
<html>
<body>
<div class="popular_causes_area section_padding">
<div class="container"> <div class="row">
<div class="col-lg-12">
<div class="causes_active owl-carousel">
<?php
try{
require('connection.php');
$sql1="select * from books ";
$rs=$db->query($sql1);
$db =null;
} catch(PDOException $ex) {
die ("Error Message ".$ex->getMessage()); }
$i=0;
while ($books = $rs->fetch()){
$i++;
$bpic = $books[2];
$btitle= $books[3];
$bq= $books[4];
$bstatus = $books[5];
$bdesc = $books[6];
$bnumber = $books[7];
$bcity = $books[8];
if ($i % 3 == 1) {
echo "<li>"; ?>
<div class="single_cause">
<div class="thumb">
<?php
if ($bpic == "" ) {
echo "<img src='img/default-image.png'
height=450px'>";
} else {
echo "<img src='img/$bpic' placeholder='nooooo'
height=450px alt=''>";
}?>
</div>
<div class="causes_content">
<div class="balance d-flex justify-content-between align-items-center">
<span>Tell:<?php echo $bnumber; ?> </span>
<span><?php echo $bcity; ?> </span>
</div>
<h4><?php echo $btitle; ?> </h4>
<p><?php echo $bdesc; echo "<br>
condation:".$bstatus; ?> </p>
</div>
</div>
</li>
<?php
} ////end of first if
if ($i % 3 == 0) {
echo "<li>"; ?>
<div class="single_cause">
<div class="thumb">
<?php
if ($bpic == "" ) {
echo "<img src='img/default-image.png' height=450px'>";
} else {
echo "<img src='img/$bpic' placeholder='nooooo' height=450px alt=''>"; ?>
</div>
<div class="causes_content">
<div class="balance d-flex justify-content-between align-items-center">
<span>Tell:<?php echo $bnumber; ?> </span>
<span><?php echo $bcity; ?> </span>
</div>
<h4><?php echo $btitle; ?> </h4>
<p><?php echo $bdesc; echo "<br>
condation:".$bstatus; ?> </p>
</div>
</div>
</li>
<?php
} //end of second if
} // end of while ?>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
This image shows what I got (all the data in one row instead of 4 books per row !):
image slider code :
<div class="popular_causes_area section_padding">
<div class="container">
<div class="row">
<div class="col-lg-12">
<div class="causes_active owl-carousel">
<div class="single_cause">
<div class="thumb">
<?php
if ($bpic == "" ) {
echo "<img src='img/default-image.png' height=450px'>";
} else {
echo "<img src='img/$bpic' placeholder='nooooo' height=450px alt=''>";
}
?>
</div>
<div class="causes_content">
<div class="balance d-flex justify-content-between align-items-center">
<span>Tell:<?php echo $bnumber; ?> </span>
<span><?php echo $bcity; ?> </span>
</div>
<h4><?php echo $btitle; ?> </h4>
<p><?php echo $bdesc; echo "<br> condation:".$bstatus; ?> </p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
I am trying to get a loop like this. Suppose i have 20 rows in table test and i need to get its id values inside a loop like below.
<section class="test">
<div class="n4">
<div class="n2">
1 // first row id
</div>
<div class="n21">
2 // second row id
</div>
</div>
</section>
<section class="test">
<div class="n4">
<div class="n2">
3 // third row id
</div>
<div class="n21">
4 // fourth row id
</div>
</div>
</section>
..... <section> ....upto all ids in the row
What i am tried to do is
Suppose table test has 20 rows.
<?php
$query=$db->query("SELECT * FROM test");
$counter=1;
foreach($query as $row)
{
?>
<section class="test">
<div class="n4">
<?php if($counter %2 !=0) ?>
<div class="n2">
<?php echo $row['id']; ?>
</div>
<?php else ?>
<div class="n21">
<?php echo $row['id']; ?>
</div>
</div>
</section>
<?php
$counter++;
} ?>
This is not showing the exact result.
Any help please?
This way will work as you want, i have try this as well
<?php
$query = [['id' => 1], ['id' => 2], ['id' => 3]] ; // example of result
for ($i=0; $i < count($query); $i+=2) {
try {
?>
<section class="test">
<div class="n4">
<div class="n2">
<?php echo #$query[$i]['id'] // ignore error ?>
</div>
<div class="n21">
<?php echo #$query[$i+1]['id'] // ignore error ?>
</div>
</div>
</section>
<?php
} catch (Exception $e) {
}
}
Above will produce result like this
<section class="test">
<div class="n4">
<div class="n2">
1
</div>
<div class="n21">
2
</div>
</div>
</section>
<section class="test">
<div class="n4">
<div class="n2">
3
</div>
<div class="n21">
</div>
</div>
</section>
You have wrong with your code, lets try this
<?php
$query=$db->query("SELECT * FROM test");
$counter=1;
foreach($query as $row)
{
?>
<section class="test">
<div class="n4">
<?php if($counter %2 !=0): /* you've missing ":" symbol*/ ?>
<div class="n2">
<?php echo $row['id']; ?>
</div>
<?php else: /* you've missing ":" symbol*/ ?>
<div class="n21">
<?php echo $row['id']; ?>
</div>
<?php endif; /* you dont need close bracket, but you need "endif" */ ?>
<?php $counter++; ?>
</div>
</section>
<?php } ?>
Here Is The Code
<div class="row">
<div class="col-md-9 column">
<div class="services-listing">
<?php if(!empty($data)):
foreach ($data as $rows ) : ?>
<div class="service">
<div class="service-img"><span><img src="dashboard/uploads/<?php echo $rows['img'];?>" alt="" /></div>
<div class="service-detail">
<h3><?php echo $rows['name'];?></h3>
</div>
</div>
<!-- Service -->
<?php endforeach; else : echo "No Record Found Against This Services"; endif;?>
</div>
<!-- Service Listing -->
</div>
i want to start design from <div class="row"> not from <div class="service"> now it repeating this div i want new <div class="row"> genrated after every 6 records
<div class="row">
<div class="col-md-9 column">
</div>
</div>
Looking for something like this, let me know if it works for you
<?php
$start = 6;
$end = 1;
if(!empty($data)){
foreach ($data as $rows ) {
if ($start % 6 == 0) { ?>
<div class="row">
<div class="col-md-9 column">
<?php } ?>
<div class="services-listing">
<div class="service">
<div class="service-img"><span><img src="dashboard/uploads/<?php echo $rows['img'];?>" alt="" /></div>
<div class="service-detail">
<h3><?php echo $rows['name'];?></h3>
</div>
</div>
<!-- Service -->
</div>
<?php if ($end % 6 == 0) { ?>
</div>
</div>
<?php } $start++; $end++;
}
}
else{ echo "No Record Found Against This Services"; }
?>
you need to wrap div row with foreach first and then:
<?php $i = 0; ?>
<?php if(!empty($data)):
foreach ($data as $rows ) : ?>
<?php $i++; ?>
<div class="row">
<div class="col-md-9 column">
<div class="services-listing">
<div class="service">
<div class="service-img"><span><img src="dashboard/uploads/<?php echo $rows['img'];?>" alt="" /></div>
<div class="service-detail">
<h3><?php echo $rows['name'];?></h3>
</div>
<!-- Service -->
</div>
<!-- Service Listing -->
</div>
<!-- col-md-9 column -->
</div>
<!-- Row -->
</div>
<?php if($i%6==0):?>
<div class="row">
<div class="col-md-9 column">
</div>
</div>
<?php endif; ?>
<?php endforeach; else : echo "No Record Found Against This Services"; endif;?>
just please re-check all divs structure to be sure whether all of them are closed
I have a table in my database with similar data, but others with more data.
What I want is to echo the data through the loop. my problem is that I want to get data from 2 to 2.
My code to display data normally is
<?php $count = $helper->count('testimony');?>
<div class="owl-4">
<?php for ($i=0; $i < $count; $i++) : ?>
<div class="item">
<?php echo '<h3>'.$helper->get('authorname', $i).'</h3>';
echo '<p>'.$helper->get('testimony', $i).'</p>' ;?>
</div>
<?php endfor; ?>
</div>
This returns
<div class="owl-4">
<div class="item">
<h3>Author 1</h3>
<p>Testimony 1</p>
</div>
<div class="item">
<h3>Author 2</h3>
<p>Testimony 2</p>
</div>
................
</div>
How do I turn gives me this
<div class="owl-4">
<div class="item">
<h3>Author 1</h3>
<p>Testimony 1</p>
<h3>Author 2</h3>
<p>Testimony 2</p>
</div>
<div class="item">
<h3>Author 3</h3>
<p>Testimony 3</p>
<h3>Author 4</h3>
<p>Testimony 4</p>
</div>
................
</div>
THANKS
Just increase the counter by 2 instead of 1 each time.
<?php $count = $helper->count('testimony');?>
<div class="owl-4">
<?php for ($i=0; $i < $count; $i += 2) : ?>
<div class="item">
<?php
echo '<h3>'.$helper->get('authorname', $i).'</h3>';
echo '<p>'.$helper->get('testimony', $i).'</p>' ;
echo '<h3>'.$helper->get('authorname', $i + 1).'</h3>';
echo '<p>'.$helper->get('testimony', $i + 1).'</p>' ;
?>
</div>
<?php endfor; ?>
</div>
You can display <div class="item">and </div> each two iterations, for example checking if $i is an even number:
<?php $count = $helper->count('testimony');?>
<div class="owl-4">
<?php for ($i=0; $i < $count; $i++) : ?>
<?php if ($i%2==0): ?>
<div class="item">
<?php endif; ?>
<?php echo '<h3>'.$helper->get('authorname', $i).'</h3>';
echo '<p>'.$helper->get('testimony', $i).'</p>' ;?>
<?php if ($i%2==0): ?>
</div>
<?php endif; ?>
<?php endfor; ?>
I need to add a div tag after each 5 items. can any one provide any assistance with this logic, i have tried but, no success.
I have this code:
<div class="product-grid">
<?php foreach ($products as $product) { ?>
<div class="item">.....</div>
<?php } ?>
</div>
I get this
<div class="product-grid">
<div class="item">.....</div>
<div class="item">.....</div>
<div class="item">.....</div>
<div class="item">.....</div>
<div class="item">.....</div>
<div class="item">.....</div>
<div class="item">.....</div>
<div class="item">.....</div>
<div class="item">.....</div>
<div class="item">.....</div>
<div class="item">.....</div>
</div>
It would look like this: (add a div after 5 item in sequence.)
<div class="product-grid">
<div class="item">.....</div>
<div class="item">.....</div>
<div class="item">.....</div>
<div class="item">.....</div>
<div class="item">.....</div>
<div class="clearfix visible-xs-block"></div>
<div class="item">.....</div>
<div class="item">.....</div>
<div class="item">.....</div>
<div class="item">.....</div>
<div class="item">.....</div>
<div class="clearfix visible-xs-block"></div>
<div class="item">.....</div>
</div>
Some help would be appreciated.
<?php
// control variable
$counter = 0;
?>
<div class="product-grid">
<?php foreach ($products as $product) { ?>
<div class="item">.....</div>
<?php
// on every #5 result
if(++$counter % 5 === 0) { ?>
<div class="clearfix visible-xs-block"></div>
<?php
}
}
?>
</div>
Use the MOD operator:
<div class="product-grid">
<?php
$i = 0;
foreach ($products as $product) {
$i++;
if($i % 5 == 0){
?>
<div class="clearfix visible-xs-block"></div>
<? } ?>
<div class="item">.....</div>
<?php } ?>
</div>
Since, it's every sixth item getting that particular class, you can use a temporary counter to keep track:
<div class="product-grid">
<?php
$i = 1;
foreach ($products as $product) {
if( $i % 6 != 0 ) {
?>
<div class="item">.....</div>
<?php
}
else {
?>
<div class="clearfix visible-xs-block"></div>
<?php
}
$i++;
}
?>
</div>
You can do it like this:
<div class="product-grid">
<?php
$i = 1;
foreach ($products as $product) {
echo '<div class="item">.....</div>';
if($i%5 == 0){
echo '<div class="clearfix visible-xs-block"></div>';
}
$i++;
}
?>
</div>