Printing a variable amount of checkboxradio depending on an array - php

I'll introduce the problem with an image :
These are checkboxradio from jquery, and what I'm trying to do is
print these checkbox but not always the same way, because the content in the checkbox will differ every time (because depending on what a user previously entered in an array)
For exemple, the suggestion 1 is of length 3 on the image, but could have a different length another time, up to a length of 10.
As well as the total number of row, which could be 1 or 2 or ... Up to 10.
I tried to code something, but it doesn't work, mostly because it's kind of disgusting and probably is a wrong way of doing it.
<?php
/*
$words is a 2 dimensional array, which contains :
row -> specific type (ex : Metal's type / Water's name...)
column -> each words of a type (ex : Iron / Copper / Barium...)
*/
$i = 1; //Iterator through lignes
$j = 0; //Iterator through columns
$length = 0; //Length of the array
/*
Print a certain number of radio button type
*/
if(!empty($words['0']) && $i==1) {
$length = count($words['0']); //Needed to know how many radio button we have to show
debug($length);
echo('Mot : 1');
//Now things starts to be wrong
?>
<html>
<fieldset>
<legend>Choose one : </legend>
<!--
Print radio button depending on the number of words,
could be all of them, or only 1.
-->
</html>
<?php
if($j < $length) {
?>
<html>
<label for="radio-1">$words['0']['0']</label>
<input type="radio" name="radio-1" id="radio-1">
</html>
<?php
$j++;
}
if($j < $length) {
?>
<html>
<label for="radio-2">$words['0']['1']</label>
<input type="radio" name="radio-1" id="radio-2">
</html>
<?php
$j++;
}
if($j < $length) {
?>
<html>
<label for="radio-3">$words['0']['2']</label>
<input type="radio" name="radio-1" id="radio-3">
</html>
<?php
$j++;
}
if($j < $length) {
?>
<html>
<label for="radio-4">$words['0']['3']</label>
<input type="radio" name="radio-1" id="radio-4">
</html>
<?php
$j++;
}
?>
<!--... Up to 10 times-->
</fieldset>
</html>
<?php
}
$i++; //Moving to the next row
//Same as above, but on an other row
if(!empty($words['1']) && $i==2) {
$length = count($words['0']);
echo('Mot : 2');
//etc...
I know it's monstrous to veteran but I couldn't find something else.
So, is there a way of printing a various amount of checkboxes properly ?
ANSWER
Thanks to the answer of Pol, I managed to do something that's working, it's still a bit disgusting, but it's wayyy better than my original way :
<?php
$i=1;
foreach ($newkeywords as $words) {?>
<h2>Groupe <?php $i?></h2>
<fieldset>
<legend>word '<?php echo($words[0])?>' :</legend>
<?php foreach ($words as $word) {?>
<label for=<?php 'checkbox-'.$i ?>><?php echo($word)?></label>
<input type="checkbox" name=<?php 'checkbox'.$i ?> id=<?php
'checkbox'.$i ?></br>
<?php $i=$i+1;?>
<?php
}
$i=1;
?></br><?php
}
?>
Still eager for other answers, it probably could be done better

Im not sure I followed but if you have a variable named 'words' and depending on how many words the length would be different. You can solve this using a foreach.
foreach($words as $word) :
// here you just print the row
// because the foreach will go through all words one by one.
// if you dont know how to access $word you can use var_dump($word)
// and then figure it out
echo '<label for="radio-1">'.$word['0'].'</label>';
echo '<input type="radio" name="radio-1" id="radio-1">':
endforeach;

Related

Finding the highest number using a while loop

I've received some c++ code regarding finding the highest number until the input been set as 0 using the form and while statement, but my echo doesn't appear, even after several times trying to key in the highest number
here some reference of C++ code that need to be transform into php format:
http://prntscr.com/plw8mw
<html>
<body>
<form method="POST">
Enter First Number:
<input type="text" name="num1"/><br>
<input type="submit"><br>
<form/>
<?php
if(isset($_POST['submit'])) {
$bignum = 0;
$num = $_POST['num1'];
while($num != 0) {
if ($num > $bignum)
$bignum = $num;
}
print "$bignum";
}
?>
<body/>
<html/>
Use echo insteed of print
echo($bignum);

I have a problem to submit inputs in one shot (php html mixed )

I made a form to get a number (quantity ) from the users ( number of samples to be tested ) , then I made a form for the users to enter comments regarding each sample ,
this works , but I don't succeed to send these comments in another php file with only one submit at the end either to echo them and or to send them in mysql .
To do this I opened a new form just after the loop to submit at the end of the total entries and not after each one . This is the problem I think
Thank you very much for solution
$i=1;
while ($i<= $_POST["quantity"]):
$quant = $_POST["quantity"]; ?>
<form>
echantillon n° <?php echo $i ?> <input type="text" name="trekking" >
</form>
<?php $i=$i+1;
endwhile;
?>
<html>
<body>
<form action="essaibg4.php" method="POST">
<input type="submit" >
</form>
</body>
</html>
I think you just need to keep the tags out of the loop. You only need one pair of those, and several tags. And there are few things to move around. I haven't tested it but it should work. You had all the peaces of the puzzle but they were not appearing in the same order.
You code becomes ...
<html>
<body>
<form action="essaibg4.php" method="POST">
<?php
$i=1;
$quant = (int) $_POST["quantity"];
while ($i <= $quant): ?>
echantillon n° <?php echo $i; ?> <input type="text" name="trekking[]" >
<?php $i=$i+1;
endwhile; ?>
<input type="submit" >
</form>
</body>
</html>
thank you for your answer , when I click on "submnit" I get "9 array array array array array array array array array "
essaibg4.php is :
<?php
echo $_POST["quantité"];
$x = 1;
while($x <= $_POST["quantité"]) {
echo $_POST["trekking"] ;
$x++;
}
?>
thank you for new help ( if you could also give me hints for storing trekking values in mysql it would be great , I am a beginner in programming and dealing with html php python mysql is a bit confusing )

importing from mysql column to checkbox php

i am trying to import from mysql column as an exploded values (category id) from table called "products", and print it in a checkbox input, and check if the value (category id) equal to the (category id) in the table called "cats", to set checkbox to "checked", this is my code:
<?php
for($i2=0;$i2<$rowsZ22;$i2++){
//exploding
$catszz2 = explode(",", $bussSubcat);
//foreach start
foreach($catszz2 as $catzz2) {
if($catzz2==$row22[$i2]["id"]){
$ischecked2="checked";
}else{
$ischecked2="";
}
}
//foreach end
?>
<li><input type="checkbox" onclick="getElementById('cats<?=$row22[$i2]["parent"]?>').checked= true;" <?=$ischecked2?> value="<?=$row22[$i2]["id"]?>" name="subcats[]" id="" /> <?=$row22[$i2]["title"]?></li>
<?php
}
?>
the code is checking the last input only (last category)
any help ?
You're running a foreach before you actually output the checkbox...
So, the $isChecked variable is actually changing in your foreach, but only the last value of the loop will be printed.
You need to move your
<li></li>inside the loop of the $isChecked
I'm not sure if this is what you want, but here goes:
<?php
for ($i2 = 0; $i2 < $rowsZ22; $i2++) {
$catszz2 = explode(",", $bussSubcat);
foreach ($catszz2 as $catzz2) {
if ($catzz2 == $row22[$i2]["id"]) {
$ischecked2 = "checked";
} else {
$ischecked2 = "";
}
?>
<li>
<input type="checkbox"
onclick="getElementById('cats<?= $row22[$i2]["parent"] ?>').checked= true;" <?= $ischecked2 ?>
value="<?= $row22[$i2]["id"] ?>" name="subcats[]" id=""/> <?= $row22[$i2]["title"] ?>
</li>
<?php
}
}
?>

Setting variable and string from POST with loop

Currently I have 100 text input boxes named contact0 through contact101. I am trying to get the Post data and name each string to itself. Meaning $contact0 = $_POST['contact0'];all the way up to $contact101 = $_POST['contact101']; there has to be a simpler way to set them in a loop or something. Overall the end result is I just want the data entered in the textbox to become the value of the textbox when submitted. Any suggestions will help I might be doing this wrong for the results I want.
for ($i = 0; ;$i++){
if($i < 101){
$contact.$i = $_POST['contact'].$i;
echo $contact.$i;
}
else{
break;
}
}
for ($i = 0; $i <= 101; $i++){
${"contact".$i} = $_POST['contact'.$i];
echo ${"contact".$i};
}
You may want to consider amending your HTML form. Rather than create 100 new variables, you can assign all the contacts to an array and then reference them with the array index.
HTML
<input type="text" name="contacts[]" value="Contact 1 name"/>
<input type="text" name="contacts[]" value="Contact 2 name"/>
// etc...
PHP
$contacts = $_POST['contacts'];
var_dump($contacts);
// prints array(0 => 'Contact 1 Name', 1 => 'Contact 2 Name'...
As it now an array, you can reference the contact e.g. $contacts[34] and know it will be a valid entry.
EDIT
A working example:
<?php
if (isset($_POST['contacts'])) {
$contacts = $_POST['contacts'];
echo "<p>The contacts are below:</p>";
print_r($contacts);
} else {
echo "<p>Please enter the contacts</p>";
}
?>
<form method="post" action="">
<?php for($x = 0; $x < 100; $x++): ?>
<input type="text" name="contacts[]" value="<?php echo (isset($contacts[$x])) ? $contacts[0] : ''; ?>"/>
<?php endfor; ?>
<input type="submit" name="submit" value="submit"/>
</form>
Edit 2
I have now made the form a loop, meaning it will create all our contact inputs for us. On top of this, if we have already posted the form each field will have the correct contact values.
If you are not aware of the syntax, echo isset($contacts[$x])) ? $contacts[$x] : ''; is a ternary operator which is a one line if/else statement.
Which can also be tested here: http://phpfiddle.org/api/run/ugb-cta

calculating values from input, in PHP

I'm studying for finals and i came across this question:
Write a php script to read a positive integer, n from a input box and calculate the value of 1+2+-- n...
ive have tried for long and done sufficient research, but i have not been able to complete this so far i have:
<html>
<head>
<title>
</title>
</head>
<body>
<form action="inputnum.php" method="post" >
num:<input type="text" name="num" size ="5"/>
<input type = "submit" value = "Submit Order" />
<?php
$num=$_POST["num"];
if ($num==0)$num=="";
for($i=0; $i<=$num; $i++){
}
echo"($num+$i)";
?>
</form>
can anyone help me out?
Thanks in advance!
<?php
$num = (int)$_POST["num"];
$total = 0;
for ($i=0; $i <= $num; $i++) {
$total = $total + $i;
}
echo $total;
?>
If your code is expecting to deal with a number, it's better to do an explicit casting via (int) of the posted value
You mixed up paranthesis, you also mix = with ==. Anyway there exists a faster way of computing such sum, i.e. n * (n + 1) / 2
<?php
$num=$_POST["num"];
if ($num==0)$num="";
else
{
for($i=0; $i<=$num; $i++){
$sum=$sum+$i;
}
}
echo $sum;
?>
To be more precise you must first check whether the submit button is set or not before calculating the sum.
Firstly, this if ($num==0)$num==""; is wrong. $num=""; is what it should be. And in anycase, this would break your if-statement.
I would suggest putting the for-loop in the if-statement and changing the condition to $num>0.
Let $i begin from 1, not 0.
You could use something like this (not tested, no PHP interpreter available here):
<html>
<head>
<title></title>
</head>
<body>
<?php
$num = (int)$_POST['num'];
if(!$num) {
?>
<form action="inputnum.php" method="post" >
num: <input type="text" name="num" size ="5"/>
<input type = "submit" value = "Submit Order" />
</form>
<?php
} else {
$total = 0;
for($i=1; $i<=$num; $i++){
$total = $total + $i;
}
echo $num;
}
?>
</body>
</html>

Categories