rickab1
rickab1

Reputation: 15

Increment Index in $_POST Array

I have a section of php code where I need for the $_POST index to increment by one each time it goes through the "do while" loop. In other words, the first occurrence would be $_POST['tableserver1'], then the next one would be $_POST['tableserver2'], etc. This loops 6 times and then stops.

I have tried using variables as the index, trying to get them to increment. I found an example in the php manual http://php.net/manual/en/language.operators.increment.php which increments the number at the end of a string, but I'm not having any luck with getting it to work inside the $_POST index.

This section of code creates a set of 6 select lists that contain names from the database. I am trying to get the select lists to populate from the $_POST value if it is set, other wise it is zero.

Here is my code:

<?php 
    $x = 1;

do {
    ?>
    <blockquote>
        <p><?php echo $x . "."; ?>
            <select name="tableserver<?php echo $x; ?>" id="tableserver<?php echo $x; ?>">
                <option selected value="0" <?php
                if (!(strcmp(0, '$tableserver.$x'))) {
                    echo "selected=\"selected\"";
                }
                ?>>Select Server</option>
                        <?php
                        do {
                            if (strpos($row_getnamesRS['service'], '22') !== false) {
                                ?>
                        <option value="<?php echo $row_getnamesRS['memberID'] ?>" <?php
                        if (!(strcmp($row_getnamesRS['memberID'], '$tableserver.$x'))) {
                            echo "selected=\"selected\"";
                        }
                        ?>><?php
                                    echo ucfirst(strtolower($row_getnamesRS['first_name']))
                                    . "&nbsp;" . ucfirst(strtolower($row_getnamesRS['last_name']))
                                    ?></option>
                        <?php
                    }
                } while ($row_getnamesRS = mysqli_fetch_assoc($getnamesRS));
                $rows = mysqli_num_rows($getnamesRS);
                if ($rows > 0) {
                    mysqli_data_seek($getnamesRS, 0);
                    $row_getnamesRS = mysqli_fetch_assoc($getnamesRS);
                }
                ?>
            </select>
        </p>
    </blockquote>
    <?php
    $x++;
} while ($x <= 6);
?>

Upvotes: 0

Views: 1105

Answers (3)

andrew
andrew

Reputation: 9583

It would be easier to post an an array

so instead of

name="tableserver<?php echo $x; ?>"

use

name="tableserver[]";

the you can just do

foreach($_POST['tableserver'] as $tableServer){....}

Upvotes: 0

Steve
Steve

Reputation: 20469

$i=0;
do{
   echo $_POST['someval'.$i];
}while(++$i < 6)

Upvotes: 1

leopik
leopik

Reputation: 2351

Perhaps like this? ...

$arr = [];

for ($i = 1; $i <= 6; $i++)
    array_push($arr, $_POST["tableserver" . $i]);

$arr; // Contains 6 values (starting from $_POST["tableserver1"] to $_POST["tableserver6"])

Upvotes: 1

Related Questions