foreach, arrays... THE DEVIL!

A

Anonymous

Guest
Code:
      $n_per_page = $_POST['n_per_page'];
      $n_remove = $_POST['n_remove'];
      $n_add = $_POST['n_add'];
      $n_comments = $_POST['n_comments'];
      $n_edit_all = $_POST['n_edit_all'];
      
      $variables = array(
        '$n_per_page',
        '$n_remove',
        '$n_add',
        '$n_comments',
        '$n_edit_all'
        );

      $changed_by = $_POST['changed_by'];
      
      foreach($variables as $key => $value) {
       if( $value >= 11 || $value >= 0 )
       {
         echo "<font color=\"red\">User level must be between 1 and 10</font><br>";
         exit();
        }
       }
Basically i want to check every value of the array and see if its >= 11 or <= 0, no matter what value i put in, it always comes out as being to high or to low..
 
A little sanity checking goes a long way..

Code:
if( $value >= 11 || $value >= 0 )
/* ------------------------^
   I'm guessing you want less-than-or-equal here,
   not greater-than-or-equal
*/
 
I am an ass hole, i dont know how i missed that, sorry, waste of time right there -.-

Although, it still doesn't work..

Code:
      $n_per_page = $_POST['n_per_page'];
      $n_remove = $_POST['n_remove'];
      $n_add = $_POST['n_add'];
      $n_comments = $_POST['n_comments'];
      $n_edit_all = $_POST['n_edit_all'];
      
      $variables = array(
        '$n_per_page',
        '$n_remove',
        '$n_add',
        '$n_comments',
        '$n_edit_all'
        );

      $changed_by = $_POST['changed_by'];
      
      foreach($variables as $key => $value) {
       if( $value >= 11 || $value <= 0 )
       {
         echo "<font color=\"red\">User level must be between 1 and 10</font><br>";
         exit();
        }
       }
 
Insolence said:
Code:
$variables = array(
   '$n_per_page',
   '$n_remove',
   '$n_add',
   '$n_comments',
   '$n_edit_all'
);

Take those out of the quotes. When you put a variable name between single quotes, the variable doesn't get evaluated, it's taken as a literal string:

Code:
<?
   $a = 'swirlee';

   echo $a;
   /* output:
         swirlee
   */

   echo '$a';
   /* output:
         $a
   */
?>

See the difference? This code should work:
Code:
<?
$variables = array(
   $n_per_page,
   $n_remove,
   $n_add,
   $n_comments,
   $n_edit_all
);
?>

As an addendum of the above (this is only tangentially related, but it's an issue I've seen here a lot lately), even though variable names, when between double quotes, will still evaluate, it's pointless to put a single variable name inside double quotes:

Code:
<?
$a = 'swirlee';

echo "$a";
/* output:
      swirlee
*/

echo $a;
/* output:
      swirlee
*/
?>

As you can see, they both have the same output, but the second saves PHP some extra work and saves you some typing.

Also, in a foreach() loop, unless you're going to use the $key, you don't have to specity it:

Code:
<?
/* you don't use the key anywerhe in the code below, so just omit it */

foreach($variables as $value) {
/* -------------------^
	notice there's no $key =>
*/
	if( $value >= 11 || $value <= 0 )
	{
		echo "<font color="red">User level must be between 1 and 10</font><br>";
		exit();
	}
}
?>
 
Thank you alot, i learned alot right there, i didn't really know the value of $key, and i thought in arrays " was string and ' was an integer, learning alot from you, thanks alot :) and thanks for being patient with me when i make a stupid error i dont notice.. I try not to post unless i've had the problem for a few hours.

On a side note, im about 20% done with the script id say, members can be added/login, you can change variables after its installed, automated installation (all you have to do is run through the config file, and run install.php) deleting members is done.

Next up... news posting/removing/editing, profiles / editing, tournaments, forums...(hardest) wish me luck :)
 
Swirlee says, "Thank you class, that'll be all for today. Make sure to read chapter 12 of your PHP forum book, there will be a possible quiz tomorrow."

Administrator, must say good choice in moderator.

BTW, Insolence, what you building over there anyways?? Just curious.
 
A Clan Management system (iS_CLAN : insolent scripting_clan)

With it you can.. host tournaments, add/remove/modify members, add/remove/modify news, it comes with a forum, link add/remove/modding, a WHOLE buncha shit :), maybe 3 weeks - 3 more months to build it depending on how hard it is to make a tournament script, ima have to do that one from scratch, gonna be pretty hard :(
 
Just in Case:
Be very modular in your coding, even if it takes time to think first, because once you start to code and screw in between,
you are done for!!

a whole lot of coding will have to go before you repair the error

best of luck :D
 
Back
Top