2012-06-16 42 views
2

我有一個頁面,我在那裏搜索作者寫的書籍(根據我2個月前的任務進行的基本搜索)。我從下拉框中選擇作者,在按「提交」按鈕後,結果應該出現。按提交按鈕後顯示數據庫條目

這裏是頁面的代碼:

<?php 

include ("includes/connections.php"); 

if($_POST) 
{ 
    if (!isset($_POST["authors"])){ 
      header("Location: searchAuthor.php");  
      exit; 
    } 

    foreach ($_POST["authors"] as $author) 
    { 
     ????????? 
    } 
} 
?> 

<?php include ("includes/connections.php"); 
function dropdown($intIdField, $strfNameField, $strlNameField, $strTableName, $strOrderField, $strNameOrdinal, $strMethod="asc") { 
    echo "<select name=\"{$strNameOrdinal}[]\">\n"; 

    echo "<option value=\"NULL\">Select Value</option>\n"; 

    $strQuery = "SELECT $intIdField, $strfNameField, $strlNameField 
       FROM $strTableName 
       ORDER BY $strOrderField $strMethod"; 

    $rsrcResult = mysql_query($strQuery) or die(mysql_error()); 

    while($arrayRow = mysql_fetch_assoc($rsrcResult)) { 
     $strA = $arrayRow["$intIdField"]; 
     $strB = $arrayRow["$strlNameField"] . " " . $arrayRow["$strfNameField"];  
     echo "<option value=\"$strA\">$strB</option>\n"; 
    } 

    echo "</select>"; 
} 
?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Add Book Information</title> 
<link href="back.css" rel="stylesheet" type="text/css" /> 
</head> 

<body> 
<h1>Search for Books of an Author</h1><table width="528" border="0" align="center"> 
    <tr> 
     <td width="480"><span id="tip">*Hitting the "Search books of Author" button without filling the fields with an asterisk will just reset the form</span></td> 
    </tr> 
    </table> 
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>" id="formBook"> 
    <table width="563" border="0" align="center">  
    <tr> 
     <td style="text-align: right"><label for="authors">Select an Author*:</label></td> 
     <td><?php dropdown("author_ID", "author_firstname", "author_lastname", "author", "author_lastname", "authors"); ?></td> 
    </tr> 
    <tr> 
     <td>&nbsp;</td> 
     <td>&nbsp;</td> 
    </tr> 
    <tr> 
     <td>&nbsp;</td> 
     <td><input type="submit" name="submit" id="submit" value="Search books of Author" /></td> 
    </tr> 
    <tr> 
     <td><div align="left"><img src="images/buttonleft.png" alt="previous" width="70" height="70" usemap="#Previous" border="0"></div></td> 
     <td><div align="right"><img src="images/buttonright.png" alt="next" width="70" height="70" usemap="#Next" border="0"> 
     <map name="Previous"> 
      <area shape="circle" coords="35,35,33" href="addSubject.php"> 
     </map> 
     <map name="Next"> 
      <area shape="circle" coords="35,35,33" href="addEdition.php"> 
     </map> 
     </div></td> 
    </tr> 
    </table> 
</form> 

</body> 
</html> 

正如你可以看到我有一個表中的一切(方便小這樣的東西)。當我按下提交按鈕時,我想要的是被選中輸入的作者,該方法將顯示查詢結果。該查詢將在foreach中執行,其中的是。然後我希望查詢的結果用於顯示在我的表內(通過添加更多行並通過php函數在每行中插入一個結果)結果。

有沒有辦法用php來做到這一點?我不知道如何使用Javascript只是PHP和HTML。即使我必須在另一個頁面中插入查詢結果並在其中顯示所有內容,我也可以。

我還沒寫過查詢。

其實foreach是在那裏把作者寫的每本書放在一個變量中。

回答

1

下面是一個例子。查詢/字段是假的。

<?php 

if (isset($_POST['Submit'])) { 

    $strQuery = "SELECT 'field1', 'field2', 'field3' 
      FROM $strTableName 
      ORDER BY $strOrderField $strMethod"; 

$rsrcResult = mysql_query($strQuery) or die(mysql_error()); 

?> 
<table> 
<td> HEADER 1 </td> <td> HEADER 2 </td> <td> HEADER 3 </td> 

<?php 

while ($row = mysql_fetch_array($rsrcResult) { 
echo "<tr><td>".$row['field1']."</td><td>".$row['field2']."</td><td>".$row['field3']."</td>"; 

} 

?> 

</table> 
+0

我知道如何做到這一點,我的問題是我如何通過在另一個PHP函數 –

+0

查詢的結果,我不知道爲什麼你需要擺在首位 – ioannis

+0

的功能,我只是擔心變量'$ rsrcResult'會被globbaly使用,所以我可以在while循環中使用它? –