foreach, arrays... THE DEVIL!

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Insolence
New php-forum User
New php-forum User
Posts: 43
Joined: Tue Aug 19, 2003 1:27 pm
Contact:

foreach, arrays... THE DEVIL!

Postby Insolence » Wed Aug 27, 2003 12:10 pm

Code: Select all

      $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..

User avatar
swirlee
Moderator
Moderator
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Re: foreach, arrays... THE DEVIL!

Postby swirlee » Wed Aug 27, 2003 1:01 pm

A little sanity checking goes a long way..

Code: Select all

if( $value >= 11 || $value >= 0 )
/* ------------------------^
   I'm guessing you want less-than-or-equal here,
   not greater-than-or-equal
*/

Insolence
New php-forum User
New php-forum User
Posts: 43
Joined: Tue Aug 19, 2003 1:27 pm
Contact:

Postby Insolence » Wed Aug 27, 2003 1:34 pm

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: Select all

      $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();
        }
       }

User avatar
swirlee
Moderator
Moderator
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Postby swirlee » Wed Aug 27, 2003 2:15 pm

Insolence wrote:

Code: Select all

$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: Select all

<?
   $a = 'swirlee';

   echo $a;
   /* output:
         swirlee
   */

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


See the difference? This code should work:

Code: Select all

<?
$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: Select all

<?
$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: Select all

<?
/* 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();
   }
}
?>

Insolence
New php-forum User
New php-forum User
Posts: 43
Joined: Tue Aug 19, 2003 1:27 pm
Contact:

Postby Insolence » Wed Aug 27, 2003 2:45 pm

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 :)

User avatar
tryton
New php-forum User
New php-forum User
Posts: 49
Joined: Wed Jul 09, 2003 8:25 am
Location: Joze
Contact:

Postby tryton » Wed Aug 27, 2003 7:28 pm

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.

Insolence
New php-forum User
New php-forum User
Posts: 43
Joined: Tue Aug 19, 2003 1:27 pm
Contact:

Postby Insolence » Wed Aug 27, 2003 8:22 pm

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 :(

User avatar
ruturajv
php-forum Super User
php-forum Super User
Posts: 1280
Joined: Sat Mar 22, 2003 9:42 am
Location: Mumbai, India
Contact:

Modular

Postby ruturajv » Wed Aug 27, 2003 9:36 pm

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


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 1 guest