Jump to content

Avoid Reptition Displaying PHP Results


AJayUK

Recommended Posts

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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 "";
?>    

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.