2012-08-10 65 views
-2

我有一個網頁,其中產生使用SELECT查詢在MySQL數據庫中軌跡的音頻播放列表,即如何在不刷新頁面的情況下使用MySQL查詢過濾頁面上列出的項目?

$query1="SELECT * FROM tracks WHERE uploaded_page='$this_path_short' ORDER BY id DESC"; 

我想用戶能夠過濾以不同的方式播放列表,並計劃在一個表單中有一系列單選按鈕,這將生成一個新的MySQL查詢來列出所需的曲目。

我想知道用此查詢更新播放列表的最佳方式。理想情況下,這可以在不刷新頁面的情況下完成。

+0

我真的希望你[正確轉義您的SQL查詢(http://bobby-tables.com/php)。 – tadman 2012-08-10 23:07:34

回答

0

在非常大的線路:

  • 只是要與過濾器/playList.php?filter=一個Ajax請求。
  • 在repsonse中,您只放置播放列表而不是html頁眉和頁腳。
  • 用新的播放列表替換播放列表的內容。

也許你可以使用jQuery

-1

每當你想新的頁面內容而無需刷新頁面 - 解決方案是AJAX。我無法解釋AJAX的幾句話,但有一個很好的教程在這裏:

http://www.w3schools.com/ajax/default.asp

+0

謝謝,我已經成功地編寫了一些用於處理表單的簡單AJAX腳本,所以我對AJAX有一些瞭解。我不確定的是AJAX腳本如何與播放列表頁面交互。推測最好的方法是從使用表單提交的變量構造新查詢,但是當頁面第一次加載時,我不會使用這些變量來構造查詢,這是我卡住的地方。 – Nick 2012-08-10 22:49:07

3

添加AJAX頭:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> 

的按鈕使用示例:

<a href="#" onClick="$('#content_s').load('/sql.php?value=some_path');">News</a> 

你可以如果需要,請將$ content_s懸停在CSS文件中。

而在你的HTML來顯示數據:

<div id="content_s"> 
</div> 

你sql.php文件將是這樣的:

<?php 
$this_path_short = $_GET['value']; // stay frosty, it's SQLi vurnable, but this will get "some_path" I belive. I didn't test this though, but it should work 
$query1="SELECT * FROM tracks WHERE uploaded_page='$this_path_short' ORDER BY id DESC"; 
... 

您可以用PHP生成

編輯編輯編輯編輯鏈接編輯編輯編輯編輯

索引。 PHP 你說的路徑到播放列表中的index.php這樣:

<html> 
<head> 
<title>Playlist</title> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> 
</head> 
<?php $path = 'http://www.youtube.com/watch?v=Jx0oOZmY5KM'; ?> 
<div id="menu"> 
<a href="#" onClick="$('#content_s').load('playlist.php/?val=<?php echo $path ?>');">Load Playlist</a> 
</div> 
<div id="content_s"> 
</div> 
</html> 

playlist.php:

<?php 
$this_path_short = $_GET['val']; 
echo '$this_path_short = ' .$this_path_short.'<br>'; 
echo 'This is how the query looks like right now: "SELECT * FROM tracks WHERE uploaded_page=\''.$this_path_short.'\' ORDER BY id DESC";'; 
?> 

這個例子將給你它是如何工作

的想法1。

enter image description here

2.

enter image description here

+0

謝謝。我現在可以看到如何將內容添加到我的頁面,但是如何更新選擇內容的sql查詢? – Nick 2012-08-10 22:55:18

+0

只需將news.txt更改爲你的sql_query.php?value = this_path_short頁面,點擊按鈕後,$ this_path_short = $ _GET ['value'];當然,這僅僅是一個例子,SQL注入是可惡的,但你明白了吧? – 2012-08-10 22:57:45

+0

謝謝。只是想確保我已經瞭解你。它會從表單中獲取值,然後以某種方式將它傳遞給頁面上已有的查詢?這是第二部分,我仍然不清楚 – Nick 2012-08-10 23:01:27