phpnewby1918 Posted January 19, 2011 Share Posted January 19, 2011 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...»»</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 :-) Quote Link to comment https://forums.phpfreaks.com/topic/225028-php-looping-with-html-tags/ Share on other sites More sharing options...
litebearer Posted January 19, 2011 Share Posted January 19, 2011 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...&#187;&#187;</a> <?PHP } // End of while ?> </div> <div class="right"> <h2>News</h2> <?PHP /* more code */ ?> Quote Link to comment https://forums.phpfreaks.com/topic/225028-php-looping-with-html-tags/#findComment-1162259 Share on other sites More sharing options...
phpnewby1918 Posted January 20, 2011 Author Share Posted January 20, 2011 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 :-) Quote Link to comment https://forums.phpfreaks.com/topic/225028-php-looping-with-html-tags/#findComment-1162678 Share on other sites More sharing options...
litebearer Posted January 20, 2011 Share Posted January 20, 2011 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 . "..."; Quote Link to comment https://forums.phpfreaks.com/topic/225028-php-looping-with-html-tags/#findComment-1162780 Share on other sites More sharing options...
phpnewby1918 Posted January 21, 2011 Author Share Posted January 21, 2011 Thanks litebearer, I think that codes a lot cleaner than mine and i will encorporate into my future scripts. Quote Link to comment https://forums.phpfreaks.com/topic/225028-php-looping-with-html-tags/#findComment-1162790 Share on other sites More sharing options...
Psycho Posted January 21, 2011 Share Posted January 21, 2011 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; } Quote Link to comment https://forums.phpfreaks.com/topic/225028-php-looping-with-html-tags/#findComment-1162801 Share on other sites More sharing options...
phpnewby1918 Posted January 22, 2011 Author Share Posted January 22, 2011 Thans for both of your help, some nice code you both use Quote Link to comment https://forums.phpfreaks.com/topic/225028-php-looping-with-html-tags/#findComment-1163632 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.