-1

I want the loop data out side forloop, but i'm getting only one value

<form action="<?=$_SERVER['PHP_SELF']?>" method="post">

               <select name="test[]"  multiple="multiple">
                    <option value="test">test</option>
                    <option value="mytesting">mytesting</option>
                    <option value="testingvalue">testingvalue</option>
                    </select>
                <input type="submit" value="Send" />
                   </form>


<?php
    $test=$_POST['test'];
    for($i=0;$i<count($test);$i++)
    {
    $tyy = $test[$i];
    }


?>

I want to get $tyy out side for loop

I want the loop data out side forloop by i am getting only one value

1
  • 1
    Why would you need a third variable? You already have all the information available in $_POST and $test. Commented Mar 4, 2015 at 9:34

6 Answers 6

1

Use this

$tyy[] = $test[$i];

or you can concat the values by using this

$tyy = "";
for($i=0;$i<count($test);$i++)
    {
    $tyy .= $test[$i];
    }

print_r($tyy)
Sign up to request clarification or add additional context in comments.

7 Comments

and one thing i have a problem for this can you solve We have a multiple select drop down but i am not getting drop like we keep for single drop here the code <select multiple> <option>test</option> <option>test1</option> <option>test1</option> </select> <select> <option>test</option> <option>test1</option> <option>test1</option> </select>
so what was prob in that ?
dropdown is not getting like single select dropdown for multiple
|
1

try this..

    $groupStr = "";
    for($i=0; $i< count($test); $i++)
        {
        $groupStr .= $test[$i];
        }

echo $groupStr;

Comments

0

Use $tyy as an array and fill that array in for loop by using array_push method.

 $tyy = array();

 $test=$_POST['test'];
for($i=0;$i<count($test);$i++)
{
   array_push($tyy, $test[$i]);
}

Comments

0

This is, because you are reassigning it every iteration. So change the line to this:

$tyy[] = $test[$i];
  //^^ See here, Now you are assign the value to a new array element every iteration

So that you have an array which you can use later

Side Note:

1. You know that you already have an array with this data in $test ? But if you want all data as one string you can use this (without a for loop):

$tyy = implode(", ", $test);

2. $_SERVER["PHP_SELF"] is just a reflection of your URL, so this is open for XSS. You can use this to make it save:

<?= htmlspecialchars($_SERVER["PHP_SELF"], ENT_QUOTES, "utf-8"); ?>

3 Comments

if i print echo $tty; outside for loop i am getting output array
@deb output array Do you mean you get array as output or what I guess you get Array to string? Also you know that you already have a array with it: $test is the same as you would have in $tyy
@deb do print_r($tty);
0

You have created one single variable, which is being overwritten each time you loop, so there is only one value to get.

If you want to access all the values, you need to save into an array instead of a single variable, or append all the values into one value

either

//create an array of values in $tyy
$tyy = $_POST['test'];
//you can now access $tyy by looping through it.

or

$test = $_POST['test'];
$for($i = 0; $i<count($test); $i++)
{
  //list all the values with commas separating them.
  $tyy .= $test[$i].", ";
}

//view the contents by echoing it.
echo $tyy;

Comments

-1

But why are you use loop for post data. Your $_POST['test'] is self array so you can use it without for loop. $array = $_POST['test']; try this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.