1

I have created a table everything works fine expect when I try to use a if statement in mine table. Im not too sure how can I fix that tried some research but unable to find answer. echoing an object by itself works but if there is a statement or some kind of condition for some reason the table doesnt like it.

if(!mysqli_query($dbConn,$sql))
 {
echo 'Not Inserted';
}

else {
   echo "
   <table class='table'>
   <tr>
   <th>Name:</th>
   <th>$forename</th>
   </tr>

   <tr>
   <th>Surname:</th>
   <th>$surname</th>
   </tr>

   <tr>
   <th>Your email:</th>
   <th>$email</th>
   </tr>

   <tr>
   <th>Your Landline number:</th>
   <th>$landLineTelNo</th>
   </tr>

   <tr>
   <th>Your Mobile number:</th>
   <th>$mobileTelNo</th>
   </tr>

   <tr>
   <th>Your address:</th>
   <th>join(', ',$Address)</th>   <----------- issue
   </tr>

   <tr>
   <th>Your preferred method of contact:</th>
   <th>$sendMethod</th>
   </tr>


   <tr>
   <th>Your category chosen:</th>
   <th>
   if($catID == \"c1\"){             <------------  issue
       echo \"Bed and Breakfast\";
   }
   elseif ($catID == \"c2\"){
       echo \"Craft Shop\";
   }
   elseif ($catID == \"c3\"){
       echo \"Post Office\";
   }
   elseif ($catID == \"c4\"){
       echo \"Tearoom\";
   }
   elseif ($catID == \"c5\"){
       echo \"Village Store\";
   }
   elseif ($catID == \"null\"){
       echo \"No Category chosen\";               
   }                                 <------------  issue


   </th>
   </tr>

   ";

Thank you

Petr Cina
  • 21
  • 8
  • You cannot have `if statements` *inside* `echos` -- It will just echo your if statement lol – Zak Apr 19 '19 at 21:42
  • 2
    Please tell me why its down voted so I can improve my questions, I am still a beginner to stack overflow and programming. – Petr Cina Apr 19 '19 at 21:43
  • 2
    It's not related to it being a table. PHP can parse variables inside double quoted strings, but not PHP code like that. You'll have to move that logic somewhere else. If you put that if/elseif stuff before the echo, you can just use `$catID` in your string. – Don't Panic Apr 19 '19 at 21:44

2 Answers2

0

You've just formatted your code wrong .. Here's the correct formatting .. You need a IDE like PHPStorm that will identify these problems for you inside the IDE before attempting to run the program.

You need to learn when to stop your outputs (echos), and then start logic ... Then continue with your outputs (echos)

The keyword you want to learn here is Concatenation

if(!mysqli_query($dbConn,$sql))
 {
echo 'Not Inserted';
}

else {
   echo "
   <table class='table'>
   <tr>
   <th>Name:</th>
   <th>$forename</th>
   </tr>

   <tr>
   <th>Surname:</th>
   <th>$surname</th>
   </tr>

   <tr>
   <th>Your email:</th>
   <th>$email</th>
   </tr>

   <tr>
   <th>Your Landline number:</th>
   <th>$landLineTelNo</th>
   </tr>

   <tr>
   <th>Your Mobile number:</th>
   <th>$mobileTelNo</th>
   </tr>

   <tr>
   <th>Your address:</th>
   <th>";

//  whatever this is needs to happen outside the echo
join(', ',$Address)  

echo "</th></tr>

   <tr>
   <th>Your preferred method of contact:</th>
   <th>$sendMethod</th>
   </tr>


   <tr>
   <th>Your category chosen:</th>
   <th>";


   if($catID == "c1"){            
       echo "Bed and Breakfast";
   }
   elseif ($catID == "c2"){
       echo "Craft Shop";
   }
   elseif ($catID == "c3"){
       echo "Post Office";
   }
   elseif ($catID == "c4"){
       echo "Tearoom";
   }
   elseif ($catID == "c5"){
       echo "Village Store";
   }
   elseif ($catID == "null"){
       echo "No Category chosen";               
   }                                


   echo "</th>
   </tr>
   ";
Zak
  • 6,976
  • 2
  • 26
  • 48
  • Interesting, my PHPStorm actually found no problems with the OP code. Maybe you have an inspection turned on that I don't? – Don't Panic Apr 19 '19 at 21:58
  • @Don'tPanic -- Maybe I should clarify this -- Meaning that the code highlighting would suggest that you are simply echoing and that there is no "actual logic" being performed .. – Zak Apr 19 '19 at 21:59
0

You cant use an if statement or a core php function ex: join inside an echo. also for readability wrape your double quoted variable with { and }


if($catID == 'c1'){
    $newCatID = 'Bed and Breakfast';
}
elseif ($catID == 'c2'){
    $newCatID = 'Craft Shop';
}
elseif ($catID == 'c3'){
    $newCatID = 'Post Office';
}
elseif ($catID == 'c4'){
    $newCatID = 'Tearoom';
}
elseif ($catID == 'c5'){
    $newCatID = 'Village Store';
}
elseif ($catID == 'null'){
    $newCatID = 'No Category chosen';
}

echo "<table class='table'>
   <tr>
   <th>Name:</th>
   <th>{$forename}</th>
   </tr>

   <tr>
   <th>Surname:</th>
   <th>{$surname}</th>
   </tr>

   <tr>
   <th>Your email:</th>
   <th>{$email}</th>
   </tr>

   <tr>
   <th>Your Landline number:</th>
   <th>{$landLineTelNo}</th>
   </tr>

   <tr>
   <th>Your Mobile number:</th>
   <th>{$mobileTelNo}</th>
   </tr>

   <tr>
   <th>Your address:</th>
   <th>" . join(', ', $Address). "</th>
   </tr>

   <tr>
   <th>Your preferred method of contact:</th>
   <th>{$sendMethod}</th>
   </tr>

   <tr>
   <th>Your category chosen:</th>
   <th>{$newCatID}</th>
   </tr>";

Ghostff
  • 1,407
  • 3
  • 18
  • 30