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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

$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

 

 

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

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();
}

?>

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.