looping php code to speed up coding - part 2

Permalink
Well yesterday I thought I had it all figured out, but I guess I was wrong -

In my controller in the save() function I was coding in the posts ( and it was working ) like so -

//start question game 1 
         $event->q1g1_date = $dth->translate('q1g1_date');
           $event->q1g1ht = $data['q1g1ht'];
                      $event->q1g1at = $data['q1g1at'];
                        $event->q1g1htscore = $data['q1g1htscore'];
                      $event->q1g1atscore = $data['q1g1atscore'];
                    //start question 1 game 2 
         $event->q1g2_date = $dth->translate('q1g2_date');
           $event->q1g1ht = $data['q1g2ht'];
                      $event->q1g2at = $data['q1g2at'];
                        $event->q1g1htscore = $data['q1g2htscore'];
                      $event->q1g1atscore = $data['q1g2atscore'];
etc.....


After realizing that I would be typing this in for 50 questions -I decided that I would just loop through each block of code and just increment the numbers with each loop. After quite a few failed attempts I got help here from hutman

And tryed this
for ($x = 1; $x <= 5; $x++) {
       $varName = "q1g$x_date";   
   $event->$varName = $dth->translate('q1g'.$x.'_date');
       $varName = "q1g$xht";      
   $event->$varName = $data['q1g'.$x.'ht'];
       $varName = "q1g$xat";      
   $event->$varName = $data['q1g'.$x.'at'];
       $varName = "q1g$xhtscore"; 
   $event->$varName = $data['q1g'.$x.'htscore'];
       $varName = "q1g$xatscore"; 
   $event->$varName = $data['q2g'.$x.'atscore'];
}


Which I thought was working because there were no errors- and the Save successfully message came up. I was happy and continued to code out the whole thing.

Now - I realize that nothing is being saved ( in the loop ) - I think the $event->$varName = $data['q2g'.$x.'atscore']; is the part that isn't working - Maybe the $varName needs to be unique with each pass also. Which would mean that the $varName would somehow have to concentate with $x ( incremented number ) to give each variable a unique value.

If anyone could help that would be great -

Related question - when debugging this - is there a way to see the post variables in the browser when I hit the save button It would help if I could see what is being sent to the db.

INTcommunications
 
INTcommunications replied on at Permalink Reply
INTcommunications
UPDATE - figured it out

original code ( from last thread on this subject )

for ($x = 1; $x <= 5; $x++) {
       $varName = "q1g$xht";      
   $event->$varName = $data['q1g'.$x.'ht'];
       $varName = "q1g$xat";      
   $event->$varName = $data['q1g'.$x.'at'];
       $varName = "q1g$xhtscore"; 
   $event->$varName = $data['q1g'.$x.'htscore'];
       $varName = "q1g$xatscore"; 
   $event->$varName = $data['q1g'.$x.'atscore'];
          }


fixed it by inserting the $varname = with $x

for ($x = 1; $x <= 5; $x++) {
 $varName = "q1g".$x."_date";   
   $event->$varName = $dth->translate('q1g'.$x.'_date');
       $varName = "q1g".$x."ht";      
         $event->$varName = $data['q1g'.$x.'ht'];
       $varName = "q1g".$x."at";      
         $event->$varName = $data['q1g'.$x.'at'];
       $varName = "q1g".$x."htscore"; 
         $event->$varName = $data['q1g'.$x.'htscore'];
       $varName = "q1g".$x."atscore"; 
        $event->$varName = $data['q2g'.$x.'atscore'];
            }


This appears to work - so if someone needs to write the same blocks of code and just increment a number- this speeds things up considerably.


This is from a questionnaire that I needed to people fill in on a form ( this is the post part in the save(); function. Named the db fields so that they would sequence by number

i.e. q1g1_date is question 1 , game 1 date
q1g1ht ( question 1 game 1 home team )

there are about 50 questions in my app so this code ( even with a couple of days research and help from forum ) has sped up production - I have also looped form elements on view to go with this. This will be my production M.O. moving on.
exchangecore replied on at Permalink Best Answer Reply
exchangecore
maybe a slightly more robust way if you have a lot of fields. You could potentially assign each of these suffixes into sub arrays based on the type of assignment you want to use if you had more than one translate function too.

$varSuffixes = array('ht','at','htscore','atscore');
for ($x = 1; $x <= 5; $x++) {
    //special case for the translate
    $varName = 'q1g'.$x.'_date';
    $dth->translate($varname);
    foreach($varSuffixes AS $suffix)
    {
        $varName = 'q1g'.$x.$suffix;
        $event->$varName = $data[$varName];
    }
}


Or something like that.
INTcommunications replied on at Permalink Reply
INTcommunications
I really like it with the array though - best answer!