Jump to content
Pardon our ads (a necessary update) ×

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
https://forums.phpfreaks.com/topic/297904-how-to-count-backwards-in-a-loop/
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.

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.

Archived

This topic is now archived and is closed to further replies.



×
×
  • 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.