creates variables in loop or fromArray - php

So i have a little problem that i cant figure out and not sure how to do it
so i have a loop that creates form boxes and im gonna send all of them to a excel file(but thats later)
now i just want to create all variables in the loop
here is the loop with 1 textbox just to show
<?php
$i=1;
while ($i<=31){
?>
<input type = "text" name="day" />
<br />
<?php
$i=$i+1;
}
?>
so now how do i create a unique name for each box? or whats the best way to do it i read that array, fromarray is good at it if so im not good with arrays so how do i do it?

Change your input name like day[]
<?php
$i=1;
while ($i<=31){
?>
<input type = "text" value="day<?php echo $i; ?>" name="day[]" />
<br />
<?php
$i++;
}
?>
When you process form value with GET/POST like bellow (My example with POST) :
<?php
$days = $_POST['day'];
foreach($days as $day) {
echo $day;
}
?>
Or Like this :
<input list="day" name="day" class="listbox">
<datalist id="day">
<?php
$i=1;
while ($i<=31){
?>
<option value="day<?php echo $i; ?>">Day <?php echo $i; ?></option>
<?php
$i++;
}
?>
</datalist>

You can do this way
<?php
$i=1;
while ($i<=31){
echo '<input type = "text" name="day' . $i .'" /><br />';
$i=$i+1;
}
?>

this is a good place to use a for loop instead of the while loop:
<?php
for($i = 1; $i <= 31; $i++) {
?>
<input type = "text" name="day-<?= $i ?>" />
<br />
<?php
}
?>

Try this :)
$i=1;
while ($i<=31){
echo '<input type = "text" name="Random' . $i . '" /><br />';
$i++;
}
?>
And btw. you can do operations on variables much easier like this Operators

Related

Problems when using nested foreach on checkboxes loaded dynamically

I need to use nested foreach for dependent checkboxes.
<input type="checkbox" name="all[]" value="<?php echo $row_bus_details['busid'];?>" >
<?php
$book_side_result = mysqli_query($db,"select * from advt_sides");
while($book_side_row=mysqli_fetch_array($book_side_result))
{
?>
<input type="checkbox" name="bookingside[]" value="<?php echo $book_side_row['advt_side_id']; ?>" id="<?php echo $book_side_row['advt_side']; ?><?php echo $row_bus_details['busid'];?>" > <?php echo $book_side_row['advt_side']; ?><br/>
<?php } ?>
I need to loop the selected values of second checkbox if the first checkbox is selected.
I wrote the code like
$i = 0;
$busid = isset($_POST['all']) ? $_POST['all'] : array();
foreach ((array)$busid as $item) {
if(!empty($_POST['bookingside'])) {
foreach($_POST['bookingside'] as $side) {
$sql_book_side=mysqli_query($db,"INSERT INTO `advt_book_side`(bus_id,sides_id) VALUES ('$item','$side')");
$i++;
}
}
}
The result I need is just like the image below
You need to save data in serialize array from in data base like:
$sql_book_side=mysqli_query($db,"INSERT INTO advt_book_side(bus_id,sides_id) VALUES ('$item',serialize(array('left'=>1,'right'=>1,'back'=>0)))");
Print check box with check uncheck using below code
$book_side_result = mysqli_query($db,"select * from advt_sides");
while($book_side_row=mysqli_fetch_array($book_side_result))
{
$array = unserialize($book_side_row['sides_id']);
foreach($array[0] as $side){
?>
<input type="checkbox" name="bookingside[]" value="<?php echo ($side)? $side:0; ?>">
<?php }
} ?>

created input tag in php for loop and get value

I want to create limit input tag with php for loop an get inputs values. My sample code is this:
<?php
$limit = 10;
for ($i=1; $i<=$limit; $i++) { ?>
<input name="<?php echo $i ?>" type="text" /><br>
<?php } ?>
Is my code correct? How can I get input values?
You can try my script.
<?php
$limit = 10;
?>
<form method="post">
<?php
for ($i = 1; $i <= $limit; $i++) {
?>
<input name="anything[]" type="text" /><br>
<?php } ?>
<input type="hidden" name="op" value="sent" />
<input type="submit" value="submit" />
</form>
<?php
if (!empty($_POST["op"])) {
for ($i = 1; $i <= $limit; $i++) {
if (strlen($_POST["anything"][$i]) !== 0) {
?>
<p>The value of the <?php echo $i; ?> text field is: <?php echo $_POST["anything"][$i]; ?>
<?php
} else {
?>
<p><?php echo $i; ?> was not set.</p>
<?php
}
}
}
Looks alright but I would use an array as the input name. For example:
<?php
$limit = 10;
for ($i=1; $i<=$limit; $i++) {
?>
<input name="number[<?php echo $i; ?>]" type="text" /><br>
<?php
}
?>
That way in the back end you can just loop through the number array like so.
foreach ($_POST['number'] as $key => $value) {
// Do stuff
}
Your code should render 10 html input fields with name 1, 2, 3, ... 10 correctly
To get input values, wrap your input fields in a form element with an action pointing to the php script in which you want to read the values (e.g. action="myscript.php").
(You should add a input type="submit" to have a way to submit the form. I assume you know HTML good enough to create a simple form.)
The script invoked by submitting the form (e.g. myscript.php) will now be able to read the values using the $_GET array. See http://php.net/manual/de/reserved.variables.get.php
You could print the values like so:
<?php
for($i=1;$i<=10; $i++) {
echo $i . ' : '. $_GET[$i];
}
?>
Edit: As #David Jones mentioned it would be better to use an array as input name

Form: make a loop to save different value of a same field name

I do a form, inside I would like to do a loop for to show the same field. Each field will have different value and I would like to use sessions to take all the value.
Here is my code:
for ($i = 1; $i <= 5; $i++) { //normally not 5 but a random number, choose by user
echo "Numero ";
echo $i;
?>
<input type="text" name="number2" id="number2"/>
<?php
}
?>
</form>
<?php
echo $_POST['number2'];
$my_array=array($_POST['number2']);
$_SESSION['countnumb']=$my_array;
in another page:
foreach($_SESSION['countnumb'] as $key=>$value)
{
echo 'The value of $_SESSION['."'".$key."'".'] is '."'".$value."'".' <br />';
}
I can't register any number. How can I do this? thanks
Basics First - ids should be unique in a webpage.
Declaring <input type="text" name="number2" id="number2"/> in a loop is wrong.
For creating multiple input using loop try like this-
echo "<input type='text' name='number[$i]' id='number{$i}' />";
<input type="text" name="number[2]" id="number2"/>
would make $_POST['number'] an array which you can loop though server side, This is described here http://www.php.net/manual/en/faq.html.php#faq.html.arrays
foreach ($_POST['number'] as $number){
echo $number;
}
for example
this would make your code
for ($i = 1; $i <= 5; $i++) { //normally not 5 but a random number, choose by user
echo "<input type="text" name="number[$i]" id="number{$i}"/> ";
?>
<?php
}
?>
</form>
<?php
print_r( $_POST['number'] );
$_SESSION['countnumb']= $_POST['number'];

value not recognized in php

<form action="" method="post">
<?php
$i=0;
while(i<4){ ?>
<input type="checkbox" name="<?php echo 'chkApprove_'.$i; ?>"/>
<?php
}
?>
<input type="submit" name="btnsubmit"/>
</form>
if(isset($_POST['btnsubmit']))
{
$i=0;
while($i<4)
{
echo $i;
$chek=$_POST['chkApprove_'.$i];// Error Undefined Index
$i++;
}
}
Error is displayed as Undefined index: chkApprove_0...chkApprove_3. Am i doing something wrong here.
$ was missing in you while loop before "i". and $i was not increamenting.
<?php
$i=0;
while($i<4){ ?>
<input type="checkbox" name="<?php echo 'chkApprove_'.$i; ?>"/>
<?php
$i++;
}
?>
try this : you start from $i=0 but not increment $i++.
<form action="" method="post">
<?php
$i=0;
while($i < 4)
{
?>
<input type="checkbox" name="<?php echo 'chkApprove_'.$i; ?>"/>
<?php
$i++;
}
?>
<input type="submit" name="btnsubmit"/>
</form>
<?php
if(isset($_POST['btnsubmit']))
{
$i=0;
while($i < 4) {
echo $i;
$chek=$_POST['chkApprove_'.$i];// Error Undefined Index
$i++;
}
}
?>
well you have a syntax error and you forget to increment $i in this loop:
$i=0;
while(i<4){ ?>
<input type="checkbox" name="<?php echo 'chkApprove_'.$i; ?>"/>
<?php
}
should be
$i=0;
while($i<4){ ?>
<input type="checkbox" name="<?php echo 'chkApprove_'.$i; ?>"/>
<?php
$i++;
}
Because you don't increment $i in the intial loop, but in the latter looop you do, you get the undefined index
Your corrected code:
<form action="" method="post">
<?php
$i=0;
while($i<4){
?>
<input type="checkbox" name="<?php echo 'chkApprove_' . $i; ?>"/>
<?php
$i++;
}
?>
<input type="submit" name="btnsubmit"/>
</form>
<?php
if (isset($_POST['btnsubmit'])) {
$i = 0;
while ($i < 4) {
echo $i;
$chek = $_POST['chkApprove_' . $i];
// Error Undefined Index
$i++;
}
}

POST a hidden input + multiple options PHP

Hi there I'm quite new to PHP
I have this problem:
I would like to POST a multiple choice + a hidden field from a form:
<?php
if (isset($_SESSION['nickname']))
{
$result = mysql_query("SELECT * FROM users");
$teamsCount = ceil(mysql_num_rows($result)/2);
for ($i=1; $i<=$teamsCount; $i++)
{
// TEST: echo $i . " TeamsCount er: " . $teamsCount. "<br>";
?>
Team <? echo $i; ?>
<form name="addTeam" action="buildTeams.php" method="POST">
<input type="hidden" name="hiddenField" value="<?php $i; ?>" />
<select name="teams[]" multiple="multiple" size="<?php echo mysql_num_rows($result); ?>">
<?php
$query = mysql_query("SELECT * FROM users");
while ($row=mysql_fetch_array($query))
{
$id=$row["ID"];
$nick=$row["Nick"];
?>
<option value="<?php echo $id; ?>"><?php echo ucfirst($nick); ?></option>
<?php
}
?>
</select>
<input type="submit" value="Make them teams!!" />
</form>
<?php
}
}
?>
I think you have an error in this line:
<input type="hidden" name="hiddenField" value="<?php $i ?>" />
It should be
<input type="hidden" name="hiddenField" value="<?php echo $i ?>" />
Edit:
Put the team id in the select name. Example:
<select name="teams[<?=$i?>][]">
And in PHP do:
foreach ($_POST['teams'] as $team_id => $choices)
I think you should check $_POST['hiddenField'] to obtain hidden value

Categories