Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-17-2013, 02:11 AM   PM User | #1
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
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..
durangod is offline   Reply With Quote
Old 02-17-2013, 02:49 AM   PM User | #2
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
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..
durangod is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:55 AM.


Advertisement
Log in to turn off these ads.