Opencart add size option to search results - php

I was wondering if someone could explain how to add options to my search results (category, manufacture) etc. I just need them in a select tag (no actions required). I tried similar code to the product options but that didn't work.
Thanks

Found solution: http://www.opencartaz.com/opencart/attribute-options-on-category-page.html
<?php foreach ($products[$j]['options'] as $option) { ?>
<select name="option[<?php echo $option['option_id']; ?>]">
<?php foreach ($option['option_value'] as $option_value) { ?>
<option value="<?php echo $option_value['option_value_id']; ?>"><?php echo $option_value['name']; ?></option>
<?php } ?>
</select>
<?php } ?>

Related

Order values in select option list - php

I am new to coding and have the below select option list that is returning unordered values. The code checks the database for existing values (number of bedrooms) and returns them as options in the list. I would like to order them in an ascending manner:
<select class="bedrooms" name="Bedrooms" id="Bedrooms" >
<option value="">BEDROOMS</option>
<?php if ($bed_room): ?>
<?php foreach ($bed_room as $key => $value): ?>
<option value="<?php echo $value['bed_room'] ?>"><?php echo $value['bed_room'] ?></option>
<?php endforeach; ?>
<?php endif ?>
</select>
You have to search for ($bed_room) in your files so you can see from where it is getting the values and from there you can sort it as you like
or you can sort the array itself like this:
natsort($bed_room);
<select class="bedrooms" name="Bedrooms" id="Bedrooms" >
<option value="">BEDROOMS</option>
<?php if ($bed_room): ?>
<?php natsort($bed_room);>
<?php foreach ($bed_room as $key => $value): ?>
<option value="<?php echo $value['bed_room'] ?>"><?php echo $value['bed_room'] ?></option>
<?php endforeach; ?>
<?php endif ?>
</select>

Php string replace of values

In opencart shop i have a product limit in category page, for example the default number of products per page.I setet up the maximul number to be 1000, but i wanna strig replace that number to "ALL".
Belo is the code for the string. <?php echo $limits['text']; ?>
I wanna string replace $limits['text'] for the value 1000 to be ALL
<select onchange="location = this.value;">
<?php foreach ($limits as $limits) { ?>
<?php if ($limits['value'] == $limit) { ?>
<option value="<?php echo $limits['href']; ?>" selected="selected"><?php echo $limits['text']; ?></option>
<?php } else { ?>
<option value="<?php echo $limits['href']; ?>"><?php echo str_replace('1000', 'ALL', $limits['text']); ?></option>
<?php } ?>
<?php } ?>
</select>
This works but in the link of the page it sould be like this
/index.php?route=product/category&path=142#/sort=p.sort_order/order=ASC/limit=1000
but is like this
/index.php?route=product/category&path=142#/sort=p.sort_order/order=ASC/limit=All
In inspect element the code is generated corectly
<select>
<option value="http://diamanti.teamweb.ro/index.php?route=product/category&path=142&limit=30" selected="selected">30</option>
<option value="http://diamanti.teamweb.ro/index.php?route=product/category&path=142&limit=90">90</option>
<option value="http://diamanti.teamweb.ro/index.php?route=product/category&path=142&limit=1000">All</option>
</select>
But when i click ALL , the link modifiecs not with the "value" But with the text...

how to set_value use for dropdown input using codeigniter?

I want to used set_value for input where type=text and i have no problem with that.
I am confused to use dropdown value. i am fetch dropdown values from database and i could not understand where i use set_value .
My code:
<select class="form-control" name="sport_name" id="sport_name">
<option value=''>--Select Sport--</option>
<?php foreach($getSport as $item):
if($item->sport_name==$mySport)
{?>
<option value="<?php echo $item->sport_id; ?>" selected><?php echo $item->sport_name; ?></option>
<?php }else{?>
<option value="<?php echo $item->sport_id; ?>"><?php echo $item->sport_name; ?></option>
<? } endforeach; ?>
</select>
You Can try This:
<select class="form-control" name="sport_name" id="sport_name">
<option value=''>--Select Sport--</option>
<?php foreach($getSport as $item):?>
<option value="<?php echo $item->sport_id; ?>" <?php if($item->sport_name==$mySport)
{ echo "Selected";} ?>><?php echo $item->sport_name; ?></option>
<? } endforeach; ?>
</select>
As per the docs, you would not use set_value for a select element:
"Permits you to set the value of an input form (text input) or textarea.".
Rather, you would use set_select.
If you use a <select> menu, this function permits you to display the menu item that was selected. The first parameter must contain the name of the select menu, the second parameter must contain the value of each item, and the third (optional) parameter lets you set an item as the default (use boolean TRUE/FALSE).
set_select($field[, $value = ''[, $default = FALSE]])
Parameters:
$field (string) – Field name
$value (string) – Value to check for
$default (string) – Whether the value is also a default one
Returns: ‘selected’ attribute or an empty string
Return type: string
Example:
<select class="form-control" name="sport_name" id="sport_name">
<option value=''>--Select Sport--</option>
<option value="one" <?php echo set_select('sport_name', 'one'); ?> >One</option>
<option value="two" <?php echo set_select('sport_name', 'two'); ?> >Two</option>
<option value="three" <?php echo set_select('sport_name', 'three'); ?> >Three</option>
</select>
your code should be work correctly, but best way: you can declare var called $selected and make comparision to assign it with selected word inside loop when the value of select == current selected value:
<select class="form-control" name="sport_name" id="sport_name">
<option value=''>--Select Sport--</option>
<?php
foreach($getSport as $item):
$selected='';
if($item->sport_name==$mySport)
{
$selected='selected';
}
?>
<option value="<?php echo $item->sport_id; ?>" <?php echo $selected; ?> >
<?php echo $item->sport_name; ?></option>
<?php endforeach; ?>
</select>
The best way of doing this, while remaining readability would be as follow:
<select class="form-control" name="sport_name" id="sport_name">
<option selected>--Select Sport--</option>
<?php foreach($getSport as $item): ?>
<option <?php if($item->sport_name == $mySport){ echo "selected"; } value="<?php echo $item->sport_id;?>"><?php echo $item->sport_name; ?></option>
<?php endforeach; ?>
</select>
Ofcourse this is thinking you've got your data correctly set as objects or if you have arrays you'd use
<?php echo $item['sport_name']; ?>

PHP & HTML : how to create dynamic drop down list

I have a little problem.
I'm searching the internet now for quite some time to find a proper solution but I was not successful so far.
This is what I want :
First, I choose Category
Then, in second selection contain all the store result from category selection.
this is my first drop down list which is contain Category :
<select name="category" class="form-control" id="select1">
<option value="-1"> - Choose One -</option>
<?php
$StoreCategoriesAPIAccessor = new StoreCategoriesAPIService(GuzzleClient::getClient());
$stores = $StoreCategoriesAPIAccessor->getStoreCategories();
foreach ($stores as $store):
?>
<option value="<?php echo $store->getStoreCategoryId(); ?>"><?php echo $store->getCategory(); ?></option>
<?php endforeach; ?>
</select>
this is my second drop down list :
<select name="category" class="form-control" id="select2">
<option value="-1"> - Choose One -</option>
<?php
$StoreAPIAccessor = new StoreAPIService(GuzzleClient::getClient());
$stores = $StoreAPIAccessor->getStores();
foreach ($stores as $store):
?>
<option value="<?php echo $store->getStoreId(); ?>"><?php echo $store->getStoreName(); ?></option>
<?php endforeach; ?>
</select>
Anyone know how to implement dynamic drop down list for this case ?
Well first of all I'd like to ask that you separate business logic from view logic as much as possible, so I will do that in this answer.
Secondly, the 2nd dropdown box logic that you have does not contain anything that would retrieve stores for a given category, I will therefore make some assumptions and you can adjust based on that.
<?php
$StoreCategoriesAPIAccessor = new StoreCategoriesAPIService(GuzzleClient::getClient());
$categories = $StoreCategoriesAPIAccessor->getStoreCategories();
if (!empty($_GET['category'])) {
$category_id = $_GET['category'];
$StoreAPIAccessor = new StoreAPIService(GuzzleClient::getClient());
$stores = $StoreAPIAccessor->getStores($category_id); // Assumption, the call to getStores() accepts category_id as a parameter
} else { // Optional as you don't need to declare variables in PHP, but its better practice to do so
$category_id = null;
$stores = array();
}
?>
<select id="select_category" name="category" class="form-control" onchange="window.location='?category=' + this.value">
<option value="">- Choose One -</option>
<?php foreach ($categories as $category): ?>
<option value="<?php echo $category->getStoreCategoryId() ?>"><?php echo $category->getCategory() ?></option>
<?php endforeach ?>
</select>
<select id="select_store" name="store" class="form-control"<?php echo $category_id == null ? " disabled='disabled'" : "">
<option value="">- Choose One -</option>
<?php foreach ($stores as $store): ?>
<option value="<?php echo $store->getStoreId() ?>"><?php echo $store->getStoreName() ?></option>
<?php endforeach ?>
</select>

Remember selected option within select menu

I've googled a lot and found alternative solutions but in my case things are a bit different.
<select name='database'>
<?php foreach($databases as $row): ?>
<option value="<?php echo $row; ?>"><?php echo $row; ?></option>
<?php endforeach; ?>
</select>
I need to display the selected option after a POST request in between the option tags but since I already have a value for that I cannot find a way to do so. The idea is that I have one form with a couple of select menus. From the first one I select a database. The second one is for selecting a table from the already chosen database and another select menu for the columns. The problem is that I'm sending a new request for both the database and table and the chosen database cannot be remembered (it just 'resets' the select menu and starts from the first value).
Here's the whole code
Right now I need to reselect the database which I've previously chosen in order to display the columns from a table.
<select name='database'>
<?php foreach($databases as $row): ?>
<option value="<?= $row; ?>"
<?php if ($row == $_POST['database']){echo " selected";}?>>
<?= $row; ?>
</option>
<?php endforeach; ?>
</select>
Wouldn't this work?
$dbms=$_POST['database'];
<select name='database'>
<?php foreach($databases as $row): ?>
<option value="<?php echo $row; ?>"
<?php if ($row == $dbms) echo " selected"; ?>
> <?php echo $row; ?></option>
<?php endforeach; ?>
</select>

Categories