Ok i’ve got an article in a database and want to show a preview/summary of the first 50 charcters or so to the users, how do i create a function to do this? I’m a bit new to this PHP lark so sorry if it sounds a bit…you know.
@DurbsauthorAug 16.2004 — #Actually please don't ignore me i need help!!
Right i thought that i had been so clever creating the following code to summrise the diffrent articles on my site, but when i executed it, it wouldn't run. I've got it to create the <hr /> but can't work out y it won't display the content from my database. Ne one got NE ideas? Heres the code:[code=php]<?php //include the database connection script include_once ('../../db_connect.php');
//query for the 10 most recent articles $query_recent = "SELECT article_id, title, content, upload_date FROM article ORDER BY upload_date desc limit 2";
$results = @mysql_query($query_recent);
if ($results) //check it mysqled ok { $summary_string = "<hr />";
@MstrBobAug 16.2004 — #Though this won't cure your problems:
$article_ID is being used in the string, when in reality it is $article_id (PHP is case sensitive!)
Also, unless you want to cut off articles mid-word, think about [URL=http://us4.php.net/manual/en/function.str-word-count.php]str_word_count[/URL] You can use it to limit your articles to a number of words, so you don't get something like this: This is a faux entry I wrote to give you an idea of what will happen with your current system. When it reaches a certain point, it will cut off mid-word in stead of letthing the p[/QUOTE] Instead, you finish the word:
This is a faux entry I wrote to give you an idea of what will happen with your current system. When it reaches a certain point, it will cut off mid-word in stead of letthing the phrase[/QUOTE]
@DurbsauthorAug 16.2004 — #cheers m8ee thats got it. I see wot you mean about cutting the words apart, didn't think to far ahead on that one. Is it possible to stop the summary at the first "." or ","?
@DurbsauthorAug 16.2004 — #with the str_word_count i'm not sure how to implment this to my advantage as it only counts the amount of words. Are you hinting at using a while loop to count the words until it reaches a set number? That wouldn't work tho would it as it doesn't count word by word, bugger.
hmmmmm.....i'll have to sit here and scratch my head for a bit.
*edit*
I think 05:30AM iiis taking it's toll on me, starting to make my self look like a retard. looking at the output for str_word_count i see what you mean, i think.
@DurbsauthorAug 16.2004 — #right i think i'm on the right track heres wot i've got: [CODE] $article_text = str_word_count($row_array[2], 2); $article_summary = substr($article_text, 1, $article_text[not sure wot to put in here]); [/CODE]So if i've got it right the string $article_text holds an array of where each word starts (in characters) from the start of the string, yes? to get this in to the substr() to only display the first 20 words it would be like this:[CODE]$article_text = str_word_count($row_array[2], 2); $article_summary = substr($article_text, 1, $article_text[20]); [/CODE]YES/NO? If i don't sort this i'll never get to sleep...
@DurbsauthorAug 16.2004 — #That was close to the answer but i found that it would set the correct length on some but not others. Luckily with my last ounce of energy i managed to come up with this to sort it out:[code=php]$array = str_word_count($row_array[2], 2); for ($word = 0; $word < 50; $word = $word + 1) { next($array2); } $end = key($array2); $article_summary = substr($row_array[2], 0, $end);[/code] Let me rest in peace (well get a couple of hours kip).