Jump to content

Limit number of words on part of output.


optrex

Recommended Posts


<?php
$link = mysql_connect ("localhost", "username", "password") or die('I cannot connect to the database.');
mysql_select_db ("database")or die("Could not select database");

$result = mysql_query("SELECT name, title, date, state, userid, textid, page FROM " . TABLE_PREFIX . "text
WHERE title = ''
AND state = 'visible'
ORDER BY dateline desc
LIMIT 5" )or die(mysql_error());
while($row = mysql_fetch_array($result)){

$comms .=<<<PRINT
<tr><td class="$bgclass" align="left"><span class="{$Style['small']}"><a href="http://website.com/page.php?=$row[textid]">Blog Comment</a> by <a href="http://website.com/page.php?u=$row[userid]">$row[name]</a><br>$row

</td></tr>
PRINT;
}
?>

 

this gives me the following output

 

blog comment by username

the contents of the comment

 

repeated 5 time for the last 5 comments.

 

What I would like to do is limit the contents of the comment to say 100 words max. As a relative newbie, could enayone please explain what I need to do? I've tried various functions, but I'm now confusing myself and need help please :)

Link to comment
https://forums.phpfreaks.com/topic/146636-limit-number-of-words-on-part-of-output/
Share on other sites


$result = mysql_query("SELECT name, title, date, state, userid, textid, SUBSTR(page,0,100) FROM " . TABLE_PREFIX . "text
WHERE title = ''
AND state = 'visible'
ORDER BY dateline desc
LIMIT 5" )or die(mysql_error());

 

Also, on an unrelated matter. This....

 

$comms .=<<<PRINT
<tr><td class="$bgclass" align="left"><span class="{$Style['small']}"><a href="http://website.com/page.php?=$row[textid]">Blog Comment</a> by <a href="http://website.com/page.php?u=$row[userid]">$row[name]</a><br>$row

</td></tr>
PRINT;
}

 

should be....

 

$comms .=<<<PRINT
<tr><td class="$bgclass" align="left"><span class="{$Style['small']}"><a href="http://website.com/page.php?={$row['textid']}">Blog Comment</a> by <a href="http://website.com/page.php?u={$row['userid']}">$row[name]</a><br>{$row['page']}
</td></tr>
PRINT;
}

 

Array indexes should be surrounded by quotes.

Thanks Thorpe.

I have made the changes to the quotes and parenthesis as directed. However, when I use the SUBSTR(page,0,100) I get a blank output for the 'page'

 

Also would the substr limit the number of characters rather than the number of words?

Sorry, your query should read.

 

$result = mysql_query("SELECT name, title, date, state, userid, textid, SUBSTR(page,0,100) AS limitpage FROM " . TABLE_PREFIX . "text
WHERE title = ''
AND state = 'visible'
ORDER BY dateline desc
LIMIT 5" )or die(mysql_error());

 

Then use $row['limitpage'] to reference it. And yes, SUBSTR will cut the string by chars. If your after a more elaberate mechanism try this function from the php manual.

Still blank on the output for 'page' from that one I'm afraid.

 

I don't really understand why it doesn't work, as without the substring its fine. If I can get this one to produce an output, before looking at the more complex alternative, that would be good.

$result = mysql_query("SELECT name, title, date, state, userid, textid, SUBSTR(page,0,100) AS limitpage FROM " . TABLE_PREFIX . "text
WHERE title = ''
AND state = 'visible'
ORDER BY dateline desc
LIMIT 5" )or die(mysql_error());
$comms .=<<<PRINT
<tr><td class="$bgclass" align="left"><span class="{$Style['small']}"><a href="http://website.com/page.php?={$row['textid']}">Blog Comment</a> by <a href="http://website.com/page.php?u={$row['userid']}">{$row['name']}</a><br>{$row['limitpage']}
</td></tr>
PRINT;
}

is my full code

 

 

updated: still blank output

 

$result = mysql_query("SELECT name, title, date, state, userid, textid, SUBSTR(page,0,100) AS limitpage FROM " . TABLE_PREFIX . "text
WHERE title = ''
AND state = 'visible'
ORDER BY dateline desc
LIMIT 5" )or die(mysql_error());
while($row = mysql_fetch_array($result)){
$comms .=<<<PRINT
<tr><td class="$bgclass" align="left"><span class="{$Style['small']}"><a href="http://website.com/page.php?={$row['textid']}">Blog Comment</a> by <a href="http://website.com/page.php?u={$row['userid']}">{$row['name']}</a><br>{$row['limitpage']}
</td></tr>
PRINT;
}

Then there are no results. Try this....

 

<?php
$sql = "SELECT name, title, date, state, userid, textid, SUBSTR(page,0,100) AS limitpage FROM " . TABLE_PREFIX . "text WHERE title = '' AND state = 'visible' ORDER BY dateline DESC LIMIT 5"
if ($result = mysql_query($sql)) {
  if (mysql_num_rows($result)) {
    while ($row = mysql_fetch_array($result)){
      echo "<tr><td class=\"$bgclass\" align=\"left\"><span class=\"{$Style['small']}\"><a href=\"http://website.com/page.php?={$row['textid']}\">Blog Comment</a> by <a href=\"http://website.com/page.php?u={$row['userid']}\">{$row['name']}</a><br>{$row['limitpage']}</td></tr>";
    }
  } else {
    echo "No results found";
  }
} else {
  echo "Query failed<br />$sql<br />" . mysql_error();
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.