![](/img/trans.png)
[英]PHP function that returns the names of all columns in a certain table, as an array
[英]Extending PHP table function - behaviour for certain columns
<?php
function f__table ($table,$cols,$order,$desc) {
$comma=implode(",",$cols);
$query = 'select '.$comma.' from '.$table.' order by '. $order.' '. $desc;
$result = mysql_query($query);
if (!$result) {
$message = 'ERROR:' . mysql_error();
return $message;
}
else {
$i = 0;
echo '<table class="tablesorter" width="960px;"><thead><tr>';
while ($i < mysql_num_fields($result)) {
$meta = mysql_fetch_field($result, $i);
//take table title and remove the F_prefix
$title=$meta->name;
$title=str_replace('F_','',$title);
$title=str_replace('_',' ',$title);
echo '<th>' . $title . ' </th>';
$i = $i + 1;
}
echo '</tr></thead><tbody>';
$i = 0;
while ($row = mysql_fetch_row($result))
{
echo '<tr>';
$count = count($row);
$y = 0;
while ($y < $count)
{
$c_row = current($row);
echo '<td>' . $c_row . '</td>';
next($row); $y = $y + 1;
}
echo '</tr>';
$i = $i + 1;
}
echo '</tbody></table>';
mysql_free_result($result);
}
// close connection
mysql_close();
// close function
}
?>
我有這個表函數,如果我的mysql數據庫中的數據使用一些jQuery添加一些功能(例如,排序列,斑馬行樣式等)從我的mysql數據庫到屏幕,它可以很好地滿足我的需求。
但是,現在我希望顯式擴展我的功能,以便在html標記中包含一個鏈接,因此例如在第1列中可能會有值1、2、3、4,我希望具有指向www.something.com/page/1的鏈接,/ 2,/ 3,/ 4等,但在另一個示例中,根據我的sql,它可能不是列1。 我知道在每種情況下都基於需要鏈接到其他頁面的字段名,一個表可能有15列,其中6列可能包含該表的每個td元素中基於該值的不同頁面的鏈接,
我可以在數組中指定它,但是如何將其構建為一個函數,使其在我的網站上是動態且可擴展的?
我在網站上手工編寫了40多個頁面的數據表,圖表和圖形,現在我希望隨着PHP技能的發展而提高
我相信我將需要一些基於數組鍵(字段名)的if語句作為指示符,並且如果該數組鍵存在於另一個數組中,則從該數組中提取值...如果這是有道理的。
如果有人可以提供示例代碼或一些邏輯以及朝正確方向的指針,將不勝感激。
為表創建,標題和顯示的每個部分創建一個函數,並為不同的顯示屬性(例如鏈接或圖像)創建函數。
創建一個類似於以下字段名的數組:(名稱,寬度,對齊方式,行為)。
'F_DESCRIPTION' => array( 'Description', 210, 'left', '_function_display' ),
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.