Dropdown Selected Value not Saving in DB - codeigniter

I am new to Codeigniter and created a project in which dropdown grab the data from table 1 and I want to save the data to table 2. However, the other value option is saving but not dropdown values. Code is given below.
Model:
function getPriviousScore() {
$this->db->select('*');
$this->db->from('ielts_previous_score');
$query = $this->db->get();
return $query->result_array();
}
Controller:
$data['ielts_previous_score'] = $this->enquiry_model->getPriviousScore();
View:
<select name="ielts_previous_score" class="form-control">
<option value=""><?php echo $this->lang->line('select'); ?></option>
<?php foreach ($ielts_previous_score as $key => $value) { ?>
<option value="<?php print_r($value['ielts_previous_score']); ?>" <?php if (set_value('ielts_previous_score') == $value['ielts_previous_score']) { ?>selected=""<?php } ?>><?php print_r($value['ielts_previous_score']); ?></option>
<?php } ?>
</select>

MODEL
function getPriviousScore() {
$this->db->select('*');
$this->db->from('ielts_previous_score');
$query = $this->db->get();
return $query->result_array();
}
CONTROLLER
$data['ielts_previous_score'] = $this->enquiry_model->getPriviousScore();
VIEW
<select name="ielts_previous_score" class="form-control">
<option value=""><?php echo $this->lang->line('select'); ?></option>
<?php foreach ($ielts_previous_score as $key => $value) { ?>
<option value="<?php echo $value['ielts_previous_score']; ?>" <?php if
(set_value('ielts_previous_score') == $value['ielts_previous_score']) { ?
>selected=""<?php } ?>><?php echo $value['ielts_previous_score']; ?></option>
<?php } ?>
</select>

Related

get value of multiselect in edit form

I want to edit a form so when i am in edit form previous edited values should shown in that field but i don't get the selected value from mysql
my edit form
<select data-placeholder="Select Rank..." class="chosen-select custom_select " multiple="" style="width: '-webkit-fill-available';" id="rank_restriction" name="rank_restriction[]">
<?php
foreach ($poll['rankname'] as $rank) { ?>
<option value="<?= $rank ?>"><?= $rank ?></option>
<?php } ?>
</select>
<?php
if (isset($edit['rank_restriction']) && !empty($edit['rank_restriction'])) {
$rank_restriction = $edit['rank_restriction']; // array format
} else {
$rank_restriction = set_value('rank_restriction'); // again in array format
}
?>
<select data-placeholder="Select Rank..." class="chosen-select custom_select "
multiple="" style="width: '-webkit-fill-available';" id="rank_restriction"
name="rank_restriction[]">
<?php
foreach ($poll['rankname'] as $rank) {
$selected = (!empty($rank_restriction) && in_array($rank, $rank_restriction)) ? 'selected' : '';
?>
<option value="<?= $rank ?>" <?php echo $selected ?>><?= $rank ?>
</option>
<?php } ?>
</select>

codeigniter dropdown select data from database

<select class="form-control">
<?php
$flag=0;
foreach ($result as $row) { ?>
if(flag==0)
{
<option value="<?php echo $row->tbl_group_id; selected="selected"?>" ><?php echo $row->tbl_group_rolename; ?></option>
flag=1;
}
else
{
<option value="<?php echo $row->tbl_group_id;?>" ><?php echo $row->tbl_group_rolename; ?></option>
}
} ?>
<option value="name">One</option>
<option value="email">Two</option>
<option value="three">Three</option>
<option value="four">Four</option>
<option value="five">Five</option>
</select>
I have one dropdownbox(on login page) in which I have to get all the data from my database and it must be shown dynamically.
My form is created in bootstrap way.
I want all the data to come from table, dynamically in dropdown and when I select it; I can register as that person for example admin/user.
I have tried nothing till now; nothing comes in my mind.
First of all, you have to fix your PHP start and end tags. This is necessary because otherwise the PHP interpreter does not know what to evaluate.
The rest is pretty straightforward. I changed your $flag to a boolean in order to check whether the current entry is the first entry and therefore should be selected. Also I removed the duplicated code and introduced an if that outputs selected="selected" for the first option.
<select class="form-control">
<?php
$first=true; // Used to select the first element
foreach ($result as $row): /* everything between here and 'endforeach' is part of the loop */ ?>
<option value="<?php echo $row->tbl_group_id ?>" <?php if ($first) echo 'selected="selected"' ?>>
<?php echo $row->tbl_group_rolename ?>
</option>
<?php if ($first) $first = false /* this makes sure that only one option element is selected */ ?>
<?php endforeach ?>
<option value="name">One</option>
<option value="email">Two</option>
<option value="three">Three</option>
<option value="four">Four</option>
<option value="five">Five</option>
</select>
The loop iterates over the rows. Note that the colon after the foreach statement uses an alternative syntax that makes it easier to read if mixed with HTML:
<?php foreach ($result as $row): ?>
<!-- HTML code here that gets printed once for each $row -->
<?php endforeach ?>
You then put some PHP code inside the HTML to make it dynamic.
<?php if ($first) echo 'selected="selected"' ?>
The code above prints selected="selected" if $first is true. After the first iteration $first becomes false:
<?php if ($first) $first = false ?>
<select class="form-control">
<?php
foreach ($result as $row) {
$sel = ($row->tbl_group_id == $someId)?'selected="selected"':'';
?>
<option value="<?php echo $row->tbl_group_id;?>" <?php echo $sel; ?> ><?php echo $row->tbl_group_rolename; ?></option>
<?php } ?>
<option value="name">One</option>
<option value="email">Two</option>
<option value="three">Three</option>
<option value="four">Four</option>
<option value="five">Five</option>
</select>

magento product count for attribute

how to show product count on attribute?
code/core/Mage/Catalog/Block/Category/View.php
public function getAllManu()
{
$product = Mage::getModel('catalog/product');
$attributes = Mage::getResourceModel('eav/entity_attribute_collection')
->setEntityTypeFilter($product->getResource()->getTypeId())
->addFieldToFilter('attribute_code', 'product_properties');
$attribute = $attributes->getFirstItem()->setEntity($product->getResource());
$product_properties = $attribute->getSource()->getAllOptions(false);
return $product_properties;
}
view.phtml
<select class="form-control" onchange="if (this.value) window.location.href=this.value">
<option>Select</option>
<?php foreach ($this->getAllManu() as $product_properties): ?>
<option value="<?php Mage::getURL() ?>catalogsearch/advanced/result/?product_properties[]=<?php echo $product_properties['value'] ?>"><?php echo $product_properties['label'] ?></option>
<?php endforeach; ?>
</select>
Gokhan,use count() function count the collection item collection
<select class="form-control" onchange="if (this.value) window.location.href=this.value">
<option>Select</option>
<?php foreach ($this->getAllManu() as $product_properties): ?>
$productcollection = Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('product_properties', $product_properties['value'])
->addAttributeToFilter('status',array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED));;
$productcollection = $productcollection->addCategoryFilter(Mage::getModel('catalog/category')->load($currcategory),true);
$productcollection->count();
<?php endforeach; ?>
</select>
$rel_products ->count();

Trying to change magento custom product list sorting like sort by special_price and most popular

I tried:
<option value="<?php echo $this->getOrderUrl('topsellings', 'desc') ?>"<?php if ($this->isOrderCurrent('topsellings') && $this->getCurrentDirection() == 'desc'): ?> selected="selected"<?php endif; ?>>
Most Popular
</option>
<option value="<?php echo $this->getOrderUrl('special_price', 'desc') ?>"<?php if ($this->isOrderCurrent('special_price') && $this->getCurrentDirection() == 'desc'): ?> selected="selected"<?php endif; ?>>
On Sale
</option>
but unable to get it. See here for the site. What am I missing?
To add a “custom” attribute, you must add the attribute into the array in the toolbar.php file. You can redeclare it(/app/code/core/Mage/Catalog/Block/Product/List/Toolbar.php ).
protected function _construct()
{
parent::_construct();
$this->_availableOrder = array(
'position' => $this->__('Best Value'),
'entity_id' => $this->__('Newest'),
'name' => $this->__('Name'),
'price' => $this->__('Price'),
'format' => $this->__('Format'),
//Custom attribute you have added.
'release_date' => $this->__('Release Date')
);
then you can call the attribute from the array this by adding into the original thread posters code.
<option value="<?php echo $this->getOrderUrl('release_date', 'desc') ?>"<?php if($this->isOrderCurrent('release_date') && $this->getCurrentDirection() == 'desc'): ?> selected="selected"<?php endif; ?>>
Release Date
</option>
If you set the attribute value of field used_for_sort_by then the template should pick this change up and you will not need to change the template code. This can be done either via a setup script or can easily be set via the attribute management part admin section.
The function getAttributesUsedForSortBy in Mage_Catalog_Model_Config will look for all attributes that have this flag set to true. The default template will update automatically becuase of the following.
<?php foreach($this->getAvailableOrders() as $_key=>$_order): ?>
<option value="<?php echo $this->getOrderUrl($_key, 'asc') ?>"<?php if($this->isOrderCurrent($_key)): ?> selected="selected"<?php endif; ?>>
<?php echo $this->__($_order) ?>
</option>
<?php endforeach; ?>

Fetch custom dropdown in Magento frontend

I have already added custom table called quantities in database. I want to show this as drop down in front end.
$model = Mage::getModel('quantities/quantities')->load($_product->getId());
How to fetch this data and show as dropdown. I am new to Magento.
Thanks in Advance.
You could try this:
$model = Mage::getModel('quantities/quantities')->load($_product->getId());
<select>
<?php foreach($model->getData() as $_data): ?>
<option><?php echo $_data->getYourAttribute() ?></option>
<?php endforeach; ?>
</select>
Granted you know what data is contained in your model. If not just var_dump($_data) or you can print_r($_data)
In the template (*.phtml) file, using Magento Block like this...
<?php
$select = $this->getLayout()->createBlock('core/html_select')
->setName('data['.$selectName.']')
->setId("sel_$selectId")
->setClass('quantity-select')
->setOptions($model->getData())
->setValue($value);
echo $select->getHtml();
?>
or Building it from scratch...
<select name="sel_name" id="sel_id">
<option><?php echo $this->__('Choose an Option...') ?></option>
<?php foreach ($model->getData() as $key => $value): ?>
<option value="<?php echo $key; ?>"><?php echo $value; ?></option>
<?php endforeach; ?>
</select>
Will this do?

Resources