データを編集および削除するオプションを使用してテーブルを表示する関数を作成しています。これらのテーブルが数回必要になるため、関数を作成しました。
関数の呼び出し方法は次のとおりです。
listSpecific(customer, array("CustomerID", "Forename", "Surname", "Email", "Secret Q", "Secret A", "Street", "City", "Postcode", "Type"), array("customerid", "forename", "surname", "email", "secretQ", "secretA", "address_street", "address_city", "address_postcode", "member_type"));
これは関数全体です。
function listSpecific($DBname, $titles=array(), $values=array()){
$numValues = count($values);
$i=0;
$sql = "SELECT ";
//Construct SQL Statement
foreach ($values as $val)
{
//Last Element of the array
if(++$i === $numValues){
$sql .= $val;
$sql .= " ";
}
else{
//The rest of the array elements
$sql .= $val;
$sql .= ", ";
}
}
$sql .= "FROM $DBname;";
//End of Constructing SQL Statement
// Execute Query
$result = mysql_query($sql) or die("An error has ocured: " . mysql_error() . ":" . mysql_errno());
//Construct table
$list = "<table>";
$list .= "<tr>";
//Cycle through array elements to get table headers
foreach ($titles as $title) {
$list .= "<th>$title</th>";
}
$list .= "<th>";
$list .= "Task";
$list .= "</tr><tr>";
$numValues = count($values);
$i=0;
//Construct the rest of the table [NOT WORKING]
while ($table = mysql_fetch_array($result)) {
$item = array();
foreach($values as $val){
//Last Element of the array
if(++$i === $numValues){
$list .= "<td>";
$item = $table[$val];
$list .= $item;
$list .= "</td>";
//Get the Item ID
$firstElement = reset($table);
$list .= "<td><a href=\"users.php?task=delete&user=$firstElement\">Delete</a></td></tr>";
}
else{
//The rest of the array elements
$list .= "<td>";
$item = $table[$val];
$list .= $item;
$list .= "</td>";
}
}
}
echo $list;
}
問題は、URL に渡されるユーザー ID が必要なため、削除ハイパーリンクを作成することです。
問題は次の行にあります。
//Get the Item ID
$firstElement = reset($table);
$list .= "<td><a href=\"users.php?task=delete&user=$firstElement\">Delete</a></td></tr>";
なぜうまくいかないのか理解しています。配列の最初の項目であるユーザー ID に戻ります。ただし、行に対して一意である必要があります。
たとえば、最初の行のユーザー ID は 57 ですが、次の行では 58 で、ハイパーリンクが変更されています。
これに対する安価な解決策は、while ループで毎回 11 個の値に戻り、中断したところから続行することです。これはどのように達成できますか?
ありがとうございました。