AJayUK Posted August 2, 2009 Share Posted August 2, 2009 Hi everyone, Im new here and would like to start using this website as I am learning PHP. I have come across a problem, what I am trying to do is to display 3 leading stories, and then 3 headlines. So far I am able to display the leading stories but I am not sure how to avoid repetition by displaying the last 3 results as only headlines. Here is my code for a better example of what I am doing: <?php $result=mysql_query("select * from news where category='psychopathic' order by id desc LIMIT 0, 3"); while ($row = mysql_fetch_assoc ($result)) { $date = $row['postdate']; $date = date("D dS M", strtotime($date) ); $title = stripslashes ($row['title']); $intro = stripslashes (strip_tags($row['intro'])); $introcount = (substr($intro, 0,90)); /* display the data */ echo "<table width='100%' border='0' cellpadding='0'> <tr> <td align='left'><a href=\"article.php?id=$row[id]\" class='darkgreen2'>$title</a></td> </tr> <tr> <td align='left'><span class='datetext2'>$date | 5 Comments</span></td> </tr> <tr> <td align='left'><span class='blacktext2'>$introcount</span></td> </tr> <tr> <td width='210' height='9' valign='top'><table width='100%' border='0' cellpadding='0' cellspacing='0'> <tr> <td width='379' height='9'></td> </tr> </table></td> </tr> </table>"; /* finish up table*/ } echo ""; ?> I would now like to display only 3 headlines ($title) after this ? Any help would be really appreciated! Quote Link to comment Share on other sites More sharing options...
ldougherty Posted August 2, 2009 Share Posted August 2, 2009 Your query only returns the first three results with the LIMIT 0,3 therefore you have no retrieved this data to display. The best bet would be to change it to LIMIT 0,6 which would then give you all of the information for the first 6 results. Then take it one step further inside the while loop with a counter in the loop. After the counter hits 3 limit the data that is being written to only titles. Quote Link to comment Share on other sites More sharing options...
AJayUK Posted August 2, 2009 Author Share Posted August 2, 2009 Okay thankyou ldougherty, I understand your theory but I still cannot find the code to complete the task. Ive been looking for a solution using google but the examples i find for using while inside a loop with a counter, I only get examples using numbers and it doesnt help me. Im going to continue to read about using while inside a loop but I would be happy if someone could post the code that would help me achieve what I am needing? I hate to sound lazy P.S - I changed the Limit to 6. Quote Link to comment Share on other sites More sharing options...
avvllvva Posted August 3, 2009 Share Posted August 3, 2009 <?php $count = 1; $result=mysql_query("select * from news where category='psychopathic' order by id desc LIMIT 0, 6"); while ($row = mysql_fetch_assoc ($result)) { $date = $row['postdate']; $date = date("D dS M", strtotime($date) ); $title = stripslashes ($row['title']); $intro = stripslashes (strip_tags($row['intro'])); $introcount = (substr($intro, 0,90)); /* display the data */ echo "<table width='100%' border='0' cellpadding='0'> <tr> <td align='left'><a href=\"article.php?id=$row[id]\" class='darkgreen2'>$title</a></td> </tr>"; if($count<=3){ echo " <tr> <td align='left'><span class='datetext2'>$date | 5 Comments</span></td> </tr> <tr> <td align='left'><span class='blacktext2'>$introcount</span></td> </tr>"; } echo " <tr> <td width='210' height='9' valign='top'> <table width='100%' border='0' cellpadding='0' cellspacing='0'> <tr> <td width='379' height='9'></td> </tr> </table> </td> </tr> </table>"; /* finish up table*/ $count++; } echo ""; ?> 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.