Jump to content

[SOLVED] Big AJAX Question


therealwesfoster

Recommended Posts

Ok, right to the subject.

I have a page setup like so.

 

There are 4 links at the top for each News Category (ex: Dogs, Cars, Planes, Phones)

When someone clicks "Phones", I want the latest news entry to show up in the "big_div", and the last 5 phone articles to show in the "links_div". And when the user clicks one of the latest 5 news articles, I want it to show up in the "big_div".

 

+----------+

|c |c |c |c|

+------+--+

|        |lnk|

|BIG    |lnk|

|        |lnk|

+------+--+

 

How would I pull the data from the DB (or xml) and list them as separate links? I cant find a tutorial on this..

 

Thanks,

  Wes

Link to comment
https://forums.phpfreaks.com/topic/87520-solved-big-ajax-question/
Share on other sites

you need to query your ajax script with two parameters; so that you can get news articles and the links. you will need to create a seperate ajax function; so that your links can send another query to your php file and return the responseText to your div. you will have to limit you last 5 links from your database with PHP, based on how ever you want them to display.

Setup your database/xml so that each article also has a category, body, and post date. Here's a MySQL example:

 

CREATE TABLE article {
    `category` VARCHAR(50),   # Should be enough for your current categories and has some room to grow
    `body` TEXT, # 65535 characters ought to do it
    `postDate` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    `id` UNSIGNED INT NOT NULL AUTO_INCREMENT,
    PRIMARY KEY(`id`)
}

 

 

And then, you can just select the most recent articles (even a dynamic amount), format that them into JSON objects, group them all into a JSON array and then output or return (depending upon your environment) your results.  Here's a brief, hopefully correct, example:

 

$category = isset($_GET['category']) ? trim($_GET['category']) : '';
$count     = isset($_GET['count'])     ? (int)$_GET['count'] : 5;

// ...
// Connect to your database, do whatever else needs to be done, etc
// ...

$query = mysql_query("SELECT `body`, `postDate`, `id` FROM `article` WHERE `category`=" .mysql_escape_string($category). " ORDER BY `postDate` DESC LIMIT " . $count);
$result = array();

// You can figure out the efficiency on your own
while ($row = mysql_fetch_assoc($query)) {
    $result[] = '{' 
      'body: "' .addcslashes($row['body'], '"'). '", ' .
      'postDate: "' .addcslashes($row['postDate'], '"'). '",' .
      'id: ' .$row['id'].
    '}';
}

// Return a JSON array
echo "[" . implode(",", $result) . "]";
die;

 

Then you can parse the responseText into JSON and have fun with your javascript from there.

 

I hope that gets you going and that my code examples are correct but remember, they're just that: examples.

Thanks alot for the reply! I've seen other people use "JSON" on their sites, but I never knew what it was. I now know that it is JSON. Thanks for introducing me to that. (I had been returning html layouts and crappy xml docs instead).

 

I learned alot off of your example, so big thanks to you

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.