Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,368
    Thanks
    262
    Thanked 32 Times in 31 Posts

    foreach inside foreach

    Hi i decided to go ahead and add changing items in my config settings dynamically so i wrote the page from scratch and im sooooo close but i just have one little issue lol...

    The problem is that i need to loop two dif arrays and process each time in the loop and i cant stop the inner foreach form completing all at once while the other one is still on the first key.

    Im sure that is what is happening because when i do the form submit both values end up the same value.

    first here is what the form looks like basically just the text layout.

    Records Per Page 15
    Session Timeout 20


    now here is what the post array look like. They are built in seperate input tags.

    PHP Code:

    Array
    (
        [
    configname] => Array
            (
                [
    0] => Records Per Page
                
    [1] => Session Timeout
            
    )

        [
    configvalue] => Array
            (
                [
    0] => 15
                
    [1] => 20
            
    )

        [
    csave] => csaved
        
    [submit] => Submit


    Thern here is the process.


    PHP Code:
    // If request is made for config Change.
    if(isset($_POST['csave']))  //this just means the form was sent
    {
      
    //set post arrays into own array names
      
    $name_array $_POST['configname'];
      
    $val_array $_POST['configvalue'];

      
    //now see how many elements in name array
      
    $elenum count($name_array); //used for i loop max

      //while both foreach are open update per value 
       
    for ($i 1$i <= $elenum$i++) 
       {

         foreach(
    $name_array as $nkey=>$nvalue)
         {
          
    $chname clearInput($nvalue);
      
          foreach(
    $val_array as $vkey=>$vvalue)
          {
          
    $chvalue intval($vvalue);

           
        
    $update="UPDATE config SET cvalue='$chvalue' WHERE cname='$chname'";
    $resu mysqli_query($myconnect$update);

        }
    //close foreach  

       
    }//close foreach

    }//close forloop 

    here is the db layout

    Code:
    ID  cname                  cvalue
    1    Records Per Page     5
    2    Session Timeout       60
    I just added the for loop a few min ago as sort of a last resort to get this to work, but i thought it was time to ask for some help with this. I was sort of thinking maybe i just need to move my forloop to inside the second foreach

    yes i just confirmed that the way its looping, watever the session timeout is set to on the form is what the rec per page is set to as well.

    Thanks.
    Last edited by durangod; 02-17-2013 at 05:10 AM.

  • #2
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,368
    Thanks
    262
    Thanked 32 Times in 31 Posts
    let me try this i just had a brain storm lol we will see


    PHP Code:

     
       
    for ($i 0$i <= $elenum$i++) 
       {
       
    $update="UPDATE config SET cvalue='$val_array[$i]' WHERE cname='$name_array[$i]'";
       
    $resu mysqli_query($myconnect$update);
       } 


    YEP THAT DID IT WHOOOO HOOOO !
    Last edited by durangod; 02-17-2013 at 02:55 AM.


  •  

    Posting Permissions

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