-4

I've a simple problem but I am a php beginner so I need your help.

I want to put a php echo into a php echo. I know that this is not possible but idk how to solve my problem.

There is a simple html list which shows the subjects only if the assigned value in the db is >0.

<li>
    <ul>
        <?php  if ($record['#1'] > "0") { echo "<li>#1</li>"; }?>
        <?php  if ($record['#2'] > "0") { echo "<li>#2</li>"; }?>
    </ul>
</li>

But I also want to show the whole list only if one of the values is >0 because otherwise the list will be empty. So I need something like this (I know it doesn't work this way):

<?php  
  if ($record['#1'] > "0" or $record['#2'] > "0") { 
     echo "
        <ul>
          <?php  if ($record['#1'] > "0") { echo "<li>#1</li>"; }?>
          <?php  if ($record['#2'] > "0") { echo "<li>#2</li>"; }?>
        </ul>
       </li>
     "; 
   }
?>
Rob
  • 26,989
  • 16
  • 82
  • 98
elkloso
  • 27
  • 1
  • 7

5 Answers5

0

You can simplify this code as:

<ul>
<?php
  if($record['#1'] > 0)
    echo "<li>#1</li>";
  if($record['#2'] > 0)
    echo "<li>#2</li>";
?>
</ul>
akbansa
  • 381
  • 1
  • 11
0

You are alredy in the PHP-Tag. So don't open it again. Close and Open echo if you want. Link this:

<?php  
  if ($record['#1'] > 0 || $record['#2'] > 0) { 
     echo "
        <ul>
          "; if ($record['#1'] > 0) { echo "<li>#1</li>"; } echo"
          "; if ($record['#2'] > 0) { echo "<li>#2</li>"; } echo"
        </ul>
       </li>
     "; 
   }
?>

Or you are looking for a way how looks a bit better, like:

<?php  
  if ($record['#1'] > 0 || $record['#2'] > 0) { 
     $li1 = "";
     if ($record['#1'] > 0)
          $li1 = "<li>#1</li>";

     $li2 = "";
     if ($record['#2'] > 0) 
          $li2 = "<li>#2</li>";

     echo "
        <ul>
          $li1
          $li2
        </ul>
       </li>
     "; 
   }
?>

Add 2 Strings:

$foo = "blabla";

echo "I say:".$foo." and soooo"; // <- I say:blabla and soooo
echo "I say:$foo and soooo"; // <- I say:blabla and soooo
Baum
  • 95
  • 10
0
<?php  if ($record['#1'] > 0 OR $record['#2'] > 0 ) { ?>
  <ul>
    <?php if ($record['#1'] > 0) { echo "<li>#1</li>"; } ?>
    <?php  if ($record['#2'] > 0) { echo "<li>#2</li>"; } ?>
  </ul>
</li>
<?php } ?>
Junaid
  • 1,270
  • 3
  • 14
  • 33
0

try this ,

<?php
    if ($record['#1'] > "0" || $record['#2'] > "0") { 
        echo "<ul>";
        if ($record['#1'] > "0") {
           echo "<li>#1</li>"; 
         }
        if ($record['#2'] > "0") { 
           echo "<li>#2</li>"; 
         }
      echo "</ul>";

 }

?>
Moby M
  • 910
  • 2
  • 7
  • 26
-1

Please change the code given below

<?php  if ($record['#1'] > "0" or $record['#2'] > "0") {  ?>
  <ul>
    <?php  if ($record['#1'] > "0") { echo "<li>#1</li>"; }?>
    <?php  if ($record['#2'] > "0") { echo "<li>#2</li>"; }?>
  </ul>
</li>  <?php }?>

I think you forgot the syntax of the php tag. So please change like this and try

Pranav MS
  • 2,235
  • 2
  • 23
  • 50
  • 1
    If you see the final `
  • ";` it suggests everything is being echoed already, in which case it won't work with PHP-tags and `if`s inside. Not my downvote btw :-) (I nearly walked into the same trap)
  • – Qirel Sep 14 '17 at 10:47
  • @Qirel this is correct please see carefully. – Pranav MS Sep 14 '17 at 10:51
  • Like I said, it's not my downvote. But that `";` that's at the end of the string in the original question suggests that OP wants to put all this in the middle of a string - which won't work. – Qirel Sep 14 '17 at 10:52
  • @Qirel Sorry i got your point. Thanks. – Pranav MS Sep 14 '17 at 10:54
  • That's right. The first snippet is the current code. So you can see that I've already a php string which should be included into another. – elkloso Sep 14 '17 at 10:59
  • @elkloso see my code you will get your answer. – Pranav MS Sep 14 '17 at 11:02