Jump to content

php looping with html tags


phpnewby1918

Recommended Posts

Hello,

 

I just wanted to run over some code i'm using to loop through for a page. And wanted people critique on it really. It is a bit messy i feel trying to include all the html tags inside the loop in order for me to achieve the page look i want and whilst maintaining to xhtml standards. Would any one please take a look at my code which works and tell me how they would do it or if its done badly as i'm interested to know

 

$q = "SELECT title, content, get, link FROM content WHERE page='index'";
$r = mysqli_query($dbc, $q);
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {

	echo '<h2>'; // print for start of H-tag
	echo $row['title']; // print title inside H-tag
	echo '</h2><p>'; // close H-tag, line break, start of P-tag
	$get = $row['get'];
	$link = $row['link'];
	$content = $row['content'];
	$content = explode(' ', $content);
	$y = 90;
	$x = 0;
		while ($x < $y) {
			echo $content[$x].' '; // print content inside P-tag
				$x++;
				} // end of content 
	echo '</p><a href="seo.php?x='; // close P-tag, start A-Tag print link
	echo "$get"; // $_GET['']
	echo '">read more...&#187;&#187;</a>'; // close  url start the anchor text and close off </a>

} // End of while
	echo '</div>'; // End of left content
	echo '<div class="right">'; // Start of right div 
	echo '<h2>News</h2>'; // News title

 

I'd appreciate any feed back from more experienced users/coders

 

Thank you in advance :-)

Link to comment
Share on other sites

Perhaps...

$q = "SELECT title, content, get, link FROM content WHERE page='index'";
$r = mysqli_query($dbc, $q);
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$title = $row['title'];
$get = $row['get'];
$link = $row['link'];
$content = explode(' ', $row['content']);
array_splice($content, 2);
?>
<h2><?PHP echo $row['title']; ?></h2><p>
<?PHP echo $content; ?>
</p>
<a href="seo.php?x=<?PHP echo $get; ?>">read more...&#38;#187;&#38;#187;</a>
<?PHP
} // End of while
?>
</div>
<div class="right">
<h2>News</h2>
<?PHP
/* more code */
?>

Link to comment
Share on other sites

Thanks for your help, Its appreciated. I'm not sure that array_splice would give me the results I wanted though? Please correct me if i'm wrong. I'm exploding part of my content per story. So i have my title, then 90 words of content that i use to echo out as an intro so to speak and a link to view the fuller article. I had never thought to close the php tags then code php inside the html. So its been a good example or alternative, so thanks :-)

Link to comment
Share on other sites

Another method...

//    get the first xx words for the teaser
$content = $row['content']);
$max_words = 90;
if (count(explode(" ", $content))> $max_words) {
$content = substr($content, 0, strnpos($content, " ", $max_words));
}
$content = $content . "...";

Link to comment
Share on other sites

explode() can be problematic since there may be instances of multiple concurrent spaces. In those instances you will get less than the 90 words you are looking for. However, the solution to that (splitting by any instances of one or more spaces) has a problem where you will remove the duplicate spaces, but that may be wanted.

 

Here is a function I use that will treat multiple spaces as a single break between words, and they are replaced as a single space in the output.

function wordSlice( $input, $offset, $length=NULL)
{
  $words = preg_split('%[\s,]+%', trim($input));
  $words = ($length) ? array_slice($words, $offset, $length) : array_slice($words, $offset);
  $output = implode(' ', $words);
  return $output;
}

 

To get a string of the first 90 words you would use like so

echo wordSlice( $content, 0, 90)

 

That one doesn't add ellipses if the string is truncated, but it could be easily added. But, I do have a different function that will truncate a string based upon the number of characters and add an ellipse if needed. But, if the length falls in the middle of a word it will truncate additional characters so it does not cut a word.

function truncateString($string, $length, $ellipse='...')
{
    if (strlen($string) <= $length) { return $string; }
    return array_shift(explode("\n", wordwrap($string, $length))) . $ellipse;
}

Link to comment
Share on other sites

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.