Results 1 to 3 of 3

Thread: problem with function placement and getting var in substr

  1. #1
    Join Date
    Jul 2008
    Posts
    134
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default problem with function placement and getting var in substr

    I'm having a hard time trying to figure out how I can get the $id var in the substr output for /news.php?id=$id
    PHP Code:
    $res mysql_query ("SELECT * FROM `news` ") or die (mysql_error ());
    $id=$r["id"];
    // function to shorten the amt of characters to be shown in the news output
    function shorten3($var_name2,$var_length2) {
            if(
    strlen($var_name2) > $var_length2) {
                return 
    substr($var_name2,0,$var_length2-3)."...<a href=\"/news.php?id=$id\">full story</a>"
               } else {
                return 
    $var_name2
              } 
             }
    while(
    $r=mysql_fetch_array($res))
       {
              
    $id=$r["id"];
          
    $title=$r["title"];
          
    $news=$r["news"];
          
    $short_news shorten3($news100);
    echo 
    "<div class=\"news_header_top\">$title</div><div style=\"padding-bottom: 5px;\">$short_news</div>"

    I've used the function in other places but they linked to a general page and not a page like news.php?id=$id

    I tried to move the function right above $short_news so the id could be gotten and the function would be above the variable $short_news that is being created but I received the cannot re-declare function error. Any help would be appreciated, thank you.

  2. #2
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    Use the global keyword, to increase the scope inside the function so it can get the $id value from outside the function:

    PHP Code:
    $res mysql_query ("SELECT * FROM `news` ") or die (mysql_error ());

    // function to shorten the amt of characters to be shown in the news output

    function shorten3($var_name2,$var_length2) {
            global 
    $id;
            if(
    strlen($var_name2) > $var_length2) {
                return 
    substr($var_name2,0,$var_length2-3)."...<a href=\"/news.php?id=$id\">full story</a>"
               } else {
                return 
    $var_name2
              } 
             }
    while(
    $r=mysql_fetch_array($res))
       {
              
    $id=$r["id"];
          
    $title=$r["title"];
          
    $news=$r["news"];
          
    $short_news shorten3($news100);
    echo 
    "<div class=\"news_header_top\">$title</div><div style=\"padding-bottom: 5px;\">$short_news</div>"

    Or if this isn't what you need / want then please elaborate.
    Last edited by Schmoopy; 04-19-2009 at 10:50 PM.

  3. The Following User Says Thank You to Schmoopy For This Useful Post:

    ?foru (04-19-2009)

  4. #3
    Join Date
    Jul 2008
    Posts
    134
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default

    Thank you Schmoopy!

    That worked perfectly for what I needed. I have never used global although I have heard of it. I never would have guessed to put it inside the function to get the result I needed. Simple and yet so useful. Thanks again.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •