2011-05-15 29 views
3

值變量通過AJAX獲取結果這一切都在真正的問題:)使用PHP來查詢數據庫,查詢數據庫再次使用的初始變量

它更清晰,當我把它放在要點什麼,我想要做的,所以這裏是:

  • 我在頁面上有兩種形式,搜索表單和'編輯配置文件'窗體。兩者都是單獨運作的。
  • 編輯配置文件表格通過我的MySQL tbl的行分頁,允許我編輯每列的值。目前設置爲包括所有行(即所有存儲的配置文件)。
  • 搜索表單採用17個不同的搜索變量中的任何一個,並搜索同一表以查找配置文件或一組配置文件。
  • 我希望能夠輸入搜索詞(例如Name:'Bob'),像我在做的那樣查詢tbl,但使用AJAX將結果的唯一ID作爲存儲在變量中的數組返回。然後,我希望能夠異步地將該變量饋送到我的編輯配置文件表單查詢中(搜索表單將具有一個提交按鈕...),所以我現在可以翻閱我表格中的所有行(例如,名稱中包含'Bob '),只有那些行。

上述可能的語言有問題嗎?任何人都可以幫我把它們拼在一起?

我處於PHP和MySQL的中間階段,但是絕對是AJAX的新手。我只用它在一個定義好的區域顯示一個文本字符串 - 正如在各處的演示中看到的那樣:)因此,對待我像一個五歲的孩子,非常感謝!

這裏是我當前的搜索查詢和編輯配置文件的形式,如果他們在所有幫助:

編輯配置文件形式:

//pagination base 
if (isset($_GET['page'])) { $page = $_GET['page']; } 
else { $page = 1; } 
$max_results = 1; 
$from = (($page * $max_results) - $max_results); 

$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM profiles"),0); 
$total_pages = ceil($total_results/$max_results); 

echo '<span id="pagination">' . 'Record ' . $page . ' of ' . $total_results . ' Records Returned. '; 

if($total_results > $max_results) 
{ 
    if($page > 1) 
    { $prev = ($page - 1); 
     echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><input type='submit' value='&lt;&lt;' /></a>"; 
    } 
    if($page == 1) 
    { echo "<input type='submit' value='&lt;&lt;' />"; } 
    if($page < $total_pages) 
    { $next = ($page + 1); 
     echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\"><input type='submit' value='&gt;&gt;' /></a>"; 
    } 
    if($page == $total_pages) 
    { echo "<input type='submit' value='&gt;&gt;' />"; 
    } 
} 
echo '</span></p>'; 
?> 

// the query, currently selecting all but which I would have include a WHERE clause (WHERE ProfileID = $profileid...) 

<?php 
$sql = ("SELECT * FROM profiles 
    ORDER BY ProfileID 
    LIMIT $from, $max_results"); 

$rs = mysql_query($sql); 

while($row = mysql_fetch_array($rs)) 
{ 

$profile = $row['ProfileID']; 

?> 

<form id="profile-form" action="profile-engine.php" method="post"> 
    <input type="hidden" name="formid" value="edit-profile"> 
    <input type="hidden" name="thisprofile" value="<?php echo $profile; ?>"> 

    <table id="profile-detail" class="profile-form"> 
    <tr> 
     <td>   
    <label for="profile-name">Name:</label> 
     <?php 
      $nameresult = mysql_query("SELECT ProfileName 
          FROM profiles 
          WHERE ProfileID = '$profile'"); 
      $row = mysql_fetch_array($nameresult); 
     ?> 
    <input type="text" class="text" name="profile-name" id="profile-name" value="<?php echo $row['ProfileName']; ?>" /> 
     </td> 

//goes on in this vein for another 16 inputs :) 

的搜索查詢:

//connection established   

$query = "SELECT * FROM profiles"; 

$postParameters = array("name","height","gender","class","death","appro","born","tobiano","modifier","adult","birth","sire","dam","breeder","owner","breed","location"); 
$whereClause = " WHERE 1 = 1"; 
foreach ($postParameters as $param) { 
    if (isset($_POST[$param]) && !empty($_POST[$param])) { 
     switch ($param) { 
      case "name": 
       $whereClause .= " AND ProfileName LIKE '%".$_POST[$param]."%' "; 
       break; 
      case "height": 
       $whereClause .= " AND ProfileHeight='".$_POST[$param]."' "; 
       break; 
      case "gender": 
       $whereClause .= " AND ProfileGenderID='".$_POST[$param]."' "; 
       break; 
//more cases.... 
     } 
    } 
} 
$query .= $whereClause; 

$result = mysql_query("$query"); 

$values = array(); 
while ($row = mysql_fetch_array($result)) { 
$values[] = $row['ProfileID']; 
    } 
/* 
//just me checking that it worked... 
foreach($values as $value => $id){ 
    echo "$id <br />"; 
} 
*/ 


    mysql_close($con); 

所以,你有它!預先感謝任何幫助!

回答

0

那麼: 搜索將搜索項複製到本地變量,服務返回您保存的結果數組,然後分頁使用JS將值放入適當的字段中。如果更改一個並保存,其提交的修改,包括原來的搜索詞,它是用來重新查詢服務,並返回更新陣列...根據需要重複

這裏的一些示例代碼(這裏有只有兩個搜索字段,我知道你需要更多):

<!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>Untitled Document</title> 
<script type="text/javascript"> 
var query = new Object(); 
var resp; 
var i; 
function next(on){ 
    i=on+1; 
    if(i==resp.results.length){i--;} 
    fillForm(i); 
} 
function prior(on){ 
    i=on-1; 
    if(i<0){i++;} 
    fillForm(i); 
} 

function fillForm(i){ 
    document.getElementById("paginate").innerHTML='<button onclick="prior('+i+')">&lt;</button>'+(i+1)+' of '+resp.results.length+'<button onclick="next('+i+')">&gt;</button>'; 
    document.getElementById("name").value=resp.results[i].name; 
    document.getElementById("height").value=resp.results[i].height; 
    document.getElementById("gender").value=resp.results[i].gender; 
    document.getElementById("class").value=resp.results[i].class; 
    document.getElementById("death").value=resp.results[i].death; 
    document.getElementById("appro").value=resp.results[i].appro; 
    document.getElementById("born").value=resp.results[i].born; 
    document.getElementById("tobiano").value=resp.results[i].tobiano; 
    document.getElementById("modifier").value=resp.results[i].modifier; 
    document.getElementById("adult").value=resp.results[i].adult; 
    document.getElementById("birth").value=resp.results[i].birth; 
    document.getElementById("sire").value=resp.results[i].sire; 
    document.getElementById("dam").value=resp.results[i].dam; 
    document.getElementById("breeder").value=resp.results[i].breeder; 
    document.getElementById("owner").value=resp.results[i].owner; 
    document.getElementById("breed").value=resp.results[i].breed; 
    document.getElementById("location").value=resp.results[i].location; 
    document.getElementById("id").value=resp.results[i].id; 
    document.getElementById("saveButton").innerHTML='<button onclick="save()">Save</button>'; 
} 
function getData(){ 
    query.name=document.getElementById('query_name').value; 
    query.gender=document.getElementById('query_gender').value; 
    var variables=''; 
    variables+='name='+query.name; 
    variables+='&gender='+query.gender; 
    var xmlhttp; 
if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
    } 
else 
    {// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
xmlhttp.onreadystatechange=function() 
    { 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) 
    { 
    resp = JSON.parse(xmlhttp.responseText); 
    fillForm(0); 
    } 
    } 
xmlhttp.open("GET","searchNav.php?"+variables,true); 
xmlhttp.send(); 
} 

function save(){ 
    var saving=""; 
    saving+='?q='+query; 
    saving+='&name='+document.getElementById('name').value; 
    saving+='&height='+document.getElementById('height').value; 
    saving+='&gender='+document.getElementById('gender').value; 
    saving+='&class='+document.getElementById('class').value; 
    saving+='&death='+document.getElementById('death').value; 
    saving+='&appro='+document.getElementById('appro').value; 
    saving+='&born='+document.getElementById('born').value; 
    saving+='&tobiano='+document.getElementById('tobiano').value; 
    saving+='&modifier='+document.getElementById('modifier').value; 
    saving+='&adult='+document.getElementById('adult').value; 
    saving+='&birth='+document.getElementById('birth').value; 
    saving+='&sire='+document.getElementById('sire').value; 
    saving+='&dam='+document.getElementById('dam').value; 
    saving+='&owner='+document.getElementById('owner').value; 
    saving+='&breed='+document.getElementById('breed').value; 
    saving+='&breeder='+document.getElementById('breeder').value; 
    saving+='&location='+document.getElementById('location').value; 
    saving+='&id='+document.getElementById('id').value; 
     var xmlhttp; 
if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
    } 
else 
    {// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
xmlhttp.onreadystatechange=function() 
    { 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) 
    { 
    resp = JSON.parse(xmlhttp.responseText); 
    fillForm(0); 
    } 
    } 
xmlhttp.open("GET","saveEdits.php"+saving,true); 
xmlhttp.send(); 
} 
</script> 
</head> 
<body> 
<div id="search"> 
    <table> 
    <tr><td>Name:</td><td><input type="text" id="query_name" /></td></tr> 
    <tr><td>Gender:</td><td><input type="text" id="query_gender" /></td></tr></table> 
    <button onclick="getData()">Search</button> 
</div> 
<div id="results"> 
    <div id="paginate"></div> 
    <input type="hidden" id="id" /> 
    <table> 
    <tr><td>Name:</td><td><input type="text" id="name" /></td></tr> 
    <tr><td>Height:</td><td><input type="text" id="height" /></td></tr> 
    <tr><td>Gender:</td><td><input type="text" id="gender" /></td></tr> 
    <tr><td>Class:</td><td><input type="text" id="class" /></td></tr> 
    <tr><td>Death:</td><td><input type="text" id="death" /></td></tr> 
    <tr><td>Appro:</td><td><input type="text" id="appro" /></td></tr> 
    <tr><td>Born:</td><td><input type="text" id="born" /></td></tr> 
    <tr><td>Tobiano:</td><td><input type="text" id="tobiano" /></td></tr> 
    <tr><td>Modifier:</td><td><input type="text" id="modifier" /></td></tr> 
    <tr><td>Adult:</td><td><input type="text" id="adult" /></td></tr> 
    <tr><td>Birth:</td><td><input type="text" id="birth" /></td></tr> 
    <tr><td>Sire:</td><td><input type="text" id="sire" /></td></tr> 
    <tr><td>Dam:</td><td><input type="text" id="dam" /></td></tr> 
    <tr><td>Breeder:</td><td><input type="text" id="breeder" /></td></tr> 
    <tr><td>Owner:</td><td><input type="text" id="owner" /></td></tr> 
    <tr><td>Breed:</td><td><input type="text" id="breed" /></td></tr> 
    <tr><td>Location:</td><td><input type="text" id="location" /></td></tr> 
    </table> 
    <div id="saveButton"></div> 
</div> 
</body> 
</html> 

和搜索:

<?php 
//connection established   
$query = "SELECT * FROM profiles"; 
$postParameters = array("name","height","gender","class","death","appro","born","tobiano","modifier","adult","birth","sire","dam","breeder","owner","breed","location"); 
$whereClause = " WHERE 1 = 1"; 
foreach ($postParameters as $param) { 
    if (isset($_POST[$param]) && !empty($_POST[$param])) { 
     $whereClause .= " AND ".$param."='".$_POST[$param]."'"; 
    } 
} 
$query .= $whereClause; 
$result = mysql_query("$query"); 
echo "{\"results\":"; 
if($row = mysql_fetch_array($result,MYSQL_ASSOC)) { 
echo "["; 
    echo "{\"name\":\"".$row["name"]."\","; 
    echo "\"height\":\"".$row["height"]."\","; 
    echo "\"gender\":\"".$row["gender"]."\","; 
    echo "\"class\":\"".$row["class"]."\","; 
    echo "\"death\":\"".$row["death"]."\","; 
    echo "\"appro\":\"".$row["appro"]."\","; 
    echo "\"born\":\"".$row["born"]."\""; 
    echo "\"tobiano\":\"".$row["tobiano"]."\""; 
    echo "\"modifier\":\"".$row["modifier"]."\""; 
    echo "\"adult\":\"".$row["adult"]."\""; 
    echo "\"birth\":\"".$row["birth"]."\""; 
    echo "\"sire\":\"".$row["sire"]."\""; 
    echo "\"dam\":\"".$row["dam"]."\""; 
    echo "\"breeder\":\"".$row["breeder"]."\""; 
    echo "\"owner\":\"".$row["owner"]."\""; 
    echo "\"breed\":\"".$row["breed"]."\""; 
    echo "\"location\":\"".$row["location"]."\""; 
    //echo "\"id\":\"".$row["id"]."\""; 
    echo "}"; 
} 
else{ 
echo "\"no\"}"; 
exit; 
} 
while($row = mysql_fetch_array($data,MYSQL_ASSOC)){ 
    echo ",{\"name\":\"".$row["name"]."\","; 
    echo "\"height\":\"".$row["height"]."\","; 
    echo "\"gender\":\"".$row["gender"]."\","; 
    echo "\"class\":\"".$row["class"]."\","; 
    echo "\"death\":\"".$row["death"]."\","; 
    echo "\"appro\":\"".$row["appro"]."\","; 
    echo "\"born\":\"".$row["born"]."\""; 
    echo "\"tobiano\":\"".$row["tobiano"]."\""; 
    echo "\"modifier\":\"".$row["modifier"]."\""; 
    echo "\"adult\":\"".$row["adult"]."\""; 
    echo "\"birth\":\"".$row["birth"]."\""; 
    echo "\"sire\":\"".$row["sire"]."\""; 
    echo "\"dam\":\"".$row["dam"]."\""; 
    echo "\"breeder\":\"".$row["breeder"]."\""; 
    echo "\"owner\":\"".$row["owner"]."\""; 
    echo "\"breed\":\"".$row["breed"]."\""; 
    echo "\"location\":\"".$row["location"]."\""; 
    //echo "\"id\":\"".$row["id"]."\""; 
    echo "}"; 
} 
echo "]}"; 
?> 
+0

哇 - lemmie嘗試了這一點。謝謝! – Eamonn 2011-05-18 08:06:57

+0

@robot大聲笑,愛字段值:) – Eamonn 2011-05-18 08:10:19

+0

@robot你可以展示如何將它用於多個搜索字段(我不知道Javascript的語法)嗎?助教。我也不確定該怎麼去服務腳本 - 它看起來像我使用硬編碼的值,而不是生成的(這只是我,我知道!)... – Eamonn 2011-05-18 08:49:04