I got 3 array's where from i make 2 select box and a checkbox. What i want to do is that when i change CMS the checkbox value needs to change like in the array.
if i choose Joomla in the select box then i want the checkbox that is made with the $aOnderdelen, then in the array $aOnderdelen i have an array with Contact-form Foto-gallery and Carousel and this 3 have an array with the name of each CMS with different values and this are the values that the checkbox needs to get when you choose one of those and the CMS.
Example: i choose Joomla and i choose a Contact-formulier than contact-formulier checbox gets 3 as value.
$aCMS = array('SilverbeeCMS','Joomla','WP','Drupal','Scott');
$prijsPerUur=1;
$basisPrijs=
array(
array('titel' => 'Kopie', 'uur' => '8'),
array('titel' => 'Maatwerk', 'uur' => '10'),
array('titel' => 'Aangekocht', 'uur' => '12'),
array('titel' => 'Custom', 'uur' => '14')
);
$aOnderdelen = array
(
'Contact-formulier' => array
(
'SilverbeeCMS'=>3,
'WP'=>2,
'Joomla'=>3,
'Drupal'=>4,
'Scott'=> 5
),
'Foto-gallery' => array
(
'SilverbeeCMS'=>1,
'WP' => 3,
'Joomla'=> 4,
'Drupal'=> 5,
'Scott'=> 6
),
'Carousel' => array
(
'SilverbeeCMS'=>1,
'WP' => 4,
'Joomla'=> 5,
'Drupal'=> 6,
'Scott'=> 7
)
);
?>
This is the HTML form where the Select and checkbox are
<form action="" method="post">
<select id="cms" class="form-control" name="cms">
<?php foreach($aCMS as $key => $value): ?>
<option value="<?php echo strtolower($aCMS[$key]); ?>">
<?php echo $aCMS[$key]; ?>
</option>
<?php endforeach; ?>
</select>
<label><?php echo $template.$verplicht; ?></label>
<select id="templates" class="form-control" name="templates">
<?php foreach($basisPrijs as $key => $value): ?>
<option value="<?php echo $basisPrijs[$key]["uur"]; ?>">
<?php echo $basisPrijs[$key]["titel"]; ?>
</option>
<?php endforeach; ?>
</select>
<?php echo $oTitel; ?>
<div class="checkbox col-xs-12">
<div class="row">
<?php foreach($aCMS as $cmsKey => $cmsValue) ?>
<?php foreach($aOnderdelen as $key => $value):
foreach($value as $key1 => $value1)
{};
$i++;
echo "<div class='checkbox'>
<label><input class='check".$i."' type='checkbox' value='".strtolower($key)."' name='".$key."'>".$key."</label></div>"
;endforeach;?>
</div>
</div>
</form>
You have to use jquery along with AJAX for changing things dynamically
Simple example as
$(document).ready(function() {
$('#selector').change(function() {
//do here things required about changing
//You can also change DOM elements according to needs
//and have Ajax requests
})
});
I found how to do it
switch($("option:selected").val())
{
case "silverbeecms":
$(".check1").val(<?php echo $aOnderdelen["Contact-formulier"]["SilverbeeCMS"] ?>)
$(".check2").val(<?php echo $aOnderdelen["Foto-gallery"]["SilverbeeCMS"] ?>)
$(".check3").val(<?php echo $aOnderdelen["Carousel"]["SilverbeeCMS"] ?>)
break;
case "joomla":
$(".check1").val(<?php echo $aOnderdelen["Contact-formulier"]["Joomla"] ?>)
$(".check2").val(<?php echo $aOnderdelen["Foto-gallery"]["Joomla"] ?>)
$(".check3").val(<?php echo $aOnderdelen["Carousel"]["Joomla"] ?>)
break;
case "wp":
$(".check1").val(<?php echo $aOnderdelen["Contact-formulier"]["WP"] ?>)
$(".check2").val(<?php echo $aOnderdelen["Foto-gallery"]["WP"] ?>)
$(".check3").val(<?php echo $aOnderdelen["Carousel"]["WP"] ?>)
break;
case "drupal":
$(".check1").val(<?php echo $aOnderdelen["Contact-formulier"]["Drupal"] ?>)
$(".check2").val(<?php echo $aOnderdelen["Foto-gallery"]["Drupal"] ?>)
$(".check3").val(<?php echo $aOnderdelen["Carousel"]["Drupal"] ?>)
break;
case "scott":
$(".check1").val(<?php echo $aOnderdelen["Contact-formulier"]["Scott"] ?>)
$(".check2").val(<?php echo $aOnderdelen["Foto-gallery"]["Scott"] ?>)
$(".check3").val(<?php echo $aOnderdelen["Carousel"]["Scott"] ?>)
break;
}
New Answer more Flexibel
First Loop through the PHP array.
<?PHP
foreach($aCMS as $cmsKey => $cmsValue)
?>
<?php
foreach($aOnderdelen as $key => $value)
:
foreach($value as $key1 => $value1){};
?>
Than use data attr to get the prices.
<input class="<?php echo strtolower($key);?>" type="checkbox" value="<?php echo strtolower($key)?>" name="<?php echo $key;?>" data-silverbeecms="<?php echo $value['SilverbeeCMS']; ?>" data-wp="<?php echo $value['WP'];?>" data-joomla="<?php echo $value['Joomla'];?>" data-drupal="<?php echo $value['Drupal'];?>" data-scott="<?php echo $value['Scott'];?>">
After you get the Price in the data you can call this data from JQUERY and do the Match.
urenOnderdelen = 0;
$("#sCms, #templates, .contact-formulier, .foto-gallery, .carousel").change(function()
{
urenOnderdelen = 0;
urenTemplate = $("#templates").val();
$(".contact-formulier, .foto-gallery, .carousel").each(function()
{
if(this.checked)
{
urenOnderdelen+=parseInt($(this).data($("option:selected").val()));
}
});
$("#output, #output1, #gVoor").hide().fadeIn(300);
urenTemplate = $("#templates").val();
urenTemplate = parseInt(urenTemplate);
urenOnderdelen = parseInt(urenOnderdelen);
totaal = urenOnderdelen + urenTemplate;
$("#output").text(euroTeken + totaal*prijs);
});
Related
I have small problem with my PHP code. I must insert to database multiple values but I dont have any idea how I can do this.
My View:
<?php foreach($myKidsGroupID as $row): ?>
<label><input type="checkbox" name="user_my_group_msg" value="<?php echo $row->id; ?>" class="my_group_msg pull-right"><?php echo $row->firstname; ?> <?php echo $row->lastname; ?></label><br>
<?php endforeach; ?>
And my Model looks like this:
...
elseif($checked_my_group == 1) {
foreach ( ?? ) {
$new_mail = array(
'to_user' => $this->input->post('user_my_group_msg'),
);
$this->db->insert('mailbox', $new_mail);
}
}
...rest code....
Inside my View I display all users as checkbox but If I select two persons I must INSERT two query to database. Anyone can help me?
simple thing is
use serialize function don't use foreach
serialize $this->input->post('user_my_group_msg')
try it once it will help you your View code will look as like
<?php foreach($myKidsGroupID as $row): ?>
<label><input type="checkbox" name="user_my_group_msg" value="<?php echo $row->id; ?>" class="my_group_msg pull-right"><?php echo $row->firstname; ?> <?php echo $row->lastname; ?></label><br>
<?php endforeach; ?>
Model will be corrected like this
<?php
elseif(sizeof($this->input->post('user_my_group_msg'))>=1) {
foreach ( $this->input->post('user_my_group_msg') as $value ) {
$new_mail = array(
'to_user' => $value,
);
$this->db->insert('mailbox', $new_mail);
}
}
.....rest code
?>
<?php
$options = array(
'infant' => '1',
'mater' => '2',
'prees' => '3',
'kinder' => '4', );
echo form_open('evals/proc_group');
echo form_checkbox('edu_level[]','1', in_array('1', $educational_levels)); echo form_label ('Infantes', 'infant', array ('class' => 'checkbox_label'));
echo form_checkbox('edu_level[]','2', in_array('2', $educational_levels)); echo form_label ('Maternales', 'mater', array ('class' => 'checkbox_label'));
echo form_checkbox('edu_level[]','3', in_array('3', $educational_levels)); echo form_label ('Preescolares', 'prees', array ('class' => 'checkbox_label'));
echo form_checkbox('edu_level[]','4', in_array('4', $educational_levels)); echo form_label ('Kindergarten', 'kinder', array ('class' => 'checkbox_label')); ?>
<input type="hidden" name="eval_id" value="<?php echo $evaluation->id ?>" />
<div class="clear"></div>
<?php echo form_submit('mysubmit','Guardar Grupo'); ?>
<div class="clear"></div>
<?php echo form_close(); ?>
<div class="clear"></div>
I need the checkboxes stay checked when i press Guardar Grupo. What can i do?
Hope this will work, I haven't checked the code myself.
var checkboxes = $('.checkbox_label');
for ( i = 0 ; i < checkboxes.length; i++ ) {
checkboxes[i].prop('checked', true)
}
You have to add this to the form's submit button click event.
Do a print_r($educational_levels) to see if the array contains the values or not. Other than that I can't see anything unusual. Also make sure that this $educational_levels is a single dimentional array.
This works perfectly for me:
public function index()
{
//$this->load->view('welcome_message');
$this->load->helper('form');
$educational_levels = array('1', '3');
echo form_checkbox('edu_level[]','1', in_array('1', $educational_levels));
}
You need to check if your array $educational_levels contains values and is not empty!.
Line 42 is the error. I'm not sure as to why it keeps saying it's not an array is one section but fails to find the array on line 42. I have tried to change the line to ($_POST['CINS'] as $cNum => $v) and ($CINS as $cNum => $v). Any insight or help would be appreciated.
<?php
$title = "fTest.php";
$action=$_SERVER['PHP_SELF'];
include("html-head.inc");
echo <<<HEREDOC
<header>
<h1>$title</h1>
</header>
HEREDOC;
if (!isset($_POST['submit']))
{
echo "<form method=\"post\" action=\"$action\">";
$CINS = array('101' => "CINS101",
'108' => "CINS108",
'121' => "CINS121",
'251' => "CINS251",
'254' => "CINS254");
echo "<p>Please pick your CINS classes:</p>";
echo "<ul>\n";
foreach ($CINS as $key => $value)
{
echo "<li>";
echo "<input type=\"checkbox\" name=\"CINSc\" value=\"$value\"/>CINS$key" ;
echo "</li>\n";
}
echo "</ul>\n";
echo "<input type=\"reset\" name=\"reset\" value\"Reset\" />";
echo "<input type=\"submit\" name=\"submit\" value\"Submit\" />";
echo "</p>";
echo is_array($CINS) ? 'Array' : 'Not an array';
echo "\n";
echo "</form>";
} // ends IF PORTION for ISSET
else
{
if (count($_POST['CINS'] > 0 ))
{
echo "<h2> Your picks are: </h2>\n";
echo "<ul>\n";
echo is_array($CINS) ? 'Array' : 'Not an array';
foreach ($_POST['CINS'] as $cNum => $v) //This is the error.
{
echo "\t<li>$v</li>\n";
} // end of FOREACH cins
echo "</ul>\n";
} // end of IF count CINS
} // end of ELSE portion for ISSET
?>
Your checkbox name should like below to consider it as an array
<input type="checkbox" name="CINS[]" value = "1" />
The <input> name is an array, and I've found a spare </p> that shouldn't be there.
Your coding style is going to cause A LOT of HEADACHES. I've cleaned it up a bit, I think this style will be much easier to handle.
<?php
$title = "fTest.php";
$action=$_SERVER['PHP_SELF'];
include("html-head.inc");
?>
<header>
<h1><?=$title?></h1>
</header>
<?php if (!isset($_POST['submit'])): ?>
<?php $CINS = array('101' => "CINS101",
'108' => "CINS108",
'121' => "CINS121",
'251' => "CINS251",
'254' => "CINS254"); ?>
<form method="post" action="<?=$action?>">
<p>Please pick your CINS classes:</p>
<ul>
<?php foreach ($CINS as $key => $value): ?>
<li>
<input type="checkbox" name="CINS[]" value="<?=$value?>" />CINS<?=$key?>
</li>
<?php endforeach; ?>
</ul>
<input type="reset" name="reset" value"Reset" />
<input type="submit" name="submit" value"Submit" />
</p> <!-- THIS TAG IS EXTRA WHERE DID IT COME FROM -->
<?= is_array($CINS) ? 'Array' : 'Not an array' ?>
</form>
<?php else if (count($_POST['CINS'] > 0 )): ?>
<h2>Your picks are: </h2>
<ul>
<?= is_array($CINS) ? 'Array' : 'Not an array' ?>
<?php foreach ($_POST['CINS'] as $cNum => $v): ?>
<li><?=$v?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 8 years ago.
<?php if ($option['type'] == 'select') { ?>
<div id="option-<?php echo $option['product_option_id']; ?>" class="option">
<?php if ($option['required']) { ?>
<span class="required">*</span>
<?php } ?>
<b><?php echo $option['name']; ?>:</b><br />
<select name="option[<?php echo $option['product_option_id']; ?>]">
<!-- <option value=""><?php echo $text_select; ?></option> -->
<?php foreach ($option['option_value'] as $option_value) { ?>
<option value="<?php echo $option_value['product_option_value_id']; ?>"><?php echo $option_value['name']; ?>
<?php if ($option_value['price']) { ?>
(<?php echo $option_value['price_prefix']; ?><?php echo $option_value['price']; ?>)
<?php } ?>
</option>
<?php } ?>
</select>
<?php if ($option_value['image'] != 'image/' && $option_value['image'] != 'image/no_image.jpg' ) { ?><?php
list($simgwidth) = getimagesize($option_value['image']);
$selectimgpos = $simgwidth + 35;
?>
<center>
<span id="img<?php echo $option['product_option_id']; ?>" style="position: absolute; display: none; left: -<?php echo $selectimgpos; ?>px; background: #eee; padding: 10px; border: 1px dotted #666; z-index: 90;">
<img src="<?php echo $option_value['image']; ?>">
<br /><strong><?php echo $option['name']; ?></strong>
</span></center><?php } ?>
</div>
<br />
<?php } ?>
Hi, I am new to programming and I need help with the above section of code. Normally this code just displays a Select dropdown box with options.
I've modified it by adding an extra image at the bottom inside a span. I've also calculated the width and repositioned the image based on its -width.
What I'd like to do next is have a javascript where if the user hovers or onFocus on the Select box will show the image inside the span. And if they change the select box, the image will change based on what they selected. I'm thinking onBlur to hide the pic after.
I've found some javascripts to do this but I need to pass dynamic variables from the php.
I found this javascript, but the code doesn't let me pass variables.
<script type="text/javascript">
function swapImage(){
var image = document.getElementById("imageToSwap");
var dropd = document.getElementById("dd");
image.src ="images/"+dropd.value+".jpg";
};
</script>
Due to constantly changing IDs generated by php I can't be using static variables.
Any help would be appreciated. Thanks all.
PS: $option_value['image'] is a php array. How can I display the first or last element in this array? Normally it's something like array_[n] but this one already has brackets? $option_value['image[0]'] ??
To pass php variable to javascript, you just need to echo them out in the right place:
// in your php file
<?php
$image_to_swap = "my_id_of_image_to_swap";
$element = "my_id_of_element";
?>
<script type="text/javascript">
function swapImage(){
var image = document.getElementById("<?php echo $image_to_swap; ?>");
var dropd = document.getElementById("<?php echo $element; ?>");
image.src ="images/"+dropd.value+".jpg";
};
</script>
As for your PS:
PHP arrays can be multidimensional, so for the following array:
$image = array(
0 => array(
0 => 'apple',
1 => 'banana'
),
1 => array(
0 => 'broccoli',
1 => 'cauliflower'
),
2 => array(
0 => 'peanut',
1 => 'walnut'
)
);
You can access the items as:
echo $image[0][0]; // outputs apple
echo $image[2][1]; // outputs walnut
And you can use strings as keys, so you could also name your keys and access them with strings:
$image = array(
'fruits' => array(
0 => 'apple',
1 => 'banana'
),
'vegetables' => array(
0 => 'broccoli',
1 => 'cauliflower'
),
'nuts' => array(
0 => 'peanut',
1 => 'walnut'
)
);
You can access the items as:
echo $image['fruits'][0]; // outputs apple
echo $image['nuts'][1]; // outputs walnut
How can I change the foreach loop below so that I can assign the $myradiooption array's key as the value for each input instead of the array's option value as I'm now doing (I still want to echo the array's option value as the label)?
<?php
$myradiooptions = array(
"grid1" => "Grid View (default)",
"list1" => "List View (1 column)",
"list2" => "List View (2 column)"
);
array(
"name" => "Category Layout",
"desc" => "description goes here",
"id" => "my_category_layout",
"type" => "radio",
"options" => $myradiooptions )
);
//switch, case "radio":
?>
<li class="section">
<label
class="left"
for="<?php echo $value['id']; ?>">
<?php echo $value['name']; ?>
</label>
<?php
$count=1;
foreach ($value['options'] as $option) {
?>
<input
type="radio"
name="<?php echo $value['id']; ?>"
id="<?php echo $count; ?>"
value="<?php echo $option; ?>"
<?php checked($option, get_settings($value['id'])); ?>
/>
<label style="color:#666; margin:0 20px 0 5px;" for="<?php echo $count; ?>">
<?php echo $option; ?>
</label>
<?php $count++;} ?>
<label class="description" style="margin-top:-5px;">
<?php echo $value['desc']; ?>
</label>
</li>
<?php break;
If you want to access the key of an array in a foreach loop, you use the following syntax:
foreach ($array as $key => $value) { ... }
References: foreach in the PHP documentation
I think what you are looking for is this:
foreach ($value['options'] as $key=>$option)
Now you can access the key as $key, and the option as $option
If you want to extract key/value pairs from an associative array, simply use...
foreach ($yourArray as $key => $value) {
...
}
...as per the PHP foreach manual page.
the whole thing can be changed to something like this for better readability...
<?php
$myradiooptions = array(
"grid1" => "Grid View (default)",
"list1" => "List View (1 column)",
"list2" => "List View (2 column)"
);
$value = array(
"name" => "Category Layout",
"desc" => "description goes here",
"id" => "my_category_layout",
"type" => "radio",
"options" => $myradiooptions
);
foreach($value as $key => $val)
{
$formHTML = "<label class='left' for='{$value['id']}'>".$value['name']."</label>";
if(is_array($val))
{
$count = 1;
foreach($val as $k => $v)
{
$formHTML .= "<input type='radio' name='{$v['id']}' id='$count' value='$v' /><label style='color:#666; margin:0 20px 0 5px;' for='$count'>$v</label>";
$count ++;
}
}
$formHTML .= "<label class='description' style='margin-top:-5px;'>".$value['desc']."</label>";
}
//switch, case "radio":
?>
<li class="section">
<?php print $formHTML; ?>
</li>
This should explain it pretty well:
http://www.tech-recipes.com/rx/295/php-syntax-iterate-over-an-associative-array/