0

I'm trying to print some results from a query, showing whether a user is a member or not. I am trying to place an IF statement within the loop, so that if the user is a Member (Member = 1), then the checkbox will be selected when the page loads, showing that the user is a member. Basically if they are a member, then "checked" is added to the input html so that the check box has a check mark in it when the page loads.

I keep getting this error:

"Parse error: syntax error, unexpected T_IF in /home/content/result.php on line 63 "

I am having trouble figuring out why my IF statement isn't working.. is it just syntax or is it not possible to have it here within the loop? Racking my brains here, I bet it's a simple answer but need help from my SO community. Thank you.

while ($results = $query->fetch()) {
           echo '<tr>',
           '<td class="text-left"><input type="checkbox" name="member"', 
          if($results['Member'] == '1') {echo "checked";},'></td>',
           '</tr>'
          ;   
Daniel C
  • 607
  • 2
  • 8
  • 20
  • You could have isolated the problem even more by just trying a simple echo statement, e.g. `echo "test" , if(true){echo "xy"}, "test";`. I mean you got the line number so you know it has something to do with the `if statement` and `echo`. *Google Pro Tip* just google abstract words which you have, e.g. `"PHP if inside echo"`. First result, your answer! – Rizier123 Jun 01 '16 at 21:01
  • I saw that answer but it doesn't help me. That's why I am asking. I did test it outside of the loop and it works.. so there is something going on inside loop. – Daniel C Jun 01 '16 at 21:04
  • What code did you test outside the loop? – Rizier123 Jun 01 '16 at 21:05
  • Show what you tried. – Rizier123 Jun 01 '16 at 21:06
  • so syntax is OK on its own but somehow not OK inside the while loop. Hence my question. – Daniel C Jun 01 '16 at 21:06
  • if($results['Member'] == '1') {echo "checked";} – Daniel C Jun 01 '16 at 21:06
  • Okay here lies the error :) So you took out the if statement and saw that it works. Now you should have took out more code to see if it still works. Then you would have seen that the if with the echo doesn't work. So to get it to work you can use a ternary and concatenate it inside the if statement. Example: `echo "xy" . (condition ? "true" : "false") . "xy";` – Rizier123 Jun 01 '16 at 21:08
  • OK but needing help on syntax with my statement. It's not as simple as what you wrote. I am new to ternary. Other answer doesn't help me. – Daniel C Jun 01 '16 at 21:10
  • So where are you stuck? How does your new code looks like? – Rizier123 Jun 01 '16 at 21:19
  • I don't understand why you are asking what the new code looks like. I am asking a question on SO, and just a minute or two after I posted, you flagged it as a duplicate (which it is not). Had you not flagged it, I may have found a useful solution from a member of the SO community. Instead, I'm now in SO purgatory. – Daniel C Jun 02 '16 at 01:00
  • The answer is in the duplicate. And if you don't show where you are stuck now nobody can help you! – Rizier123 Jun 02 '16 at 01:01
  • No, the answer is not in the duplicate. And, if you didn't mark it as a duplicate, I may have found an answer.. instead I have your comments suggesting vague answers, and no answer. So much for SO. :( – Daniel C Jun 02 '16 at 01:04
  • Ternary operators are for if/else statements. My statement is only an if statement. – Daniel C Jun 02 '16 at 01:18
  • OK here is the solution, instead of the if clause I write: ,$action = (empty($results['Member']) ? "" : "checked"), – Daniel C Jun 02 '16 at 01:27

0 Answers0