Loop and Post together


#1

I am currently using a server that will not allow using a variable that is undefined. The way I can get around this when using $_GET and $_POST is [php]
if (isset($_POST[‘variable’])) {

$variable = $_POST[‘variable’];

} else {

$variable = “”;

}
[/php]

But the problem is: I have a form and the number of input boxes depends on the number of records in the mysql table. Then what they put in the input boxes gets sent to another page. I need to use the solution above for every input box.

The following is what I have attempted to do ( $num_rows is the number of rows in the mysql, and the input boxes names are menu(id)).

[php]

if ( $num_rows == “” ) {
} else {
while ( $num_rows >= 1 ) {

$menu.$num_rows = $_POST[‘menu’.$num_rows];

$num_rows–;
}
}
[/php]

it is meant to do:

$menu1 = $post [‘menu1’]:
$menu1 = $post [‘menu2’];
$menu1 = $post [‘menu3’];
$menu1 = $post [‘menu4’];
$menu1 = $post [‘menu5’];
… until it runs out of records

Could someone help me currect my code?

Thanks,


#2

u have the posiebilty to make post-variables an array as well:

<input type="text" name="menus[1]" value="" />
<input type="text" name="menus[2]" value="" />
<input type="text" name="menus[3]" value="" />
...

or

<input type="text" name="menus[]" value="" />
<input type="text" name="menus[]" value="" />
<input type="text" name="menus[]" value="" />
...

then u may use:
[php]if (isset($_POST[‘menus’])) {

$menus = $_POST[‘menus’];

} else {

$menus = array();

}[/php]

i don’t know what u wanna do with the data later, but if u access them with a foreach($menus as $num => $menu) u will get no probs.

otherwise u have to use ${‘menu’.$num_rows} and of cause a fixed loop (othewise the later $menuX vars are not defined as well)

P.S.: have u tryed to use ini_set(‘error_reporting’,E_ERROR); to get rid of the notices?


#3

For undefined variable you can use ‘@’.

So, you code will look:
[php]
if ( $num_rows == “” ) {
} else {
while ( $num_rows >= 1 ) {

$menu.$num_rows = @$_POST[‘menu’.$num_rows];

$num_rows–;
}
}
[/php]


#4

As Alexandr said, you can use the @ to suppress warnings and such, but this can also suppress errors. It can cause a lot of problems when you come back to your code a month later and want to make some changes. They are an option, just keep in mind that you must always keep them in mind when debugging code.

I guess I rather to whatever it takes to get rid of any errors rather than just hide them. Better coding practices helps to produce better code.


#5

Thank you, all that helped a lot. I have got it working now. I have a tiny question which does not need a new thread.

I have some variables.

e.g.

$var = “Season 1”;
$var2 = “Season 2”;

I want to be able to just pick out the 1 or the 2 of the variable, but the number will be anything from 1-9 and it may be series not season. Is there any command that can just get the last number/letter out of the variable?

Thanks,


#6

substr() or regex’s http://php.net/manual/en/ref.pcre.php


#7

cheers substr() works perfectly!