SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Wizard geiger's Avatar
    Join Date
    Jul 2001
    Posts
    2,459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    foreach $row[key] rename $key

    It it possible to write some sort of loop to simplify lists such as the following?
    PHP Code:
    $mid=            $row['mid'];
    $mbid=            $row['mbid'];
    $make=            $row['make'];
    $model=            $row['model']; 

  2. #2
    SitePoint Addict CWebguy's Avatar
    Join Date
    Mar 2009
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure what you are trying to say.

  3. #3
    SitePoint Wizard geiger's Avatar
    Join Date
    Jul 2001
    Posts
    2,459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    After a mySQL query, for instance, all my data is read into $row via mysql_fetch_array(). When I use the values later in the script, I'd like to be able to refer to simply the field name rather than $row['field'], so I've written out long lists transferring the values to corresponding variables.

    Is there some sort of foreach loop I can run to do this for the entire array without having to write out each variable individually?

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    foreach($row as $key => $value) {
      $
    $key $value;

    You should be careful with this, though, in case you overwrite another variable needed by your script. You might be better off prefixing the field names, like this:

    PHP Code:
    foreach($row as $key => $value) {
      
    $r_{$key} = $value;

    This would prefix all the variables with r_

  5. #5
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Sure, take a look at extract().

    Use it carefully though.

    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  6. #6
    SitePoint Wizard geiger's Avatar
    Join Date
    Jul 2001
    Posts
    2,459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the function. Could you please explain why using extract() on input variables is bad?

  7. #7
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    As SJH stated, in longer scripts, it would be far too easy for extract to overwrite an existing, trusted variable into an unchecked, unsanitised user defined variable.

    Havoc man, HAVOK!
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  8. #8
    SitePoint Wizard geiger's Avatar
    Join Date
    Jul 2001
    Posts
    2,459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, didn't see SJH's post. Sorry about that.
    Your foreach does the same thing as extract(), right?

    Thanks
    Last edited by geiger; Mar 12, 2009 at 15:35.

  9. #9
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    extract has options for how it should behave should a collision or invalid variable name be encountered.

  10. #10
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by geiger View Post
    Your foreach does the same thing as extract(), right?
    Pretty much yes, but I'd recommend the use of extract() instead of my original suggestion.

    I always try and make a point of remembering that this function exists but it always slips my mind when I need to use it, and consequently end up using a foreach loop. Maybe I should get it tattooed on my forearm.

  11. #11
    SitePoint Wizard geiger's Avatar
    Join Date
    Jul 2001
    Posts
    2,459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's useful Just to make sure, is there anything wrong with how I'm doing this?
    PHP Code:
    $row mysql_fetch_array$sql);
            
    extract$rowEXTR_OVERWRITE );
            
            
    $notes htmlentities$notes );
            
    $fueltype htmlentities$fueltype );
            
    $strategyhtmlentities$strategy );

    echo 
    "$cid$notes"

  12. #12
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    overwrite would be correct. It's a good idea to explicitly list your columns in your sql query(for more reasons than this). If you're using the star, eg SELECT *, then if you every change the table column names, or add new ones, now all of a sudden you have new variables floating around your script because of extract.

  13. #13
    SitePoint Wizard geiger's Avatar
    Join Date
    Jul 2001
    Posts
    2,459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, I never use *. Here's a sample of how I'm fetching data right now. I need to add parameterizing still, and also validate the data going in. Then—if I'm right—I'll be secure?
    PHP Code:
    $sql mysql_query(
        
    " SELECT
            calc_gallons,
            calc_mpg,
            cid,
            cpg,
            enddate,
            fueltype,
            in_gallons,
            in_miles,
            in_mpg,
            notes,
            strategy,
            tid
        FROM
            fe_tanks
        WHERE
            tid = 
    $tid ")
        or
            die( 
    mysql_error() );

    $row mysql_fetch_array$sql );
    extract$rowEXTR_OVERWRITE );

    $notes htmlentities$notes );
    $fueltype htmlentities$fueltype );
    $strategy htmlentities$strategy ); 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •