SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Call to Undefined Function Question

    I used a function from this thread http://www.sitepoint.com/forums/showthread.php?t=489276 to create a script that lists the grandparents of various values in a database table.

    Now I'm trying to use the getChild script on another site to list a value's children. But I get this error:

    Fatal error: Call to undefined function getChild()...
    This is my script...

    PHP Code:
    $Child getChild($MyURL);
    function 
    getChild($MyURL) {
      
    $query "SELECT TOP.Name, TOP.Parent, TOP.Site
      FROM g_topics TOP
      WHERE TOP.Name = '
    $MyURL' AND TOP.Site = '$MySiteID'";
       
    $results mysql_query($query) or die ("An error occurred: ".mysql_error());
       
    $myrow mysql_fetch_array($results);
       return 
    $myrow['Child'];
    }
    echo 
    $Child
    ...where $MyURL equals the last segment of my URL. For example, if my URL is MySite/Topics/Geography, then $MyURL = 'Geography', which matches a value in my database that happens to have "children" (Continents, Oceans, Nations, etc.).

    Can someone tell me why my function is "undefined" on this particular script when it works for grandparents?

    Thanks.
    Last edited by geosite; Jul 17, 2007 at 20:52.

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    That can't be your code for two reasons:

    1) It would sooner produce a syntax error due to the missing single quote in the third to last line.

    2) Correcting the syntax error, that code runs, without an undefined function error.

  3. #3
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry about that; I typed that code in, as my other computer wasn't connected to the Internet. Anyway, I just edited the first post, pasting in the correct script - and I do get the error I described. But you say it doesn't produce the error for you?

    I thought perhaps I was simply entering the value $MyURL incorrectly. I've tried changing it to '.$MyURL.' or simply replacing it with a specific value, like 'Geography', but I either get the same error message or a syntax error.

    Thanks.

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Running the updated code, I still do not get an undefined function error.

    What happens if you move the first line where you call the function to below where you define the function?

  5. #5
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes geosite, i also did not get any error with your code that you have posted above. I just change the database table name and fields. It is returning my correct value. Could you please post your whole code so that the experts here can see again?
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  6. #6
    SitePoint Addict Php_penguin's Avatar
    Join Date
    Aug 2004
    Location
    Colwyn Bay, Wales, UK
    Posts
    287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try changing your error reporting to E_ALL:
    Code:
    error_reporting(E_ALL);
    this will help expose anything else going on. There may be something in the function parsing stage that is causing a problem.

    Post any errors/notices here

  7. #7
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry for the late response; I had to run to work.

    Anyway, it appears that my problem was primarily a syntax error(s). First, I had originally inserted the function in a PHP switch. When I removed it from the switch and tweaked it a bit to fit my database table, I finally got it to work - but only if I replace TOP.Site = '$MySiteID' with a specific value (TOP.Site = 'GW'). (I've tried all sorts of variations of $MySiteID, with various combinations of double quotes, single quotes and periods.) And if I stick it back inside the PHP switch, it doesn't work, even with TOP.Site = 'GW'.

    I also tried to display one of the grand children ($GC), but I haven't got that to work so far. This is my updated script:

    PHP Code:
    $Child $getChild($MyURL);
    $GC getChild(getChild($MyURL));

    function 
    getChild($MyURL)
    {
     
    $query "SELECT TOP.Name, TOP.Parent, TOP.Site
     FROM g_topics TOP
     WHERE TOP.Parent = '
    $MyURL' AND TOP.Site = 'GW'";
      
    $results mysql_query($query) or die ("An error occurred: ".mysql_error());
     
    $myrow mysql_fetch_array($results);
     return 
    $myrow['Name'];
    }

    echo 
    $Child;
    echo 
    $GC
    Perhaps I should explain what I'm trying to do. I have a list of articles in a database table with a parent-child relationship...

    NAME / PARENT
    Geography / Topics
    Nations / Geography
    States / Nations
    Provinces / Nations
    Counties / States
    I also have a script that displays each article's children. So if a visitor types in MySite/Topics/Nations, they get a list of all of nation's children: States and Provinces. But Counties has no children. so if they type in MySite/Topics/Counties, they get no list of children. However, the MECHANISM still displays.

    For example, if you look at http://www.geosymbols.org/World/United_States , you'll see a couple JavaScript-driven tabs near the top of the page that say "My Regions" and "My Children." If there ARE no children, the My Children tab won't open; but I don't want to display a tab that says "My Children" at all.

    So I'm trying to create a script that says, "If I have no children, do NOT include file a1/inc/Top/Children.php, but if I do have children, DO include file a1/inc/Top/Children.php."

    Anyway, I can probably accomplish this with a traditional parent-children script, but I thought I'd give this getChild function a try. It looks like I'm just a syntax error away from making it work.

    Thanks.

  8. #8
    SitePoint Addict Php_penguin's Avatar
    Join Date
    Aug 2004
    Location
    Colwyn Bay, Wales, UK
    Posts
    287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well i assume you do a db call to link to the children? so could you not also do a COUNT(*) at the same time?

  9. #9
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Php_penguin View Post
    well i assume you do a db call to link to the children? so could you not also do a COUNT(*) at the same time?
    Perfect; thanks!


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
  •