0

Hi I'm having trouble figuring out a way to include if clauses within a foreach. I'm writing a page that allows the user to see the data they entered in in the previous form before committing it to the database. They can choose to select from data thats previously been entered for different sizes, quantities, etc, as well as entering their own new options. Right now the way I have it set all of the choices appear but they are all on one row and stacked within a cell. My problem is I need it to create a new row and print $size and $side for each value.

<tr>
    <td>
<?php
    if(isset($_POST['var1'])) {
        $var1 = $_POST['var1'];
        foreach($var1 as $value) {
            echo $value . "<br>";
        }
    }
    if(isset($_POST['new_var1'])) {
        $new_var1 = $_POST['new_var1'];
        foreach($new_var1 as $value) {
            echo $value . "<br>";
        }
    }
?>  
    </td>
    <td>
<?php
    if(isset($_POST['var2'])) {
        $var2 = $_POST['var2'];
        foreach($var2 as $value) {
            echo $value . "<br>";
        }
    }    
    if(isset($_POST['new_var2'])) {
        $new_var2 = $_POST['new_var2'];
        foreach($new_var2 as $value) {
            echo $side . "<br>";
        }
    }
?>
    </td>
</tr>

Is there a way to inlude if statements within a foreach so I can check to see what choices are set, then cycle through all the choices at once and then include the <tr></tr> tags within the foreach? I was thinking of doing it by making multiple if statement that check each combination of isset for my variables and have foreach statements that only include the set choices but that seems very clunky and a roundabout way to do what I need.

5
  • Are screw_side and screw_size the same length? Commented Jun 6, 2012 at 20:34
  • Do you know that you can close and reopen php tags as you like ? eg: <?php if ($test) { ?><br><?php }?> Commented Jun 6, 2012 at 20:35
  • @JeromeWAGNER: Why would you do that here? Commented Jun 6, 2012 at 20:35
  • @Eric: I am trying to understand the question and the level of expertise of the user. It seems to me that part of the problem comes from the coupling/decoupling of the datas and the html Commented Jun 6, 2012 at 20:38
  • The solution you came up with seems clunky because you do not separate logic from presentation. Most of solutions will be clunky in that case. Commented Jun 6, 2012 at 20:39

3 Answers 3

2

I don't really understand your situation, but I'd suggest changing your form to be like this:

<input name="screws[0][size]" />
<input name="screws[0][side]" />

<input name="screws[1][size]" />
<input name="screws[1][side]" />

<input name="screws[2][size]" />
<input name="screws[2][side]" />

When PHP gets that, you can do:

<?php
$screws = @$_GET['screws'];
$newscrews = @$_GET['newscrews'];
?>
<table>
    <?php if($screws) foreach($screws as $screw): ?>
    <tr>
        <td><?php echo $screw['size'] ?></td>
        <td><?php echo $screw['side'] ?></td>
    </tr>
    <?php endforeach ?>
</table>
Sign up to request clarification or add additional context in comments.

9 Comments

@ Eric so you suggest changing my form so that all the variables associated with 'screw' are a multidimensional array and then running a foreach through that? so it would be <input name="screw[][size]"><input name="screw[][location]> etc?
That makes the most sense to me. After all, each size field is linked to the side field, so it makes sense to have an array of screws, rather than an array for each property. I think you have to put numberic indices in the square brackets though - PHP only infers the index at the end, IIRC.
@user1440701: You might not have realized, but @$_GET['screws'] prevents you from having to do isset. Instead of giving a key error, it just returns NULL if the key is not set.
Hmm i tried that with this code $screws = $_POST['screw']; if($screws) { foreach($screws as $screw){ ?> <tr> <td><?php echo $screw['size']; ?></td> <td><?php echo $screw['side']; ?></td> <td><?php echo $screw['location']; ?></td> <td><?php echo $screw['quantity']; ?></td> </tr> Now however, in the first row, it will return the size in the first cell, but errors in the next three cells. In the second row, error in the first cell, side in the second, and errors in the next two, and so on and so forth. It ends up making a diagnol line of values across.
I have a javascript on my form that allows users to add extra inputs for another screw so I cant really use number indices in my input names, before i had them set like <input name="screw_size[]"> so if they added another screw it would just tack on. Now my input names are like <input name="screw[size][]"> and im getting an undefined index error.
|
0

Yes, conditionals are allowed in foreach statements. You'd include them normally. Also, the code you have is vulnerable to XSS (cross site scripting) attacks. You may want to sanitize the data being submitted by post.

I'm sorry if I misunderstood the question, it's worded a bit.. interestingly..

EDIT: if(isset($var1) && isset($var2) &&...etc)

Comments

0

Here's another solution using your existing data format:

<?php
$sizes = @$_GET['screw_sizes'];
$sides = @$_GET['screw_sides'];
$screws = array()

if($sides && $sizes) {
    $n = min(count($sizes), count($sides))
    for ($i=0; $i < $n; $i++) {
        $screws[$i]['side'] = $sides[$i];
        $screws[$i]['size'] = $sizes[$i];
    }
}
?>

The way to echo it out is the same:

<table>
    <?php foreach($screws as $screw): ?>
    <tr>
        <td><?php echo $screw['size'] ?></td>
        <td><?php echo $screw['side'] ?></td>
    </tr>
    <?php endforeach ?>
</table>

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.