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
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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.