Hello, and thanks in advance for your help...
I've been using a shortcut when it comes to article printing in that i just put two <br> tags inbetween each paragraph of whatever article i put into the database.
I have to stop doing this, so i looked it up and i found the nl2br() function in PHP.. unfortanatly the way that the function to print the article works would no longer work if i were to use nl2br().
You see, i also have in the function a method to break up the paragraphs of the article and place them into an array, and then i use the array and number of items in the array to determine where the pictures are distributed whithin the article.
the line that gets the paragraphs into the array is:
// $body being the full article
$paragraphs = explode("<br><br>",$body);
Is there a way where i can keep the same functionality but do away with the need to type in <br> tags everytime i submit an article?
thanks again for your help...
mike
For those who want to see the full function:
<?php
function printArticle ($articleID, $isComments) {
// If first time to page, then no article was chosen before, so get latest article
if ($articleID == 0) {
$sql = "Select * FROM articles ORDER BY id DESC LIMIT 0, 1";
} else {
$sql = "Select * FROM articles WHERE id = '$articleID'";
$pagination = "yes";
}
$result = mysql_query($sql)
or die("Unable to retrieve latest article:".mysql_error());
while ($rec = mysql_fetch_array($result)) {
$articleID = $rec["id"];
$date = $rec["date"];
$time = $rec["time"];
$day = $rec["day"];
$postedBy = $rec["posted_by"];
$title = $rec["title"];
$body = $rec["body"];
$category = $rec["category"];
// Print out the title of the article
echo '
<h1 class="h1_articleTitle">'.$title.'</h1>
';
// Call date function. (prints out IE. thursday, 14 October, 2004)
printDate($date, $time, $day);
// Print out the category of the article
echo '
<br><i><font color="#999999">'.$category.'</font></i><br>
';
// Dealing with the pictures. Returns all the pictures for a particular article based on file name.
// pics are numbered as such: article_id '_' pic number. IE. 13_2.jpg
function getFiles($dirname, $key) {
$handle=opendir($dirname);
while ($file = readdir($handle))
{
if($file=='.'||$file=='..')
continue;
else
{
if (eregi($key, $file)) { $result_array[]=$file; }
}
}
closedir($handle);
return $result_array;
}
// set $key for picture handling function
$a = '^' . $articleID . "_";
$pictures = getFiles("article_pics", $a);
$num_pics = count($pictures);
// The offending line: This is where the body text is broken up according to the BR tags
$paragraphs = explode("<br><br>",$body);
$num_para = count($paragraphs);
// Pictures are distributed through the article based on how many pictures there are and how many paragraphs there are.
if (!$pictures) {
$space = 1;
}
else {
// Basically if there are 3 pictures and 9 paragraphs. there will be a picture every third paragraph.
$space = $num_para / $num_pics;
}
// Because array's start at zero.
$num_pics = $num_pics-1;
$num_para = $num_para-1;
// lets ignore the decimal points.
$space = round($space);
$pic_count = 0;
$dev = $space;
// as long as there are more paragraphs... keep printing
for ($para_count = 0; $para_count <= $num_para; $para_count++) {
// if there are pictures, print the first one for the first paragraph
if ($pic_count <= $num_pics && $para_count == 0) {
// make sure that the pictures alternate sides using the if else statement in each echo.
echo '
<p>
<img src="article_pics/'.$pictures[$pic_count].'" border="1" hspace="5" '; if ($pic_count % 2 == 0) { echo ' align="right" '; } else { echo ' align="left" '; } echo ' >'. $paragraphs[$para_count].'
</p>
';
$pic_count++;
}
// Print remaining pictures equally spaced out among the remaining paragraphs.
else if ($pic_count <= $num_pics && $para_count == $dev) {
echo '
<p>
<img src="article_pics/'.$pictures[$pic_count].'" border="1" hspace="5" '; if ($pic_count % 2 == 0) { echo ' align="right" '; } else { echo ' align="left" '; } echo ' >'. $paragraphs[$para_count].'
</p>
';
$pic_count++;
$dev = $dev+$space;
}
else {
echo '
<p>'.$paragraphs[$para_count].'</p>
';
}
}
// the following just retrieves the number of comments for each article and works out the pagination
// whatever that is below here is not necessary to solve the above problem.. i just kept it in for those interested.
// nothing special
$numComments = numArticleComments ($articleID);
echo '
<br><font size="2">
<img src="images/obmon.gif" width="103" height="19" hspace="20">
</font>
';
if ($isComments == 1) {
echo '
<br><br>
<a href="comments.php?id='.$articleID.'#post">post comment</a> <a href="comments.php?id='.$articleID.'">read comments ('.$numComments.')</a>
';
}
// If $page exists set up pagination
if (!$pagination) {
break;
} else {
// Count the number of rows in the table
$resRows = mysql_query("SELECT * FROM articles");
$numRows = mysql_num_rows($resRows);
$previous = $articleID-1;
$next = $articleID+1;
if ($articleID == 1) {
echo '
<p align="right">< Previous Post <a href="read_entry.php?id='.$next.'">Next Post ></a></p>
';
}
else if ($articleID == $numRows) {
echo '
<p align="right"><a href="read_entry.php?id='.$previous.'">< Previous Post</a> Next Post ></p>
';
} else {
echo '
<p align="right"><a href="read_entry.php?id='.$previous.'">< Previous Post</a> <a href="read_entry.php?id='.$next.'">Next Post ></a></p>
';
}
}
}
}