remove duplicate id from an array in for loop - php

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;

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">';

how to display 3 columns dynamic data from database using bootstrap 4

Following is my code I am displaying details as 3 columns per row using Bootstrap class row.
I tried like changing div and some condition
<div class="container">
<?php
if($lclResult->rowCount() > 0){
while($row = $lclResult->fetch(PDO::FETCH_ASSOC)) {
# code...
$lclUserName = $row['us_business_name'];
$lclImage1 = $row['us_image1'];
$lclCategory = $row['us_category'];
$lclArea = $row['us_area'];
?>
<div class="row">
<div class="col-sm-4">
<div class="card">
<img class="card-img-top " src="<?php echo $lclImage1 ?>" alt="Card image" style="width:100%; height: 158px;">
<div class="card-body">
<h4 class="card-title"><?php echo $lclUserName?></h4>
<p class="card-text" style="font-size: 25px;"><?php echo $lclCategory?></p>
<hr>
<i class="fa fa-map-marker" style="font-size: 23px;"><span> </span><?php echo $lclArea?></i>
<!-- See Profile -->
</div>
</div>
<?php
}
?>
</div>
<?php
} else {
?>
<div class="container">
<div class="row">
<div class="col-sm-12">
<h1> NO RESULT FOUND...</h1>
</div>
</div>
</div>
<?php
}
?>
</div>
</div>
I want to display 3 columns per row to display data. If anyone knows Please guide me with the above code.
Change your code like this
<div class="container">
<div class="row">
<?php
if($lclResult->rowCount() > 0){
while($row = $lclResult->fetch(PDO::FETCH_ASSOC)) {
# code...
$lclUserName = $row['us_business_name'];
$lclImage1 = $row['us_image1'];
$lclCategory = $row['us_category'];
$lclArea = $row['us_area'];
?>
<div class="col-sm-4">
<div class="card">
<img class="card-img-top " src="<?php echo $lclImage1 ?>" alt="Card image" style="width:100%; height: 158px;">
<div class="card-body">
<h4 class="card-title">
<?php echo $lclUserName?>
</h4>
<p class="card-text" style="font-size: 25px;">
<?php echo $lclCategory?>
</p>
<hr>
<i class="fa fa-map-marker" style="font-size: 23px;">
<span>
</span>
<?php echo $lclArea?>
</i>
<!-- See Profile -->
</div>
</div>
</div>
<?php
}
?>
<?php
} else {
?>
<div class="container">
<div class="row">
<div class="col-sm-12">
<h1> NO RESULT FOUND...
</h1>
</div>
</div>
</div>
<?php
}
?>
</div>
</div>

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>

Hiding empty array values in CodeIgniter

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!!!
}

How can I echo in else that "The searched business does not exist"?

My last question was how to access model's data in one of its view after got successful answer I implemented it. This was my question
Now its working good, but how can I echo that when a user search a business, which does not exist in the database, the system echo
Business not exist
I tried to insert else in the foreach loop, but it echoes "Business not exist" many times.
I am posting user searched business name in my landing.php which is my layout to searching view.
I am pasting my Landing.php, Business controller and the view file which I called Searching
My landing.php:
<form action="business/searching" method="POST">
<div class="form-group form-group-lg">
<h2 class="title">
Find the best places to eat, drink, shop, or visit in Islamabad.
</h2>
<div class="col-sm-5 col-md-5 col-lg-5 col-md-offset-1">
<input type="text" class="form-control" name="business" id="lg" placeholder="I'm looking for...."/>
</div>
<div class="col-sm-3 col-md-3 col-lg-3">
<input type="text" class="form-control" id="sm" placeholder="Islamabad" disabled=""/>
</div>
<div class="col-sm-3 col-md-3 col-lg-3">
<input type="submit" class="btn btn-primary btn-lg"/>
</div>
</div>
</form>
In my Business controller
public function actionSearching()
{
$models = Business::model()->findAll();
$modeladd = Address::model()->findAll();
$this->layout='//layouts/main';
$this->render(
'searching',
array(
'models' => $models,
'modeladd' => $modeladd,
)
);
}
Searching view Its bit complicated but on ask i can explain.
<div class="gap"> </div>
<div class="container">
<div class="row">
<div class="col-md-9">
<ul class="booking-list">
<li>
<?php foreach($models as $model) //getting business model from searching action in business controller
{
$name=$model->business_name; //storing name
$id=$model->id; //storing id in order to use this id in address if as addres has no business_name
if($name== $_POST["business"]) //matching name
{ ?>
<a class="booking-item" href="<?php Yii::app()->request->baseUrl;?>/business/userbusiness/<?php echo $id?>">
<div class="row">
<div class="col-md-5">
<img src="<?php Yii::app()->request->baseUrl;?>/img/<?php echo $model->image;?>" alt="Not availble" title="Image business" />
</div>
<div class="col-md-7">
<div class="booking-item-rating">
</div>
<h5 class="booking-item-title"><?php echo $model->business_name; ?></h5>
<?php foreach($modeladd as $mo) //getting address model from searching action in business controller
{
$addbizid=$mo->business_id; //businessid from address table
if($id== $addbizid) //wanted to match business name, but couldnot as addres has no business name only business_id, so i get business_id
// from above after the searched business name is matched and now comparing that id with addres->businessid
{
$street=$mo->street_number; //if match store address in these variable and then simple print them below
$sector=$mo->sector;
$city=$mo->city; ?>
<p class="booking-item-address"><i class="fa fa-map-marker"></i> <?php echo $street; echo $sector; ;echo $city;?> <?php }} ?></p>
<p class="booking-item-description"> <?php echo $model->business_description;?>
</p>
</div>
</div>
<?php }} ?>
</a>
</li>
</ul>
</div>
<div class="col-md-3">
<div class="booking-item-dates-change mb30">
<h3>Not here? Tell us what we're missing.</h3><p>If the business you're looking for isn't here, add it!</p>
<i class="fa fa-plus"></i> Add Business Page
</div>
</div>
</div>
<div class="gap"></div>
</div>
<div class="gap"></div>
</div>
You can use endif, end foreach loop if you want to use if else in foreach loop.
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
//echo $_POST["business"]; echo "</br>";
//foreach($modeladd as $mo)
// {
//
// $street=$mo->street_number;
//
// $address=$mo->sector;
//
// $city=$mo->city;
// $business_id=$mo->business_id;
// echo $street; echo ("<br />");echo $address; echo ("<br />");echo $city; echo ("<br />");echo $business_id;
//}
?>
<?php $matchFound = false; ?>
<div class="gap"> </div>
<div class="container">
<div class="row">
<div class="col-md-9">
<ul class="booking-list">
<li>
<?php foreach($models as $model): ?>
<?php //removing foreach outerloop
$name=$model->business_name; //storing name
$id=$model->id; //storing id in order to use this id in address if as addres has no business_name
?>
<?php if($name== $_POST["business"]): //matching name
$matchFound = true; //removing if match
?>
<a class="booking-item" href="<?php Yii::app()->request->baseUrl;?>/business/userbusiness/<?php echo $id?>">
<div class="row">
<div class="col-md-5">
<img src="<?php Yii::app()->request->baseUrl;?>/img/<?php echo $model->image;?>" alt="Not availble" title="Image business" />
</div>
<div class="col-md-7">
<div class="booking-item-rating">
</div>
<h5 class="booking-item-title"><?php echo $model->business_name; ?></h5>
<?php foreach($modeladd as $mo) //getting address model from searching action in business controller
{
$addbizid=$mo->business_id; //businessid from address table
if($id== $addbizid) //wanted to match business name, but couldnot as addres has no business name only business_id, so i get business_id
// from above after the searched business name is matched and now comparing that id with addres->businessid
{
$street=$mo->street_number; //if match store address in these variable and then simple print them below
$sector=$mo->sector;
$city=$mo->city; ?>
<p class="booking-item-address"><i class="fa fa-map-marker"></i>
<?php echo $street; echo $sector; ;echo $city;?>
<?php
}
}
?>
</p>
<p class="booking-item-description"> <?php echo $model->business_description;?>
</p>
</div>
</div>
<?php
break;
endif; //removing if matc outer loop
endforeach; //removing for each outer loop
?>
<?php if (!$matchFound): ?>
<?php echo "No data found for the room number you entered."; ?>
<?php endif; ?>
</a>
</li>
</ul>
</div>
<div class="col-md-3">
<div class="booking-item-dates-change mb30">
<h3>Not here? Tell us what we're missing.</h3><p>If the business you're looking for isn't here, add it!</p>
<i class="fa fa-plus"></i> Add Business Page
</div>
</div>
</div>
<div class="gap"></div>
</div>
<div class="gap"></div>
</div>

Categories