how can I add a class in php while loop? - php

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>

Related

Get 2 rows from a table having same column values in PHP

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
}

How to change <div> property of certain content panel in a php webpage?

<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="row text-center">
<?php
if($_GET[id] == 0)
{
$querysb = mysql_query("SELECT * FROM services WHERE parentid !=0 order by parentid, cid ");
}
else
{
$querysb = mysql_query("SELECT * FROM services WHERE parentid='".$_GET[id]."'");
}
while($rowsb = mysql_fetch_assoc($querysb))
{
if($val == '6' || $val =='10'){
$classname = 'whitebg';
} else {
$classname = 'bg-blue co-white';
}
?>
<div class="col-md-4 mrgnBtm15">
<div class="<?php echo $classname;?> padding30" style="min-height: 250px">
<h3 class="service-heading">
<?php echo $rowsb['cname'];?>
</h3>
<h4>
RS <?php echo $rowsb['price'];?><br>
</h4>
<div class="mrgnTop15 clearfix"></div>
<a class="btn bg-orange co-white" href="<?php echo MYWEBSITE;?>servicedetail/<?php echo to_prety_url($rowsb['cname']).'-'.$rowsb['cid'];?>.html">
<font style="size:14px; color:#000; font-weight:bolder;font-family: "Open Sans";">Register</font></a>
</div>
</div>
<?php } ?>
</div>
</div>
</div>
I am working on a dynamic website. here is the code of a particular page. In this page there is a div section with class="col md-4". If the number of content panel in that division appears to 5 or 7 in that case I want only the last panel (i.e 5th and 7th) to be in full column (col-12). What should I do?
Since you are using col-md-4, 3 divs will be shown each row. So I think what you are looking for is a way to make the last div full width if its going to be alone in its row. In that case, you will need to make it col-md-12 on 4th (not 5th) and 7th and 10th records and so on. This is exactly what the following code does,
I think what you want to do is show the
<?php
if($_GET[id] == 0)
{
$querysb=mysql_query("SELECT * FROM services WHERE parentid !=0 order by parentid, cid ");
}
else
{
$querysb=mysql_query("SELECT * FROM services WHERE parentid='".$_GET[id]."'");
}
$number_of_records = mysql_num_rows($querysb);
$counter = 1;
while($rowsb=mysql_fetch_assoc($querysb))
{
if($val == '6' || $val =='10'){
$classname= 'whitebg';
}else{
$classname= 'bg-blue co-white';
}
//if($number_of_records %3 ==1 && $counter == $number_of_records)
if(($number_of_records == 5 || $number_of_records == 7) && $counter == $number_of_records)
$col_class = "col-md-12";
else
$col_class = "col-md-4";
?>
<div class="<?php echo $col_class;?> mrgnBtm15">
<div class="<?php echo $classname;?> padding30" style="min-height: 250px">
<h3 class="service-heading">
<?php echo $rowsb['cname'];?>
</h3>
<h4>
RS <?php echo $rowsb['price'];?><br>
</h4>
<div class="mrgnTop15 clearfix"></div>
<a class="btn bg-orange co-white" href="<?php echo MYWEBSITE;?>servicedetail/<?php echo to_prety_url($rowsb['cname']).'-'.$rowsb['cid'];?>.html">
<font style="size:14px; color:#000; font-weight:bolder;font-family: "Open Sans";">Register</font></a>
</div>
</div>
<?php
$counter++;
} ?>
</div>
</div>
</div>

How to select two rows each time from a table in SQL and display them in PHP?

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

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();
}

PHP Iterate 12 items perform echo on every third

Hi I have been chasing my tail for a while on this one and wondered if someone could solve my headache.
Basically I am rendering 12 items to the page. Each 3 items need to be wrapped in a row like:
<div class='row'>
<div class='item'>
</div>
<div class='item'>
</div>
<div class='item'>
</div>
</div>
<div class='row'>
<div class='item'>
</div>
<div class='item'>
</div>
<div class='item'>
</div>
</div>
<div class='row'>
<div class='item'>
</div>
<div class='item'>
</div>
<div class='item'>
</div>
</div>
<div class='row'>
<div class='item'>
</div>
<div class='item'>
</div>
<div class='item'>
</div>
</div>
Thanks in advance.
Hi I think some code would help:
$i=0;
foreach($posts as $p) {
$i++
}
So basically within the for each I will be outputting a row and 3 items.The results are coming from a database query. I have tried a few different approaches such as
if($i == 1) {echo "<div class='row'>";}
if ($counter % 4 == 0) {
echo "</div><div class='row'>";
}
However I keep failing, please note these are just snippets of code.
You need to use two loops:
// Outer loop for each row
for ($row = 0; $row < 4; $row++) {
echo '<row>';
// Inner loop for the items
for ($item = 0; $item < 3; $item++) {
echo '<item>';
}
echo '</row>';
}
You should have done it yourself. it needs to know very basic of loop.
try like this:
for($i=0;$i <= 3; $i++){ //outer loop for 12 times
echo "<row>"; // start row
for ($j=0;$j<3;$j++){ // inner loops for echoing 3 times
echo "<item>"; //echo item
}
echo "</row>"; // end row
}
demo : https://eval.in/107129
I have used "\n" for new line in demo. if you needed new line then you can use <br />

Categories