foreach loop variables.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cheesecaker
    New Member
    • Feb 2007
    • 66

    foreach loop variables.

    How would one force variables within a foreach loop take effect globally, even outside the loop? I know foreach variables are usually local to the loop, but I'd like to work around that. Thanks in advance.
  • kovik
    Recognized Expert Top Contributor
    • Jun 2007
    • 1044

    #2
    Originally posted by cheesecaker
    How would one force variables within a foreach loop take effect globally, even outside the loop? I know foreach variables are usually local to the loop, but I'd like to work around that. Thanks in advance.
    That depends on what you want to alter. The point of the "as $foo" is to allow you to avoid altering data. But if you use the original variable name, you can alter it.

    Example:

    [code=php]foreach($items as $id => $item)
    {
    $item['qty'] += $id; // Doesn't affect anything outside of the loop
    $items[$id]['qty'] += $id; // Affects the original $items array
    }[/code]

    Give an example of what you want to do.

    Comment

    • mwasif
      Recognized Expert Contributor
      • Jul 2006
      • 802

      #3
      Do you want to use array keys as variables? To achieve this, you can use variable variable method

      [PHP]$items = array("var1"=>" value1", "var2"=>"value2 ");

      foreach($items as $k=>$v)
      {
      $$k = $v;
      }

      echo $var1;[/PHP]

      Now you can use array keys as variables.

      Comment

      • cheesecaker
        New Member
        • Feb 2007
        • 66

        #4
        Okay, well, what I'm really trying to do is apply mysqli_real_esc ape_string() to each value in an array, without having to do it manually. But I don't think this is the right way to go about it now.

        Comment

        • kovik
          Recognized Expert Top Contributor
          • Jun 2007
          • 1044

          #5
          Originally posted by cheesecaker
          Okay, well, what I'm really trying to do is apply mysqli_real_esc ape_string() to each value in an array, without having to do it manually. But I don't think this is the right way to go about it now.
          You only need to escape string as they are going into the database. The only time you should call that function is while writing the SQL query.

          Comment

          • cheesecaker
            New Member
            • Feb 2007
            • 66

            #6
            Originally posted by volectricity
            You only need to escape string as they are going into the database. The only time you should call that function is while writing the SQL query.
            Yes, but I've got my $_POST array of user input, which I need to place inside the database. I'd prefer not to do this, though.
            [PHP]$query = "INSERT INTO database (field1,field2, field3) VALUES ('".mysql_real_ escape_string($ _POST['var1'])."','".mysql_r eal_escape_stri ng($_POST['var2'])."','".mysql_r eal_escape_stri ng($_POST['var3'])."')";[/PHP]

            Comment

            • yellowness
              New Member
              • Jul 2006
              • 1

              #7
              To do long queries like that, I create an array at the beginning of my save function of all the variables I'm trying to put into a db. So if my form had the fields, firstname, lastname, phone, and favorite color:
              $fields = ARRAY("lastname ","phone","favo rite_color");

              Then, I build my query string like this:
              Code:
              $fields = ARRAY("lastname","phone","favorite_color");
              $query = sprintf("INSERT INTO table SET firstname='%s'",mysql_real_escape_string($_REQUEST['firstname']));
              
              foreach ($fields as $field){
                 $query .= sprintf(",$field='%s'",mysql_real_escape_string($_REQUEST[$field]));
              }
              Obviously, your db column names have to match your form names. Also, one of the fields has to be hard coded into the first part of the query, so that you can have the comma separation be correct (so you don't end on a comma).

              One benefit of this is that if you ever add a field in your form, all you have to do is add it to the $fields array, and it will automatically be dealt with.

              Hopefully that helps :)

              Comment

              Working...