0

I am trying to echo two different parts of HTML code based on the results from the if statement, which are again taken from the database. The number of results is not definite and it will vary based on the user input. It all works well except echoing the HTML. Here is the code:

<?php  
        $query = mysql_query("SELECT dt, soba1, soba2, soba3, soba4, soba5, soba6,     soba7, soba8, soba9, soba10 FROM calendar_table WHERE (dt BETWEEN '$dateFrom' AND '$dateTo')"); 
        while($row = mysql_fetch_assoc($query))
        {
            $date = $row['dt']; 
            $soba1 = $row['soba1'];
            $soba2 = $row['soba2'];
            $soba3 = $row['soba3'];
            $soba4 = $row['soba4'];
            $soba5 = $row['soba5'];
            $soba6 = $row['soba6'];
            $soba7 = $row['soba7'];
            $soba8 = $row['soba8'];
            $soba9 = $row['soba9'];
            $soba10 = $row['soba10'];

           echo " 
             <div class='column'> 
                <p class='dateBox'> $date </p>
                ". 
                    ( ($soba1 != NULL) ?
                      echo "<p class='status'> <input type='text' name='persons'> </p>"
                    :
                      echo "<p class='status'> $soba1 </p>"
                    ."  
                <p class='status'> $soba2 </p>
                <p class='status'> $soba3 </p>
                <p class='status'> $soba4 </p>
                <p class='status'> $soba5 </p>
                <p class='status'> $soba6 </p>
                <p class='status'> $soba7 </p>
                <p class='status'> $soba8 </p>
                <p class='status'> $soba9 </p>
                <p class='status'> $soba10 </p>
              </div>"; 
            }; 
     ?> 

Any suggestions on how this can be writen differently, or modified so that it works. THe error that i am getting is Parse error: syntax error, unexpected '=' in C:\xampp\htdocs\plazahotel\cro\confirm.php on line 137

1
  • 1
    You can't use echo within and echo.. Commented Apr 28, 2014 at 10:05

3 Answers 3

1

You can only use following syntax:

 echo ($variableIsTrue ? 'Output A' : 'Output B');

But you have already an echo (starts at the DIV). You Syntax is wrong. Remove the unwanted echo's and add an ):

        <p class='dateBox'> $date </p>
            ". 
                ( ($soba1 != NULL) ?
                 "<p class='status'> <input type='text' name='persons'> </p>"
                :
                  "<p class='status'> $soba1 </p>"
                ) //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ADD!!!!!!!!!!!!!!!!!!!!!!!!!!
                ."  
            <p class='status'> $soba2 </p>
Sign up to request clarification or add additional context in comments.

Comments

1

Remove the echo call from within the ternary. You also are missing a closing ), so it becomes:

       echo " 
         <div class='column'> 
            <p class='dateBox'> $date </p>
            ". 
                (($soba1 != NULL) ?
                  "<p class='status'> <input type='text' name='persons'> </p>"
                :
                  "<p class='status'> $soba1 </p>")
                ."  
            <p class='status'> $soba2 </p>
            <p class='status'> $soba3 </p>
            <p class='status'> $soba4 </p>
            <p class='status'> $soba5 </p>
            <p class='status'> $soba6 </p>
            <p class='status'> $soba7 </p>
            <p class='status'> $soba8 </p>
            <p class='status'> $soba9 </p>
            <p class='status'> $soba10 </p>
          </div>"; 

1 Comment

Thanks in a million. I've been killing myself two days with this, and it was so simple.
0

You cannot have nested echo, for the sake of easier reading, try:

<?php
echo "<div class='column'> 
      <p class='dateBox'> $date </p>";
if($soba1 != NULL){
      echo "<p class='status'> <input type='text' name='persons'> </p>";
}
else
{
      echo "<p class='status'> $soba1 </p> 
            <p class='status'> $soba2 </p>
            <p class='status'> $soba3 </p>
            <p class='status'> $soba4 </p>
            <p class='status'> $soba5 </p>
            <p class='status'> $soba6 </p>
            <p class='status'> $soba7 </p>
            <p class='status'> $soba8 </p>
            <p class='status'> $soba9 </p>
            <p class='status'> $soba10 </p>
          </div>";
} 

2 Comments

why do this? you can do it with ternary operator as MrCode shown. i.e. do it inline.
@LatheesanKanes Like I said, there are too many quotes and dots + more quotes with semi-colon makes it harder to read.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.