looping php code to speed up coding
Permalink 1 user found helpful
Hi -
I am finding that the project I am working on right now - I have a lot of code that I am repeating over and over with only a number increasing with each block. I am trying to use a for statement to loop over the code and increase a variable with each loop. Firstly, I don't know if I am doing this correctly ( placing the variables ) The actual loop works as that is easy to debug. But what I want to do is stick blocks of code into my controller.
In the code above I am trying to increment the $x variable ( replace with number )
Not sure if the syntax is correct or how to debug it. If I try to print it out - it takes the code literally and shows nothing.
I realize that this is probably a PHP thing as I have explained before I am new to Concrete and PHP but have a lot of years of coldfusion programming. i know what I want code to do but not quite sure which way to attack it.
Thanks
I am finding that the project I am working on right now - I have a lot of code that I am repeating over and over with only a number increasing with each block. I am trying to use a for statement to loop over the code and increase a variable with each loop. Firstly, I don't know if I am doing this correctly ( placing the variables ) The actual loop works as that is easy to debug. But what I want to do is stick blocks of code into my controller.
// loop data for($x = 1; $x <= 5; $x++){ $event->q1g$x_date = $dth->translate('q1g$x_date'); $event->q1g$xht = $data['q1g$xht']; $event->q1g$xat = $data['q1g$xat']; $event->q1g$xhtscore = $data['q1g$xhtscore']; $event->q1g$xatscore = $data['q1g$xatscore']; }
In the code above I am trying to increment the $x variable ( replace with number )
Not sure if the syntax is correct or how to debug it. If I try to print it out - it takes the code literally and shows nothing.
I realize that this is probably a PHP thing as I have explained before I am new to Concrete and PHP but have a lot of years of coldfusion programming. i know what I want code to do but not quite sure which way to attack it.
Thanks
I am just trying to keep from typing the same block of code over and over and just changing the number in the ( db.field )
I can see on my first post that I didn't escape the ' i.e.
'q1g'.$x.'ht' etc.
I am trying to get this in ( five blocks pf code ) into 5 lines of code by switching out the number ( $x) - I have already done it long hand - I want to learn this so if I had 25 blocks or a 100 blocks I could do more quickly
Here is what I have now that works ( long way )
This how I want to do it ( this doesn't work ) -
In the code above $data = $post
Thanks
I can see on my first post that I didn't escape the ' i.e.
'q1g'.$x.'ht' etc.
I am trying to get this in ( five blocks pf code ) into 5 lines of code by switching out the number ( $x) - I have already done it long hand - I want to learn this so if I had 25 blocks or a 100 blocks I could do more quickly
Here is what I have now that works ( long way )
//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']; //start question 1 game 3 $event->q1g3_date = $dth->translate('q1g3_date'); $event->q1g3ht = $data['q1g3ht'];
Viewing 15 lines of 30 lines. View entire code block.
This how I want to do it ( this doesn't work ) -
// loop data for($x = 1; $x <= 5; $x++){ $event->q2g$x_date = $dth->translate('q2g'.$x.'_date'); $event->q2g$xht = $data['q2g'.$x.'ht']; $event->q2g$xat = $data['q2g'.$x.'at']; $event->q2g$xhtscore = $data['q2g'.$x.'htscore']; $event->q2g$xatscore = $data['q2g'.$x.'atscore']; }
In the code above $data = $post
Thanks
I believe you can achieve this by doing
One thing to note with this code is that you can put variables inside of a double quote string (like the $varName definitions) and they will get evaluated, but if you use single quotes you have to do it like the $data piece.
This should work, if I'm reading this correctlyhttp://www.php.net/manual/en/language.variables.variable.php...
for ($x = 1; $x <= 5; $x++) { $varName = "q2g$x_date"; $event->$varName = $dth->translate('q2g'.$x.'_date'); $varName = "q2g$xht"; $event->$varName = $data['q2g'.$x.'ht']; $varName = "q2g$xat"; $event->$varName = $data['q2g'.$x.'at']; $varName = "q2g$xhtscore"; $event->$varName = $data['q2g'.$x.'htscore']; $varName = "q2g$xatscore"; $event->$varName = $data['q2g'.$x.'atscore']; }
One thing to note with this code is that you can put variables inside of a double quote string (like the $varName definitions) and they will get evaluated, but if you use single quotes you have to do it like the $data piece.
This should work, if I'm reading this correctlyhttp://www.php.net/manual/en/language.variables.variable.php...
That works!
Thanks so much, now I have to do pretty much the same thing on the view page. It is a mix of divs and form helper inputs ( php + html ) - but I think it may be easier.
This has saved me a ton of time -> foward.
I hope this helps others. I worked in an office once and a girl was using excel and a calculator. She was inputting costs in a column and then adding them up. Kind of deflated her a little when I went over and clicked the SUM icon.
I have always said that if it seems like there should be a better and more efficient way of doing something, there probably is. Just need to find it - thanks again.
Thanks so much, now I have to do pretty much the same thing on the view page. It is a mix of divs and form helper inputs ( php + html ) - but I think it may be easier.
This has saved me a ton of time -> foward.
I hope this helps others. I worked in an office once and a girl was using excel and a calculator. She was inputting costs in a column and then adding them up. Kind of deflated her a little when I went over and clicked the SUM icon.
I have always said that if it seems like there should be a better and more efficient way of doing something, there probably is. Just need to find it - thanks again.
Okay the form part wasn't as straight forward as I anticipated. I can get the loop going on everything but the control form helpers i.e.
without loop
then with a loop
I get a error at the $dth->date('q2g.'$i.'_date', $data['qig'.$i.'_date'],array('style' => 'width:50px;'))
tried with double quotes, without quotes - with form->text - it works on the label - this is really starting to bug me.
pulling hair out.
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in /Users/anyone/Dropbox/DevSites/BlankDev/c5Dev/packages/predictions/single_pages/dashboard/predictions/add_predictions.php on line 345
Tried -
without loop
div class="row-fluid" style="border-bottom:1px solid #ccc;padding-bottom:10px;"> <div class="span1"> 4 -</div> <div class="span2"> <label class="control-label" for="q1g4_date">Game date</label> <div class="controls"> <?php echo $dth->date('q1g4_date',$data['q1g4_date'],array('style' => 'width:50px;')) ?> </div> </div> </div>
then with a loop
<?php for ($i = 1; $i <= 5; $i++) { echo "<div class='row-fluid' style='border-bottom:1px solid #ccc;padding-bottom:10px;'> <div class='span1'> loop- $i -</div> <label class='control-label' for='q2g".$i."_date'>Game date</label> <div class='controls'> $dth->date('q2g.'$i.'_date', $data['qig'.$i.'_date'],array('style' => 'width:50px;')) </div> </div> "; } ?>
I get a error at the $dth->date('q2g.'$i.'_date', $data['qig'.$i.'_date'],array('style' => 'width:50px;'))
tried with double quotes, without quotes - with form->text - it works on the label - this is really starting to bug me.
pulling hair out.
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in /Users/anyone/Dropbox/DevSites/BlankDev/c5Dev/packages/predictions/single_pages/dashboard/predictions/add_predictions.php on line 345
Tried -
Try it like this
<?php for ($i = 1; $i <= 5; $i++) { ?> <div class='row-fluid' style='border-bottom:1px solid #ccc;padding-bottom:10px;'> <div class='span1'> loop- <?php echo $i; ?> -</div> <label class='control-label' for='q2g<?php echo $i; ?>_date'>Game date</label> <div class='controls'> <?php echo $dth->date('q2g'.$i.'_date', $data['qig'.$i.'_date'], array('style' => 'width:50px;')); ?> </div> </div> <?php } ?>
You are a genius!
That works great. Wish I could double mark this one.
Now I completely get it. ( I hope )
This should be a how-to because it is a major time saver.
That works great. Wish I could double mark this one.
Now I completely get it. ( I hope )
This should be a how-to because it is a major time saver.
I'm not sure if I 100% understand what you are trying to do, but it looks like you have data coming through (not sure from where), I am assuming that you have the data coming in correctly and are just trying to process it.
What I would suggest is rather than using an object just create an array so it would look like this.
In order to debug this to see what you are getting you can either do
Again, I'm not sure exactly what you are trying to do with this information so I might have it all wrong, but maybe something here will help you out.