Php string replace of values - php

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...

Related

Selected as Selected Database value Codeigniter

In user edit module have a dropdown in which i show options from database.
Now i select a value from dropdown and want to set it as selected.
SO next time when i edit user i can see which was the last value selected.
When i edit a user i select some value from dropdown, and then save the record back to database. Next time when i open that record i want the option i selected last time to be shown selected.
I tried this :
<tr>
<td>Base INI File</td>
<?php
if(isset($_GET['id']))
{
$id=$_GET['id'];
btn_edit_file($id);
}
?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<td>
<select required name="base_ini_id" id="base_ini_id" class="form-control">
<option value="">Select</option>
<?php foreach($base as $value) { ?>
<option id="emp" class="specialLink" value="<?php echo $value->id;?>"><?php echo $value->base_ini_filename;
if($value->id == $value->base_ini_filename){echo "selected='selected'";} ?> </option>
<?php } ?>
</select>
</td>
<td>
<?php echo btn_edit('customer/upload_ini/edit_ini_custom/'); ?>
</td>
<script type="text/javascript">
$(document).ready(function() {
$('#base_ini_id').change(function() {
var id = $("#base_ini_id").val();
var url = "/bizrtc/customer/upload_ini/edit_ini_custom/";
$("#edit_link").attr("href",url+ id);
$("#edit_link").attr("target","_blank");
});
});
</script>
</tr>
do this :
<select required name="base_ini_id" id="base_ini_id" class="form-control">
<option value="">Select</option>
<?php foreach($base as $value)
{
?>
<option id="emp" class="specialLink" value="<?php echo $value->id;?>"
<?php if($value->id == $user->base_ini_id){echo "selected";} ?>>
<?php echo $value->base_ini_filename;?></option>
<?php } ?>
</select>
Try below code:
<select required name="base_ini_id" id="base_ini_id" class="form-control">
<option value="">Select</option>
<?php foreach($base as $value) { ?>
<option id="emp" class="specialLink" value="<?php echo $value->id;?>" <?php if($value->id == $value->base_ini_filename){echo "selected='selected'";} ?> >
<?php echo $value->base_ini_filename; ?>
</option>
<?php } ?>
</select>
Aside from the incorrect closing tag in HTML, I also would like to note that you might want to double check your condition:
if($value->id == $value->base_ini_filename)
Seems like you are comparing very different thing
where is the user saved value ?
first you should get the user saved value into one variable
you option tag should look like below
<option id="emp" class="specialLink" value="<?php echo $value->id;?>">
<?php
if($value->id == $usersavedvalue){echo "selected='selected'";} ?> ><?php echo $value->base_ini_filename; ?> </option>

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']; ?>

set_select() not working in codeigniter framework

I want the select box show the same value before the from submit after the redirection in this code $cat value come to the session i am not using validation rule for select box i want only the select box not change after the redirection
Select Leave Category...
leave_category_id ?>"
<?php echo set_select('leave_category_id', $cat, true); ?>> <?php echo $v_category->category ?> </option>
<?php endforeach;
?>
</select>
Try
<select name="leave_category_id" class="form-control" onchange="check_leave_category(this.value)" required >
<option>Select Leave Category...</option>
<?php foreach ($all_leave_category as $v_category) :
// Using short tag
//$v_category->leave_category_id == $cat ? $selected = 'selected' : $selected = '';
// without using short tag
if($v_category->leave_category_id == $cat) $selected = 'selected'; else $selected = '';
?>
<option value="<?php echo $v_category->leave_category_id ?>" <?php echo $selected?> > <?php echo $v_category->category ?> </option>
<?php endforeach;
?>
</select>
There are two things to keep in mind: First you have to use the validation class to be able to use the set_select() function. Second you have to pass the database information to that function, like this:
<select name="myselect">
<option value="one" <?php echo set_select('myselect', 'one', ($model->selection == 'one')); ?> >One</option>
<option value="two" <?php echo set_select('myselect', 'two', ($model->selection == 'two')); ?> >Two</option>
<option value="three" <?php echo set_select('myselect', 'three', ($model->selection == 'three')); ?> >Three</option>
</select>
If $model->selection is two, then the second option will be selected in the example above.
see the link

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>

osclass Cities dropdown based on Region Select

Im using osclass for a local classifieds website and im facing the following problem. I need to import 12 regions and 7000 cities/villages.
In the main.php im using a horizontal search bar with fields search text, categories (dropdown), Regions (dropdown), City (dropdown), Max price(text) and min Price (text)
For the Regions and Cities im using the code in inc.search.php
<?php $aRegions = Region :: newInstance()->listAll();?>
<?php if (count($aRegions) > 0) {?>
<select name="sRegion" id="sRegion">
<option value="">Select a Region</option>
<?php foreach ($aRegions as $region) {?>
<option value="<?php echo $region['s_name'];?>"><?php echo $region['s_name'];?> </option>
<?php } ?>
</select>
<?php } ?>
<?php $aCities = City::newInstance()->listAll(); ?>
<?php if(count($aCities) > 0 ) { ?>
<select name="sCity" id="sCity">
<option value="">Select a city</option>
<?php foreach($aCities as $City) { ?>
<option value="<?php echo $City['s_name'] ; ?>"><?php echo $City['s_name'] ; ?></option>
<?php }?>
</select>
<?php }?>
The problem is that the above code brings all cities when page loads and does not check what region is selected first. That means that the city dropdown will be filled with 7000 cities/villages when page loads.
I tried to remove lines
<?php foreach($aCities as $City) { ?>
<option value="<?php echo $City['s_name'] ; ?>"><?php echo $City['s_name'] ; ?></option>
<?php }?>
so when page loads, the city dropdown is empty but i dont know how to fill the dropdown with cities depend on region select.
The solution to how to use region/city in search form is described here http://forums.osclass.org/jobs/cities-dropdown-based-on-region-select/

Categories