Hi...

I want to display associative using variable as its key value.
I used the following script.
But it is not functioning.

<body>
<?php
if(isset($_POST['st']))
{
while ($item = current($_POST)) 
{

        echo $_POST[$key];

    next($_POST);
}
}
?>
<form id="form1" name="form1" method="post" action="mysql_inj.php">
  <p>
    <label for="textfield"></label>
    <input type="text" name="t1" id="textfield" />
  </p>
  <p>
    <input type="text" name="t2" id="textfield2" />
  </p>
  <input type="submit" name="st" id="textfield3" />
</form>
</body>
</html>

I am presuming your are allocating a value to your $item variable somewhere above this code. But you have two problems, the first being that if you are going to use the While loop you need to use == not just =:

while ($item == current($_POST))

Secondly, you cannot just echo $_POST[$key] as $key has nothing assigned to it.

It would be much better to use a foreach loop:

foreach ($_POST as $key => $value) {
    if ($item == $value) {
        echo $value
    }
}

take a look at array_keys

maybe something like...

$array_keys = array_keys($_POST);
foreach($array_keys as $key){
   echo $key." = ".$_POST[$key]; // or whatever you want to do here
}

Sorry Its my code mistake.Actually the code is

    <?php


        > if(isset($_POST['st']))
        > {
        > while ($item = current($_POST))
        > {
          $key=key($_POST);
        > echo $_POST[$key];
        > next($_POST);
        > }
        > }
        > ?>

Thanks for help

foreach ($_POST as $key => $value) {
     echo $value;
}

What's still wrong ? The above method should work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.