alphy
alphy

Reputation: 291

Create a for loop that is consistent in php

I Have a for loop that i have been trying to work with to achieve a given goal, The loop should increment only the Y parameter for every time it runs, when i do a dry code for the same thing it works but if i try a loop everything becomes misaligned

Here is the hard coded sample

         //   for($s=0; $s<count($standards);$s++){

            $pdf->SetXY(32, 132);
            $pdf->Write(1, $standards[0]->name);

            $pdf->SetXY(106, 132);
            $pdf->Write(1, $standards[0]->rs_code);

            $pdf->SetXY(153, 132);
            $pdf->Write(1, round($standards[0]->potency,2));

             $pdf->SetXY(32, 139);
            $pdf->Write(1, $standards[1]->name);

            $pdf->SetXY(106, 139);
            $pdf->Write(1, $standards[1]->rs_code);

            $pdf->SetXY(153, 139);
            $pdf->Write(1, round($standards[1]->potency,2));

           // }

Now the dynamic for loop, I want that if the first Y values for the three XYs is 132, on the next run it should be 132+7, and continue incrementing like that for all loop runs, the below is returning data but misaligned as compared to the static one above

         $ya=(int)132;

           for($s=0; $s<count($standards);$s++){

            $pdf->SetXY(32, $ya+=7);
            $pdf->Write(1, $standards[$s]->name);

            $pdf->SetXY(106, $ya+=7);
            $pdf->Write(1, $standards[$s]->rs_code);

            $pdf->SetXY(153, $ya+=7);
            $pdf->Write(1, round($standards[$s]->potency,2));

             $pdf->SetXY(32, $ya+=7);
            $pdf->Write(1, $standards[$s]->name);

            $pdf->SetXY(106, $ya+=7);
            $pdf->Write(1, $standards[$s]->rs_code);

            $pdf->SetXY(153, $ya+=7);
            $pdf->Write(1, round($standards[$s]->potency,2));

            }

the result should be something like

 A      B     C
 D      E     F

and not

  A 
           B    
                  C

  D     
           E   
                   F

Upvotes: 2

Views: 46

Answers (1)

Abhishek Patel
Abhishek Patel

Reputation: 814

Hope this helps. You need to increment it just once. That too after printing first three statements.

       $ya=(int)132;

       for($s=0; $s<count($standards);$s++){

        $pdf->SetXY(32, $ya);
        $pdf->Write(1, $standards[$s]->name);

        $pdf->SetXY(106, $ya);
        $pdf->Write(1, $standards[$s]->rs_code);

        $pdf->SetXY(153, $ya);
        $pdf->Write(1, round($standards[$s]->potency,2));

         $pdf->SetXY(32, $ya+=7);
        $pdf->Write(1, $standards[$s]->name);

        $pdf->SetXY(106, $ya);
        $pdf->Write(1, $standards[$s]->rs_code);

        $pdf->SetXY(153, $ya);
        $pdf->Write(1, round($standards[$s]->potency,2));

        }

Upvotes: 1

Related Questions