News Story Preview.. Read more link.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
odie2828
Forum Commoner
Posts: 39
Joined: Tue Aug 05, 2008 4:40 pm

News Story Preview.. Read more link.

Post by odie2828 »

Everah | Please use bbCode tags when posting code in the forums. We have that rule in place for a reason.

I want to only show a preview of the text that is getting pulled from MYSQL, and the have it place a "read more" link. then when you click the link only that story will come uup in a pop up window.

Here is my code:

Code: Select all

<?php
 
 
 
include("config.php");
 
       $result = mysql_query("SELECT * FROM news ORDER BY newsid DESC",$connect);
 
        
        $text = $_POST['text1'];
        $newtext = wordwrap($text, 8, "\n");
        
        while($myrow = mysql_fetch_assoc($result))
 
             {
    $text = $myrow['text1'];
        
               echo "<b>Title: ";
 
               echo $myrow['title'];
 
               echo "</b><br>On: <i>";
 
               echo $myrow['dtime'];
 
               echo "</i><hr align=left width=160>";
 
               echo wordwrap($text, 30, "<br />");
 
               echo "<br><a href=\"read_more.php?newsid=$myrow[newsid]\">Read More...</a>
 
                || <br><hr align=left width=160>";
 
             }
 
?>

Please help......
Last edited by RobertGonzalez on Tue Aug 05, 2008 4:45 pm, edited 1 time in total.
Reason: Apparently it was too hard to actually push the code button.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: News Story Preview.. Read more link.

Post by RobertGonzalez »

What is wrong with your code now?
odie2828
Forum Commoner
Posts: 39
Joined: Tue Aug 05, 2008 4:40 pm

Re: News Story Preview.. Read more link.

Post by odie2828 »

ok i am sorry i am new to this.

i figured it out though. thank you.

here is my new code:

Code: Select all

<?php
 
 
 
include("config.php");
 
       $result = mysql_query("SELECT * FROM news ORDER BY newsid DESC",$connect);
 
        
        $text = $_POST['text1'];
        $newtext = wordwrap($text, 8, "\n");
        
        while($myrow = mysql_fetch_assoc($result))
 
             {
        $text = $myrow['text1'];
        $text2 = wordwrap($text, 30, "<br />");
               
 
               echo "<b>Title: ";
 
               echo $myrow['title'];
 
               echo "</b><br>On: <i>";
 
               echo $myrow['dtime'];
 
               echo "</i><hr align=left width=160>";
 
               echo substr($text2, 0, 200);
 
               echo "<br><a href=\"read_more.php?newsid=$myrow[newsid]\">Read More...</a>
 
                || <br><hr align=left width=160>";
 
             }
 
?>
odie2828
Forum Commoner
Posts: 39
Joined: Tue Aug 05, 2008 4:40 pm

Re: News Story Preview.. Read more link.

Post by odie2828 »

How do i limit the number of news storries that it will show when it is fetching the results?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: News Story Preview.. Read more link.

Post by RobertGonzalez »

by using a limit clause in your SQL or by setting a loop limit when displaying.
Dynamis
Forum Contributor
Posts: 122
Joined: Thu Jul 10, 2008 3:15 pm
Location: Indiana, US

Re: News Story Preview.. Read more link.

Post by Dynamis »

If I am understanding you correctly, you want to limit how many news stories are printed out. So instead of using:

Code: Select all

while($myrow = mysql_fetch_assoc($result))
Use a for loop:

Code: Select all

for($x=0;$myrow = mysql_fetch_assoc($result) && $x < 20; $x++)
Replace 20 with how many stories you want it to show.
odie2828
Forum Commoner
Posts: 39
Joined: Tue Aug 05, 2008 4:40 pm

Re: News Story Preview.. Read more link.

Post by odie2828 »

That didnt work.

it didnt display any data from the SQL.

can i put somthing in this clause:

Code: Select all

 
$result = mysql_query("SELECT * FROM news ORDER BY newsid DESC",$connect);
 
Dynamis
Forum Contributor
Posts: 122
Joined: Thu Jul 10, 2008 3:15 pm
Location: Indiana, US

Re: News Story Preview.. Read more link.

Post by Dynamis »

Thats what Everah mentioned. You use the MySQL LIMIT statement. If this doesn't explain enough, just google "MySQL Limit".
odie2828
Forum Commoner
Posts: 39
Joined: Tue Aug 05, 2008 4:40 pm

Re: News Story Preview.. Read more link.

Post by odie2828 »

Thank you very much for your help!!

I used the LIMIT code.

Code: Select all

 
$result [color=#0000FF]= mysql_query[/color]("[color=#FF0000]SELECT * FROM news ORDER BY newsid DESC LIMIT 0, 5"[/color],$connect);
 
Post Reply