How to show just one time data while use foreach - php

I made a foreach to find out the ID of the item contained because there is a product that has a special packaging, I did a foreach like this
else if($value['id'] != 13){
foreach ($_packaging as $key => $value) {
if($value->p_id != 7){
$img_first = '';
$p_name = "p_name_" . $this->config->item("language");
$p_price = "p_price_" . $_currency;
$p_info = "p_info_" . $this->config->item("language");
if($key == 0){
$img_first = 'data-img-class="first"';
}
$text_price = "FREE";
if($value->$p_price != 0){
$text_price = currency_display($value->$p_price);
}
if($value->p_manage_stock){
if(($value->p_stock_qty - $value->p_stock_used) >= $_total_qty){
?>
<option data-img-label="<?php echo "<p class='packaging-price'>".$text_price."</p>" . "<br> <span class='packaging-info'>".$value->$p_info."</span>"; ?>" data-img-src="<?php echo base_url('assets/img/packaging/'.$value->p_image); ?>" <?php echo $img_first; ?> data-price="<?php echo $value->$p_price; ?>" value="<?php echo $value->p_id; ?>"> <?php echo $text_price; ?> </option>
<?php
}
}else{
?>
<option data-img-label="<?php echo "<p class='packaging-price'>".$text_price."</p>" . "<br> <span class='packaging-info'>".$value->$p_info."</span>"; ?>" data-img-src="<?php echo base_url('assets/img/packaging/'.$value->p_image); ?>" <?php echo $img_first; ?> data-price="<?php echo $value->$p_price; ?>" value="<?php echo $value->p_id; ?>"> <?php echo $text_price; ?> </option>
<?php
}
}
}
}
The Full code i save in pastebin
Full code in this link
I have 2 different products and the id != 13
but the problem is when there are 2 different products ID in the dining cart, the packaging options look double as below
What if I only want to display it once? because the id from multiple data packaging is the same?

Related

WordPress: How to order child pages in a select menu?

I have a select box, which allows you to navigate between parent pages and their children, they are given a specific order, in order to display them in a certain way on the website.
I believe that the line of code that interest you is perhaps this one
$children = get_pages("title_li=&child_of=" . $parent . "&echo=0");
The full code is below. You can see that the order of the pages on the site is different than the order of the pages in the select menu, that is because wordpress sorts per order, my pages are sorted alphabetically. How to sort them by order, so that what you see on the website matches the select box?
<?php
// determine parent of current page
if ($post->post_parent)
{
$ancestors = get_post_ancestors($post->ID);
$parent = $ancestors[count($ancestors) - 1];
}
else
{
$parent = $post->ID;
}
$children = get_pages("title_li=&child_of=" . $parent . "&echo=0");
?>
<div id="info-select-wrap" class="single-france-select">
<select id="info-select"class="fr-select" >
<?php
if (get_the_ID() == $parent->ID): ?>
<option value="<?php
echo get_the_permalink($parent->ID); ?>" selected>  <?php
echo get_the_title($parent->ID); ?></option>
<?php
else: ?>
<option value="<?php
echo get_the_permalink($parent); ?>">  <?php
echo get_the_title($parent); ?></option>
<?php
endif;
foreach($children as $child):
if (has_children($child))
{
if (get_the_ID() == $child->ID)
{ ?>
<option value="<?php
echo get_the_permalink($child->ID); ?>" selected>  <?php
echo get_the_title($child->ID); ?></option>
<?php
}
else
{ ?>
<option value="<?php
echo get_the_permalink($child->ID); ?>"> <?php
echo get_the_title($child->ID); ?></option>
<?php
}
}
else
{
if (get_the_ID() == $child->ID)
{ ?>
<option value="<?php
echo get_the_permalink($child->ID); ?>" selected>  <?php
echo get_the_title($child->ID); ?></option>
<?php
}
else
{ ?>
<option value="<?php
echo get_the_permalink($child->ID); ?>"> <?php
echo get_the_title($child->ID); ?></option>
<?php
}
}
endforeach; ?>
</select>
</div>
I tried
$children = get_pages("title_li=&child_of=" . $parent . "&echo=0&orderby='menu_order'&order='ASC'");
Didn't solve it.
Issue solved by adding sort_column=menu_order
$children = get_pages("title_li=&child_of=" . $parent . "&echo=0&sort_column=menu_order");
Just a quick note, &echo=0 is useless, you can remove it.

Locations display ossclas

I use osclass and I have this code for display regions and their cities. All working good but I need display regions and cities only with items, how can i do it? Please help me i'm beginner.
<?php
$aRegions = Region::newInstance()->getByCountry('AT');
if(count($aRegions) > 0 ) {
?>
<ul>
<?php
foreach($aRegions as $region) {
//print_r ($region);
echo "<li>";
//$region['pk_i_id'].
?>
<div class="accordionButton">
<a href="javascript:void()">
<?php echo $region['s_name']."\n"; ?>
</a>
<?php // echo "</em>(". $region['items'].")</em>";?>
</div>
<?php
$aCities = City::newInstance()-> getByRegion($region['pk_i_id']);
if(count($aCities) > 0 ) {
echo "<div class=\"accordionContent\">";
echo "<ul>";
foreach($aCities as $city) {
// print_r ($city);
//$city["pk_i_id"].'
echo "<li>";
echo "<a href='". osc_search_url( array( 'sRegion'=>$region["s_name"], 'sCity' => $city['s_name'] ) ) ."'> ";
echo $city["s_name"]."\n";
echo "</em>(". $city['items'].")</em>";
echo "</a>";
echo "</li>";
}
}
echo "</ul>";
echo "</li>";
}
?>

Endforeach loop different class for first item

I'm not that good at php. What i have is a list of items looped with endforeach. What I want is that the first loop will have a class of col-lg-12 and from the second one that class will become col-lg-6. How can I achive that?
Here's the code:
<?php $firstLoop = true;
foreach( $network_value as $key => $value ){
if( $firstLoop ){
echo '<div class="col-lg-12">';
}
echo '<div class="col-lg-6">';
$firstLoop = false;
} ?>
^This is the code that I've tried, but it's not working how i wanted.
<?php if ($img): ?>
<img src="<?php echo $thumb->src ?>" width="<?php echo $thumb->width ?>" height="<?php echo $thumb->height ?>" alt="" />
<?php endif; ?>
<h4>
<div class="circle"><?php echo $datePublic = date('d M', strtotime($page->getCollectionDatePublic())); ?></div>
<br>
<a class="blogTitle" href="<?php echo $url ?>" target="<?php echo $target ?>"><?php echo $title ?></a></h4>
<h6>Posted by <?php echo $author; ?></h6>
<br>
<p><?php echo $description ?></p>
</div>
<?php endforeach; ?>
The most simple way to do it is to add a counter and check if it is the first value in the counter.
<?php
$counter = 0;
foreach( $network_value as $key => $value )
{
if($counter == 0){
echo '<div class="col-lg-12">';
} else {
echo '<div class="col-lg-6">';
}
$counter++;
}
?>
Also I want to add that there are two ways of using foreach and if-statements, but you are trying to mix them, which is wrong.
The first method is using brackets "{" and "}":
foreach($users as $user) {
// do things for each user
echo $user->name; // Example of writing out users name
}
And if-statement:
if(true) {
// do something
}
The second method is using "foreach(statement):" and "endforeach;"
foreach($users as $user):
// do things for each user
echo $user->name; // Example of writing out users name
endforeach;
And if-statement:
if(true):
// do something
endif;
Edited:
In your case you can use:
<?php
foreach ($pages as $key=>$page):
if($key==0){
echo '<div class="col-lg-12">';
} if($key==1){
echo '<div class="col-lg-6">';
}
endforeach; ?>
Or you can use:
<?php
foreach ($pages as $key=>$page):
if($key==0){
echo '<div class="col-lg-12">';
} else {
echo '<div class="col-lg-6">';
}
endforeach; ?>
OLD:
foreach($array as $key=>$row){
if($key==0){
echo '<div class="col-lg-12"></div>';
}
if($key==5){
echo '<div class="col-lg-6"></div>';
}
// OR try use %
if($key%2 == 0){
echo '<div class="col-lg-12"></div>';
} else {
echo '<div class="col-lg-12"></div>';
}

How can I get this to display in 1 loop

In the backend of my website, I have added more categories as follows
<strong>Tour category : </strong><br />
<select name="category_id">
<?php
foreach($category_lists as $list){
if($list['id'] == $arr_collect['category_id']){
$sel = ' selected="selected" ';
}else{
$sel = null;
}
echo '<option value="'.$list['id'].'"'.$sel.'>'.$list['name'].'</option>';
}
?>
</select>
<br clear="all" />
<strong>Tour category 2: </strong><br />
<select name="category_id2">
<option value="">Select Another</option>
<?php
foreach($category_lists as $list){
if($list['id'] == $arr_collect['category_id2']){
$sel = ' selected="selected" ';
}else{
$sel = null;
}
echo '<option value="'.$list['id'].'"'.$sel.'>'.$list['name'].'</option>';
}
?>
</select>
<br clear="all" />
<strong>Tour category 3: </strong><br />
<select name="category_id3">
<option value="">Select Another</option>
<?php
foreach($category_lists as $list){
if($list['id'] == $arr_collect['category_id3']){
$sel = ' selected="selected" ';
}else{
$sel = null;
}
echo '<option value="'.$list['id'].'"'.$sel.'>'.$list['name'].'</option>';
}
?>
</select>
In the front end, I have this
<?php
$sql_ci = "SELECT * FROM location WHERE parent_id <> 0 ORDER BY parent_id, sort ";
$result_ci = mysql_query($sql_ci);
$cities = $Manager->fetchAssoc($result_ci); //peek_array($cities);
if(!empty($cities)){
foreach($cities as $city){
$i = 1; // for active category
$sql_count = "SELECT * FROM tour t, tour_category c
where t.location_id = {$city['id']} and t.type = 'P'
group by t.category_id AND t.category_id2"; //echo $sql_count.'<br />';
$result_count = mysql_query($sql_count);
$count = $Manager->fetchAssoc($result_count);
?>
<ul class="tours-accord tour-private" id="tourcity<?php echo $city['id'];?>">
<?php
$sql_ca = "SELECT * FROM tour_category ORDER BY sort";
$result_ca = mysql_query($sql_ca);
$categories = $Manager->fetchAssoc($result_ca);
if(!empty($categories)){
foreach($categories as $cat){
$sql_tour = "SELECT t.* FROM tour t
WHERE
t.touronline = 'yes'
AND t.category_id = {$cat['id']}
AND t.location_id = {$city['id']}
and t.type = 'P'
ORDER BY t.tourcode";
$result_tour = mysql_query($sql_tour);
$tours = $Manager->fetchAssoc($result_tour);
if(!empty($tours)){
?>
<li style="border-bottom:1px solid gray;">
<?php echo $city['name']; ?> - <?php echo $cat['name']; //echo ' '.$i.' ';?>
<ul>
<?php foreach($tours as $tour){
if(!empty($tour['intro_image'])){
$img = $tour['intro_image'];
}else{
$img = 'default.jpg';
}
?>
<a href="tour-details.php?code=<?php echo $tour['tourcode'];?>" class="tourname">
<li class="<?php if($i == count($count)){echo 'active';} ?>">
<img src="uploaded-items/tour-introimage/<?php echo $img; ?>" width="150" height="100" />
<span class="tourname"><?php echo $tour['tourname'].'(Code: '.$tour['tourcode'].')'; ?></span>
<p><?php echo $tour['intro']; ?></p>
<div class="home-tour-info">
<img src="images/btn-more-info-green-dark.jpg" alt="More info" class="moreinfo"/>
<div class="home-tour-price"><?php echo $tour['promotion_label']; ?></div>
</div>
</li></a>
<?php } ?>
</ul>
</li>
<?php $i++;}}} //each category ?>
The way I made it work was post the last bit 3 times and changed category_id to category_id2, category_id3. But this displayed duplicate categories.
How can I make all this just in 1 loop instead of 3?
Your backend loop is like wise as follows, as a optimising code,
<?
$numcount = 3;
for($i=0;$i<$numcount;$i++) { ?>
<strong>Tour category <?=$i+1?>: </strong><br />
<select name="category_id<?=$i+1?>">
<?php
foreach($category_lists as $list){
if($list['id'] == $arr_collect['category_id'.($i+1)]){
$sel = ' selected="selected" ';
}else{
$sel = null;
}
echo '<option value="'.$list['id'].'"'.$sel.'>'.$list['name'].'</option>';
}
?>
</select>
<br clear="all" />
<? } ?>

html select box - load image for default selected option

Today I faced with a problem about html select
here is the code :
<select id="optdon" onchange="showimage(this);" name="optdon" style="width:<?php echo $drop_width; ?>px;">
<option value="">--Select don--</option>
<?php foreach($for_don as $donname) { if(isset($donname['don_name']) && $donname['status'] == 1) { ?>
<option value="<?php echo $donname['don_id']; ?>"><?php echo $donname['don_name']?></option>
<?php } } ?>
</select>
<br /><br />
<div align="center">
<?php $i = 0; foreach($for_don as $donimage) { if(isset($donimage['don_image'])) { ?>
<div class="<?php echo $i; ?>" style="display:none;" id="<?php echo $donimage['don_id']; ?>"><img src="<?php echo $donimage['don_image']?>" /></div>
<?php $i = $i + 1;} }?>
function showimage(ids){
clearall();
if(ids.value != ''){
jQuery('#'+ids.value).show();
}
}
What is the project ? I need to select first option !!!
It is easy but when i set the first option selected it's image doesn't load in the page first load . option's image appears on changing options.
I have these two answers for selecting first option (when thre is just one option )
suggestion 1
<?php $x = 0; foreach($for_don as $donname){ if(isset($donname['don_name']) && $donname['status'] == 1) { $x = $x + 1; }} ?>
<select id="optdon" onload="showimage(this);" name="optdon" style="width:<?php echo $drop_width; ?>px;">
<option value="">--Select don--</option>
<?php $z = 0; $selected='selected' ?>
<?php foreach($for_don as $donname) { if(isset($donname['don_name']) && $donname['status'] == 1) { $z = $z + 1; ?>
<option <?php if($z==1 && $x == 1)echo $selected; ?> value="<?php echo $donname['don_id']; ?>" ><?php echo $donname['don_name']?></option>
<?php } } ?>
</select>
<br /><br />
<div align="center">
<?php $i = 0; foreach($for_don as $donimage) { if(isset($donimage['don_image'])) { ?>
<div class="<?php echo $i; ?>" style="display:none;" id="<?php echo $donimage['don_id']; ?>"><img src="<?php echo $donimage['don_image']?>" /></div>
<?php $i = $i + 1;} }?>
</div>
suggestion 2 :
<?php $x = 0; foreach($for_don as $donname){ if(isset($donname['donation_name']) && $donname['status'] == 1) { $x = $x + 1; }} ?>
<?php if($x == 1) { ?>
??????
<?php } else { ?>
thank you
Add this JavaScript:
var optdon = document.getElementById('optdon');
optdon.onchange.call(optdon);
This will trigger the onChange event handler, allowing the initial option to have its effect.

Categories