2016-05-22 7 views
-2

ausgeführt wird Ich versuche, meine PHP while Schleife innerhalb eines html Echo Echo.Echo PHP, wenn MySQLi Abfrage

Im Grunde möchte ich in einem anderen Echo Echo. Ist das möglich?

Hier ist mein Code:

<?php 

session_start(); 
require 'config.php'; 

if (@$_SESSION['username']) { 

    $query = "SELECT * FROM `crew_info`"; 
    $query_run = mysqli_query($conn,$query); 

    echo '<!DOCTYPE html> 
    <html> 
    <head> 
     <title>All Crew</title> 
    </head> 
    <body> 

     <table> 
      <tr> 
       <th>First Name: </th> 
       <th>Middle Name: </th> 
       <th>Last Name: </th> 
       <th>Age: </th> 
       <th>Birth Date: </th> 
       <th>Birth Place: </th> 
       <th>Gender: </th> 
       <th>Martial Status: </th> 
       <th>Nationality: </th> 
       <th>Email Address: </th> 
       <th>Address 1: </th> 
       <th>Address 2: </th> 
       <th>Course: </th> 
       <th>School Graduated: </th> 
       <th>Remarks: </th> 
       <th>Date Added: </th> 
       <th>Status: </th> 
      </tr> 
      <tr> 
      while($record = mysqli_fetch_assoc($query_run)) { 
      echo "<tr>"; 
      echo "<th>".$record['first_name']."</th>"; 
      echo "<th>".$record['middle_name']."</th>"; 
      echo "<th>".$record['last_name']."</th>"; 
      echo "<th>".$record['age']."</th>"; 
      echo "<th>".$record['birth_date']."</th>"; 
      echo "<th>".$record['birth_place']."</th>"; 
      echo "<th>".$record['gender']."</th>"; 
      echo "<th>".$record['martial_status']."</th>"; 
      echo "<th>".$record['nationality']."</th>"; 
      echo "<th>".$record['email_address']."</th>"; 
      echo "<th>".$record['address_1']."</th>"; 
      echo "<th>".$record['address_2']."</th>"; 
      echo "<th>".$record['course']."</th>"; 
      echo "<th>".$record['school_graduated']."</th>"; 
      echo "<th>".$record['remarks']."</th>"; 
      echo "<th>".$record['date_added']."</th>"; 
      echo "<th>".$record['crew_status']."</th>"; 
     echo "</tr><br>"; 
      </tr> 
     </table> 

    </body> 
    </html>'; 
    }  
} 
else { 
    header('Location: /practice1/index.php'); 
} 

?> 

Ich hoffe jemand kann mir helfen

+0

PHP funktioniert nicht so. Sie können Ihren Code auf einmal ausführen (tun Sie die tatsächlichen Echos, die Sie wollen, anstatt PHP zum Echo von PHP zu verwenden), oder Sie können eine Datei mit PHP speichern und sie später ausführen, vielleicht mit einem Include. Was versuchst du eigentlich? –

+0

Ich versuche, alle Datensätze in der Tabelle zu echo. Wie Sie oben sehen können, ist der HTML-Code innerhalb einer if-Anweisung und sobald er korrekt ist, wird der gesamte HTML-Code ausgeführt. aber im Probleme, mit dem „Echo innerhalb echo“, was leider – unknown

+0

Beachten Sie, dass für Konfigurationsdateien sollten Sie 'require_once()' eher dann 'require()' =] –

Antwort

2

Änderung Ihr Code dazu:

echo '<!DOCTYPE html> 
    <html> 
    <head> 
     <title>All Crew</title> 
    </head> 
    <body> 

     <table> 
      <tr> 
       <th>First Name: </th> 
       <th>Middle Name: </th> 
       <th>Last Name: </th> 
       <th>Age: </th> 
       <th>Birth Date: </th> 
       <th>Birth Place: </th> 
       <th>Gender: </th> 
       <th>Martial Status: </th> 
       <th>Nationality: </th> 
       <th>Email Address: </th> 
       <th>Address 1: </th> 
       <th>Address 2: </th> 
       <th>Course: </th> 
       <th>School Graduated: </th> 
       <th>Remarks: </th> 
       <th>Date Added: </th> 
       <th>Status: </th> 
      </tr> 
      <tr>'; 
      while($record = mysqli_fetch_assoc($query_run)) { 
      echo "<tr>"; 
      echo "<th>".$record['first_name']."</th>"; 
      echo "<th>".$record['middle_name']."</th>"; 
      echo "<th>".$record['last_name']."</th>"; 
      echo "<th>".$record['age']."</th>"; 
      echo "<th>".$record['birth_date']."</th>"; 
      echo "<th>".$record['birth_place']."</th>"; 
      echo "<th>".$record['gender']."</th>"; 
      echo "<th>".$record['martial_status']."</th>"; 
      echo "<th>".$record['nationality']."</th>"; 
      echo "<th>".$record['email_address']."</th>"; 
      echo "<th>".$record['address_1']."</th>"; 
      echo "<th>".$record['address_2']."</th>"; 
      echo "<th>".$record['course']."</th>"; 
      echo "<th>".$record['school_graduated']."</th>"; 
      echo "<th>".$record['remarks']."</th>"; 
      echo "<th>".$record['date_added']."</th>"; 
      echo "<th>".$record['crew_status']."</th>"; 
      echo '</tr>'; 
      } 
     echo '</table> 

    </body> 
    </html>'; 
0

Es gibt kein Echo innerhalb Echo ...

Nicht:

echo ' aaa echo "bbbb"; ccccc '; 

Aber:

echo ' aaa '; 
echo "bbbb"; 
echo 'ccccc '; 

So wird Ihr Code aussehen:

echo '<!DOCTYPE html> 
    <html> 
    <head> 
     <title>All Crew</title> 
    </head> 
    <body> 

     <table> 
      <tr> 
       <th>First Name: </th> 
       <th>Middle Name: </th> 
       <th>Last Name: </th> 
       <th>Age: </th> 
       <th>Birth Date: </th> 
       <th>Birth Place: </th> 
       <th>Gender: </th> 
       <th>Martial Status: </th> 
       <th>Nationality: </th> 
       <th>Email Address: </th> 
       <th>Address 1: </th> 
       <th>Address 2: </th> 
       <th>Course: </th> 
       <th>School Graduated: </th> 
       <th>Remarks: </th> 
       <th>Date Added: </th> 
       <th>Status: </th> 
      </tr> 
      <tr>'; 
      while($record = mysqli_fetch_assoc($query_run)) { 
      echo "<tr>"; 
      echo "<th>".$record['first_name']."</th>"; 
      echo "<th>".$record['middle_name']."</th>"; 
      echo "<th>".$record['last_name']."</th>"; 
      echo "<th>".$record['age']."</th>"; 
      echo "<th>".$record['birth_date']."</th>"; 
      echo "<th>".$record['birth_place']."</th>"; 
      echo "<th>".$record['gender']."</th>"; 
      echo "<th>".$record['martial_status']."</th>"; 
      echo "<th>".$record['nationality']."</th>"; 
      echo "<th>".$record['email_address']."</th>"; 
      echo "<th>".$record['address_1']."</th>"; 
      echo "<th>".$record['address_2']."</th>"; 
      echo "<th>".$record['course']."</th>"; 
      echo "<th>".$record['school_graduated']."</th>"; 
      echo "<th>".$record['remarks']."</th>"; 
      echo "<th>".$record['date_added']."</th>"; 
      echo "<th>".$record['crew_status']."</th>"; 
      echo '</tr>'; 
      } 
     echo '</table> 

    </body> 
    </html>'; 
+0

danke @nospor der Code hat funktioniert. Aber gibt es einen leichteren Weg? – unknown

+0

Nun ... Es ist ein einfacher Weg. Sie müssen nur daran denken, von echo zu normalen Befehlen zu beenden - so wie die Leute es tun. Sie können auch etwas über Templates System wie 'twig' und allgemeine Informationen über MVC lernen. – nospor

+0

danke für den Rat @nospor. im Anfänger in PHP – unknown

0

Oder ähnlich wie folgt aus: (Anmerkung; Der tr über der while-Schleife muss ebenfalls entfernt werden, da sich bereits einer innerhalb der while-Schleife befindet.

<?php 
$loop = ""; 
while ($record = mysqli_fetch_assoc($query_run)) { 
    $loop .= "<tr>"; 
    $loop .= "<th>" . $record['first_name'] . "</th>"; 
    $loop .= "<th>" . $record['middle_name'] . "</th>"; 
    $loop .= "<th>" . $record['last_name'] . "</th>"; 
    $loop .= "<th>" . $record['age'] . "</th>"; 
    $loop .= "<th>" . $record['birth_date'] . "</th>"; 
    $loop .= "<th>" . $record['birth_place'] . "</th>"; 
    $loop .= "<th>" . $record['gender'] . "</th>"; 
    $loop .= "<th>" . $record['martial_status'] . "</th>"; 
    $loop .= "<th>" . $record['nationality'] . "</th>"; 
    $loop .= "<th>" . $record['email_address'] . "</th>"; 
    $loop .= "<th>" . $record['address_1'] . "</th>"; 
    $loop .= "<th>" . $record['address_2'] . "</th>"; 
    $loop .= "<th>" . $record['course'] . "</th>"; 
    $loop .= "<th>" . $record['school_graduated'] . "</th>"; 
    $loop .= "<th>" . $record['remarks'] . "</th>"; 
    $loop .= "<th>" . $record['date_added'] . "</th>"; 
    $loop .= "<th>" . $record['crew_status'] . "</th>"; 
    $loop .= '</tr>'; 
} 

$out = '<!DOCTYPE html> 
    <html> 
    <head> 
     <title>All Crew</title> 
    </head> 
    <body> 
     <table> 
      <tr> 
       <th>First Name: </th> 
       <th>Middle Name: </th> 
       <th>Last Name: </th> 
       <th>Age: </th> 
       <th>Birth Date: </th> 
       <th>Birth Place: </th> 
       <th>Gender: </th> 
       <th>Martial Status: </th> 
       <th>Nationality: </th> 
       <th>Email Address: </th> 
       <th>Address 1: </th> 
       <th>Address 2: </th> 
       <th>Course: </th> 
       <th>School Graduated: </th> 
       <th>Remarks: </th> 
       <th>Date Added: </th> 
       <th>Status: </th> 
      </tr>'; 

echo $out . $loop . '</table> 
    </body> 
    </html>';