0

I found many examples of this getting data from a mysql query. What I want to pass back to jQuery function is parsed html content in JSON format. What I want to do is separate PHP code from my initial load of the page. I'm doing this in jQuery Mobile. I know, my description is bad, it's hard to phrase.

I get article names and links from my database and output listview in index.html. When the user clicks on an article, I'm passing the link in the URL, getting it in jQuery and trying to call a PHP file which Parses that URL. Here's ALL the code:

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).'}';
?>

Everything loads fine but the next page comes up empty. Here's the next 3 files...Sorry that this is such a long topic.

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;
}

And finally 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) .'}';
?>

So those are the 6 files. On the newstext.html, the URL contains the URL which I need to pass to my PHP file. I'm sure the problem is in the last three files. What am I doing wrong here? Thanks in advance!

Update: Console error fixed, but still no output on the page.

Update2: From searching around, I saw somewhere that json_encode only accepts arrays. My $a in last PHP script is content from a page containing all kinds of html tags. How can I turn this into an array? To make one key/value with the value being all that html content?

4

1 回答 1

1

错误getUrlVars is not defined newstext.js:3说明了一切。您正在调用一个未在任何地方定义的函数。我做了一个快速搜索,你可能想要这个

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;
}
于 2013-04-03T22:13:01.097 回答