katie77 Posted January 13, 2008 Share Posted January 13, 2008 Ok I've got a news system and I'm querying the database to retrieve the last 3 news items like this: $query = "SELECT news_id, category, title, text, img_url, linkto, ext_url, DATE_FORMAT(date,'%d %M, %Y') as sd FROM $table $where_category ORDER BY date DESC LIMIT $start, 3" or DIE ("Can't retrieve"); $result=@mysql_query($query) or die("Error in News"); Now in three different places in my page I'm wanting the news titles to appear. There'll be a main news item with the first, there'll be a sub item (formatted quite differently) with the second and a third at the bottom of the page. As I've already retrieved the above to display news on the page, is there any way I could just ask it to retrieve the title of nth row? ie so in one place I could just have: <td>".$getNews['title'][2]."</td> to retrieve the title from the second row? As the layouts are so different, it's not something that's easy to put in a loop and I'm keen to make the code as optimised as possible. Many thanks! Quote Link to comment https://forums.phpfreaks.com/topic/85809-do-i-need-a-new-query-for-this/ Share on other sites More sharing options...
tinker Posted January 13, 2008 Share Posted January 13, 2008 As far as I know and i've searched, you need to iterate through. Therefore as long as you do it in order then its ok, otherwise (slightly more overhead) put it all into an array which can be accessed at any point. Or when you get it, iterate through and generate your blocks there and then, either into string vars or into <divs> which are positioned into place with css... Quote Link to comment https://forums.phpfreaks.com/topic/85809-do-i-need-a-new-query-for-this/#findComment-437937 Share on other sites More sharing options...
Barand Posted January 13, 2008 Share Posted January 13, 2008 After your code store the rows selected in an array $news = array(); while ($row = mysql_fetch_assoc($result) { $news[] = $row; } retrieve with, eg, echo $news[0]['title]; Quote Link to comment https://forums.phpfreaks.com/topic/85809-do-i-need-a-new-query-for-this/#findComment-437957 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.