0 レビュー
4 回答
php-MYSQLデータベースのデータをテーブルに表示する(個別にではない)
データベースの値をテーブルに表示したい。以下の値を表示することができました。ただし、テーブルはすべて互いに分離されています。
すべてを1つのテーブルにまとめるにはどうすればよいですか?
<html>
<head>
</head>
<body>
<?php
$con = mysqli_connect('localhost', 'root', '', 'yourspace');
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM profiles");
while ($row = mysqli_fetch_array($result)) {
echo "<div style='border:solid 1px #ccc;padding:10px;float:left;margin-top:10px;'>";
echo "<table border='1'> <tr> <th>Firstname</th> <th>Lastname</th>
<th>Age</th>
<th>gender</th>
<th>hometown</th>
<th>University</th>
<th>Occupation</th> </tr>";
echo "<tr>";
echo "<td>" . $row['first_name'] . "</td>";
echo "<td>" . $row['last_name'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['gender'] . "</td>";
echo "<td>" . $row['hometown'] . "</td>";
echo "<td>" . $row['university'] . "</td>";
echo "<td>" . $row['occupation'] . "</td>";
echo "</tr>";
echo "</table>";
echo "</div>";
}
mysqli_close($con);
?>
</body>
</html>
わからない
0
レビュー
答え :
解決策:
最初に脳に働きかけ、次にコーディングを開始します。
テーブルの行のみをループの一部として出力する必要があります。
<html>
<head>
</head>
<body>
<?php
$con = mysqli_connect('localhost', 'root', '', 'yourspace');
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM profiles");
echo "<div style='border:solid 1px #ccc;padding:10px;float:left;margin-top:10px;'>";
echo "<table border='1'>";
echo "<tr> <th>Firstname</th>
<th>Lastname</th> <th>Age</th>
<th>gender</th>
<th>hometown</th>
<th>University</th>
<th>Occupation</th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['first_name'] . "</td>";
echo "<td>" . $row['last_name'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['gender'] . "</td>";
echo "<td>" . $row['hometown'] . "</td>";
echo "<td>" . $row['university'] . "</td>";
echo "<td>" . $row['occupation'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "</div>";
mysqli_close($con);
?>
</body>
</html>
わからない
0
レビュー
答え :
解決策:
次のように、テーブルの作成と終了部分をwhileループに入れないようにします。
echo "<table border='1'> <tr> <th>Firstname</th> <th>Lastname</th>
<th>Age</th>
<th>gender</th>
<th>hometown</th>
<th>University</th>
<th>Occupation</th> </tr>";
while ($row = mysqli_fetch_array($result)) {....
// print the records <Tr> <Td......
}
echo "</table>";
わからない
0
レビュー
答え :
解決策:
ループを正しく実行していません。実際、プロファイルテーブルの各レコードに対して、 <table>
タグを作成していますがこれは適切ではありません。
これを行う正しい方法は次のとおりです:
<html>
<head>
</head>
<body>
<?php
$con = mysqli_connect('localhost', 'root', '', 'yourspace');
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit(); //Exit not to display the table since there is an mysqli error
}
echo "<div style='border:solid 1px #ccc;padding:10px;float:left;margin-top:10px;'>
<table border='1'> <tr> <th>Firstname</th> <th>Lastname</th>
<th>Age</th>
<th>gender</th>
<th>hometown</th>
<th>University</th>
<th>Occupation</th> </tr>
<tr>";
$result = mysqli_query($con, "SELECT * FROM profiles");
while ($row = mysqli_fetch_array($result)) {
echo "<td>" . $row['first_name'] . "</td>";
echo "<td>" . $row['last_name'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['gender'] . "</td>";
echo "<td>" . $row['hometown'] . "</td>";
echo "<td>" . $row['university'] . "</td>";
echo "<td>" . $row['occupation'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "</div>";
mysqli_close($con);
?>
</body>
</html>
お役に立てば幸いです。
わからない
0
レビュー
答え :
解決策:
while ($row = mysqli_fetch_array($result)) {
echo "<div style='border:solid 1px
#ccc;padding:10px;float:left;margin-top:10px;'>";
echo "<table border='1'> <tr> <th>Firstname</th> <th>Lastname</th>
<th>Age</th>
<th>gender</th>
<th>hometown</th>
<th>University</th>
<th>Occupation</th> </tr>";
これはあなたの問題です。 whileループが実行されている間、および実行されるたびに新しいテーブルが作成されるため、このコードをwhileループの外のその直前に配置する必要があります。 whileループ内では、既存のテーブルに新しい列を追加するためのコードが必要です。
わからない
同様の質問
私たちのウェブサイトで同様の質問で答えを見つけてください。