-1

I am nesting PHP in HTML, and I'm getting an "unexpected if" error. But I have no idea what's causing it. The are a lot of single and double quote combinations, so it's a bit confusing, but I think it should work. Can anyone see what the error is?

This is the line that's causing the error, and below that is the entire code:

  .if($index['status']=='Billable'){
                        $status1 = $index['status'];
                        $status2 = '';
                                                    }

This is the entire code block:

    <?php
    foreach($sessions as $index)
            {       
            echo "<div class='wrap'>
                    <div class='well session'> 

                            Clinic: <a class='clinic-link' title='Click to see clinic sessions'>".$index['org']."</a><br>

                            Trainer: ".$index['trainer']."<br>

                            Date: ".$index['session_date']."<br>

                            Duration: ".$index['duration']." hours<br>

                            Status: ".$index['status']."<br><br> 

                            Note: ".$index['session_note']."<br><br>

                            <a class='btn btn-lg btn-default edit' class='edit-button' onclick='openEdit(this)'>Edit</a>
                    </div>                 

                    <div class='well edit-form'>
                            <form action='".$_SERVER['PHP_SELF']."' method='POST'>
                                    <label for='session_edit_clinic'>Clinic:</label><br>
                                            <input type='text' name='session_edit_clinic' id='session_edit_clinic' value='".$index['org']."'><br><br>

                <label for='session_edit_trainer'>Trainer:</label><br>
                                            <input type='text' name='session_edit_trainer' id='session_edit_trainer' value='".$index['trainer']."'><br><br> 

                                    <label for='session_edit_date'>Date:</label><br> 
                                            <input type='date' name='session_edit_date' id='session_edit_date' value='".$index['session_date']."'><br><br>                                        

                                    <label for='session_edit_duration'>Duration:</label><br>
                                            <input type='text' name='session_edit_duration' id='session_edit_duration' value='".$index['duration']."'><br><br>

                                    <label for='session_edit_note'>Note:</label><br>
                                            <textarea  name='session_edit_note' id='session_edit_note' cols='56' rows='7'>".$index['session_note']." </textarea> <br><br>"


                    .if($index['status']=='Billable'){
                        $status1 = $index['status'];
                        $status2 = '';
                                                    }

                    else{
                        $status1 = '';
                                                    $status2 = $index['status'];
                                                    }."
                                                    <input type='radio' name='status' value='".$status1."'> Billable<br>
                            <input type='radio' name='status' value='".$status2."'> Not Billable<br>


                                    <div class='lower-div'>                                               
                                            <input type='submit' class='btn btn-lg btn-default edit session_edit_submit' name='session_edit_submit' id='session_edit_submit' value='Save Changes'>
                                            <a class='btn btn-lg btn-primary close-edit' class='close-edit' style='margin-left: 20px;' onclick='closeEdit(this)'>Close</a>
                                    </div>    
                            </form> 
                    </div> 
                    </div>                  

                    ";
            }

    ?>    
Argut
  • 1
  • 5
  • `echo`ing a massive string of HTML is just going to make your code hard to manage. Don't do that. Just drop out of php mode (with `?>`) and go back into PHP mode only when you need to (e.g. to echo variables). Don't forget to use `htmlspecialchars()` to convert your text to safe HTML. – Quentin Sep 27 '17 at 23:11

1 Answers1

2

You are trying to concatenate an if statement with a string.

"foo" . if (...) { ... } else { ... } . "$something_from_inside_the_if";

You can't do that.

Move the if to before you start building the string.

if (...) { ... } else { ... }
"foo" . "$something_from_inside_the_if";
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • There is no part of a string within the if statement. The double quotes close before the if statement and reopen after it (leaving and reentering HTML). What I thought could be the problem was the .s, but I removed them and am getting the same message. If my asessment is wrong, please explain where you see a string in the if statement as I may be missing that. Thank you – Argut Sep 27 '17 at 23:20
  • ok I see what you mean now. I'm putting an if statement in what I'm echoing with my PHP. I need to put this above everything. Thanks for pointing that out. – Argut Sep 27 '17 at 23:36