Optgroups are closed after 1 item. Dynamic list - php

I have a select form object in index.php. The items are gotten from mySQL. What im trying is to achieve is: Create an optgroup whenever catID = NULL. In this case Section1 and Section2 should be optgroups. But the optgroups are closed after 1 option. Any tips? screenshot:
database->table:
catId catName
NULL --Section1--
1010 Bilar
1020 Bildelar & tillbehör
1030 Motorcycklar
1040 Motorcycklar delar & tillbehör
1050 Moped
1070 Lastbil & Truck
NULL --Section2--
2010 Bygg och Trädgår
2020 Möbler & Heminredning
2030 Husgeråd & Vitvaror
2040 Verktyg
HTML:
<?php $count = 0; ?>
<?php foreach($categories as $category ): $count++;?>
<?php if($category['catId']==NULL){?>
<optgroup label="<?php $category['catName']?>">
<?php } else{ ?>
<option value="<?php echo $category['catId'];?>"><?php echo $category['catName'];?></option>
<?php
$temp = $categories[($count+1)];
if($temp['catId']==NULL)?>
</optgroup>
<?php } ?>
<?php endforeach;?>
</select>

Try this :
<?php $first_opt = TRUE; ?>
<?php foreach($categories as $category ): ?>
<?php if($category['catId']==NULL){?>
<?php if($first_opt):?>
<optgroup label="<?php echo $category['catName']?>">
<?php $first_opt = FALSE;?>
<?else:?>
</optgroup><optgroup label="<?php echo $category['catName']?>">
<?endif;?>
<?php } else{ ?>
<option value="<?php echo $category['catId'];?>"><?php echo $category['catName'];?></option>
<?php } ?>
<?php endforeach;?>
</optgroup>
</select>

Related

adding category and its subcategories in html select

i am trying to add father categories then its subcategories as options in select , so i did the following
<select class="form-control" name="select">
<?php foreach($father as $father) : ?>
<option value="<?php echo $father->id; ?>">
*Category: <?php echo $father->name." :-"; ?>
</option><?php foreach($child as $child) : ?><?php if($child->category_id==$father->id){?>
<option value="<?php echo $child->id; ?>">
-Sub-Category: <?php echo $child->name; ?>
</option><?php } endforeach; ?><?php endforeach; ?>
</select>
here is my controller
public function addg()
{
$this->load->model('catagories_model');
$data['userdata'] = $this->session->all_userdata();
$data['father'] = $this->catagories_model->get_catagories();
$data['child'] = $this->catagories_model->get_catagories_child();
$this->load->view('catagories/grand/add', $data);
}
but it loops into the first father category and display its children and just displays the other father categories without going into the if statement what could be wrong ?
This line isn't doing anything:
<?php foreach($child as $child) : ?>
It should be:
<?php foreach($childs as $child) : ?>
Assuming $childs is the array where you're storing all the subcategories.

I need to select instead of echo/print [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have 3 courses on my site, so when you click sign up you are going to the sign up page where I have one drop down menu and I need to show course which is selected. Now I did everything but I am using echo, and than I have problem because I have 2 same courses.
You will see in the code:
$myCourse = intval($_GET['course_id']);
if ($myCourse) { ?>
<select id='course' class='form-text'>
<?php
foreach ( $courses as $course ) {
if ( $myCourse == $course->id ):
?>
<option selected data-price="<?php print $course->price ?>"><?php print $course->post_name ?></option>
<?php
endif;
?>
<option data-price="<?php echo $course->price ?>"><?php echo $course->post_name ?></option>
<?php
}
?>
</select>
<?php
} else {
?>
<pre>
<select id='select-course' class='signup__form-text'>
<option selected disabled>-- Select Course --</option>
<?php
foreach ( $courses as $course ) {
?>
<option data-price="<?php echo $course->price ?>"><?php echo $course->post_name ?></option>
<?php
}
?>
</select>
<?php
}
return ob_get_clean();
SO on the spot where is "print" I need something to select that choice not to print it out. Because I echo my 3 courses later and now I add existing course.
So I need select instead of print.
Can someone give me some tip?
check this code
<select id='select-course' class='signup__form-text'>
<!-- If have id select it in drop down -->
<?php
foreach ( $courses as $course ) {
if ( $myCourse == $course->id ) { ?>
<option selected data-price="<?php echo $course->price ?>"><?php echo $course->post_name ?></option>
<?php } else { ?>
<option data-price="<?php echo $course->price ?>"><?php echo $course->post_name ?></option>
<?php } ?>
<?php } ?>
</select>
not tested
you can also use this code
<select id='select-course' class='signup__form-text'>
<?php
foreach ( $courses as $course ) { ?>
<option <?php if ( $myCourse == $course->id ) { ?> selected <?php } ?> data-price="<?php echo $course->price ?>"><?php echo $course->post_name ?></option>
<?php } ?>
</select>
also this is work
<select id='select-course' class='signup__form-text'>
<?php
foreach ($courses as $course) { ?>
<option <?php if ($myCourse == $course->id) {
echo 'selected="selected"';
} ?> data-price="<?php echo $course->price ?>"><?php echo $course->post_name ?></option>
<?php } ?>
</select>
Use ternary operators
<select>
<?php
foreach($courses as $course){
echo ($myCourse == $course->id ? "<option selected value='$course->price'</option>" : "<option value='$course->price'</option>"
}
?>
</select>

Wordpress get posts as a function

Hi I'm trying to create a function to show a list of posts in my admin menu of wordpress but because I'm calling the function a few times on the same page I needed to add some extra statements but it breaks the output and I don't know why
Here is my current code that outputs the basics:
function test() {
// The Query
query_posts( array ('posts_per_page' => -1 ) );
// The Loop
while ( have_posts() ) : the_post();
?>
<option value="<?php the_permalink() ?>"><?php the_title(); ?></option>
<?php endwhile;
// Reset Query
wp_reset_query();
}
Output code:
<select>
<?php test(); ?>
</select>
Returned Output:
<select>
<option value="http://website/posttitle1">POST TITLE 1</option>
<option value="http://website/posttitle2">POST TITLE 2</option>
<option value="http://website/posttitle3">POST TITLE 3</option>
</select>
But I need to add a select option on like this:
function test($select) {
// The Query
query_posts( array ('posts_per_page' => -1 ) );
// The Loop
while ( have_posts() ) : the_post();
if ($select == the_permalink()) { $selected = " selected"; }
?>
<option value="<?php the_permalink() ?>"><?php the_title(); ?></option><?php echo "\n"; ?>
<?php endwhile;
// Reset Query
wp_reset_query();
}
Output code:
<select>
<?php test("..GET Permalink from Database.."); ?>
</select>
But then this is my output:
<select>
http://website/posttitle1<option value="http://website/posttitle1">POST TITLE 1</option>
http://website/posttitle2<option value="http://website/posttitle2">POST TITLE 2</option>
http://website/posttitle3<option value="http://website/posttitle2">POST TITLE 3</option>
</select>
I dont understand?
the_permalink() print the value and get_permalink() returns the value.
Try this.
Change the following line
if($select == the_permalink()) { $selected = " selected"; }
to
if ($select == get_permalink()) { $selected = " selected"; }
And this line to
<option value="<?php the_permalink() ?>"><?php the_title(); ?></option><?php echo "\n"; ?>
this
<option value="<?php echo get_permalink() ?>"><?php echo get_the_title(); ?></option><?php echo "\n"; ?>

How to create custom attribute to be a child of other attribute as dropdown catalog input type

I'm trying to create something like part finder (product finder).
I was thinking to edit layered navigation.
So now I have drop-boxes instead of list using this code for
app/design/frontend/default/carpoint/template/catalog/layer/filter.phtml
<select onchange="setLocation(this.value)">
<?php $count = 0; ?>
<?php foreach ($this->getItems() as $_item): ?>
<?php $count++; ?>
<?php if($count == 1): ?>
<option value='' disabled selected style='display:none;'>Choose <?php echo $attribute_code = $_item->getFilter()->getName();?> </option>
<?php endif; ?>
<option <?php if ($_item->getCount() > 0): ?> value="<?php echo $this->urlEscape($_item->getUrl()) ?>">
<?php echo $_item->getLabel() ?> <?php else: echo '>' . $_item->getLabel() ?> <?php endif; ?> (<?php echo $_item->getCount() ?>) </option>
<?php endforeach; ?>
</select>
How can I edit custom attribute eg "Type" so he is associated with parent attribute eg "Brand".
I something like this:

Magento - displaying parent and child categories in a dropdown menu

We're currently working on our category pages within Magento and have the following code to provide a list of child categories for the category we are in.
We have the following setup:
Store
- Cat 1 - Gloss Colours (Parent)
-- Sub cat 1 - Zurfiz (Child)
-- Sub cat 2 - Parapan (Child)
Screenshot of dropdown: http://awesomescreenshot.com/048kga35a
We are looking to modify the code to show a option of "Show All" and link to the parent category within the dropdown menu as well as the child categories, does anyone know what code we should pull up for this?
From app\design\frontend\default[our template]\template\catalog\navigation\left.phtml
<?php if (!Mage::registry('current_category')) return ?>
<?php $_categories=$this->getCurrentChildCategories() ?>
<?php
$oLayer = null;
$oCat = $this->getCurrentCategory();
# level 3 categories display their category siblings
if (3 === (int)$oCat->getData('level')) {
# get the layer object
$oLayer = Mage::getSingleton('catalog/layer');
$oParentCategory = $oCat->getParentCategory();
# set the parent category as the current category
$oLayer->setData('current_category',$oParentCategory);
# load the child categories
$_categories = $this->getCurrentChildCategories();
}
?>
<?php if($_categories->count()): ?>
<div class="box layered-nav">
<?php
# switch back the current category
if (null !== $oLayer) $oLayer->setData('current_category',$oCat);
?>
<script language="javascript" type="text/javascript" >
function jumpto(x){
if (document.form1.jumpmenu.value != "null") {
document.location.href = x
}
}
</script>
<div class="cat-dropdown">
<form name="catlist">
<select name="jumpmenu" onChange="jumpto(document.catlist.jumpmenu.options[document.catlist.jumpmenu.options.selectedIndex].value)">
<option value="#">Choose a brand</option>
<?php foreach ($_categories as $_category): ?>
<?php if($_category->getIsActive()): ?>
<option value="<?php echo $this->getCategoryUrl($_category) ?>"><?php echo $this->htmlEscape($_category->getName()) ?> (<?php echo $_category->getProductCount() ?>)</option>
<?php endif; ?>
<?php endforeach ?>
</select>
</form>
</div>
</div>
<?php endif; ?>
Use this code..
<?php
//$id = whatever
$subcats = Mage::getModel('catalog/category')->load($id)->getAllChildren();
?>
<select name="jumpmenu">
<option value="#">Choose a brand</option>
<?php
foreach(explode(',',$subcats) as $subCatid)
{
$_category = Mage::getModel('catalog/category')->load($subCatid);
//print_r($_category);
?>
<option value="<?php echo $_category->getCategoryUrl() ?>"><?php echo htmlEscape($_category->getName()) ?></option>
<?php } ?>
</select>

Categories