optrex Posted February 24, 2009 Share Posted February 24, 2009 <?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 Quote Link to comment Share on other sites More sharing options...
trq Posted February 24, 2009 Share Posted February 24, 2009 $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. Quote Link to comment Share on other sites More sharing options...
optrex Posted February 24, 2009 Author Share Posted February 24, 2009 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? Quote Link to comment Share on other sites More sharing options...
trq Posted February 24, 2009 Share Posted February 24, 2009 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. Quote Link to comment Share on other sites More sharing options...
optrex Posted February 24, 2009 Author Share Posted February 24, 2009 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. Quote Link to comment Share on other sites More sharing options...
trq Posted February 24, 2009 Share Posted February 24, 2009 Are you using $row['limitpage'] to reference it? Quote Link to comment Share on other sites More sharing options...
optrex Posted February 24, 2009 Author Share Posted February 24, 2009 yes I am Quote Link to comment Share on other sites More sharing options...
optrex Posted February 25, 2009 Author Share Posted February 25, 2009 $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 Quote Link to comment Share on other sites More sharing options...
trq Posted February 25, 2009 Share Posted February 25, 2009 Your missing your while loop and your call to mysql_fetch_assoc. Quote Link to comment Share on other sites More sharing options...
optrex Posted February 25, 2009 Author Share Posted February 25, 2009 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; } Quote Link to comment Share on other sites More sharing options...
trq Posted February 25, 2009 Share Posted February 25, 2009 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(); } ?> Quote Link to comment Share on other sites More sharing options...
optrex Posted February 25, 2009 Author Share Posted February 25, 2009 Parse error: syntax error, unexpected T_IF in /home/file.php on line 7 line 7 is if ($result = mysql_query($sql)) { if there were no results, then why do they display when the substr is not in place? Quote Link to comment Share on other sites More sharing options...
niranjnn01 Posted February 25, 2009 Share Posted February 25, 2009 place a ; at the end of line $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" Quote Link to comment Share on other sites More sharing options...
optrex Posted February 25, 2009 Author Share Posted February 25, 2009 LOL - doesn't that one prove I'm a noobie. Many thanks for you time in helping out by the way. Ok, output is the same - nothing for the limitpage reference. Quote Link to comment 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.