Jump to content


Photo

How to get a function to work [RESOLVED]


  • Please log in to reply
4 replies to this topic

#1 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 07 September 2006 - 12:55 PM

I have got a function to shorten the length of a string so it if is more than 40 characters it has .... at the end

I 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

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

If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#2 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 07 September 2006 - 01:00 PM

function DisplayURL() {
    global $url;
    if (strlen($url) > 40) {   
         $url = substr($url,0,40); 
         echo $url ."..."; } 
    else { 
         echo $url;
    }
}
you need ot either make $url a global or use it like this..

function DisplayURL($url) {
    if (strlen($url) > 40) {   
         $url = substr($url,0,40); 
         echo $url ."..."; } 
    else { 
         echo $url;
    }
}

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



regards
Liam
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#3 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 07 September 2006 - 01:06 PM

Thanks Liam...that worked

Another 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 tags

Is 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>
If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 07 September 2006 - 02:13 PM

You should return the string from the function instead of echoing it:
<?php
function DisplayURL($url) {
    if (strlen($url) > 40) return(substr($url,0,40) . '...');
    else return($url);
}?>

Ken

#5 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 07 September 2006 - 02:29 PM

Thanks Ken....spot on
If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users