Jump to content

how to count backwards in a loop


shaddf

Recommended Posts

iam trying to get data from many input fields in a table based on their column position.they are also named based on their column position..
if in the next column the input is empty, i should be able to track the previously full input and place its value  instead. but so far it is always returning empty .
where  am going wrong  with my code below

 

 

  $d=0;
$arr=array();
   while($d<$_POST['total']){
    $d++;

            if($_POST['outcome'.$d]!==""){
                $out=$_POST['outcome'.$d];
                $act=$_POST['activity'.$d];
                $ind=$_POST['indicator'.$d];
                $tar=$_POST['target'.$d];
            }else if($_POST['outcome'.$d]==""&& $_POST['activity'.$d]!==""){//$pos=$d
                #get the previously  filled input
                for($fulo=$d;$fulo>0;$fulo--){
                    if($_POST['outcome'.$fulo] !==""){
                        $out=$_POST['outcome'.$fulo];break;
                    }else{continue;}
                }
                $act=$_POST['activity'.$d];
                $ind=$_POST['indicator'.$d];
                $tar=$_POST['target'.$d];
            }
    //}
$arr[]=  array($out,$act,$ind,$tar );
    }
 
Link to comment
Share on other sites

i'm not sure what overall goal you are trying to accomplish based on your description and program logic (showing us what result you expect and what result you are actually getting from your code would be helpful), but making a series of numbered form field names - outcome1, outcome2, ... is not the easiest way of handling sets of form field data. use an array name instead - name='outcome[]' you can either leave the array index empty, which will result in sequentiality numbered indexes in the submitted array, or you can supply your own associative or numerical array indexes to tie each form field to specific meaning data on the server.

Edited by mac_gyver
Link to comment
Share on other sites

Not tested, but in the last three lines in coded section $d should ( could ) be $fulo .

                for($fulo=$d;$fulo>0;$fulo--){
                    if($_POST['outcome'.$fulo] !==""){
                        $out=$_POST['outcome'.$fulo];break;
                    }else{continue;}
                }
                $act=$_POST['activity'.$d];
                $ind=$_POST['indicator'.$d];
                $tar=$_POST['target'.$d];

and the else{continue;) is most probably not needed.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.