AdRock Posted September 7, 2006 Share Posted September 7, 2006 I have got a function to shorten the length of a string so it if is more than 40 characters it has .... at the endI am having problems calling the function to display the string. it displays nothing.This is the first time i've used a function so i don't know if i've done it right[code]function DisplayURL() { if (strlen($url) > 40) { $url = substr($url,0,40); echo $url ."..."; } else { echo $url; }}//show data matching query: echo "<table border='0' id='MyTable' class='style3' cellpadding='3'>\n";echo "<tr bgcolor='#CDE1FF'><th>Name</th><th>Location</th><th>URL</th></tr>\n";for($i = 0; $i < $numofrows; $i++) { $row = mysql_fetch_array($q); //get a row from our result set $url = $row['url']; if($i % 2) { //this means if there is a remainder echo "<tr bgcolor='#FFFFCC'>\n"; } else { //if there isn't a remainder we will do the else echo "<tr bgcolor='#FFFFFF'>\n"; } echo "<td width='30%'>".$row['name']."</td><td width='30%'>".$row['location']."</td><td width='40%'><a href='".$row['url']."' target='_blank'>".DisplayURL()."</a></td>\n"; echo "</tr>\n";}[/code] Quote Link to comment Share on other sites More sharing options...
shocker-z Posted September 7, 2006 Share Posted September 7, 2006 [code]function DisplayURL() { global $url; if (strlen($url) > 40) { $url = substr($url,0,40); echo $url ."..."; } else { echo $url; }}[/code]you need ot either make $url a global or use it like this..[code]function DisplayURL($url) { if (strlen($url) > 40) { $url = substr($url,0,40); echo $url ."..."; } else { echo $url; }}[/code] echo "<td width='30%'>".$row['name']."</td><td width='30%'>".$row['location']."</td><td width='40%'><a href='".$row['url']."' target='_blank'>".DisplayURL($url)."</a></td>\n"; regardsLiam Quote Link to comment Share on other sites More sharing options...
AdRock Posted September 7, 2006 Author Share Posted September 7, 2006 Thanks Liam...that workedAnother problem now....the urls are displayed outside of the table before the first row.How do i get them to go back into the right table row as an hyperlink.....it's lost the anchor tagsIs it becuase the function echoes the $url and in the line that calls the function the whole line is being echoed to there's an echo within an echo> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted September 7, 2006 Share Posted September 7, 2006 You should return the string from the function instead of echoing it:[code]<?phpfunction DisplayURL($url) { if (strlen($url) > 40) return(substr($url,0,40) . '...'); else return($url);}?>[/code]Ken Quote Link to comment Share on other sites More sharing options...
AdRock Posted September 7, 2006 Author Share Posted September 7, 2006 Thanks Ken....spot on 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.