Jump to content

character limiting with PHP


stuart.cole

Recommended Posts

Hi

I'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']}&nbsp;&nbsp;&nbsp;";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 ;)
Link to comment
Share on other sites

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;
}
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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]

Regards
Huggie
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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 :)

Regards
Huggie
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.