PHP Populate Select box and set based on an existing value - select

I couldn't find a solution on here, thought I wasn't sure what to search for. I have a select box that is populated from my database using PDO. However I was unsure on how to set the selected value to a team that has already been set from the DB.
Update The code only shows the team that matches the variable, however I need to show all teams in the drop down.
<option <?php if($row4['team_id'] == $team_id) { ?>
selected="selected" > <?php }
echo $row4['team_name'];?>
</option>
<?php } ?>
OLD code
$stmt4 = $conn ->prepare('SELECT team_id, team_name FROM team_tbl ORDER BY team_name');
$stmt4->execute();
while($row4 = $stmt4->fetch(PDO::FETCH_ASSOC)) {
echo '<option <? if ($row4['team_id'] == $team_id){?> selected="selected"<? } ?>>'.$row4['team_name'].'</option>';
}
?>

<option <?php if($row4['team_id'] == $team_id) { ?> selected="selected" <?php } ?>></option>
Add the above code to the option field.

Related

SemanticUI Multiple select dont post multiple values

Im using Semantic UI for design and some functionalities and I failed to make multiple value posting in dropdown select - it adds blocks into the select input, but when submitting, posts only numeric string - one of the input values (instead of array). Seems to post the highest number from selection.
Can anyone tell me, how to make it working? (loading previous values works perfect);
<select class="ui dropdown" id="subjs" name="subj" multiple>
<?php
$selected = $cats->getsubj();
$subs = $cats->getsubj(true);
if (isset($subs) && count($subs) > 0)
for ($i = 0; $i < count($subs); $i++) {
$opt = (in_array($subs[$i], $selected)) ? ' selected="selected"' : '';
?>
<option class="subjs" value="<?php echo $subs[$i]['ID']; ?>" <?php echo $opt; ?>><?php echo $subs[$i]['title']; ?></option>
<?php
}
?>
</select>
JS:
<script>
$('.ui.dropdown').dropdown({
maxSelections: 2
});
</script>
PHP:
var_dump($_POST);
the script is listing of categories from database into select as options. Value for each option is it's ID (integer). One article may be liked with more than one category
All advices apreciated... Thanx
Well for all, who make stupid mistakes, simple add '[]' to the multiple select name parameter to post array.
<select name="x[]"></select>

How use form select in cake php with foreach

I want to fill a form select with informations of a table :"foreach"
i don't find a solution , someone can help me !i use cakephp 2.5.5 .
i want solution like that , but with cake php .
<select id="Select" name="section_id" class="form-control">
#foreach($sections as $section)
<option value="{{$section->id}}">{{$section->section_name}}</option>
#endforeach
</select>
Try this
First fetch data from database like this :
$result = $this->ModelName->findById('id');
$this->set('result',$result);
Then check in view file
<select id="Select" name="section_id" class="form-control">
#foreach($sections as $section)
<option <?php echo ($result['ModelName']['section_id'] == $section->id) ? 'selected="selected"' : '' ?> value="{{$section->id}}">{{$section->section_name}}</option>
#endforeach
</select>
Use Cakephp form helper to create select box so cake automatic display selected data like this
$this->From->select('ModelName.select_box_name',$sections);

Magento - Disable customer of Changing / Reset email

For security reasons I need to disable the possibility in form the user to change his email. Once set it can't be change. How can I do this?
I have found :
http://www.magentocommerce.com/boards/viewthread/8622/
Maybe usefull for someone ...
Make the following changes:
In template/customer/account/dashboard/info.phtml
<div class="inner-head">
<h5><?php echo $this->__('Contact Information') ?></h5>
Edit
</div>
<p>
<?php echo $this->htmlEscape($this->getCustomer()->getFirstname()) ?>
<?php echo $this->htmlEscape($this->getCustomer()->getLastname()) ?><br />
<?php echo $this->htmlEscape($this->getCustomer()->getEmail()) ?><br />
<?php echo $this->__('Change Password') ?>
</p>
and Replace with:
<div class="inner-head">
<h5><?php echo $this->__('Contact Information') ?></h5><br />
</div>
<p>
<?php echo $this->htmlEscape($this->getCustomer()->getFirstname()) ?>
<?php echo $this->htmlEscape($this->getCustomer()->getLastname()) ?><br />
<?php echo $this->__('Change Password') ?>
</p>
Extracted and adapted from the magento community forums.
The problem was that we had an extension that override the user model/controller/view, example: social login, and because we decided not to use it we Disable it in the advanced settings.
Because it was disable, the list of users was not showing in the back-end, but I could create a new customer, yet in the New customer view the Addresses were not showing either.
So, we activated all the extensions again and suddenly the customers list appears. So like this we found out which extension was breaking and fixed it.
In file :
app\code\core\Mage\Customer\controllers\AccountController.php
/**
* Change customer password action
*/
public function editPostAction()
{
if (!$this->_validateFormKey()) {
return $this->_redirect('*/*/edit');
}
if ($this->getRequest()->isPost()) {
$customer = Mage::getModel('customer/customer')
->setId($this->_getSession()->getCustomerId())
->setWebsiteId($this->_getSession()->getCustomer()->getWebsiteId());
$fields = Mage::getConfig()->getFieldset('customer_account');
$data = $this->_filterPostData($this->getRequest()->getPost());
//=========
// ADD THAT // customer cannot change his email // Le customer ne peut pas modifier son email
if(isset($data['email'])){
$data['email'] = $this->_getSession()->getCustomer()->getData('email');
}
//========END
foreach ($fields as $code=>$node) {
if ($node->is('update') && isset($data[$code])) {
$customer->setData($code, $data[$code]);
}
}
$errors = $customer->validate();
if (!is_array($errors)) {
$errors = array();
}

"Available Product Listing Sort By" will not remove options from drop-down

I want to remove/hide some attribute sort options in my categories.
For this I unchecked "use all attributes" and selected the attributes I want to display in the sort select.
After this I cleared cache and reindexed categories and products data.
But I still have all attributes showing in the sort by select. Can somone help me please?
I'm using a modified toolbar.phtml to hide the "position" sort option, but I think this has nothing to do with the problem:
<div class="sort-by">
<label><?php echo $this->__('Sort By') ?></label>
<select onchange="setLocation(this.value)">
<?php foreach($this->getAvailableOrders() as $_key=>$_order): ?>
<?php if ($_order != $this->__('Position')) : // Remove "Position" from the sort option list ?>
<option value="<?php echo $this->getOrderUrl($_key, 'asc') ?>"<?php if($this->isOrderCurrent($_key)): ?> selected="selected"<?php endif; ?>>
<?php echo $this->__($_order) ?>
</option>
<?php endif; ?>
<?php endforeach; ?>
</select>
<?php if($this->getCurrentDirection() == 'desc'): ?>
<img src="<?php echo $this->getSkinUrl('images/i_desc_arrow.gif') ?>" alt="<?php echo $this->__('Set Ascending Direction') ?>" class="v-middle" />
<?php else: ?>
<img src="<?php echo $this->getSkinUrl('images/i_asc_arrow.gif') ?>" alt="<?php echo $this->__('Set Descending Direction') ?>" class="v-middle" />
<?php endif; ?>
</div>
EDIT:
Here's an image from my display settings inside the category:
But inside my storeview all options are displayed
The problem for me was exactly what the error was saying, it just was hard to understand. You can't pick a default sort option that isn't enabled
Okay, fixed the problem:
I changed the settings in all store views, but it didn't changed in all sotre views. When I tried to change the setting Use Default Value in my "languagesviews". I get the error "Default Product Listing Sort by not exists on Available Product Listing Sort By".
So I "just" need to change all categorysettings of each language.
EDIT:
After editing some categories and checking/unchecking "use default values" the error is gone and I set all "use default values" to yes.

automatically add selected to dropdown menu using GET variable

I am using a captcha in a form. When the user submits an invalid answer to the captcha, the page reloads repopulating the fields with the user data. My question is, is there a better (or easier) way to check which dropdown value was selected.
This is what I have now that works:
php
<?php
$state = $sf_params->get('state');
?>
html
<span>State </span>
<select size="1" name="state">
<option value="" selected="selected"></option>
<option value="AL" <?php if ($state == "AL") { echo "selected = 'selected'"; } ?>>Alabama</option>
<option value="AK" <?php if ($state == "AK") { echo "selected = 'selected'"; } ?>>Alaska</option>
<option value="AZ" <?php if ($state == "AZ") { echo "selected = 'selected'"; } ?>>Arizona</option>
<option value="AR" <?php if ($state == "AR") { echo "selected = 'selected'"; } ?>>Arkansas</option>
Is there another solution to getting the GET variable, then parsing the options, looking for the matched value?
Well, it's not really a good practice to build a form on your own. Usually you should use widget from sfForm class, etc ..
But, if you really want to build this kind of stuff, you should bring back a good helper from sf1.0 that helps you building form tag. In your case, the function options_for_select and select_tag might be useful:
options_for_select:
Returns a formatted set of <option> tags based on optional $options array variable.
select_tag:
Returns a <select> tag, optionally comprised of <option> tags.
options_for_select took as second parameter, the selected value you want.
So just, create a file called lib/helper/FormHelper.php and put these two functions inside. Don't forget to load the helper in your view :
<?php use_helper('Form') ?>
And use options_for_select & select_tag to build your select.
First move the values of the select to an array in the Controller like
$states = array('AL' => 'Alabama', ... );
Then you can use the Helper j0k mentioned or go by feet:
By assigning the vars in the controller.
$this->states = $states;
In the Template:
<?php foreach ($states as $key => $value): ?>
<option value="<?php echo $key ?>" <?php if ($state == $value) { echo "selected = 'selected'"; } ?>><?php echo $value ?></option>
<?php endforeach; ?>
But I also recommend to use the Helper.