stuart.cole Posted October 10, 2006 Share Posted October 10, 2006 HiI've been on a steep learning curve with PHP and My SQL - and I think this is a PHP question.I am pulling news from the database to a frame, where I want to show the date, headline, subhead and an amount of the story that will fit in the frame - probably limited to around 200 charcters or so... the main text area it is pullin from though vcan have up to 2000 chracters in it. How do I limit the output?I'm at this pont so far which works apart from limiting the text...<? $query = "SELECT * FROM News LIMIT 0,1";$result = mysql_query($query);while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ echo"<h6>{$row['Date']} ";echo "<br /><A Href='{$row['Newsid']}'>{$row['Heading']}</A>";echo "<br>{$row['Subhead']}";echo "<br />{$row['Main']}</h6>";}?>I dare say I'll need help with the output file from the links as well - but thats for another post ;) Quote Link to comment https://forums.phpfreaks.com/topic/23524-character-limiting-with-php/ Share on other sites More sharing options...
vbnullchar Posted October 10, 2006 Share Posted October 10, 2006 try this function trunc($details,$max) { if(strlen($details)>$max) { $details = substr($details,0,$max); $i = strrpos($details," "); $details = substr($details,0,$i); $details = $details."..."; } return $details;} Quote Link to comment https://forums.phpfreaks.com/topic/23524-character-limiting-with-php/#findComment-106737 Share on other sites More sharing options...
stuart.cole Posted October 10, 2006 Author Share Posted October 10, 2006 I'm sure what you put is fab - but I am a true noob! How would I integrate this with the code I already employ as stated above? I've tried placing this (and changing the $details to $Main - and $max to $200 which suit the table I am working from) and putting it where the last ;echo call is - with no joy - and then just running your script without any of mine - but that also doesn't work.Probably silly questions I know - but any advice?[quote author=vbnullchar link=topic=111054.msg449749#msg449749 date=1160478796]try this function trunc($details,$max) { if(strlen($details)>$max) { $details = substr($details,0,$max); $i = strrpos($details," "); $details = substr($details,0,$i); $details = $details."..."; } return $details;}[/quote] Quote Link to comment https://forums.phpfreaks.com/topic/23524-character-limiting-with-php/#findComment-106747 Share on other sites More sharing options...
HuggieBear Posted October 10, 2006 Share Posted October 10, 2006 You need to call the trunc() function with some values. Using the code that both of you posted, I'd try something like this...[code]<?php$query = "SELECT * FROM News LIMIT 0, 1";$result = mysql_query($query);function trunc($details, $max){ if (strlen($details) > $max){ $details = substr($details, 0, $max); $i = strrpos($details," "); $details = substr($details, 0, $i); $details = $details."..."; } return $details;}while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ $main = trunc($row['Main'], 200); // You see the call here to the above function echo <<<HTML <h6>{$row['Date']}<br><a href="{$row['Newsid']}">{$row['Heading']}</a><br>{$row['Subhead']}<br>{$main}</h6>HTML;}?>[/code]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/23524-character-limiting-with-php/#findComment-106749 Share on other sites More sharing options...
stuart.cole Posted October 10, 2006 Author Share Posted October 10, 2006 Thanks to you both - with the application example/advice it makes a lot more sense now - cheers Huggie! Some of us need our hands held through this horrible learning curve :) Quote Link to comment https://forums.phpfreaks.com/topic/23524-character-limiting-with-php/#findComment-106769 Share on other sites More sharing options...
HuggieBear Posted October 10, 2006 Share Posted October 10, 2006 No problem, that's what we're here for! :DRegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/23524-character-limiting-with-php/#findComment-106770 Share on other sites More sharing options...
stuart.cole Posted October 12, 2006 Author Share Posted October 12, 2006 On the same note ...What if I need to limit the character count more than once?If so, what is the best way to do it? I've tried what seems logical and have got stuck with the below which (obviously to those who understand it) doesn't work.Thoughts?[code]<? $query = "SELECT * FROM News ORDER BY Time DESC LIMIT 0, 1";$result = mysql_query($query);function trunc($details, $max){ if (strlen($details) > $max){ $details = substr($details, 0, $max); $i = strrpos($details," "); $details = substr($details, 0, $i); $details = $details."..."; } return $details;}while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ $main = trunc($row['Main'], 200);{ $heading = trunc($row['Heading'], 75);{ $subhead = trunc($row['Subhead'], 100); // You see the call here to the above function echo <<<HTML {$row['Date']}<br><a href="newsitem.php?id={$row['Newsid']}">{$heading}</a><br>{$subhead}<br>{$main}HTML;}?></div>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/23524-character-limiting-with-php/#findComment-107959 Share on other sites More sharing options...
HuggieBear Posted October 13, 2006 Share Posted October 13, 2006 You have a couple of rogue characters there. Remove the '[color=green][b]{[/b][/color]' from the end of the following lines...[code=php:0]$main = trunc($row['Main'], 200);{$heading = trunc($row['Heading'], 75);{[/code]Let me know if you still have issues after this :)RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/23524-character-limiting-with-php/#findComment-108238 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.