A. php後台查出數據,輸出在html表格里,怎麼做
從資料庫中查詢數據表,比如文章表,首先會得到一個二維數組。這個數組包含了文章表中的所有記錄。接下來,我們需要使用PHP語言進行數據處理,並將這些數據輸出到HTML表格中。
具體來說,我們可以使用PHP的預處理語句來查詢資料庫,並將查詢結果存儲在一個變數中。這個變數就是上述提到的二維數組。然後,我們需要使用while循環遍歷這個數組,從而獲取到每一條記錄的具體信息。在循環內部,我們可以使用echo或print語句,將這些信息輸出到HTML表格中。
為了使代碼更加清晰易讀,建議將HTML和PHP代碼分開寫。你可以先編寫HTML代碼,定義表格結構,然後在需要插入數據的地方使用PHP代碼插入相應的數據。例如,你可以使用table標簽定義表格,tr標簽定義表格行,td標簽定義表格單元格,然後在php代碼中插入數據,使用echo語句將數據輸出到相應的td標簽中。
這里有一個簡單的示例代碼,幫助你更好地理解這個過程:
<?php
$servername = "localhost";
$username = "root";
$password = "123456";
$dbname = "test";
<?php
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT * FROM articles";
$result = $conn->query($sql);
?>
<table>
<tr><th>ID</th><th>標題</th><th>內容</th></tr>
<?php
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
?>
<tr><td><?php echo $row["id"] ?></td><td><?php echo $row["title"] ?></td><td><?php echo $row["content"] ?></td></tr>
<?php }
}
?>
</table>
當然,你也可以使用HTML和PHP混合編寫的方式,但是建議將兩者分開,以便更好地維護代碼。
B. php寫一個數組用html表格輸出這個數組
1、首先輸入:
<?php
class xtable
{
private $tit,$arr,$fons,$sextra;
public function __construct()
{
$this->tit=array();// strings with titles for first row
$this->arr=array();// data to show on cells
$this->fons=array("#EEEEEE","#CCEEEE");// background colors for odd and even rows
$this->sextra="";// extra html code for table tag
}
C. 用php寫一個數組包括5行數據,數組的格式是:學號,姓名,性別,年齡,班級;將數組的內容寫入到stu.csv中
源代碼如下:
<html>
<head>
<title>打開CSV文件</title>
</head>
<p>
<center>
<?php
$t_array=array(
array("1","張三","男","21","0921212"),
array("2","李四","女","20","0921212"),
array("3","王五","女","21","0921212"),
array("4","小六","男","22","0921212"),
array("5","田七","女","20","0921212")
);
$handle=fopen("html/stu.csv","w");
foreach ($stu as $line)
{fputcsv($handle,$line);}
fclose($handle);
echo"<table border=1><tr><td>學號</td><td>姓名</td><td>性別</td><td>年齡</td><td>班級</td></tr>";
while (list($key,$value)=each($t_array))
{
list($XH,$XM,$XB,$NL,$BJ)=$value;
echo "<tr><td>$XH</td><td>$XM</td><td>$XB</td><td>$NL</td><td>$BJ</td></tr>";
}
echo"</table>";
?>
</p>
</center>
</body>
</html>
,剩下的就是你自己建立一個CSV文件,把對應的內容填寫完整就行了,記得把內容換一下,不要照抄了啊
D. php中一個二維數組,怎麼吧數組中的數據作為value插入到資料庫某個表中
首先,你資料庫用的是什麼?用什麼連接的資料庫?
如果用values的形式插入語句只能用for反復執行(以mysql為例)
for($i=0;$i<66;$i++){
mysql_query("insertintousers(user_id,user_name)value(array[$i]['user_id'],array[$i]['name'])");
}
或者一次性生成表然後插入,需要盡量保證表結構一致
$str="insertintousers("
for($i=0;$i<66;$i++){
$str.="select".array[$i]['user_id']."asuser_id,".array[$i]['name']."asname";
}
$str.=")";
mysql_query($str)