我希望在其他地方没有问过这个问题因为我看了但是现在这样。我写了这个函数来为我创建表。它有效,但我遇到的问题是它每次调用此函数时都会运行查询。有没有办法调用此函数并将查询传递给函数?
function createawardtables($awardname, $awardshortname, $maxawards, $id){
$query="SELECT * FROM awards WHERE id = $id";
$result = mysql_query($query) or die("There was a problem with the SQL query: " . mysql_error());
while($row = mysql_fetch_array($result)){
$order = array("","1st","2nd","3rd","4th","5th","6th","7th","8th",'9th',"10th");
echo "<table><th colspan=4><font color=maroon size='4pt'><u><b>Orders of the $awardfullname</b></u></font></th>";
for($i=1; $i<$maxawards+1; $i++) {
${$awardshortname.$i} = dateconvert(($row["$awardshortname$i"]), 2);
${$awardshortname.$i.'by'} = $row["$awardshortname{$i}by"];
${$awardshortname.$i.'memo'} = $row["$awardshortname{$i}memo"];
echo "<tr>
<td>$order[$i] Order given on </td>
<td><input type=text name='$awardshortname$i' title='mm/dd/yyyy' value= '${$awardshortname.$i}' size=8/></td>
<td>by <input type=text name='$awardshortname{$i}by' title='Name of who gave the award.' value='${$awardshortname.$i.'by'}'size=15/></td>
<td>for <input type=text name='$awardshortname{$i}memo' title='What the award was give for.' value='${$awardshortname.$i.'memo'}'size=15/></td>
</tr>";
if($i==10 or $awardshortname=="initiate"){
echo "</table><br />";
};
};
};
mysql_free_result($result);
};
答案 0 :(得分:2)
当然,只需在函数中添加一个新参数(如$awardname
),并在调用'mysql_query()`时使用它。
另外请注意,强烈建议不要使用mysql_*
函数:
自PHP 5.5.0起,此扩展程序已弃用,并将在中删除 未来。相反,应该使用MySQLi或PDO_MySQL扩展。
答案 1 :(得分:0)
你去吧
function createawardtables($result, $awardname, $awardshortname, $maxawards, $id)
{
while($row = mysql_fetch_array($result)){
$order = array("","1st","2nd","3rd","4th","5th","6th","7th","8th",'9th',"10th");
echo "<table><th colspan=4><font color=maroon size='4pt'><u><b>Orders of the $awardfullname</b></u></font></th>";
for($i=1; $i<$maxawards+1; $i++) {
${$awardshortname.$i} = dateconvert(($row["$awardshortname$i"]), 2);
${$awardshortname.$i.'by'} = $row["$awardshortname{$i}by"];
${$awardshortname.$i.'memo'} = $row["$awardshortname{$i}memo"];
echo "<tr>
<td>$order[$i] Order given on </td>
<td><input type=text name='$awardshortname$i' title='mm/dd/yyyy' value= '${$awardshortname.$i}' size=8/></td>
<td>by <input type=text name='$awardshortname{$i}by' title='Name of who gave the award.' value='${$awardshortname.$i.'by'}'size=15/></td>
<td>for <input type=text name='$awardshortname{$i}memo' title='What the award was give for.' value='${$awardshortname.$i.'memo'}'size=15/></td>
</tr>";
if($i==10 or $awardshortname=="initiate"){
echo "</table><br />";
};
};
};
mysql_free_result($result);
};
和函数调用
$query="SELECT * FROM awards WHERE id = $id";
$result = mysql_query($query) or die("There was a problem with the SQL query: " . mysql_error());
createawardtables($result, $awardname, $awardshortname, $maxawards, $id);