Jump to content

How to Set This Up with a For Loop?


glassfish

Recommended Posts

Code:

<?php
    /*
     * HASHTAGS
     */
    if(isset($_POST['hashtag0'])) {
        $hashtag0 = $_POST['hashtag0'];
    }
    if(isset($_POST['hashtag1'])) {
        $hashtag1 = $_POST['hashtag1'];
    }
    if(isset($_POST['hashtag2'])) {
        $hashtag2 = $_POST['hashtag2'];
    }
    if(isset($_POST['hashtag3'])) {
        $hashtag3 = $_POST['hashtag3'];
    }
    if(isset($_POST['hashtag4'])) {
        $hashtag4 = $_POST['hashtag4'];
    }
    if(isset($_POST['hashtag5'])) {
        $hashtag5 = $_POST['hashtag5'];
    }
    if(isset($_POST['hashtag6'])) {
        $hashtag6 = $_POST['hashtag6'];
    }
    if(isset($_POST['hashtag7'])) {
        $hashtag7 = $_POST['hashtag7'];
    }
    if(isset($_POST['hashtag8'])) {
        $hashtag8 = $_POST['hashtag8'];
    }
    if(isset($_POST['hashtag9'])) {
        $hashtag9 = $_POST['hashtag9'];
    }
    if(isset($_POST['hashtag10'])) {
        $hashtag10 = $_POST['hashtag10'];
    }
    if(isset($_POST['hashtag11'])) {
        $hashtag11 = $_POST['hashtag11'];
    }
    if(isset($_POST['hashtag12'])) {
        $hashtag12 = $_POST['hashtag12'];
    }
?>

<?php

// Stores the hashtags taken from the form inside an array.
        $hashtags = array($hashtag0, $hashtag1, $hashtag2, $hashtag3, $hashtag4, $hashtag5, $hashtag6, $hashtag7, $hashtag8, $hashtag9, $hashtag10, $hashtag11, $hashtag12 );

?>

Basically, the script checks if the "$_POST['']" variable from the form is set and then it stores the "given" "values" from the form inside an array.

 

I tried it with this:

<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <?php
        for($i = 0; $i < 12; $i++){
           echo "<input type='text' name='hashtag" . $i . "' />";
        }
    ?>
    <input type="submit" name="submit" />
</form>

<?php

if(isset($_POST['submit'])){
    for($i = 0; $i < 12; $i++) {
        if(isset($_POST['hashtag$i'])){
            $hashtags = array($_POST['hashtag$i']);
        }
    }

        print_r($hashtags);
}
?>

With this example I get the following notice:

Notice: Undefined variable: hashtags in C:\xampp\htdocs\hashtags\index.php on line 19

It points to "print_r()".

 

I would appreciate the suggestions.

Link to comment
https://forums.phpfreaks.com/topic/291983-how-to-set-this-up-with-a-for-loop/
Share on other sites

PHP variables do not get interpreted when not wrapped in double quotes.  So the post var you are checking in the loop is 'hashtag$i' which (if you had error checking turned on ) would give you an error message itself.  If you use double quotes then you would check 'hashtag1', 'hashtag2' , etc.

Addendum:

Okey I have tried it with this.

<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <?php
        for($i = 0; $i < 12; $i++){
           echo "<input type='text' name='hashtag" . $i . "' />";
        }
    ?>
    <input type="submit" name="submit" />
</form>

<?php

if(isset($_POST['submit'])){

    $hashtags = array();
    for($i = 0; $i < 12; $i++) {

        if(!empty($_POST["hashtag$i"])){
            $hashtags[] = $_POST["hashtag$i"];
        }

    }

    print_r($hashtags);

}
?>

This works.

 

Though, I have to specifically use "!empty", any reasons why?

 

If I use "isset" I get this:

Array ( [0] => test [1] => test [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => ) 

Thanks for the answer!

No, no, no.

 

You do not use numbered variables or names at all. This is very, very poor design. You want a list instead.

 

Unfortunately, PHP is rather quirky when it comes to processing multiple form parameters of the same kind. You need a special syntax for this:

<input type="text" name="foo[]"> 

Note the brackets. This turns $_POST['foo'] into an array. 

Addendum:

<?php
if(isset($_POST['submit'])){

    if(isset($_POST['hashtag'])){
        $hashtags = $_POST['hashtag'];
    }

    print_r($hashtags);
}

?>

Both, "isset" and "!empty" is printing the following:

 

I would appreciate suggestions on this one:

Array ( [0] => test [1] => test [2] => test [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => )
 

Note:

With this I am using "hashtag[]" in the form.

Addendum:

 

I have solved it like this:

<?php
if(isset($_POST['submit'])){

    $hashtags = array();
    for($i = 0; $i < count($_POST['hashtag']); $i++) {
        if (!empty($_POST['hashtag'][$i])) {
            $hashtags[] = $_POST['hashtag'][$i];
        }
    }
    print_r($hashtags);
}

?>

Thanks for the answers.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.