0

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);
?>

3 Answers 3

1
$i=0;
do{
   echo $_POST['someval'.$i];
}while(++$i < 6)
Sign up to request clarification or add additional context in comments.

2 Comments

Yes! This is what I was trying to do. Somehow I must have had my syntax wrong. But this works! Thank you!
With this I was able to eliminate 27 lines of code that were used to define the post variables.
1

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"])

Comments

0

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){....}

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.