therealwesfoster Posted January 24, 2008 Share Posted January 24, 2008 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 Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted January 27, 2008 Share Posted January 27, 2008 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. Quote Link to comment Share on other sites More sharing options...
ithicus Posted February 3, 2008 Share Posted February 3, 2008 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. Quote Link to comment Share on other sites More sharing options...
therealwesfoster Posted February 3, 2008 Author Share Posted February 3, 2008 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.