An array problem...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Xephon
Forum Newbie
Posts: 20
Joined: Sat May 15, 2004 10:58 pm

An array problem...

Post by Xephon »

Ok can some one please explain this section of code from phpfreak's tutorial for me:

Code: Select all

while($row = mysql_fetch_array($sql)){ 
    foreach( $row AS $key => $val ){ 
        $$key = stripslashes( $val ); 
    }
2 things im having a problem uderstanding are:
$row AS $key => $val - what is it doing here? i never really grasped the whole $key => $val thing, and what does $row AS mean?
and
$$key = stripslashes( $val ); - why are there 2 $'s and why is he assigning $val to $key?


the file it comes from is this:

Code: Select all

<? 
/* Check User Script */ 
session_start();  // Start Session 

include 'db.php'; 
// Conver to simple variables 
$username = $_POST&#1111;'username']; 
$password = $_POST&#1111;'password']; 

if((!$username) || (!$password))&#123; 
    echo "Please enter ALL of the information! <br />"; 
    include 'login_form.html'; 
    exit(); 
&#125; 

// Convert password to md5 hash 
$password = md5($password); 

// check if the user info validates the db 
$sql = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password' AND activated='1'"); 
$login_check = mysql_num_rows($sql); 

if($login_check > 0)&#123; 
    while($row = mysql_fetch_array($sql))&#123; 
    foreach( $row AS $key => $val )&#123; 
        $$key = stripslashes( $val ); 
    &#125; 
        // Register some session variables! 
        session_register('first_name'); 
        $_SESSION&#1111;'first_name'] = $first_name; 
        session_register('last_name'); 
        $_SESSION&#1111;'last_name'] = $last_name; 
        session_register('email_address'); 
        $_SESSION&#1111;'email_address'] = $email_address; 
        session_register('special_user'); 
        $_SESSION&#1111;'user_level'] = $user_level; 
         
        mysql_query("UPDATE users SET last_login=now() WHERE userid='$userid'"); 
         
        header("Location: login_success.php"); 
    &#125; 
&#125; else &#123; 
    echo "You could not be logged in! Either the username and password do not match or you have not validated your membership!<br /> 
    Please try again!<br />"; 
    include 'login_form.html'; 
&#125; 
?>
and the link to the tutorial im talking about is this:
http://www.phpfreaks.com/print.php?cmd=tutorial&tut_id=40

thx in advance.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Re: An array problem...

Post by feyd »

Xephon wrote:$row AS $key => $val - what is it doing here? i never really grasped the whole $key => $val thing, and what does $row AS mean?
AS is part of the foreach() command, the variable to it's left is the array you wish to iterate through. $key => $val refers to the associative breakdown of each element in the array $row. It functions like this: $row[$key] = $val;
$$key = stripslashes( $val ); - why are there 2 $'s and why is he assigning $val to $key?
The double $ here is creating a variable with the name contained in $key. Say for instance,

Code: Select all

$key = "name";
// then we do:
$$key = "feyd";
// it's as if we did
$name = "feyd";
that clear?
Post Reply