Jump to content

Recommended Posts

Hello,

 

I'm not finding any tutorials on this so I'm looking for some help and my books aren't helping.  I'm building a PHP/mySQL site where there will be two news content areas on the home page.  On the left is a repeat region of dynamic text just to show the headline and the first few words of text (a short preview).  When one of these are clicked I want the complete article to appear in the content area on the right.  The articles are coming from a single column in the database so my problem is that I can't figure out how to limit the amount of text that is displayed in each field of the repeat region on the left (the previews).  It needs to display just the headline and 2 or 3 lines of text.  I found one article that used a $position command but I couldn't get it to work.

 

Beyond the question of how do I limit the output for the preview area repeat regions, I have one other question.  I mentioned that I want the complete article to update on the right side of that page when one of the previews are clicked.  I'd like to do this without a page refresh.  I did see that it can be done with AJAX but this is a major part of the site and I would hate to see it be useless on anyone without javascript enabled.  Is there a way to do this or am I going to have to use the master detail set to refresh on another page.

 

Hopefully, I've got this explained good enough, this is my first dynamic site.

 

Thanks in advance,

I think what you're looking for is something like http://ca.php.net/manual/en/function.substr.php which will return a section of a string.

 

However, I'd suggest looking into creating your own version of the function as that way will cut off words.. perhaps substr to capture the first 200 characters, and then drop the characters after the last space and replace it with .... You can couple substr with strrpos http://ca.php.net/manual/en/function.strrpos.php to achieve this.

 

As for reloading the text without reloading the page, sadly, without javascript your'e out of luck. However, if the content stays fairly static, I'd suggest implementing a caching system. That way at least you don't have to poll the database every refresh.

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_substr

 

Best practice here is not to limit to x number of characters as this may lead to some ambarrasing lines of text instead limit to some amount of words by using a combination of words and the total length of the string. If the total length exceeds the maximum length then x number of words less to meet the maximum length.

Thanks for the replies.  I did go back and made another try on the one script that I had found but it still doesn't work for some reason.  If I can get it to work it supposedly lengthens the maximum character amount so it displays complete words to avoid the embarrassing partial words.

 

Here's what I have in there right now:

 

          <div><?php echo $row_rs_news_content['nf_article_heading']; ?><?php echo $row_rs_news_content['nfcreatedon']; ?><?php echo $row_rs_news_content['nfarticle']; ?>

          <?

$position=80; // Define how many characters you want to display.

 

$message=echo $row_rs_news_content['nfarticle'];

$post = substr($message,$position,1); // Find what is the last character displaying. We find it by getting only last one character //from your display message.

 

if($post !=" "){ // In this step, if last character is not " "(space) do this step .

 

// Find until we found that last character is " "(space)

// by $position+1 (14+1=15, 15+1=16 until we found " "(space) that mean character 20)

while($post !=" "){

$i=1;

$position=$position+$i;

 

$post = substr($message,$position,1);

}

 

}

 

$post = substr($message,0,$position); // Display your message

echo $post;

echo "...";

?>

 

Currently it's showing an error on this line --- $message=echo $row_rs_news_content['nfarticle'];

 

Sorry for the lack of good information but this is only my 4th site design and my first php so I don't know much.  Normally, I would read the books etc before I dove in but I'm under the gun to get this thing up and running so I'm having to learn as I go.

 

Xangelo - I just want to be sure I know what you're talking about when you mentioned caching and whether or not the info is static.  Each row in the database will be static, in other words once that article has been loaded in the database then it won't change but new rows will be added all the time.  I assume that would mean that it is static but just wanted to get more input from you on that.

 

Now that I've found out that the page has to be refreshed that brings up another question.  The menu buttons on the index page will be used to filter the articles like "articles by state"  If the detail has to come up on a new page will it be possible to have the filtering remain in place on the next page or will it have to be filtered again?

 

Thanks again for your help.

A quick interesting problem :) Here's a quick solution:

 

function excerpt($message,$size = 100){

$message = substr($message,0,$size);
$point = strrpos($message,' ');
$message = substr($message,0,$point);

return $message.' ...';
}

 

That function will accept any string and an integer size. It then runs it through the substr function to ensure that the output is at MOST $size. Then it gets the position of the last " " character and reruns the substr function with this new value. It then returns the excerpt with a ' ...' appended.

Hi Xangelo,

 

I hate to keep bothering you with this but I've been working on it all morning and still can't get it to work.  In one configuration that I tried earlier it did limit the amount of characters but when I changed the size parameter it didn't change the number of characters that were returned.  It still had a fatal error in it at the end of the text too.  Additionally, there's something weird happening in the formatting of the output in that it blows the page apart.

 

I've made what changes I thought needed to be made for my circumstance and here is the code that I'm using now if you'd be so kind to look at it:

  <td width="287" bgcolor="#CCCCCC" class="content_thumnail_area"><?php do { ?>
          <div><?php echo $row_rs_news_content['nf_article_heading']; ?><br />
            <?php echo $row_rs_news_content['nfcreatedon']; ?><br />
	    <?php echo $row_rs_news_content['nfarticle']; ?> 
            <? function excerpt($nfarticle,$size = 50){
   
   $message = substr($nfarticle,0,$size);
   $point = strrpos($message,' ');
   $message = substr($message,0,$point);

   return $message.' ...';
}?></div>
                <?php } while ($row_rs_news_content = mysql_fetch_assoc($rs_news_content)); ?></td>
        </tr>

 

I've included the lines above where I'm calling the dynamic text.  I thought the line calling nfarticle could be a problem since it had no parameters but removing it didn't help either.

 

Again, sorry to keep bothering you with this.  I bought "PHP for Dummies" but I guess I should've got "PHP for Idiots"

 

Thanks again in advance.

 

Roger

 

 

The beauty of functions is that they don't need to show up where you use them. For example, you could have all your functions at the bottom of the page. To use it, you would just do this:

 

echo excerpt($row_rs_news_content['nfarticle'],50);

 

You don't need to modify the function in any way for it to work. Functions are like separate little blocks of code. You just have to pass them some values, and they work with them and spit out the desired result.

 

I don't like posting personal links, but I did a quick explanation of this earlier today on my website. It goes through a complete breakdown of the function and how it works: http://wheremy.feethavebeen.com/?p=146

Thanks to Xangelo's code I'm now able to limit the output as needed but now I'm up against my next problem and once again I'm looking for some help.  I can get the first record to appear as planned but this a repeating region and when it gets to the second record in the recordset I get an error and none of the text displays.  Searches for information on this subject have turned up nothing and my books don't cover it either.

 

I would really appreciate any help you can give.

 

Here's the error message I'm getting:

Fatal error: Cannot redeclare excerpt() (previously declared in C:\xampp\htdocs\theracersresource\index.php:192) in C:\xampp\htdocs\theracersresource\index.php on line 192

 

And this is the code on line 191 and 192:

<?php echo $row_rs_news_content['nfcreatedon']; ?><br />

            <?php function excerpt($message,$size = 300){

 

And here is the code with a few lines above and below the function lines.

            <td width="889" bgcolor="#CCCCCC" class="content_thumnail_area"><?php do { ?>
          <div><?php echo $row_rs_news_content['nf_article_heading']; ?><br />
            <?php echo $row_rs_news_content['nfcreatedon']; ?><br />
            <?php function excerpt($message,$size = 300){
   
   $message = substr($message,0,$size);
   $point = strrpos($message,' ');
   $message = substr($message,0,$point);

   return $message.' ...'; 
} 
?>
            <?php echo excerpt($row_rs_news_content['nfarticle'],300); ?><br />
            <br />
         </div>
            <?php } while ($row_rs_news_content = mysql_fetch_assoc($rs_news_content)); ?></td>
        </tr>

 

Thanks again in advance, you guys have been great help.

 

Roger

SaturdayNightSpecial, place the function code outside your do-while loop. The error you are encountering is because during the second iteration through the loop it encounters the function and tries to create it. However, it already exists, so it throws that error. You can put it right before the start of the do { section.

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.