Auto Increment Array

Hey

Showing you what I am trying to achieve may be all that is needed, auto incrementing integer everytime user submits form and a new set of values are saved to my array...

[conditions] => Array
        (
            [complete] => Yes
            [used] => Yes
            [sets] => Array
                (
                    [1] => Array
                        (
                            [columnid] => 4
                            [oldvalue] => aa
                            [newvalue] => bb
                        )

                    [5] => Array
                        (
                            [columnid] => 4
                            [oldvalue] => aa
                            [newvalue] => bb
                        )

                    [6] => Array
                        (
                            [columnid] => 4
                            [oldvalue] => aa
                            [newvalue] => bb
                        )

                )

        )

Above the values 1,5,6 are to continue increase, without missing 2,3,4 but that happened during testing and keeps happening. What is the best approach for this?

Thanks all
Ryan

if( !isset( $_POST['eci_csvcolumns_submit_main'] ) ){ $error = 1; }
			if( !isset( $_POST['eci_oldvalue_submit'] ) ){  $error = 1;  }
			if( !isset( $_POST['eci_newvalue_submit'] ) ){  $error = 1; }
			
			if( $error != 1)
			{
				$csv['conditions']['used'] = 'Yes';
				
				if( isset( $csv['conditions']['sets'] ) )
				{
					$counter = 0;
					
					foreach( $csv['conditions']['sets'] as $set ){ ++$counter; }
					
					echo $counter .'=counter';
					
					$id = $counter + 1;// we get the next id for a set					
					
					$csv['conditions']['sets'][$id]['columnid'] = $_POST['eci_csvcolumns_submit_main'];
					$csv['conditions']['sets'][$id]['oldvalue'] = $_POST['eci_oldvalue_submit'];
					$csv['conditions']['sets'][$id]['newvalue'] = $_POST['eci_newvalue_submit'];
				}
				else
				{					
					$csv['conditions']['sets'][0]['columnid'] = $_POST['eci_csvcolumns_submit_main'];
					$csv['conditions']['sets'][0]['oldvalue'] = $_POST['eci_oldvalue_submit'];
					$csv['conditions']['sets'][0]['newvalue'] = $_POST['eci_newvalue_submit'];
				}
				
				update_option(eci_getprofilename( $currentfile ),$csv);
				eci_message( 'Success - you have setup a Match And Change condition' );	
			}

Open in new window

LVL 2
Ryan BayneWordPress DeveloperAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
kumaranmcaConnect With a Mentor Commented:
Hi  TycoonMillion,

Try the following code...to resolve your problem...Foreach using $key its resolve your problem like below...


                                        foreach( $csv['conditions']['sets'] as $key=>$set ){
                                        $csv['conditions']['sets'][$key]['columnid'] = $_POST['eci_csvcolumns_submit_main'];
                                        $csv['conditions']['sets'][$key]['oldvalue'] = $_POST['eci_oldvalue_submit'];
                                        $csv['conditions']['sets'][$key]['newvalue'] = $_POST['eci_newvalue_submit'];
                                                      
                                                            //++$counter;
                                                            }

<?php
if( !isset( $_POST['eci_csvcolumns_submit_main'] ) ){ $error = 1; }
                        if( !isset( $_POST['eci_oldvalue_submit'] ) ){  $error = 1;  }
                        if( !isset( $_POST['eci_newvalue_submit'] ) ){  $error = 1; }
                        
                        if( $error != 1)
                        {
                                $csv['conditions']['used'] = 'Yes';

//Print your array ita comes....your expecting array
print '<pre>';
print_r($csv['conditions']['sets']);
print '</pre>';
                                
                                if( isset( $csv['conditions']['sets'] ) )
                                {
                                        //$counter = 0;
                                        
                                        foreach( $csv['conditions']['sets'] as $key=>$set ){ 
                                        $csv['conditions']['sets'][$key]['columnid'] = $_POST['eci_csvcolumns_submit_main'];
                                        $csv['conditions']['sets'][$key]['oldvalue'] = $_POST['eci_oldvalue_submit'];
                                        $csv['conditions']['sets'][$key]['newvalue'] = $_POST['eci_newvalue_submit'];
									
										//++$counter; 
										}
                                        
                                        //echo $counter .'=counter';
                                        
                                        //$id = $counter + 1;// we get the next id for a set                                      
                                        
                                }
                                else
                                {                                       
                                        $csv['conditions']['sets'][0]['columnid'] = $_POST['eci_csvcolumns_submit_main'];
                                        $csv['conditions']['sets'][0]['oldvalue'] = $_POST['eci_oldvalue_submit'];
                                        $csv['conditions']['sets'][0]['newvalue'] = $_POST['eci_newvalue_submit'];
                                }
                                
                                update_option(eci_getprofilename( $currentfile ),$csv);
                                eci_message( 'Success - you have setup a Match And Change condition' ); 
                        }
?>

Open in new window

0
 
Avinash ZalaWeb ExpertCommented:
From your code

for below lines:
$csv['conditions']['sets'][$id]['columnid'] = $_POST['eci_csvcolumns_submit_main'];
$csv['conditions']['sets'][$id]['oldvalue'] = $_POST['eci_oldvalue_submit'];
$csv['conditions']['sets'][$id]['newvalue'] = $_POST['eci_newvalue_submit'];

Value if $id is not set proper that's why you have a problem for that


Hope this helps.
Addy
0
 
hieloCommented:
somewhere (not shown in your posted code) you are currently have:
print_r( $csv );

to get those arrays in sequential order, IMMEDIATELY before that line put:
//save copy of sets
$tmp=$csv['conditions']['sets'];

//remove them all
unset($csv['conditions']['sets']);

//now get a sequentially index set from the previously saved copy
$csv['conditions']['sets'] = array_values($tmp);

//verify output:
print_r($csv);
0
 
Ryan BayneWordPress DeveloperAuthor Commented:
This is the method I'm looking at right now, never used this before.

foreach( $csv['conditions']['sets'] as $key=>$set )

However can someone please tell me where to set the value of $key. The array result using it is this...

[] => Array

hielo I'm not sure about your code but I'll need to read into array_values.

Thanks.
0
 
hieloCommented:
when you use this:
foreach( $csv['conditions']['sets'] as $key=>$set )
$key will be "auto-set" to the corresponding index for the array element on each iteration. So, if your array was:
Array( "greeting"=>"hello", "farewell"=>"goodbye", "3"=>"Odd Number")
On
1st iteration, $key=greeting
2nd iteration, $key=farewell
3rd iteration, $key=3

Notice that the array keys do NOT even have to be numeric. If you want them in numeric order, then initialize a numeric counter outside the foreach:
$counter=-1;
foreach( $csv['conditions']['sets'] as $key=>$set )
{
  ++$counter;//increment it immediately

  ...

  //then to store the values use $counter instead
 $csv['conditions']['sets'][$counter]['columnid'] = $_POST['eci_csvcolumns_submit_main'];
 $csv['conditions']['sets'][$counter]['oldvalue'] = $_POST['eci_oldvalue_submit'];
 $csv['conditions']['sets'][$counter]['newvalue'] = $_POST['eci_newvalue_submit'];

}
0
All Courses

From novice to tech pro — start learning today.