/    Sign up×
Community /Pin to ProfileBookmark

Making a summary

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.

Cheers,
Durbs

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@DurbsauthorAug 16.2004 — Ignore me, accidentally missed the section headed "substring selection" in my little book.

Cheers NEhow,

Durbs
Copy linkTweet thisAlerts:
@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 />";

while ($row_array = mysql_fetch_row($results))
{
$article_id = $row_array['article_id'];
$article_title = $row_array['title'];
$article_text = $row_array['content'];
$article_summary = substr($article_text, 1, 180);

$summary_string .= "<h1><a href="index.php?ARTICLE_ID=$article_ID">$article_title</a></h1><p><a href="index.php?ARTICLE_ID=$article_ID">$article_summary</a></p>";
}

//display the results

echo $summary_string;

}

else
{
echo '<p>The database could not be accessed.</p><p>' . mysql_error() . '</p>';
}

// close the database
mysql_close();
?>[/code]


I am a bit of a novice when it come to PHP so don't laugh if its really obvious...

Cheers,

Durbs
Copy linkTweet thisAlerts:
@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]

Just a thought...

Edit: also, shouldn't it be:

$row_array[0];

$row_array[1];

$row_array[2];
Copy linkTweet thisAlerts:
@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 ","?
Copy linkTweet thisAlerts:
@MstrBobAug 16.2004 — [code=php]
$dpos=strpos($article_text, ".");
$article_summary = substr($article_text, 0, $dpos);
[/code]
Copy linkTweet thisAlerts:
@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.
Copy linkTweet thisAlerts:
@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...
Copy linkTweet thisAlerts:
@MstrBobAug 16.2004 — Damn, you're keeping me up! Okay, this SHOULD give you a 20 word summary, if not, well, I'll respond tomorrow...

[code=php]
<?PHP
$array1=str_word_count($row_array[2], 1);
$array2=str_word_count($row_array[2], 2);

while ($word = current($array2)) {
if ($word == $array1[5])
{$end=key($array2);break;}
next($array2);
}
$article_summary=substr($row_array[2], 0, $end);
?>
[/code]
Copy linkTweet thisAlerts:
@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).

PS. Cheers MstrBob you've been a absolute star.

Cheers,

Durbs
×

Success!

Help @Durbs spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...