2013-04-03 34 views
0

我發現很多這樣從mysql查詢中獲取數據的例子。我想回傳給jQuery函數的是JSON格式的解析html內容。我想要做的是從頁面的初始加載中分離出PHP代碼。我在jQuery Mobile中這樣做。我知道,我的描述很糟糕,很難說清楚。.getJSON函數,追加到PHP的url,從PHP文件返回數據

我從index.html中的數據庫和輸出列表視圖中獲取文章名稱和鏈接。當用戶點擊一篇文章時,我將URL中的鏈接傳遞給jQuery,並嘗試調用解析該URL的PHP​​文件。這裏的所有代碼:

的index.html:

<body> 
    <div data-role="page" id="mainNews"> 
    <div data-role="content"> 
     <ul id="fnews" data-role="listview"></ul> 
    </div> 
</div> 
<script src="js/getmainnews.js"></script> 
</body> 

getmainnews.js:

var serviceURL = "http://localhost/basket/services/"; 
var news; 
$('#mainNews').bind('pageinit', function(event) { 
getNews(); 
}); 
function getNews() { 
$.getJSON(serviceURL + 'getmainnews.php', function(data) { 
    $('#fnews li').remove(); 
    mainnews = data.items; 
    $.each(mainnews, function(index, mnews) { 
     $('#fnews').append('<li data-icon="false"><a style="white-space:normal;" href="newstext.html?url=http://www.basket-planet.com' + mnews.link + '">' + mnews.article + '</a></li>'); 
    }); 
    $('#fnews').listview('refresh'); 
}); 
} 

getmainnews.php:

<?php 
include 'connect_to_mysql.php'; 
mysql_query("SET NAMES UTF8"); 
$sql = mysql_query("SELECT * FROM basket_news"); 
$mainnews = array(); 
while ($r = mysql_fetch_assoc($sql)){ 
$mainnews[] = $r; 
} 
echo '{"items":'. json_encode($mainnews).'}'; 
?> 

一切負載罰款,但接下來的頁面出現空了。接下來的3個文件...對不起,這是一個很長的話題。

newstext.html:

<body> 
<div data-role="page" id="newstext"> 
    <div data-role="content"> 
     <div id="textcontent"></div> 
    </div> 
</div> 
</body> 

newstext.js:

var serviceURL = "http://localhost/basket/services/"; 
$('#newstext').bind('pageshow', function(event) { 
var url = getUrlVars()["url"]; 
$.getJSON(serviceURL + 'getnewstext.php?url='+url, displayNewsText); 
}); 
function displayNewsText(data){ 
var newstext = data.item; 
$('#textcontent').append(newstext); 
} 
function getUrlVars() { 
var vars = [], hash; 
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&'); 
for(var i = 0; i < hashes.length; i++) 
{ 
    hash = hashes[i].split('='); 
    vars.push(hash[0]); 
    vars[hash[0]] = hash[1]; 
} 
return vars; 
} 

最後getnewstext.php:

<?php 
include_once ('simple_html_dom.php'); 
    $url = $_GET['url']; 
    $html = file_get_html(''.$url.''); 
    $article = $html->find('div[class=newsItem]'); 
    $a = str_get_html(implode("\n", (array)$article)); 
    @$a->find('div[style*=float]', 0)->outertext = ''; 
    @$a->find('h3', 0)->outertext = ''; 
    @$a->find('div[id=comments]', 0)->outertext = ''; 
    @$a->find('script', 0)->outertext = ''; 
    @$a->find('a[href*=register]', 0)->outertext = ''; 
    @$a->find('div', 4)->outertext = ''; 
    @$a->find('div', 6)->outertext = ''; 
    echo '{"item":'. json_encode($a) .'}'; 
?> 

因此,這些都是在6個文件。在newstext.html中,URL包含我需要傳遞給我的PHP文件的URL。我確定這個問題出現在最後三個文件中。我在這裏做錯了什麼?提前致謝!

更新:控制檯錯誤已修復,但頁面上仍然沒有輸出。

更新2:從四處搜索,我看到json_encode只接受數組。最後一個PHP腳本中的$ a是來自包含各種html標籤的頁面的內容。我怎樣才能把它變成一個數組?要使一個鍵/值的值是所有html內容?

回答

1

錯誤getUrlVars is not defined newstext.js:3說明了一切。你正在調用一個沒有在任何地方定義的函數。我做了一個快速搜索,你可能想要this

function getUrlVars() 
{ 
    var vars = [], hash; 
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&'); 
    for(var i = 0; i < hashes.length; i++) 
    { 
     hash = hashes[i].split('='); 
     vars.push(hash[0]); 
     vars[hash[0]] = hash[1]; 
    } 
    return vars; 
} 
+0

謝謝,這擺脫了那個錯誤。現在我猜測我的最後一個PHP腳本有問題。我仍然得到一個空白頁。那裏有什麼問題? – denikov 2013-04-03 22:25:48