nathanhornby Posted December 15, 2007 Share Posted December 15, 2007 Ok here's my code: <? mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query="SELECT * FROM links ORDER BY linkid DESC"; $result=mysql_query($query); $num=mysql_numrows($result); $i=0; while ($i < $num) { $linkurl=mysql_result($result,$i,"linkurl"); $linkurl='<a href="'.$linkurl.'">'.$linkurl.'</a>'; if (strlen($linkurl) >= 70) { $linkurl = substr($linkurl, 0, 70).'...'; } echo ' <span class="maintext"> <br>- '.$linkurl.'<br> </span> '; $i++; } ?> The data is being called fine, and the link is displaying (as a link) and contains the full string so it can be selected, however when it comes to actuall shotening the length of the displayed link (with strlen) it seems to be doing some odd things; the read out looks a little like this: - - http://youtube.com/wa... - http://ww... - http://www.consolisearch.com As you can see it's definately not shortening by the 70 character limit I set, it's not even affecting the correct data (as the 2nd and 3rd url are both less than 70 characters they shouldnt even be shortened) and the first link doesn't display at all. Am I missing something really obvious, or could it be something in the database itself, perhaps I need to set the field up specificly for dealing with the characters you see in urls (/?:. etc) ? Any help appreciated, Thanks. Quote Link to comment Share on other sites More sharing options...
themistral Posted December 15, 2007 Share Posted December 15, 2007 Hi nathanhornby! It may not be anything to do with your problem but you have $linkurl='<a href="'.$linkurl.'">'.$linkurl.'[/url]'; and the closing tag should be </a> $linkurl='<a href="'.$linkurl.'">'.$linkurl.'</a>'; Quote Link to comment Share on other sites More sharing options...
nathanhornby Posted December 15, 2007 Author Share Posted December 15, 2007 Hi, That's actually this forum that's changing that end tag, my end tag is < / a > (it really doesn't want me to write that) Stupid forum Quote Link to comment Share on other sites More sharing options...
themistral Posted December 15, 2007 Share Posted December 15, 2007 Well you shouldn't need to do anything else to the database. It seems there might be 2 problems and the first one is the problem with the if statement. Try echoing the strlen of the urls to see if the logic is working correctly. Quote Link to comment Share on other sites More sharing options...
nathanhornby Posted December 15, 2007 Author Share Posted December 15, 2007 <span class="maintext"> '.$linkurl.'<br> '.strlen($linkurl).'<br> </span> Ok the read out looks like this: http://youtube.com/wa... 73 http://ww... 73 http://www.consolisearch.com 73 It's missing off the first URL completely, this is the only one that is actually longer than 70 characters. None of those urls are 73 characters either :\. Quote Link to comment Share on other sites More sharing options...
themistral Posted December 15, 2007 Share Posted December 15, 2007 Just seen the error (I hope!) Change $num=mysql_numrows($result); to $num=mysql_num_rows($result); Quote Link to comment Share on other sites More sharing options...
nathanhornby Posted December 15, 2007 Author Share Posted December 15, 2007 Nicely spotted; I changed it but it hasn't made any difference, so i'm guessing that syntax was still valid. btw the links in the post are cut off where the text is cut off, but in reality those links are actually intact, it's only the text itself that's cut off as required. i.e. it actually reads: <a href="http://youtube.com/watch?v=vAkOti7pJJg">http://youtube.com/wa...<br> 73<br> </span> <span class="maintext"> <a href="http://www.fallingpixel.com/news/article.php?id=62">http://ww...<br> 73<br> </span> <span class="maintext"> <a href="http://www.consolisearch.com">http://www.consolisearch.com</a...<br> 73<br> Quote Link to comment Share on other sites More sharing options...
themistral Posted December 15, 2007 Share Posted December 15, 2007 Ok, the other thing is that the variable $i is not being incremented so instead of $i=0; while ($i < $num) { try for ($i=0;$i<$num;$i++) { Quote Link to comment Share on other sites More sharing options...
nathanhornby Posted December 15, 2007 Author Share Posted December 15, 2007 Ok changed that, but unfortunately it hasn't changed anything Quote Link to comment Share on other sites More sharing options...
themistral Posted December 15, 2007 Share Posted December 15, 2007 Hiya! It's because you set everything to $linkurl - when you test the if statement you a testing a long line of code beginning <a href..... Set $linkurl=mysql_result($result,$i,"linkurl"); to use a different variable name and call that in the logic. Quote Link to comment Share on other sites More sharing options...
nathanhornby Posted December 15, 2007 Author Share Posted December 15, 2007 Ok I've changed it to the following (let me know if I misunderstood): <? mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die("Unable to select database"); $query="SELECT * FROM links ORDER BY linkid DESC"; $result=mysql_query($query); $num=mysql_num_rows($result); for ($i=0;$i<$num;$i++) { $linkurl=mysql_result($result,$i,"linkurl"); $url='<a href="'.$linkurl.'">'.$linkurl.'</a>'; if (strlen($url) >= 70) { $url = substr($url, 0, 70).'...'; } echo ' <span class="maintext"> '.$url.'<br> '.strlen($linkurl).'<br> </span> '; } ?> The read out now looks like this: http://youtube.com/wa... 38 http://ww... 50 http://www.consolisearch.com 28 So at least now it knows how many characters are there ... but it doesnt seem to be shortening correctly; and the first link still isn't displaying. Quote Link to comment Share on other sites More sharing options...
nathanhornby Posted December 15, 2007 Author Share Posted December 15, 2007 Ah I think I might have it, Changed $linkurl=mysql_result($result,$i,"linkurl"); $url='<a href="'.$linkurl.'">'.$linkurl.'</a>'; if (strlen($url) >= 70) { $linkurl = substr($linkurl, 0, 70).'...'; }echo ' <span class="maintext"> '.$url.'<br> '.strlen($linkurl).'<br> </span> to $linkurl=mysql_result($result,$i,"linkurl"); $url='<a href="'.$linkurl.'">'.$linkurl.'</a>'; if (strlen($url) >= 70) { $url = substr($linkurl, 0, 70).'...'; }echo ' <span class="maintext"> '.$url.'<br> '.strlen($linkurl).'<br> </span> Im guessing that was what you meant ... although now the '...' is coming up unconditionally, whether it has 70+ characters or not :\ but it is shortening effectively: http://bp2.blogger.com/_ez9UzAGPSPU/Rhv0hLgdEXI/AAAAAAAAACo/IJrba0dnpr... 90 http://youtube.com/watch?v=vAkOti7pJJg... 38 http://www.fallingpixel.com/news/article.php?id=62... 50 http://www.consolisearch.com... 28 Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted December 15, 2007 Share Posted December 15, 2007 You really should change your style and use the mysql_fetch_assoc() function to retrieve your rows. It will make your code cleaner (IMHO). Here is my take on your code (I also fixed your problem) <?php mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query="SELECT * FROM links ORDER BY linkid DESC"; $result=mysql_query($query); while ($rw = mysql_fetch_assoc($result)) { $disp_url = (strlen($rw['linkurl']) > 70)?substr($rw['linkurl'],0,70) . '...':$rw['linkurl']; echo ' <span class="maintext"> <br>- <a href="'. $rw['linkurl'] . '">' . $disp_url . '</a><br> </span> '; } ?> Ken Quote Link to comment Share on other sites More sharing options...
nathanhornby Posted December 15, 2007 Author Share Posted December 15, 2007 Ok The working code is: <? mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die("Unable to select database"); $query="SELECT * FROM links ORDER BY linkid DESC"; $result=mysql_query($query); $num=mysql_num_rows($result); for ($i=0;$i<$num;$i++) { $linkurl=mysql_result($result,$i,"linkurl"); $url='<a href="'.$linkurl.'">'.$linkurl.'</a>'; if (strlen($linkurl) >= 70) { $url = '<a href="'.$linkurl.'">'.substr($linkurl, 0, 70).'...'.'</a>'; } echo ' <span class="maintext"> '.$url.'<br> '.strlen($linkurl).'<br> </span> '; } ?> That produces exactly what I was after, thanks themistral and thanks for the tip kenrbnsn, ill take a look at that now. Nathan 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.