简体   繁体   中英

Echo inside an echo - PHP

I have this PHP-code:

        <!-- PHP bliver brugt til dynamisk at hente madtabeldata fra 'datatab'-->
        <?php
            // Forbinder til database
            $db = include("connect2db.php");
            // Viser danske bogstaver
            $db->set_charset("utf8");

            // SQL query henter al data fra 'datatab' 
            $query="select * FROM datatab";

            $result = $db->query($query); // Placerer resultaterne af query i en variabel
            $num_results = $result->num_rows; 

            // Viser hver rækkes data i en tabel
            for ($i=0; $i <$num_results; $i++)
            {
                $row = $result->fetch_assoc();
                echo "<tr>";
                echo '<td>'.$row['dato'].'</td>
                      <td>'.$row['vaegt'].'</td>
                      <td>'.$row['motion'].'</td>
                    </tr>';
            }
        ?>
    </table>            
</div>

I want to include this echo inside my PHP-code:

<?php
    if (isset ($_SESSION['username'])) {
        echo '*PHP-code*';
    } else {
        echo '<p>"Please log in to see the table"</p>';
    }
?>

How can I do this successfully? When I enter it, it says that there is an error in the server.

You cannot echo "inside" an echo. You have to just copy your code there without the echos.

 <?php
    if (isset ($_SESSION['username'])) {
      // Forbinder til database
      $db = include("connect2db.php");
      // Viser danske bogstaver
      $db->set_charset("utf8");

      // SQL query henter al data fra 'datatab' 
      $query="select * FROM datatab";

      $result = $db->query($query); // Placerer resultaterne af query i en variabel
      $num_results = $result->num_rows; 
      echo "<div><table>";
      // Viser hver rækkes data i en tabel
      for ($i=0; $i <$num_results; $i++)
      {
        $row = $result->fetch_assoc();
        echo "<tr>";
        echo '<td>'.$row['dato'].'</td>
        <td>'.$row['vaegt'].'</td>
        <td>'.$row['motion'].'</td>
        </tr>';
      }
      echo "</table></div>";
   }
   else {
     echo '<p>"Please log in to see the table"</p>';
   }
?>

Alternatively you can put all your table in a string var and then echo that var:

<?php
    // Forbinder til database
    $db = include("connect2db.php");
    // Viser danske bogstaver
    $db->set_charset("utf8");

    // SQL query henter al data fra 'datatab' 
    $query="select * FROM datatab";

    $result = $db->query($query); // Placerer resultaterne af query i en variabel
    $num_results = $result->num_rows; 
    $table = "<table>";
    // Viser hver rækkes data i en tabel
    for ($i=0; $i <$num_results; $i++)
    {
        $row = $result->fetch_assoc();
        $table .= "<tr>";
        $table .= '<td>'.$row['dato'].'</td>
              <td>'.$row['vaegt'].'</td>
              <td>'.$row['motion'].'</td>
            </tr>';
    }
    $table .= "</table>";
?>

and later...

echo $table

but do not connect to db and elaborate the table if user is not connected of course.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM