I have two array. What I want to do is compare the key ['user_id'] of two arrays, if they are the same, pass the ['user_id'] and ['ref'] in hidden form. I tried to put them into two foreach, but system seems into a dead lock.
<?php foreach($_SESSION['printing_set'] as $data) { ?>
<?php foreach(getProvenaMailableUserlist() as $userlist){ ?>
<input type="hidden" name="reference[<?php echo $data['user_id'] ?>]" value="<? if($userlist['user_id'] == $data['user_id']){echo $userlist['ref'];} ?>" />
<?php } ?>
<?php } ?>
What is the right way to do that?
What you are doing is printing again and again the part of '<input type="hidden" name="...'. here is what you should do
<?php
echo '<input type="hidden" name="reference[' . $data['user_id'] . ']" value="'; //olny one time.
foreach($_SESSION['printing_set'] as $data) {
foreach(getProvenaMailableUserlist() as $userlist){
if($userlist['user_id'] == $data['user_id']){
echo $userlist['ref']; //only if condition is true
}
}
}
echo '" />'; //only one time
?>
You've got some funky formatting going on, so it's hard to tell where the error might be. Try it like this:
<?php
foreach($_SESSION['printing_set'] as $data) {
foreach(getProvenaMailableUserlist() as $userlist){
$value = "";
if($userlist['user_id'] == $data['user_id'])
$value = $userlist['ref'];
echo "<input type='hidden' name='reference$user_id' value='$value' /> \n";
}
}
?>
Related
I am making a site with checkboxes for toppings on pizza.
I have the problem that whenever I select one topping it does show and image of that topping but if I select two toppings it still only shows one image.
How can I make it so that it echoes the images of multiple checkboxes?
This is my index.php for the checkboxes and values:
<html>
<head>
</head>
<body>
<form action="process.php" method="POST">
<h4>Kies een Pizza:</h4><br>
<input type="checkbox" name="check[]" value="Pepperoni">Pepperoni<br>
<input type="checkbox" name="check[]" value="Ananas">Ananas<br>
<input type="checkbox" name="check[]" value="Ansjovis">Ansjovis<br>
<input type="checkbox" name="check[]" value="Broccoli">Broccoli<br>
<h4>Kies een bodem:</h4><br><br>
Megadik: <input type="radio" name="bodem" value="Megadik"><br>
Dun: <input type="radio" name="bodem" value="Dun"><br>
Calzone: <input type="radio" name="bodem" value="Calzone"><br>
</p>
<p>
<input type="submit" name="submit[]" value="Koop Pizza">
</p>
</form>
<br/><br/>
Home
</body>
</html>
And this is my process.php where it needs to happen:
<html>
<?php
if(isset($_POST['bodem']) && ($_POST['check'])){
$bodem = $_POST["bodem"];
echo("Je koos een pizza met:<br/> ");
}
else{
echo("Geen pizza gekozen!");
}
$selected_radio = $_POST['bodem'];
if(isset($_POST['submit'])){}
if(!empty($_POST['check'])){}
foreach($_POST['check'] as $selected){
echo $selected."</br>";
}
//Toppings
if (($selected == 'Pepperoni')) {
echo "<IMG SRC=\"pepperoni.jpg\">";
}
if (($selected == 'Ananas')) {
echo "<IMG SRC=\"ananas.jpg\">";
}
if (($selected == 'Ansjovis')) {
echo "<IMG SRC=\"ansjovis.jpg\">";
}
if (($selected == 'Broccoli')) {
echo "<IMG SRC=\"broccoli.jpg\">";
}
//Bodems
if(($selected_radio == 'Megadik')) {
echo ("<p>En de bodem Megadik</p>");
echo "<IMG SRC=\"megadik.jpg\">";
}
if(($selected_radio == 'Dun')) {
echo ("<p>En de bodem Dun</p>");
echo "<IMG SRC=\"dun.jpg\">";
}
if(($selected_radio == 'Calzone')) {
echo ("<p>En de bodem Calzone</p>");
echo "<IMG SRC=\"calzone.png\">";
}
?>
<br/><br/>
Home
</html>
Im sorry its in Dutch.
The value of $selected is updated each iteration of the loop. Then when the loop is done it will keep the last value. It will not "remember" all the values it had while looping. So you need to echo the img tag in the loop (or, if you want them separately somewhere else, in another loop).
Also, you can shorten the code by not having an if clause per topping, but instead using the fact that the file names of the images correspond to the topping names.
Then you get something like this:
foreach($_POST['check'] as $selected) {
echo $selected . "</br>";
echo "<img src=\"" . $selected . ".jpg\">";
}
Or if you want the separately:
foreach($_POST['check'] as $selected) {
echo $selected . "</br>";
}
//Some other stuff...
foreach($_POST['check'] as $selected) {
echo "<img src=\"" . $selected . ".jpg\">";
}
Please note that is is recommended to write HTML tags in lowercase.
What happens when this piece of code executes:
foreach ($_POST['check'] as $selected){
echo $selected."</br>";
}
On each iteration you echo the value. But when foreach is over $selected value is the last value of $_POST['check']. So, only your last value is checked against you if statements.
So the answer is - move your if statements into a foreach loop.
You get this error because you change the value of $selected inside the loop, but then check it outside the loop after it finishes:
You could make life easier on yourself by just applying the image inside your loop:
foreach($_POST['check'] as $selected){
echo $selected."</br>";
echo "<IMG SRC=\"{$selected}.jpg\">";
}
Here is a working sample: https://eval.in/437716
I'm building a very basic questionnaire (with response page) and it works but my form takes up too much space and looks clunky. How would i loop these question arrays so i can save space?
Here is a small bit of my code asking 2 questions:
<h3>Organisation?</h3>
<p><b>It was well organised</b><p>
<?php
$answers = array("Disagree","Agree");
for($i=0;$i<2;$i++) {
print '<input type="radio" name="org" value="'.$answers[$i].'">'.$answers[$i];
};
?>
</p>
<p><b>Adequate materials?</b><p>
<?php
$answers = array("Disagree","Agree");
for($i=0;$i<2;$i++) {
print '<input type="radio" name="lib" value="'.$answers[$i].'">'.$answers[$i];
};
?>
</p>
You should put the two rows in one php tag and use foreach instead, also you should let your php print the headers.
<?php
$answers= array("Disagree","Agree");
echo "<p><b>It was well organised</b></p><p>";
foreach ($answers as $value) {
print '<input type="radio" name="org" value="'.$value.'"> '.$value;
};
echo "</p>";
echo "<p><b>Adequate materials?</b></p><p>";
foreach ($answers as $value) {
print '<input type="radio" name="lib" value="'.$value.'"> '.$value;
};
echo "</p>";
?>
Sidenote: You have 4 opening p tags <p> and just 2 closing tags </p> in your own code. I added another two closing tags in my code where I expected them to be.
You could do:
<?php $questions = array(
'org' => 'It was well organized',
'lib' => 'Adequate materials?',
); ?>
<h3>Organisation?</h3>
<?php foreach ($questions as $f => $question): ?>
<p><b><?= $question; ?></b></p>
<?php foreach (array('Disagree', 'Agree') as $answer): ?>
<input type="radio" name="<?= $f ?>" value="<?= $answer ?>"> <?= $answer ?>
<?php endforeach; ?>
<?php endforeach; ?>
I think you are looking for something like this:
<h3>Organisation?</h3>
<?php
$answers = array("Disagree","Agree");
$first = ""; $second = "";
foreach($answers as $answer) {
$first .= '<input type="radio" name="org" value="' . $answer . '">' . $answer;
$second .= '<input type="radio" name="lib" value="' . $answer . '">' . $answer;
}
echo "<p><b>It was well organised</b></p><p>" . $first . "</p>";
echo "<p><b>Adequate materials?</b></p><p>" . $second . "</p>";
?>
Why not just make it a function?
function answers($name, $array)
{
$returnStr="";
for($i=0;$i<count($array);$i++)
{
$returnStr.='<p><input type="radio" name="'.$name.'" value="'.$array[$i].'"/> '.$array[$i].'</p>';
}
return $returnStr;
}
Then just call it
<h3>Organisation?</h3>
<p><b>It was well organised</b></p>
<?php echo answers("org",array("Agree","Disagree"));?>
So basically, I have an array of Modules and I want to have a drop-down menue that users can then select what grade they got. This works fine, however, I would like the results to be stored inside of an array, to however many values they selected. So for example:
If someone selected "40" in Mod1 and in Mod2 they selected "20" then the array would be like this:
mod1=>40
mod2=>20
...
Here is the code so far, it's probably something stupid, I just cannot get my head around it.
<?php
$modules = array('Mod1', 'Mod2', 'Mod3');
if(!isset($_POST['submitted']))
{
echo '<form method="post">';
echo 'Please enter the grades you got for each Module: <br />';
foreach($modules as $module)
{
echo $module . ': <input type="text" name="grades[]" value=""> <br />';
}
echo '<br /><input type="submit" name="submit" value="Go!">';
echo '<input type="hidden" name="submitted" value="TRUE">';
}else{
$input = $_POST['score[]'];
foreach($modules as $i => $module){
$input[$module] = $input[$i];
var_dump($input[$module] = $i);
//unset($input[$i]);
}
//var_dump($input);
}
?>
<select name="score[<?php echo $module; ?>]">
should get you going :)
the array will look exactly like you narrowed it down in the intro.
You could use a name that groups the values of the selects in POST into one array:
echo '<select name="score[]">';
You can then use:
$input = $_POST['score[]'];
foreach($modules as $i=>$module){
$input[$module] = $input[$i];
unset($input[$i]);
}
var_dump($input);
just change your name attribute to an array:
echo '<select name="score[]">';
the the $_POST variable will be in an array
I'm trying to keep a list of checkboxes checked until post is valid and no errors. Below is the code I'm using. I would be thankful for any help.
<?php foreach ($drinks_checkbox as $option => $options){ ?>
<input type='checkbox' id='drinks[]' name='drinks[]' value='<?php echo $option;?>' <?php if(!empty($_POST['drinks'])){if($_POST['drinks']==$option){ echo "checked='checked'" ; }}?> /><?php echo $options;?><br />
<?php } ?>
I can successfully display the checked checkboxes using implode however I need help for the above..
$_POST['drinks'] is an array. Also, the id value does not need to be 'drinks[]'
Try something like this:
$drinksIndex = 0;
$drinksPost = $_POST['drinks'];
foreach ($drinks_checkbox as $option => $options){ ?>
<input type='checkbox' id='drinks<?php echo $drinksIndex; ?>' name='drinks[<?php echo $drinksIndex; ?>]' value='<?php echo $option;?>'<?php
if( !empty($drinksPost[$drinksIndex]) ) echo " checked='checked'";
$drinksIndex++;
?> /><?php
echo $options;?><br />
<?php
} ?>
Amended the !empty($drinksPost[$drinksIndex]) part and changed to associative.
If this doesn't work, can you include $drinks_checkbox
I've problem with multiple form at one page. At page index I include 4 forms
include('./poll_1a.php');
include('./poll_2a.php');
include('./poll_3a.php');
include('./poll_4a.php');
The form code at every poll page is the same. I include some unique markers ($poll_code) for every scripts but the effect is when I use one form - the sending variable are received in the others. But I would like to work each form individually.
The variable $poll_code is unique for every script -> 1 for poll_1, 2 for poll_2 etc.
The same situation is with $cookie_name
$cookie_name = "poll_cookie_".$poll_code;
than, as I see, cookies have different names.
$poll_code = "1"; // or 2, 3, 4
?>
<p>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" name="<?php echo $poll_code; ?>">
<input type="hidden" name="poll_cookie_<?php echo $poll_code; ?>" value="<?php echo $poll_code; ?>">
<table>
<?php
//print possible answers
for($i=0;$i<count($answers);$i++){
?><tr><td style="\text-allign: left;\"><input type="radio" name="vote_<?php echo $poll_code; ?>" value="<?php echo $i; ?>"> <?php echo $answers[$i]; ?></td></tr><?php
}
echo "</table>";
echo "<br>";
if ($_COOKIE["$cookie_name"] == $poll_code ) {
echo "<br> nie można głosować ponownie ...";
} else {
?>
<p><input type="submit" name="submit_<?php echo $poll_code; ?>" value="głosuj !" onClick="this.disabled = 'true';"></p>
<?php
}
?>
</form>
</p>
Q: how to make this forms to work individually at one page?
//------------------- EDIT
the receiving part of the script
$votes = file($file);
$total = 0;
$totale = 0;
$poll_cookie = 0;
if (isset($_POST["vote_$poll_code"]) && isset($_POST["poll_cookie_$poll_code"])) {
$vote = $_POST["vote_$poll_code"];
$poll_cookie = $_POST["poll_cookie_$poll_code"];
}
//submit vote
if(isset($vote)){
$votes[$vote] = $votes[$vote]+1;
}
//write votes
$handle = fopen($file,"w");
foreach($votes as $v){
$total += $v;
fputs($handle,chop($v)."\n");
}
fclose($handle);
Of course, the $file have the unique declaration too (at top of the script, under the $poll_code declaration).
$file = "poll_".$poll_code.".txt";
I think the issue might be that <?php echo $poll_code; ?> is outside the loop so maybe that it's always using the same value assigned to it, maybe put it inside the loop