Hiding empty array values in CodeIgniter - php

I have a loop within PHP (codeigniter framework) that uses an array ($products) to display product data. Not all the products in the array have values.
Is there anyway that I can hide the call to display those values if they do not exist within the array.
Full code for the loop below;
<!-- Product item row -->
<?php foreach($products as $product): ?>
<div class="row product-list-row">
<div class="col-md-2 item-img-container" align="center">
<img class="img-responsive item-img" src="http://products.supercompare.co.uk<?= $product['logo']?>" alt="<?= $product['name']?> - supacompare.co.uk" width="190px" height="120px" /><br />
<!--<a role="button" data-toggle="collapse" href="#item-terms" aria-expanded="false" aria-controls="item-terms">LESS DETAILS &#9660</a>-->
</div>
<div class="col-md-7 item-text">
<h3><?= $product['name']?></h3>
<div class="content-wrapper">
<div class="content-1">
<p><strong><?= $product['custom_fields'][0]['field']?></strong>
<br /><?= $product['custom_fields'][0]['value']?></p>
</div>
<div class="content-2">
<p><strong><?= $product['custom_fields'][1]['field']?></strong>
<br /><?= $product['custom_fields'][1]['value']?></p>
</div>
<div class="content-3">
<p><strong><?= $product['custom_fields'][2]['field']?></strong>
<br /><?= $product['custom_fields'][2]['value']?></p>
</div>
<div class="content-4">
<p><strong><?= $product['custom_fields'][3]['field']?></strong>
<br /><?= $product['custom_fields'][3]['value']?></p>
</div>
<div class="content-bottom">
<p><?= $product['footer_text']?></p>
</div>
</div>
</div>
<div class="col-cta"><a class="apply-btn" href="<?= $product['tracking_link']?>" target="_blank">SEE DEAL ยป</a></div>
<!--<div class="clearfix"></div>-->
<!--<div id="item-terms" class="col-md-12 footer-terms" style="background-color:#B4B4B4; padding:10px; margin-top:10px;"><?= $product['footer_text']?></div>-->
</div>
<?php endforeach; ?>

You can do that by checking the array for not empty by !empty.
if(!empty($product['custom_fields'][0]['value'])){
//your code
}
According to your code.
<?php if(!empty($product['custom_fields'][0]['value'])){ ?>
<div class="content-1">
<p><strong><?= $product['custom_fields'][0]['field']?></strong>
<br /><?= $product['custom_fields'][0]['value']?></p>
</div>
<?php } ?>

here is an example :
<?php
if(isset($product['custom_fields'][0]['value']) && $product['custom_fields'][0]['value'] !=""){
//your code
}
?>

foreach ($products as $product) {
foreach ($product['custom_fields'] as $value) {
if (!$value) {
continue 2;
}
}
//your code here!!!
}

Related

Dynamic div close in php while loop

code i have written below is working fine but at the end of the looping the div is not closed its still opening a loop
<div class="carousel-inner">
<div class="item active">
<div class="row">
<?php
$recent_projects_sql="SELECT * from recent_projects where service_type='upholstery'";
$recent_projects_conn=mysql_query($recent_projects_sql) or die(mysql_error());
$i=0; $split=0;
while($projects=mysql_fetch_array($recent_projects_conn)) {
$i++;
?>
<div class="col-sm-3">
<div class="col-item" style="">
<div class="photo-shadow"></div>
<div class="photo">
<img src="admin/assets/images/uploads/projects/<?php echo $projects['attachment1']; ?>" alt="User one">
</div>
<div class="info">
<div class="name">
<?php echo $projects['service_name']; ?>
</div>
<div class="degination">
<?php echo $projects['sub_title']; ?>
</div>
<div class="buttons">
<a class="btn btn-theme ripple-effect" href="#">View More</a>
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
<?php
$split++;
if ($split % 4 == 0){
echo '</div></div><div class="item"><div class="row">';
}
}
?>
</div>
</div>
The Div has splited very well but in end of the loop div has not been closed. Thats only the problem please provide me the help to sort out the problem
When I inspect the element the last loop will show at the given result as follows:
<div class="col-sm-3">
<div class="col-item">
<div class="photo-shadow"></div>
<div class="photo">
<img src="admin/assets/images/uploads/projects/1557301934.jpg" alt="User one">
</div>
<div class="info">
<div class="name">UPHOLSTERY</div>
<div class="degination">UPHOLSTERY</div>
<div class="buttons">
<a class="btn btn-theme ripple-effect" href="#">View More</a>
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
</div></div><div class="item"><div class="row">
I want to remove the two opening div's as dynamically. How can i set this to remove opened div's at then end of the looping
I just took a quick look and it looks like you are not closing the "carousel-inner" div
<div class="carousel-inner">
<div class="item active">
<div class="row">
<?php
$recent_projects_sql = "SELECT * from recent_projects where service_type='upholstery'";
$recent_projects_conn = mysql_query( $recent_projects_sql ) or die( mysql_error() );
$i = 0;
$split = 0;
while ( $projects = mysql_fetch_array( $recent_projects_conn ) ) {
$i ++;
?>
<div class="col-sm-3">
<div class="col-item" style="">
<div class="photo-shadow"></div>
<div class="photo">
<img src="admin/assets/images/uploads/projects/<?php echo $projects['attachment1']; ?>"
alt="User one">
</div>
<div class="info">
<div class="name">
<?php echo $projects['service_name']; ?>
</div>
<div class="degination">
<?php echo $projects['sub_title']; ?>
</div>
<div class="buttons">
<a class="btn btn-theme ripple-effect" href="#">View More</a>
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
<?php $split ++;
if ( $split % 4 == 0 ) {
echo '</div></div><div class="item"><div class="row">';
}
}
?>
</div>
</div>
Add a Boolean check for the execution of loop, such as $check = true;, add this within the loop.
after the loop add this
if($check){
echo " </div></div>";
}
That's because at the end of iteration (in case of mod 4 and even without it), you keep 2 divs opened
echo '</div></div><div class="item"><div class="row">';

Bootstrap, why are products displayed arbitrarily?

Have a website in three languages. The porducts select from a mysql database and showed wiht a do-while-loop.
<!-- Shop Page Area Start-->
<div class="shoppage-area">
<div class="container">
<div class="about-desc">
<h2>
<?php echo $page['subtitle']; ?>
</h2>
<?php echo $page['content']; ?>
</div>
<div class="row">
<?php
do {
?>
<!--Product Start-->
<div class="col-lg-3 col-md-3 col-sm-6 col-xs-12">
<div class="sngle-product">
<div class="product-thumb">
<img src="<?php echo $product['img']; ?>" alt=""/>
<h2>
<a href="#">
<?php echo $product['name']; ?>
</a>
</h2>
</div>
<h3>
<?php echo $product['desc']; ?>
</h3>
<span><?php echo $product['size']; ?></span> <span class="price"><?php echo $product['price']; ?> LE</span>
<div class="product-overlay">
<ul>
<li>
<div class="product-quantity">
<div class="input-number">
<input type="text" value="1" name="quantity">
</div>
</div>
</li>
<li><a class="orderbtn" href="#" data-uid="<?php echo $product['uniqueid']; ?>">ORDER</a></li>
</ul>
</div>
</div>
</div>
<!-- Product End -->
<?php
} while ($product = $res->fetch_assoc()) ?>
</div>
<!-- end Row -->
</div>
</div>
The problem is that in one language showed correctly and in the other i get a lot spaces between the products. See the image
enter image description here
How i can solve this
Add an additional class flex-row to the product wrapping row element. And try applying the following styles.
.flex-row{
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
}
You can divide your column like this
<div class="shoppage-area">
<div class="container">
<div class="about-desc">
</div>
<div class="row">
<?php $i=1;
do {
?>
<div class="col-lg-3 col-md-3 col-sm-6 col-xs-12">
<div class="sngle-product">
<div class="product-thumb">
<img src="<?php echo "test"; ?>" alt=""/>
<h2><?php echo "test"; ?> </h2>
</div>
<h3><?php echo ($i==2)? " testtesttesttesttesttest testtesttesttest test": "test"; ?></h3>
<span><?php echo "test"; ?></span> <span class="price"><?php echo 12321; ?> LE</span>
<div class="product-overlay">
<ul>
<li>
<div class="product-quantity">
<div class="input-number">
<input type="text" value="1" name="quantity">
</div>
</div>
</li>
<li><a class="orderbtn" href="#" data-uid="<?php echo "1231"; ?>">ORDER</a></li>
</ul>
</div>
</div>
</div>
<?php
echo $i%4;
if($i%4== 0 && $i > 1) {
echo '</div><div class="row">';
}
$i++;
} while ($i<=10) ?>
</div>
</div>
</div>

I need to display 3 unique products in each div

I need to display 3 unique products in each div, like this: Sample output
How can I get the different products in one loop? At present in the below code, I'm getting the same product 3 times.
<?php
foreach ($rows as $display) {
?>
<div class="col-md-4 col-sm-6">
<div class="section-title">
<div class="pull-right pr-icon">
<i class="fa fa-diamond"></i> Top Suppliers
</div>
<div class="main-title">
<b> Selected Products</b>
</div>
<div class="sub-title">
Source the latest items
</div>
</div>
<div class="row">
<div class="col-md-4">
<img src="<?php echo 'data:image;base64,'.$display->image; ?>" class="img-responsive image product-item" />
<div class="product-description">
<p class="text-center"><?php echo $display->name; ?></p>
<p class="desc-btn">VIEW</p>
</div>
</div>
</div>
</div>
<?php
}
?>
From controller:
public function index()
{
$this->load->database();
$data['rows']=$this->homemodel->fetchData();
$this->load->view('home',$data);
$this->load->library('image_lib');
}
Model code:
<?php
class homemodel extends CI model {
function __construct()
{
parent::__construct();
}
public function fetchData()
{
$query=$this->db->select('*');
$this->db->from('display');
$query = $this->db->get();
return $query->result();
}
}
?>
<?php
$chunkedArray = array_chunk($rows, 3); //productList Array, number of chunks/parts
foreach($chunkedArray as $newRow) {
echo '<div class="row">';
foreach ($newRow as $display) {
?>
<div class="col-md-4 col-sm-6">
<div class="section-title">
<div class="pull-right pr-icon">
<i class="fa fa-diamond"></i> Top Suppliers
</div>
<div class="main-title">
<b> Selected Products</b>
</div>
<div class="sub-title">
Source the latest items
</div>
</div>
<div class="row">
<div class="col-md-4">
<img src="<?php echo 'data:image;base64,'.$display->image; ?>" class="img-responsive image product-item" />
<div class="product-description">
<p class="text-center"><?php echo $display->name; ?></p>
<p class="desc-btn">VIEW</p>
</div>
</div>
</div>
</div>
<?php
}
echo '</div>';
}
?>
array_chunk would do the work..
know more on array_chunk here

remove duplicate id from an array in for loop

i have different parameters...on the products...but i want to display only those parameters that are clicked by the user.i have done this but i want to delete the duplicacy.. my code is:-
$ab=$_POST['data1'];
$karan=explode(",",$ab);
array_pop($karan);
for($i=0;$i<count($karan);$i++){
'karan['.$i.'] = '.$karan[$i];
$posts = $wpdb->get_results("SELECT * FROM demo_postmeta WHERE meta_value='".$karan[$i]."' ");
$out=array();
foreach($posts as $post){
if (!isset($out[$post->post_id])){
$out[$post->post_id][$post->meta_key]=$out;
$ram=get_the_post_thumbnail($post->post_id,"_thumbnail_id",true);
$title=get_post_meta($post->post_id,"_product_title",true);
$price=get_post_meta($post->post_id,"_product_year",true);
$link=get_post_meta($post->post_id,"_product_likes",true);
?>
<div id="itemListContent" class="in-list">
<div class="the-list">
<div class="li first row clearfix" style="cursor: pointer;">
<div class="c-1 table-cell">
<div class="cropit">
<a class="pics-lnk important-rule" href="http://uzodocs.com/PROJECTS/demo/products?post_id=<?php echo $post->post_id ?>" style="text-decoration: none;">
<?php echo $ram; ?>
</a>
</div>
</div>
<div class="second-column-container table-cell">
<h3>
<a href="http://uzodocs.com/PROJECTS/demo/products?post_id=<?php echo $post->post_id ?>" title="Kenxinda Watch Mobile Dual SIM with FREE Bluetooth Headset free home delivery - Delhi" class="important-rule" style="text-decoration: none;">
<?php echo $title; ?>
</a>
</h3>
<div class="c-4">
</div>
<span class="itemlistinginfo clearfix">
<span>
Delhi |
Cameras
- Camera Accessories
</span>
<span>
</span>
</span>
</div>
<div class="third-column-container table-cell">
<?php echo $price; ?>
</div>
<div class="fourth-column-container table-cell"><br> </div>
</div>
<div class="li even row clearfix" style="cursor: pointer;">
<div class="c-1 table-cell">
</div>
</div>
</div>
</div>
<?php
}
$out[$post->post_id][$post->meta_key][] = $post->post_id;
}
}
die;
?>
UPDATE:
This may be a way to solve your problem:
if (!in_array($post->post_id, $out)){
$out[] = $post->post_id;
You can remove the last $out[$post->post_id][$post->meta_key][] = $post->post_id;
Just put you required data into
$out[$post->post_id][$post->meta_key][] = "data you need".
and put this array out of for scope. just before die;

Reversing the second div set order with separate class in foreach loop

Reversing the second div set order with separate class in foreach loop.
It will be a shelf like structure. I tried to alternate the .tl .tr class.
OR
Can we able to use css to align the second set.
Code:
<?php foreach ($products as $product) { ?>
<div class="row">
<div class="shelf">
<div class="span4"><span class="tl">
<?php if ($product['thumb']) { ?>
<img src="<?php echo $product['thumb']; ?>" alt="<?php echo $product['name']; ?>" />
<?php } ?>
</span></div>
<div class="span2">
<div class="name"><?php echo $product['name']; ?></div>
<?php if ($product['price']) { ?>
<div class="price">
<?php if (!$product['special']) { ?>
<?php echo $product['price']; ?>
<?php } else { ?>
<span class="price-old"><?php echo $product['price']; ?></span> <span class="price-new"><?php echo $product['special']; ?></span>
<?php } ?>
</div>
<?php } ?>
<div class="cart">
<input type="button" value="<?php echo $button_cart; ?>" onclick="addToCart('<?php echo $product['product_id']; ?>');" class="button" />
</div>
</div>
<div class="span2">Span 2</div>
<div class="span4"><span class="tr">Span4</span></div>
</div>
</div>
<?php } ?>
Css:
.shelf .tl {
margin-left: 58px;
}
.shelf .tr {
margin-left: -33px;
}
I think this will solve problem, it implements a counter to track whether current product should go on left or right
$product_counter = 0;
<?php foreach ($products as $product) { ?>
<?php $product_counter++; ?>
<?php if(!($product_counter%2 ==0)) { ?>
<div class="row">
<div class="shelf">
<div class="span4">
// image echo code here
</div>
<div class="span2">
// price, add to cart etc
</div>
<?php } else { ?>
<div class="span2">
// price, add to cart etc
</div>
<div class="span4">
// image echo code here
</div>
</div> <!-- shelf div closing tag -->
</div> <!-- row div closing tag -->
<?php } ?> <!-- closing else -->
<?php } ?> <!-- closing foreach-->
<!-- in case if there were odd numbers of total product, then close the row and shelf div after foreach -->
<?php if(!($product_counter%2 ==0)) { ?>
</div>
</div>
<? } ?>

Categories