2013-03-15 88 views
-1

我一直在使用此代碼在我的網站上顯示一些推薦。自動提交表單和MYSQL詢問

這是到目前爲止的代碼:

// Select testimonials from database 
$q = "SELECT testimonial, city_name, name, web_address, image_name, membership_type 
     FROM testimonials 
     INNER JOIN city ON city.city_id = testimonials.city_id 
     ORDER BY date_added DESC LIMIT $start, $display"; 

$r = mysqli_query($dbc, $q); 

if ($records >= 1) { 

    while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { 
     $testimonial = $row['testimonial']; 
     //echo $testimonial; 
     $mytestimonial = nl2br($testimonial); 
     $city    = $row['city_name']; 
     $name    = $row['name']; 
     $url    = $row['web_address']; 
     $imageName  = $row['image_name']; 
     $type    = $row['membership_type']; 

     echo '<div class="testimonial-row"> 
        <div class="testimonial-image"> 
         <img src="'.UPLOAD_DIR.$imageName.'" /> 
        </div> 
        <div class="testimonial"> 
         <h2>'.$name.'</h2> 
         <h3>'; 
         if($type==1){ 
          echo 'A teacher'; 
         }elseif($type==2){ 
          echo 'An Institute'; 
         }elseif($type==3){ 
          echo 'A Student'; 
         } 
         echo " from <strong>$city</strong></h3> 
         <blockquote>$mytestimonial</blockquote> 
         <p class='user-url'><a href=''>$url</a></p> 
        </div>     
       </div>";  
    } 
} else { 
    echo "There is no any testimonial to display at this time. Please try again later."; 
} 

上面的代碼,我可以展示我的全部證明後運行。我有3種不同的推薦,目前在我的頁面中顯示所有種類的推薦。

現在我要選擇一個解決方案來根據其類型篩選和顯示它們。

這是我的選擇框:

<select class="select" name="type"> 
    <option value="1">Tutor</option> 
    <option value="2">Institute</option> 
    <option value="3">Student</option> 
</select> 

當選擇從選擇框,我過濾應requering我原來的查詢與選擇的類型發生的選項。我有一個解決方案與jQuery的顯示/隱藏功能,但它不符合預期的結果。

注:我不能使用提交按鈕與此選擇框。這就是爲什麼我正在尋找Ajax或Jquery的解決方案。

任何人都可以告訴我是否有可能嗎?

謝謝。

+0

肯定可以....在選擇框的變化情況.. AJAX調用,將篩選結果... – alwaysLearn 2013-03-15 12:04:26

+0

我可以知道我該怎麼做?我試着用jquery,但無法讓它工作。 – TNK 2013-03-15 12:06:16

+0

可能會幫你 http://stackoverflow.com/questions/14603807/php-mssql-filtering-from-user-input-html/14604096#14604096 – Samy 2013-03-15 12:07:31

回答

1

這可能是解決方案... 傳遞你的價值是這樣的:

    <select name="jumpMenu" class="sort-by-select" id="jumpMenu" onchange="MM_jumpMenu('parent',this,0)"> 
         <option value=""> Select type </option> 
         <option value ="yourpage?type=1">Tutor</option> 
         <option value ="yourpage?type=2">Institute</option> 
         <option value ="yourpage?type=3">Student</option> 

        </select> 

<script type="text/javascript"> 
function MM_jumpMenu(targ,selObj,restore) //v3.0 
{ 
    eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'"); 
    if (restore) selObj.selectedIndex=0; 
} 
</script> 

之後得到像這樣的價值..

if(isset($_GET['type']) && $_GET['type'] != "") 
{ 
$Q = $_GET['type']; 
    } 

在查詢將這個值

SELECT testimonial, city_name, name, web_address, image_name, membership_type 
    FROM testimonials 
    INNER JOIN city ON city.city_id = testimonials.city_id where membership_type='$Q' 
    ORDER BY date_added DESC LIMIT $start, $display 
+0

感謝您的回覆。你能告訴我如何重新查詢我的原始碼 – TNK 2013-03-15 12:23:25

+0

請查看我編輯的代碼... – Dev 2013-03-15 12:27:00

+0

但是,這段代碼並不適用於我的分頁。你能告訴我原因嗎? – TNK 2013-03-15 12:56:33

1

這是代碼示例代碼,可以給你一些想法,我建議你..

$(document).ready(function(){ 


$('#type').change(function(){ 
    var data = $("#type").val(); 
$.ajax({ 

     url : "yourdomain/yourfilterpage.php", 
     data: {'type':data}, 
     type : 'POST' , 
     success : function(data){ $('#divtoshowdata').html(data)} 
    }); 
}) 

}) 

<select class="select" name="type" id='type'> 
<option value="1">Tutor</option> 
<option value="2">Institute</option> 
<option value="3">Student</option> 

上yourfilterpage.php讓您的過濾結果