LukeBateson Posted May 31, 2011 Share Posted May 31, 2011 Right, it's hard to explain, but I'll try my best. Litterally, I have a 'tab' box on my homepage. And a table called 'news'. Now, the table has the rows "shorttitle" "shortstory" and "image" in it. I want the title the three latest titles to be on the three tabs, and the story and image in the respective content areas. (The table auto increments the ID, and so order by ID descending limit of 3 yes?) Now, the code for the tabs is: <?php $query = "SELECT * FROM news where published = '1' ORDER by id DESC LIMIT 3"; $row_news = mysql_fetch_array($query); ?> <div id="myTabs"> <ul> <li><a href="#firsttab"></a><? echo $row_news['shorttitle']; ?></li> <li><a href="#secondtab">newsID2</a></li> <li id="last"><a href="#thirdtab">newsID3 </a></li> </ul> <div id="firsttab" class="tab_content"> <div class="rafat"> <img src="<? echo $row_news['image']; ?>" /> <p> <? echo $row_news['shortstory']; ?> </p> </div> </div> <div id="secondtab" class="tab_content"> <div class="rafat"> <img src="newsIMAGE2" /> <p> newsSTORY2 </p> </div> </div> <div id="thirdtab" class="tab_content"> <div class="rafat"> <img src="newsIMAGE3" /> <p> newsSTORY3 </p> </div> </div> </div> Now, i've filled in the first tab with the MYSQL queries, but i'm unsure how to fill in the other two, because of the way it's coded.. I can't do a 'repeating' code, because of the IDs of the divs, and the three tabs are bunched together.. Anyone any ideas? Much apreciated. Thanks Luke Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 31, 2011 Share Posted May 31, 2011 Dump the results for all three records into an array, then use the array for the output <?php $query = "SELECT * FROM news WHERE published = '1' ORDER by id DESC LIMIT 3"; $row_news = mysql_fetch_array($query); $news = array(); while($row = mysql_fetch_assoc($row_news)) { $news[] = $row; } ?> <div id="myTabs"> <ul> <li><a href="#firsttab"><?php echo $news[0]['shorttitle']; ?></a></li> <li><a href="#secondtab"><?php echo $news[1]['shorttitle']; ?></a></li> <li id="last"><a href="#thirdtab"><?php echo $news[2]['shorttitle']; ?></a></li> </ul> <div id="firsttab" class="tab_content"> <div class="rafat"> <img src="<?php echo $news[0]['image']; ?>" /> <p><?php echo $news[0]['shortstory']; ?></p> </div> </div> <div id="secondtab" class="tab_content"> <div class="rafat"> <img src="<?php echo $news[1]['image']; ?>" /> <p><?php echo $news[1]['shortstory']; ?></p> </div> </div> <div id="thirdtab" class="tab_content"> <div class="rafat"> <img src="<?php echo $news[2]['image']; ?>" /> <p><?php echo $news[2]['shortstory']; ?></p> </div> </div> Quote Link to comment Share on other sites More sharing options...
LukeBateson Posted May 31, 2011 Author Share Posted May 31, 2011 I got this error Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/lukebate/public_html/**/includes/functions.php on line 68 Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/lukebate/public_html/**/includes/functions.php on line 70 Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 31, 2011 Share Posted May 31, 2011 Change this $row_news = mysql_fetch_array($query); $news = array(); while($row = mysql_fetch_assoc($row_news)) To this $result = mysql_query($query) or die(mysql_error()); $news = array(); while($row = mysql_fetch_assoc($result)) Quote Link to comment Share on other sites More sharing options...
LukeBateson Posted May 31, 2011 Author Share Posted May 31, 2011 Brilliant! Thanks! 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.