TABLE
id m_id image
1 1 img1.jpg
2 2 img2.jpg
3 1 img11.jpg
4 2 img22.jpg
5 1 img111.jpg
........ etc
What i need is - Get only 2 rows from the table group by m_id as below
//here getting images where m_id=1 and it is not more than 2
<section class='test'>
<div class="n-2">
<img src='img1.jpg'>
</div>
<div class="n-2">
<img src='img11.jpg'>
</div>
</section>
//next fetching images with m_id=2
<section class='test'>
<div class="n-2">
<img src='img2.jpg'>
</div>
<div class="n-2">
<img src='img22.jpg'>
</div>
</section>
......
What i tried is
<?php
$query=$db->query("Select * from gallery order by m_id asc");
$result = $query->fetchAll(\PDO::FETCH_ASSOC);
for ($i=0; $i < count($result); $i+=2) {
?>
<section class='test'>
<div class="n-2">
<img src='<?php $result[$i]['image']; ?>'>
</div>
<div class="n-2">
<img src='<?php $result[$i+1]['image']; ?>'>
</div>
</section>
<?php
}
?>
But i am getting all the fields to the showed section class'test', How to get the only 2 rows as showed section class.
Any experts?
Hi this query will get you only two record
you should read the MySql tutorials in w3schools.com
Select * from gallery order by m_id asc LIMIT 2
Update 1: this will work hopefully in your case.
$query = $db->query("Select * from gallery order by m_id asc");
$items = $query->fetchAll(\PDO::FETCH_ASSOC);
$result = [];
$temp = [];
foreach ($items as $item) {
$m_id = $item['m_id'];
if (!isset($temp[$m_id]) || count($temp[$m_id]) < 2) {
$temp[$m_id][] = $item['image'];
$result = $item['image'];
}
}
for ($i = 0; $i < count($result); $i += 2) {
?>
<section class='test'>
<div class="n-2">
<img src='<?php $result[$i]['image']; ?>'>
</div>
<div class="n-2">
<img src='<?php $result[$i + 1]['image']; ?>'>
</div>
</section>
<?php
}
Related
I am using Bootstrap to create a row of 6 colunm images.
<div class="row">
<?php
$sql = //my query
while($row = mysqli_fetch_array($sql)){
?>
<div class="col-xs-6 col-md-2">
<img id="img" class="img-responsive" src="img/<?php echo $row['image']; ?>" />
</div>
<?php
}
?>
</div>
Which works fine when there is only 6 rows from the database, however when there is 7 the extra one gets bumped to the far right rather than the far left on the next row, see example below.
| img | img | img | img | img | img
| img
Normal result i am looking for would be
| img | img | img | img | img | img
| img
I want to create a new bootstrap row after every 6 results, any help would be appriciated
You could fetch all the values into an array first, count how many elements are in the array and then dynamically control what span classes you use.
<div class="row">
<?php
$sql = //my query
$images = array();
while($row = mysqli_fetch_assoc($sql)){
$images[] = $row['image'];
}
$column_class = count($images) > 6 ? "col-xs-3 col-md-1" : "col-xs-6 col-md-2";
foreach ($images as $image) {
?>
<div class="<?= $column_class ?>">
<img id="img" class="img-responsive" src="img/<?= $image ?>" />
</div>
<?php
}
?>
i have seen this before when the heights of the images are not identical. the float left only floats to the point where the bottom line is uneven. try adding a style height with a reasonable fixed height.
<div class="row">
<?php
$sql = //my query
while($row = mysqli_fetch_array($sql)){
?>
<div class="col-xs-6 col-md-2">
<img id="img" class="img-responsive" style="height:100px" src="img/<?php echo $row['image']; ?>" />
</div>
<?php
}
?>
</div>
I ran into this problem in a recent project and was able to solve it with this code. I only have two views so I only need the two lines but you could have more if you wanted. You can see this in action on this page
<div class="visible-md-block visible-lg-block clearfix">
<div class="visible-sm-block visible-xs-block clearfix">
Here is the full php code for the gallery page
<?php
$c = 1;
foreach($images as $image) {?>
<div class="col-xs-4 col-md-3">
<a class="thumbnail" href="path to full size image" data-toggle="lightbox" data-footer="<?php echo $image['caption']; ?>">
<img class="img-responsive" src="path to thumbnail" alt="...">
</a>
</div>
<?php
if(($c % 4) == 0) {
echo '<div class="visible-md-block visible-lg-block clearfix"></div>';
}
if(($c % 3) == 0) {
echo '<div class="visible-sm-block visible-xs-block clearfix"></div>';
}
$c++;
}
?>
Sorry while this code works for when you want the number of images in a row to change at different screen sizes I just re read your question and you say you just want 6 across all the time so you could just use one line
if(($c % 6) == 0) {
echo '<div class="clearfix"></div>';
}
The $c variable is just a counter and the code $c % 6 == 0 will put the clearfix div every time $c is divisible by 6.
I am trying add 2 bootstrap class col-md-8 after 1 loop and then 2 col-md-4 class in php while loop. This process should be same in full while loop. So the result will look like this :
My current code is bellow but it's not showing the result what I need, can't get an idea how the loop will like !
Full code :
<div class="row text-center">
<h2>What we offer</h2>
<hr class="separator">
<?php $get_menu_class=m ysqli_query($conn, "SELECT pcat_name, pcat_image FROM product_category ORDER BY pcat_id DESC"); $x=0; while($menu_class_result=m ysqli_fetch_array($get_menu_class) ) { $menu_class_name=h tmlspecialchars($menu_class_result[ 'pcat_name']); $menu_class_image=h tmlspecialchars($menu_class_result[ 'pcat_image']); if($x & 1) { $col='8' ; }else { $col='4' ; } ?>
<div class="col-sm-<?php echo $col; ?>">
<div class="we-offer">
<a href="area">
<img src="<?php echo IMG_DIR."/menu_class/$menu_class_image"; ?>" alt="" class="img-responsive center-block">
<h3><?php echo ucfirst($menu_class_name); ?></h3>
</a>
</div>
</div>
<?php $x++; } ?>
</div>
Latest code :
<div class="row text-center">
<h2>What we offer</h2>
<hr class="separator">
<?php $get_menu_class=m ysqli_query($conn, "SELECT pcat_name, pcat_image FROM product_category ORDER BY pcat_id DESC"); $x=0; while($menu_class_result=m ysqli_fetch_array($get_menu_class) ) { $menu_class_name=h tmlspecialchars($menu_class_result[ 'pcat_name']); $menu_class_image=h tmlspecialchars($menu_class_result[ 'pcat_image']); $col=( (($x+1)/2)%2)? "8": "4"; ?>
<div class="col-sm-<?php echo $col; ?>">
<div class="we-offer">
<a href="area">
<img src="<?php echo IMG_DIR."/menu_class/$menu_class_image"; ?>" alt="" class="img-responsive center-block">
<h3><?php echo ucfirst($menu_class_name); ?></h3>
</a>
</div>
</div>
<?php $x++; } ?>
</div>
Current Result :
[![enter image description here][2]][2]
Try this logic please: $col = ((($i+1)/2)%2)?"8":"4";
https://3v4l.org/GHGVp
As you can see it outputs the desired results.
The col for loop 0 is col4
The col for loop 1 is col8
The col for loop 2 is col8
The col for loop 3 is col4
The col for loop 4 is col4
The col for loop 5 is col8
The col for loop 6 is col8
The col for loop 7 is col4
The col for loop 8 is col4
The col for loop 9 is col8
The col for loop 10 is col8
The col for loop 11 is col4
The col for loop 12 is col4
The col for loop 13 is col8
The col for loop 14 is col8
The col for loop 15 is col4
The col for loop 16 is col4
The col for loop 17 is col8
The col for loop 18 is col8
The col for loop 19 is col4
You just need to replace in your code the
if($x & 1) {
$col = '8';
}else {
$col = '4';
}
with
$col = ((($x+1)/2)%2)?"8":"4";
I would just keep track of a displayed variable. If you define it as 1 first then you will only show the desired first column set the 1st time and then it will switch to the other.
You wouldn't need to keep track of $x then. I wouldn't call this a simple math problem and the other OK answer that is here doesn't fit your use case.
<div class="row text-center">
<h2>What we offer</h2>
<hr class="separator">
<?php
$get_menu_class = mysqli_query($conn, "SELECT pcat_name, pcat_image FROM product_category ORDER BY pcat_id DESC");
$col = 4;
$displayed = 1;
while($menu_class_result = mysqli_fetch_array($get_menu_class) ) {
$menu_class_name = htmlspecialchars($menu_class_result['pcat_name']);
$menu_class_image = htmlspecialchars($menu_class_result['pcat_image']);
?>
<div class="col-sm-<?php echo $col; ?>">
<div class="we-offer">
<a href="area">
<img src="<?php echo IMG_DIR."/menu_class/$menu_class_image"; ?>" alt="" class="img-responsive center-block">
<h3><?php echo ucfirst($menu_class_name); ?></h3>
</a>
</div>
</div>
<?php
if($displayed){
switch($col){
case 4:
$col = 8;
break;
case 8:
$col = 4;
break;
default:
$col = 4;
}
$displayed = 0;
} else {
$displayed = 1;
}
}
?>
</div>
I have a table with fields like id, date, heading, news. I want to display the fields date, heading and news in an Owl Carousel slider. Can anyone suggest how to pick two rows at a time from an SQL table to display two entries at a time in the Owl Carousel slider. I have used an SQL query like this:
<?php
$sql3 = "SELECT date, heading, news FROM news ORDER BY news.date LIMIT 0, 1";
$result3 = mysql_query($sql3) or die(mysql_error());
while($row3 = mysql_fetch_array($result3)) {
?>
<div class="item ">
<div class="l_blk">
<div class="news_container">
<div class="col-md-1 date_b"><p>Mar<br>09</p></div>
<div class="col-md-11 cont_b">
<p>
<span class="news_t">"<?php echo $row3['heading']; ?>"</span><br>
<?php echo $row3['news']; ?>
</p>
</div>
</div>
</div>
<div class="l_blk">
<div class="news_container">
<div class="col-md-1 date_b"><p>Mar<br>09</p></div>
<div class="col-md-11 cont_b">
<p>
<span class="news_t">"<?php echo $row3['heading']; ?>"</span><br>
<?php echo $row3['news']; ?>
</p>
</div>
</div>
</div>
</div>
<?php
}
?>
Can anyone suggest how to do this?
$sql3 = "SELECT date, heading, news FROM news ORDER BY news.date LIMIT 0, 1";
Don't you change the LIMIT 0, 2
Which means select 2 starting with the value at index 0?
Once you've done that, take out the duplicate code inside the loop
Try this pls.
<?php
$sql3 = "SELECT date, heading, news FROM news ORDER BY news.date"; // No limit
$result3= mysql_query($sql3) or die(mysql_error());
$counter = 0;
while($row3 = mysql_fetch_array($result3, MYSQL_ASSOC))
{
if($counter % 2 == 0){
echo '<div class="item ">'.PHP_EOL;
}
?>
<div class="l_blk">
<div class="news_container">
<div class="col-md-1 date_b"><?php $row3['date'] ?></div>
<div class="col-md-11 cont_b">
<p>
<span class="news_t">"<?php echo $row3['heading']; ?>"</span><br>
<?php echo $row3['news']; ?>
</p>
</div>
</div>
</div>
<?php
if($counter % 2 == 1){
echo '</div>'.PHP_EOL;
}
$counter++;
}
if($counter % 2 == 1){
echo '</div>'.PHP_EOL;
}
?>
i want to display only those images according to the category_id selected but iam not getting images here and and for all category_id the value showing is category_id=9
here is my controller
public function men_clothing($p_id=null)
{
$data['active_mn']='men_clothing';
$data['men']=$this->roxmodel->get_category_by_parent($p_id=8);
$data['kids']=$this->roxmodel->get_category_by_parent($p_id=9);
$config['base_url'] = base_url().'roxcontrol/men_clothing';
$config['per_page'] = 9;
$config['uri_segment'] = 3;
$config['total_rows'] = $this->roxmodel->count_gallery();
$data['galllery']=$this->roxmodel->get_gallery_men_images($p_id,$config['per_page'],$this->uri->segment(3));
var_dump($data['galllery']);
$this->load->library('pagination',$config);
$data['page_links'] = $this->pagination->create_links();
$this->load->view('men_clothing',$data);
}
here is my model
public function get_gallery_men_images($p_id=null)
{
$this->db->where('gallery.category_id',$p_id);
$this->db->select('gallery.*,category.category_name');
$this->db->join('category','category.id=gallery.category_id');
return $this->db->get('gallery')->result();
}
here is my view page
<ul class="sidebar-tags">
<li>View All </li>
<?php foreach($men as $row) {?>
<li><?php echo $row->category_name; ?> <span> </span></li>
<?php } ?>
</ul>
<?php if( !empty($galllery) ) {
foreach($galllery as $row){?>
<div class="col-lg-4 col-md-4 col-sm-4">
<div class="two-product">
<!-- single-product start -->
<div class="single-product">
<!-- <span class="sale-text">Sale</span>-->
<div class="product-img">
<a href="#">
<img class="primary-image" ima="<?php echo base_url();?>images/<?php echo $row->image;?>" src="<?php echo base_url();?>images/<?php echo $row->image;?>" alt="" />
<img class="secondary-image" ima="<?php echo base_url();?>images/<?php echo $row->image;?>" src="<?php echo base_url();?>images/<?php echo $row->image;?>" alt="" />
</a>
<div class="action-zoom">
<div class="add-to-cart">
<i class="fa fa-search-plus"></i>
</div>
</div>
<!-- <div class="price-box">
<span class="new-price">$110.00</span>
</div>-->
</div>
<div class="product-content">
<h2 class="product-name"><?php echo $row->title;?></h2>
<p><?php echo $row->description;?></p>
</div>
</div>
<!-- single-product end -->
</div>
</div>
<?php }}?>
here the image is not displaying and getting like this
SELECT `gallery`.*, `category`.`category_name` FROM `gallery` JOIN `category` ON `category`.`id`=`gallery`.`category_id` WHERE `gallery`.`category_id` = 9
my table(gallery) looks like this
id image category_id
93 img1455604030.jpg 10
94 img1455605183.jpg 11
95 img1455616291.jpg 11
96 img1455617201.jpg 10
97 img1455617299.jpg 10
98 img1455681918.jpg 13
99 img1455681957.jpg 12
there is a problem in you join query
$this->db->select('*');
$this->db->from('gallery');
$this->db->where('category.id',$p_id);
$this->db->join('category', 'category.id = gallery.category_id');
$query = $this->db->get()->result();
return $query;
If what you want is to get or Filter result which has the same category_id: the query should be like this
public function get_gallery_men_images($p_id=null)
{
$img = $this->db->select('gallery.*')
// why not add condition IF p_id = null
// execute only if p_id not null otherwise it return error
if($p_id != null):
->where('gallery.category_id',$p_id)
endif;
// we are using LEFT JOIN to fetch the category data's
->join('category','category.id=gallery.category_id', 'left')
->get('gallery');
return $img->result();
}
i have a litte problem! i want to fetch the last 3 rows from my mysql database.. (works)
<?php
$sql = "SELECT id,title,description FROM news ORDER BY `id` DESC LIMIT 0, 3";
$result = mysql_query($sql);
while ($list = mysql_fetch_assoc($result))
{
?>
<div class="one-of-three">
<div class="box">
<h2><?PHP echo ($list['id']);?> <?PHP echo (htmlentities($list['title'],ENT_QUOTES,'UTF-8'));?></h2>
<div class="content clearfix">
<p><?PHP echo (htmlentities($list['description'],ENT_QUOTES,'UTF-8'));?></p>
</div>
</div>
</div>
<?php
}
?>
my problem is the html layout! i have 3 div boxes wich i will float..
<div class="one-of-three">
[...]
</div>
<div class="two-of-three">
[...]
</div>
<div class="three-of-three">
[...]
</div>
but my result gives me
<div class="one-of-three">
[...]
</div>
<div class="one-of-three">
[...]
</div>
<div class="one-of-three">
[...]
</div>
i tried different things but i dont how it works :(
you can give me a hint?
You should probably get an array $numbers = array('one', 'two', 'three');. Then have a variable $i = 0; and increment it in the loop. The first line should then be like: <div class="<?php echo $numbers[$i++]; ?>-of-three">.
<?php
$n = array(1 => 'one', 2 => 'two', 3 => 'three');
while ($list = mysql_fetch_assoc($result)) {
?>
<div class="<?php echo $n[$list['id']]; ?>-of-three">