How to check options are selected or not? - php

if(isset($_POST["marketing"])){
foreach($_POST as $key => $value){
if(strlen($value)<1)
continue;
echo "<option value='".$value."' selected >".$value."</option>";
}
I have create dynamic options in php. All option are selected by default . I want to add all selected option into database.
How can i check options is selected or not?

Try this, we do not know what else is being posted through your form though..
if (isset($_POST["marketing"])){
foreach($_POST as $key => $value)
{
$iselected = ($value == $_POST['marketing']) ? 'selected' : '';
echo "<option value='" . $value . "' $iselected>" . $value . "</option>";
}
}

Related

Loading issue while fetching values of dropdown attribute in magento

There is one attribute named "Model" whose type is drop down. Attribute code is wheel_model.
This drop down have 2585 options.
I have used this drop down on many pages to allow user to select wheel model and filter product list.
Below is the code to fetch drop down value from database:
$arrval = array();
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'wheel_model');
foreach ($attribute->getSource()->getAllOptions(false) as $option) {
$arrval[$option['value']] = $option['label'];
}
I am getting all drop down values in array and I am using it in phtml as follow:
<select name='wheel_model'>
<option>Select Model</option>
<?php
foreach ($arrval as $value => $label) {
echo "<option value='" . $value . "'>" . $label . "<option>";
}
?>
</select>
Its taking too much time to load drop down values. Is there any way to store this drop down in cache and retrieve from cache whenever required?
Use the code below to store data in the cache and that’s all.
$cacheId = 'my_cache_id';
if (false !== ($data = Mage::app()->getCache()->load($cacheId))) {
$data = unserialize($data);
} else {
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'wheel_model');
foreach ($attribute->getSource()->getAllOptions(false) as $option) {
$data[$option['value']] = $option['label'];
}
Mage::app()->getCache()->save(serialize($data), $cacheId);
}

PHP - Update HTML Select list to show current value

I'm working on a simple HTML form that will include a drop down list for the selected Salesperson. The list is coming from a database and is in an array that is stored in a variable $ids and looks like this:
Array ( [John Jones] => JJ [Sally Smith] => SS [Victor Howards] => VH [Barnie Kemp] => BK )
Here's how the select list is currently constructed:
<?php
$output = "";
$selected = false;
foreach ($ids as $id => $value) {
$output .= "<option value=\"$value\"";
if ($id == $record->getField('initials')) {
$selected = true;
$output .= " selected";
$selectedID = $record->getField("id");
}
$output .= ">$id</option>";
}
if (!$selected) {
$chosen = $record->getField("initials");
$output = "<option value=\"$chosen\" selected>$chosen</option>$output";
}
echo $output;
?>
I'm trying to update the select list to show the current value for this field as the selected option. I have the name/value stored in these variables:
$initials
$name
So if $initials = 'SS' I would like Sally Smith to be the selected value in the list.
Try to use as simple as follow
<?php
$output = "";
foreach ($ids as $id => $value) {
$selected = "";
if ($id == $record->getField('initials')) {
$selected = "selected";
}
$output .= "<option value='$value' $selected>$id</option>";
}
echo $output;
?>
Do not make your code complex.. do it like this one:
$value=$record->getField('initials');
foreach ($ids as $id => $valueData) {
if($id==$value)
echo "<option value='".$id."' selected='selected' >$id</option>";
else
echo "<option value='".$id."'>$id</option>";
}

PHP shows array keys 1 number higher

I want to get all keys of an array showing on my page.
Right now I have this:
$subjectcodes[1] = "Mathematics";
$subjectcodes[2] = "Physics";
$subjectcodes[3] = "Charlie";
$subjectcodes[4] = "Chemistry";
$subjectcodes[5] = "Biology";
$subjectcodes[6] = "English";
$subjectcodes[7] = "Dutch";
$subjectcodes[8] = "German";
$subjectcodes[9] = "Sociology";
$subjectcodes[10] = "Physical Education";
$subjectcodes[11] = "Art";
$subjectcodes[12] = "General Science";
$subjectcodes[13] = "Philosophy";
$subjectcodes[14] = "Management and Organization";
$subjectcodes[15] = "Research and Design";
foreach ($subjectcodes as &$value) {
$key = key($subjectcodes);
echo "<option value=" . $key . ">" . $value . "</option>";
}
When I go to my page with that code I get:
<option value=2>Mathematics</option>
<option value=3>Physics</option>
<option value=4>Charlie</option>
<option value=5>Chemistry</option>
<option value=6>Biology</option>
<option value=7>English</option>
<option value=8>Dutch</option>
<option value=9>German</option>
<option value=10>Sociology</option>
<option value=11>Physical Education</option>
<option value=12>Art</option>
<option value=13>General Science</option>
<option value=14>Philosophy</option>
<option value=15>Management and Organization</option>
<option value=>Research and Design</option></select>
As you can see all keys are 1 higher number then supposed. And the last option doesn't even have a key...
Does anyone why this is, and how I can solve this?
Thanks!
Try:
foreach ($subjectcodes as $key=>$value) {
//$key = key($subjectcodes);
echo "<option value=" . $key . ">" . $value . "</option>";
}
you can use key and value in foreach(), so change:
foreach ($subjectcodes as &$value) {
$key = key($subjectcodes);
echo "<option value=" . $key . ">" . $value . "</option>";
}
to
foreach ($subjectcodes as $key => $value) {
echo "<option value=" . $key . ">" . $value . "</option>";
}
Foreach iterates over the array, this means that when you use the key method the "current" array element is the next on your foreach cycle. Your code is not well constructed for what you are trying to acomplish.
Use this instead:
foreach ($subjectcodes as $key => $value) {
echo "<option value=" . $key . ">" . $value . "</option>";
}
Note: Unless you want to change the value of the array element inside the foreach cycle there is no need to use &$value.
A note to complete the info about using reference as foreach loop variables:
From http://php.net/manual/en/control-structures.foreach.php:
Reference of a $value and the last array element remain even after
the foreach loop. It is recommended to destroy it by unset().
So if you decided to use a reference as a foreach variable, you have unset it after the foreach.
foreach ($subjectcodes as &$value)
{
}
unset($value)

PHP and HTML: acquire all GET-values

I am in need of a PHP-program which will take all values from a HTML-link (with GET), regardless of how much one changes things in the address-bar, and then prints it out on the page. The code I have now is this:
HTML:
Link
PHP:
<?php
Echo "Value1: "; Echo $_GET["a"]; Echo "\n";
Echo "Value2: "; Echo $_GET["b"]; Echo "\n";
Echo "Value3: "; Echo $_GET ["c"];
?>
It works as intended for just these values, but it cannot cope with, for example, another variable being added in the address bar. I need some kind of PHP-function which can look for all kinds of variables through the GET-function.
Any help is appreciated!
Use a loop (modify as needed):
foreach ($_GET as $num => $value)
{
echo 'Value ' . $num . ': ' . htmlentities($value). "<br>" . PHP_EOL;
}
References:
Control structures
htmlentities()
Something like this:
<?php
foreach ($_GET as $key => $value) {
echo htmlspecialchars($key) . " - " . htmlspecialchars($value) . "<br>";
}
?>
<?php
print_r($_GET);
?>
Use a foreach loop
foreach ($_GET as $key => $val){
echo htmlentities($key).": ".htmlentities($val)."<br />\n";
}
or simply -
echo "<pre>".print_r($_GET,1)."</pre>";
To get all values and their corresponding names:
foreach($_GET as $key => $value)
{
echo $key . ' - ' . $value;
}
$my_get = array(); // store $_GET vars
if ('GET' == $_SERVER['REQUEST_METHOD']) // check that request method is "get"
foreach ($_GET as $key => $val)
{
$my_get[$key] = $val; // store
}
output $my_get:
array (
'a' => 'value1',
'b' => 'value2',
'c' => 'value3',
)
Since $_GET is nothing but an array, you can use for each loop to get the elements of it.
you get all $_GET values using print_r($GET) ...
otherwise you could to something like this
foreach(array_keys($_GET) as $key) {
echo htmlspecialchars($_GET[$key]);
}
Later Edit : took into account the security issue exposed in the comment
using this method is good because you know what kind of variable you have then you could do something cool like :
if ($key == 'a') do this
if ($key == 'b' && $_GET[$key] == 'bar') do that

Is this conversion correct?

I've tried converting this ASP code to PHP, and would like to know if it is correct:
ASP:
Set emp = Server.CreateObject("Scripting.Dictionary")
EM_GENERAL=0
EM_AUDIO=1
EM_VIDEO=2
emp.Add EM_GENERAL, "General"
emp.Add EM_AUDIO, "Audio"
emp.Add EM_VIDEO, "Video"
For each em in Emp
Response.Write "<option value=" + CStr(em)
If em = CInt(IT_FIELD) Then
Response.Write " selected"
End If
Response.Write ">"
Response.Write Emp.Item(em)
Next
PHP:
$EM_GENERAL=0;
$EM_AUDIO=1;
$EM_VIDEO=2;
$emp = array();
$emp[$EM_GENERAL] = "General";
$emp[$EM_AUDIO] = "Audio";
$emp[$EM_VIDEO] = "Video";
foreach ($emp as $em) {
echo "<option value=" + ($em);
if ($em == intval($IT_FIELD)) {
echo " selected";
}
echo ">";
echo $em;
}
In VB, the For Each loop iterates over the Dictionary keys, not the values, so in your ASP code, em is the numeric key of your emp entries. In PHP, foreach iterates over the values, so $em is the value, not the key. Your if ($em == intval($IT_FIELD)) check will not work as expected. Fortunately the php foreach loop also supports the syntax foreach ($array as $key => $value), where $value would be equivalent to Dictionary.Item(key).
Try this instead:
foreach ($emp as $em => $value) {
echo "<option value=" + ($em);
if ($em == intval($IT_FIELD)) {
echo " selected";
}
echo ">";
echo $value;
}

Categories