Help with php code

I’m added some codes to a custom template in Participants Database, a WordPress plugin and it’s giving me a critical error. I’m totally new to php, so I’m a bit clueless. Thanks for your help!

     while ( $this->have_fields() ) : $this->the_field();
        <tr class="<?php $this->field->print_element_class() ?>">
            <?php if($this->field-group==("One-time activities") && $this->field->value()==("Completed")) {
            $points_for_one_time_challenges = $points_for_one_time_challenges + 25; }
            <?php endif ?>
            <?php if($this->field_group==("Daily activities")) {
            $points_for_daily_challenges = $points_for_daily_challenges + ($this->field->value() * 10);}
            <?php endif ?>
<?php endwhile; // field loop  ?>

Then once outside of the field loop and the field group loop, I added:

      <th><h3><?php $this->echo "Points for Challenges:" ?></h3></th>
        <?php $this->print_points_for_one_time_challenges; ?>
        <?php $this->print_points_for_daily_challenges; ?>

Here, you are opening PHP tags while PHP is still open.

This does not look right. echo should not belong to an object.

What error?

You also seem to be mixing your if/endif clauses - you use an open-brace { at the start of your if() clause, but use endif; at the end. While I only have experience of using braces, the documentation suggests that the alternate structures cannot be mixed, so you must either use if() { ... } or if() : ... endif'. You also don’t have a ; after each endif, but I’m not sure if that matters when you’re closing PHP immediately afterwards.

But, the error message(s) will help with that.

Is this right? I moved the endif back to whether it belonged in the original template-- right above my added code. It didn’t have a ; after it.

            <?php if( $this->field-group==("One-time activities") && $this->field->value()=="Completed") {
            $points_for_one_time_challenges = $points_for_one_time_challenges + 25; } 

            <?php if( $this->field-group==("Daily activities)  {
            $points_for_daily_challenges = $points_for_daily_challenges + ($this->field->value() * 10); }

I also removed the echo in the print section. I don’t really need a heading for it.

Whoops! Just noticed a missing ) after the second if.

<?php if( $this->field-group==("Daily activities) ) { $points_for_daily_challenges = $points_for_daily_challenges + ($this->field->value() * 10); } ?>

As you only have a single statement inside each if(), technically you don’t need braces at all, but I tend to use them because it saves remembering to add them in if I add another statement.

What difference has that made to your error message(s), whatever it was / they were?

Thanks for your help!
Originally I have another condition in there to say if the value>0 but I decided I didn’t need it because if it’s 0, it can just add the 0 to the points.
Unfortunately, there’s no change to the critical error message.

Generally the wording of the error message is a big clue to the cause of the error.

Hyphens are not allowed in PHP variable names: ( $this->field-group )

 <?php if($this->field-group==("One-time activities") && $this->field->value()==("Completed")) {
            $points_for_one_time_challenges = $points_for_one_time_challenges + 25; }
            <?php endif ?>

Try this:

 <?php if($this->field_group==("One-time activities") && $this->field->value()==("Completed")) {
            $points_for_one_time_challenges = $points_for_one_time_challenges + 25; }
            <?php endif ?>

Unfortunately, there’s also no change to the fact that you haven’t told us what that critical error message says. I can’t help any further until you do that. We can try things all day, and then maybe your error message is complaining about something entirely unrelated.

Oh, that’s interesting, I hadn’t picked up on those being variable names. Come to that, spaces wouldn’t be ideal, either, if I’m reading it correctly.

In these two lines:

<?php $this->print_points_for_one_time_challenges; ?>
<?php $this->print_points_for_daily_challenges; ?>

shouldn’t there be () on the end to denote that these are functions? I haven’t done a massive amount with OO, and nothing with Wordpress, but I thought that the parentheses were required even when there are no parameters to be passed to the function. And presumably there are no syntax errors in the definition of those two functions.

But, we’re all guessing, when the critical error message would probably tell us (a) what the error is, and (b) what line it is on.

Yes, if they are functions (methods), but their absence would suggest they are object properties.

The You’ve encountered a critical error message is what I get if I try to link to the web page with the shortcode that points to this program. That’s all it says. The Participant Database developer said that means it found the program but there are syntax errors in it. I have the Participant Database debugger on, but it doesn’t act like it sees me linking to the program – nothing shows up in the log.

I got this idea of using field-group in the code above before the while loop for inside the group

but now I see that I can just refer to it as group, so the first line becomes:

            <?php if ( $this->group==("One-time activities") && $this->field->value()==("Completed")) {
            $points_for_one_time_challenges = $points_for_one_time_challenges + 25; } ?>

I’ll have to see if that clears the error. My system is down right now.
I’m not sure which space is.a problem. I see spaces throughout the codes that was originally there in the template, so I think that’s ok.

I see what you mean with the (). I see now that all the print ends with a (), so it should be:

     <?php $this->print_points_for_one_time_challenges(); ?>
     <?php $this->print_points_for_daily_challenges(); ?>

Seeing it in context, a snippet of PHP on its own, they may well be methods, as a property alone would do nothing.

Don’t you have PHP error reporting on? That should give you more info than this.

Well, thanks to everyone, the critical error message is gone.
However, it didn’t do anything I tried to get it to do. No printing of any points.

I just realized I should initiate the fields I’m calculating, so I’m putting them before the first while loop. There are 2 loops. The program loops through the field groups, and within each group is all the fields in that group.

 <?php $points_for_one_time_challenges = 0; ?>
 <?php $points_for_daily_challenges = 0; ?>
 <?php $total_points = 0; ?>

I’m assuming that once I calculate them, they would be in the database.

Just noticed something else.
I need $this in front of them.

<?php $this->points_for_one_time_challenges = 0; ?> 
<?php $this->points_for_daily_challenges = 0; ?> 
<?php $this->total_points = 0; ?>

Then later on, I don’t need the $ in front of the variables, only in front of $this. Right?

               <?php if( $this->group==("One-time activities") && $this->field->value()=="Completed") {
                points_for_one_time_challenges = points_for_one_time_challenges + 25; } 

Similarly, where I print,

          <?php $this->print_points_for_one_time_challenges(); ?>

You don’t have $this in front of them when you add values to them:

 $points_for_one_time_challenges = $points_for_one_time_challenges + 25; }

No, not right. You’ll get syntax errors on this line:

points_for_one_time_challenges = points_for_one_time_challenges + 25; }

What is in your definition of the print_points_for_one_time_challenges() function? You said it’s not printing the point, earlier.


A couple of useful PHP tips with examples:

// Beware: should not be set on live server
ini_set('display_errors', 'true’);

// your code

// display Objects arrays, variables, etc
echo '<pre>'; print_r($variable); echo '</pre>'; // pre adds line-feeds
die('Stopped processing here ==>  ' .__line__);

It’s not clear to us, as we don’t have the full code to see it in context.
But you will use $this to make something belong to the object of the current class you are working in, be that an object’s property (variable) or an object’s method (function).

Certainly, you should have error reporting on, or we are all fumbling in the dark.

Ok, I’ll add $this-> in front of the variables where I add values.
How do I add the print definition? I thought it’s automatic when you put print_ in front of the variable.

Thanks, John! I’ll add the error reporting code.