Home > Software design >  I want to display echo within echo using while
I want to display echo within echo using while

Time:09-12

I need help with my code! I am trying to create a table, that is shown if you are logged in with text WITHIN a table column! So I need to use echo two times, and I read that you need to use a while statement to do this! But it displays the whole while statement, and not just "Sandwich Fun".


if(isset($_SESSION["loggedin"])) {
    echo "You are not logged in.";
    } else {
    echo "<!DOCTYPE html> <!-- 5 -->
<html>
   <head>
  <title>Index - Sandwich Fun&trade;</title>
    <meta charset=\"UTF-8\">
    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">
  
    <meta name=\"robots\" content=\"noindex\"> 

    <style type=\"text/css\">
    <!--
    .smorgass { position: absolute; top: 0; left: 0; width: 100%; }
    table, th, td { border: 1px solid black; }
    -->
    </style>
</head>
   <body>

<table height=\"500\" class=\"smorgass\">
<tr><th colspan=\"3\">while ( 1 === 1 ) {
echo \"Sandwich Fun&trade;\";
}</th></tr>
<tr><td></td><td></td><td></td</tr>
<tr><td></td></tr>
  </table>

</body>
  </html>"; } ?>

What do I do?

CodePudding user response:

You are getting confused between php and html. The while has to be done in php. You use <?php to go into php and ?> to go out of it. Its much easier to do that, otherwise there is confusion with quotes etc

Something like this

<?php
if(isset($_SESSION["loggedin"])) {
    echo "You are not logged in.";
    } 
else {?>

<!DOCTYPE html> <!-- 5 -->
<html>
  <head>
  <title>Index - Sandwich Fun&trade;</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="robots" content="noindex"> 

    <style type="text/css">
    <!--
    .smorgass { position: absolute; top: 0; left: 0; width: 100%; }
    table, th, td { border: 1px solid black; }
    -->
    </style>
  </head>
  <body>

  <table height="500" >
  <tr><th colspan="3">
  <?php
  while ( 1 === 1 ) {
  echo "Sandwich Fun&trade;";
  }?>
  </th></tr>
  <tr><td></td><td></td><td></td</tr>
  <tr><td></td></tr>
  </table>

  </body>
</html>
<?php }?>

CodePudding user response:

You should break your echo and then insert while statement (or use some templating languages):

if (isset($_SESSION["loggedin"])) {
    echo 'You are not logged in.';
} else {
    echo <<< HTML
    <!DOCTYPE html> <!-- 5 -->
    <html>
       <head>
      <title>Index - Sandwich Fun&trade;</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
      
        <meta name="robots" content="noindex"> 
    
        <style type="text/css">
        <!--
        
        .smorgass { position: absolute; top: 0; left: 0; width: 100%; }
        
        table, th, td { border: 1px solid black; }
        
        -->
        </style>
    </head>
       <body>
    
    <table height="500" >
    <tr><th colspan="3">
    HTML;

    while (1 === 1) {
        echo 'Sandwich Fun&trade;';
    }

    echo <<< HTML
        </th></tr>
    <tr><td></td><td></td><td></td</tr>
    <tr><td></td></tr>
      </table>
    
    </body>
      </html>
    HTML;
}

Please note that you have infinite loop


I suggest using HereDoc syntax in order to reduce escape hell of \"

CodePudding user response:

To work more clearly and efficiently I would suggest you to do the login check like this. Put this above your page.

<?php
    if (!isset($_SESSION["loggedin"])) {
    //  ^ redirect to login if the variable is NOT set
        header("Location: login.php");
    }
?>
<html>
  <head>
    <title>Index - Sandwich Fun&trade;</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="robots" content="noindex"> 
    <style type="text/css">
  </head>
  <body>
  <table height="500" >
    <tr><th colspan="3">
    <?php
    while (1 === 1) {
        echo 'Sandwich Fun&trade;';
    }
    ?>
    </th></tr>
    <tr><td></td><td></td><td></td</tr>
    <tr><td></td></tr>
  </table>
  </body>
</html>

This way you will work more organized and it will make inserting php loops etc within a html page much easier. If someone is not logged in it will simply send them back to login.php, I know you didn't ask for this but I do think this will help you a bunch.

CodePudding user response:

This is the code I have now


  if(isset($_SESSION["login"])) {
    header("Location: login.php");
  }

?>

<!DOCTYPE html> <!-- 5 -->
<html>
   <head>
  <title>Index - Sandwich Fun&trade;</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
  
    <meta name="robots" content="noindex"> 

    <style type="text/css">
    <!--
    
    .smorgass { position: absolute; top: 0; left: 0; width: 100%; }
    
    table, th, td { border: 1px solid black; }
    
    -->
    </style>
</head>
   <body>

<table height="500" >
<tr><th colspan="3">

<?php echo "Sandwich Fun&trade;" ?>

</th></tr>
<tr><td></td><td></td><td></td</tr>
<tr><td></td></tr>
  </table>

</body>
  </html>```

Is it right?
  •  Tags:  
  • php
  • Related