| Forum Home | ||||
| Press F1 | ||||
| Thread ID: 63572 | 2005-11-15 01:12:00 | php for, foreach loop help | Morgenmuffel (187) | Press F1 |
| Post ID | Timestamp | Content | User | ||
| 404392 | 2005-11-15 01:12:00 | Hi What i am trying to do is create input boxes for the input of part numbers, i could just have 10 boxes coded in html but I thought using a loop would be better (make it easier to expand at a later date), all the boxes appear ok, but the php counter doesn't seem to work with already set variables confused yet This is where i am having the problem, if i set a variable to have a value like below $part_3 = '11234'; should that not be echoed out in the below code $counter = 3; echo $part_[$counter]; returns nothing, zip, nada, not a sausage Here is it in my code <?php for ( $counter = 1; $counter <= 10; $counter += 1) { $part_3 = '11234';//should put value in 3rd box ?> <tr valign="top"> <td valign="middle" <?php if(!($zz[$counter]) || !($yy[$counter])) echo 'class="error"'; ?>>Part Number</td> <td><input type="text" name="part_<?= $counter ?>" size="30" value="<? echo $part_[$counter]; ?>"></td> </tr> <?php } ?> this is what shows on the live page (view source) <tr valign="top"> <td valign="middle" class="error">Part Number</td> <td><input type="text" name="part_1" size="30" value=""></td> </tr> <tr valign="top"> <td valign="middle" class="error">Part Number</td> <td><input type="text" name="part_2" size="30" value=""></td> </tr> <tr valign="top"> <td valign="middle" class="error">Part Number</td> <td><input type="text" name="part_3" size="30" value=""></td> </tr> How it is supposed to work When the user enters the part numbers and then presses submit all the values are passed to an error checking code, all the values that pass the criteria re written into th dbase, while those that fail are returned to the form highlighted in red ('error' class) The error checking code will also use a loop like the one in the above code -- It hasn't been written yet because the above code doesn't seem to match the value $part_3 with $part_[$counter] where counter is 3, so the error check loop obviously wouldn't work either any suggestions or help are greatly appreciated |
Morgenmuffel (187) | ||
| 404393 | 2005-11-15 13:43:00 | First page <?php $numOfParts = 0; for ($i=1; $i < 10; $i++) { echo "<tr valign=\"top\">"; echo "<td valign=\"middle\" class=\"error\">Part Number</td>"; echo "<td><input type=\"text\" name=\"part" . $i . "\" size=\"30\" value=\"\"></td></tr>"; $numOfParts++; } echo "<td><input type=\"hidden\" name=\"numOfParts\" value=\"" . $numOfParts . "\"></td></tr>"; ?> Second page <?php $numOfParts = $_POST["numOfParts"]; for ($i=1; $i < $numOfParts; $i++) { $textBoxName = part . $i; //part1, part2 etc $value = $_POST[$textBoxName]; if ($value == "") { echo "error on line " . $i; } } ?> I think that might be better, but in your code up there you've got an error in this line: "<td><input type="text" name="part_<?= $counter ?>" size="30" value="<? echo $part_[$counter]; ?>"></td> " Gotta start the PHP tags with <?php not <? |
MikeS (756) | ||
| 404394 | 2005-11-16 00:49:00 | Excellent that worked in with my other code perfectly, stopped me tearing out my ever decreasing hair Thanks ps as for the php tags <?= $counter ?> -- works fine it just echos out the value, quicker than typing <?php echo $counter; ?> can't remember where i found it, I think it only works for variables, not functions but not sure of that, i really only use it for echoing variables out into the HTML |
Morgenmuffel (187) | ||
| 404395 | 2005-11-22 13:25:00 | Cool, I might use that in future myself. :) | MikeS (756) | ||
| 1 | |||||