Website creation query - too much text!

Soldato
Joined
31 Oct 2003
Posts
4,577
Location
Derby, UK
Right i am developing a site on my computer and the index.php page will, to begin with, have a very long article. Whats the easiest way to truncate it so theres only a certain number of words shown, then theres a link to another page with the full article, or the continuation of the text.

Im trying my best to make the site simple to maintain with regards to adding articles etc. Hope you understand and ill be really greatful for any advice!

Thanks, Rob :)
 
PHP:
function truncate($input, $len = 500) {
	$output = substr($input, 0, $len) . "...";
}

That function will truncate your text.
 
Last edited:
Ah thankyou kindly i knew it would be something relatively easy!

Now how would you recommend i structure the site? Lets say every week a new article gets put on the site (probably just in a text file thats read in by php, for now anyway). How would i do it so that aswell as a section of the article appearing on the index page, it also had its own page. I guess this is getting into the territory of having lots of includes. Just not sure how to structure the site.

Cheers,

Rob
 
Because i gave myself the challenge of making the site from scratch. Now ive got a fairly long way already considering my original php knowledge. Its just a project for myself to be honest, to get to know php and web design more in general.

So any ideas about the structure?
 
robjf said:
Because i gave myself the challenge of making the site from scratch. Now ive got a fairly long way already considering my original php knowledge. Its just a project for myself to be honest, to get to know php and web design more in general.

So any ideas about the structure?

I've designed systems like this from scratch before.

Along these lines

|index
|
|-includes
|--inc.sql.php
|--inc.config.php
|--inc.header.php
|--inc.footer.php
|
|-functions
|--func.components.php
|--func.modules.php
|
|-components
|--com_main
|---com_main.php
|---com_main.html.php
|--com_etc
|
|-modules
|--mod.bbcode.php

File structure.

files are accessed by index.php?com=com_main as an example. SQL used to store default components. Functions are global functions inside classes. $func_components->load_components(); as an example.

modules are usually accessed from components. $output = $mod_bbcode->parse($input);

Index looks something like this:

Code:
<?php

session_start();

define("PATH_SECURE", TRUE);

include('./functions/func.loader.php');
$func_loader = new func_loader;

$func_loader->load_functions();
$func_loader->load_components();
.....

I am currently developping a completly stand alone system along the lines of what you are making. Msn on profile if you would like more info :)
 
Last edited:
Jaffa_Cake said:
I am currently developping a completly stand alone system along the lines of what you are making. Msn on profile if you would like more info :)


Adding now, cheers for the info!
 
Jaffa_Cake said:
PHP:
function truncate($input, $len = 500) {
	$output = substr($input, 0, $len) . "...";
}

hi there, didn't see the point in doing my own thread for this as one is kind of open.

I too am trying to truncate my text but i'm having problems with it.

I don't fully understand how i add this function and then get it to truncate only a certain field from my database.

Im wanting to truncate for my homepage which will have a preview bit for the latest news bit and i know the title wil be fine but i want it to show like the first paragraph of the message partof the news, which in the databse is called post. At the moment i use the following code to ech out the whole news post. how would i add include the truncate function?

Code:
<?
include ('mysql_connect.php');
$query = "SELECT id, title, author, category, post, DATE_FORMAT(date, '%M %d, %Y') as sd FROM blog";
$result = @mysql_query($query);

if ($result) {
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {


echo "

<h3>$row[title]</h3><br />

<i>Posted on</i> $row[sd] <i>by </i> $row[author]

<br /><br />

$row[post]

<br /><br />

<hr />

";

}
} else {
echo 'There are no new articles to display<br />';
}
?>
 
Mammalian said:
Code:
<?
include ('mysql_connect.php');
$query = "SELECT id, title, author, category, post, DATE_FORMAT(date, '%M %d, %Y') as sd FROM blog";
$result = @mysql_query($query);

if ($result) {
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {


echo "

<h3>$row[title]</h3><br />

<i>Posted on</i> $row[sd] <i>by </i> $row[author]

<br /><br />

" . content_truncate($row[post]) . "

<br /><br />

<hr />

";

}
} else {
echo 'There are no new articles to display<br />';
}

function content_truncate($input, $len = 500) {
    $output = substr($input, 0, $len) . "...";
}  
?>

Try that :)
 
Last edited:
Mammalian said:
thanks for the quick reply jaffa. i put it in but doesn't seem to be showing any text for the post. Any clues why?

Post updated, try that

Edit: I'l actually go test my code before posting it.
 
My my, stupid error.

Forgot

return $output; in the function :D

fixed code:

Code:
<?
include ('mysql_connect.php');
$query = "SELECT id, title, author, category, post, DATE_FORMAT(date, '%M %d, %Y') as sd FROM blog";
$result = @mysql_query($query);

if ($result) {
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {


echo "

<h3>$row['title']</h3><br />

<i>Posted on</i> $row['sd'] <i>by </i> $row['author']

<br /><br />

" . content_truncate($row['post']) . "

<br /><br />

<hr />

";

}
} else {
echo 'There are no new articles to display<br />';
}

function content_truncate($input, $len = 500) {
    $output = substr($input, 0, $len) . "...";
    return $output;
}  
?>

Note:

I fixed small bits of your code.

$row[author] should be $row['author']. PHP should be giving you errors if you have errors enabled. Your much better of splitting your PHP and your HTML.

I tend to put my HTML inside a function, then send the results from the SQL to the function. Example:

Code:
$result = mysql_query("SELECT * FROM `books`");
$row = mysql_fetch_array($result, MYSQL_ASSOC);
book_html($row['title'], $row['author'], $row['caption']);

function book_html($title, $author, $caption);
?>
<b>This book was written by <?php echo $author; ?> <br /><br />
The name of the book is <?php echo $title; ?> <br /><br />
A small caption: <?php echo $caption; ?> </b>
<?php
}

This makes your code MUCH easier to read and so much easier when it starts getting large. You can also split up your HTML and PHP into different files, sending information from the logic file with all your queries to the HTML file which will output information.

Regards :)
 
Jaffa_Cake said:
$row[author] should be $row['author']. PHP should be giving you errors if you have errors enabled.

the weird thing is php is giving me errors if i put it the as $row['author'] but is ok with $row[author]. dont know whats thats about then, my code my be really screwed in the head or something.

Thanks for the help as i got it working a treat now.
 
Mammalian said:
the weird thing is php is giving me errors if i put it the as $row['author'] but is ok with $row[author]. dont know whats thats about then, my code my be really screwed in the head or something.

Thanks for the help as i got it working a treat now.

What version of PHP are you using?
 
Back
Top Bottom