SitePoint Sponsor

User Tag List

Results 1 to 20 of 20
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2011
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    undefined function

    I keep getting undefined function error from these two pages:

    PHP Code:
    <?php 
    include_once('resources/init.php');?>
    <!DOCTYPE html>

    <html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
       
        <!--[if lt IE 9]>
        <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
       
        <title> Category List </title>
    </head>

    <body>
        <?php
        
    foreach ( get_categories() as $category ) {
            echo 
    $category['name'] . '<br/>';
        }
        
    ?>
    </body>
    </html>
    PHP Code:
    <?php
    include_once('resources/init.php');

    if ( isset(
    $_POST['name']) ) {
        
    $name trim($_POST['name']);
        
        if ( empty(
    $name) ) {
            
    $error ='You must submit a category name. ';
        } else if ( 
    category_exists($name) ) {
            
    $error 'That category already exists. ';
        } else if (
    strlen($name) > 24 ) {
            
    $error 'Category names can only be up to 24 characters.';
        }
        
        if ( ! isset(
    $error) ) {
            
    add_category($name);
        }
    }
    ?>
    <!DOCTYPE html>

    <html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        
        <!--[if lt IE 9]>
        <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
        
        <title> Add a Category </title>
    </head>

    <body>
        <h1> Add a Category </h1>
        
        <?PHP
        
    if (isset($error) ) {
            echo 
    "<p> {$error} </p>\n";
        
    ?>
        
        <form action="" method="post">
            <div>
                <label for="name"> Name </label>
                <input type="text" name="name" value="">
            </div>
            <div>
                <input type="submit" value="Add Category">
            </div>
        </form>
    </body>
    </html>
    both should be pulling the information from following page but I don't think they are connected properly:

    PHP Code:
    <? php

    function add_post($title$contents$category) {

    }

    function 
    edit_post($id$title$contents$category) {

    }

    function 
    add_category($name) {
        
    $name mysql_real_escape_string($name);
        
        
    mysql_query("INSERT INTO 'categories' SET 'name' = '{$name}' ");
    }

    function 
    delete($field$id) {

    }

    function 
    get_posts($id null$cat_id null) {

    }

    function 
    get_categories($id null) {
        
    $categories = array();
        
        
    $query mysql_query("SELECT 'id', 'name' FROM 'categories' ");
        
        while ( 
    $row mysql_fetch_assoc($query) ) {
            
    $categories[] = $row;
        }
        
        return 
    $categories;
    }

    function 
    category_exists($name) {
        
    $name mysql_real_escape_string($name);
        
        
    $query mysql_query("SELECT COUNT(1) FROM 'categories' WHERE 'name' = '{$name}' ");
        
        
        return ( 
    mysql_result($query0) == '0' ) ? false true;

    }
    ?>

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Please post the exact error you're getting

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    For sanity's sake, temporarily place a simple echo statement in init.php.

    PHP Code:
    <?php 
    echo 'EEK!';
    If the inclusion went OK, you should see that echo'ed text in the source code of your html page.

  4. #4
    SitePoint Enthusiast
    Join Date
    Jan 2011
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Fatal error: Call to undefined function get_categories() in C:\xampp\htdocs\test\blog\category_list.php on line 18

  5. #5
    SitePoint Enthusiast
    Join Date
    Jan 2011
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Fatal error: Call to undefined function category_exists() in C:\xampp\htdocs\test\blog\add_category.php on line 9

  6. #6
    SitePoint Enthusiast
    Join Date
    Jan 2011
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it' echoed ok

  7. #7
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,191
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    In the last snippet of code you posted your init.php file? If so your opening PHP tag has a space in it which would cause it to output as plain text instead of running as PHP code, if you remove the space the error should stop.

  8. #8
    SitePoint Enthusiast
    Join Date
    Jan 2011
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that did not work

  9. #9
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,191
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    You didn't answer my initial question, i asked if the code you posted above exists in the init.php file as if it doesn't that will also cause the error. I double checked the function names and they are both spelled the same way.

  10. #10
    SitePoint Enthusiast
    Join Date
    Jan 2011
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry about that here is the inti.php code
    PHP Code:
    <?php
    include_once('config.php');

    mysql_connect(DB_HOSTDB_USERDB_PASS);
    mysql_select_db(DB_NAME);

    include_once(
    'func/blog.php');

  11. #11
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    now remove the echo test from init and put it in config.php, if that appears in the source code of your page, then move it to blog.php and test it again.

    If all of that works then you have proved that all the dependent files are wired up correctly (unless you have another level of includes in them as well ).

    So then move the EEK! into your suspect function.

  12. #12
    SitePoint Enthusiast
    Join Date
    Jan 2011
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok cool it is not showing up when I put it in the blog.php file

  13. #13
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,019
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    At the start of the third script in the OP (which I presume is resources/init.php) it starts with <? php try removing that space (if the server has got the allowing of short tags disabled it may be causing a problem.
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  14. #14
    SitePoint Enthusiast
    Join Date
    Jan 2011
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    after fixing the space issue and a couple of other small things i get this error:

    Fatal error: Call to undefined function get_post() in index.php on line 4

    here is the php for that:

    PHP Code:
    <?php 
    include_once('resources/init.php');

    $posts get_post();
    ?>

  15. #15
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,019
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    Going by the same script (3rd one in the OP) shouldn't the line

    PHP Code:
    $posts get_post(); 
    be

    PHP Code:
    $posts get_posts(): 
    ?
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  16. #16
    SitePoint Enthusiast
    Join Date
    Jan 2011
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\test\blog\resources\func\blog.php on line 43

    Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\test\blog\index.php on line 25

    blog:

    PHP Code:
    <?php
    function add_post($title$contents$category) {
         
    $title                mysql_real_escape_string($title);
         
    $contents           mysql_real_escape_string($contents);
         
    $category           = (int)$category;
        
         
    mysql_query("INSERT INTO 'post' SET
                                            'cat_id'                     = 
    {$category},
                                            'title'                         = '
    {$title}',
                                            'contents'                = '
    {$contents}',
                                            'date_posted'      = NOW()"
    )
                                            or die(
    mysql_error());
    }

    function 
    edit_post($id$title$contents$category) {

    }

    function 
    add_category($name) {
         
    $name mysql_real_escape_string($name);
        
         
    mysql_query("INSERT INTO 'categories' SET 'name' = '{$name}' ");
    }

    function 
    delete($table$id) {
         
    $table mysql_real_escape_string($table);
         
    $id = (int) $id;
        
         
    mysql_query("DELETE FROM '{$table}' WHERE 'id' = {$id}");
    }

    function 
    get_posts($id null$cat_id null) {
         
    $post = array();

         
    $query "SELECT 'posts' . 'id' AS 'post_id', 'categories . 'id' AS 'category_id',
                                            'title', 'contents', 'date_posted', 'categories' . 'name'
                             FROM 'post'
                             INNER JOIN 'categories' ON 'categories' . 'id' = 'post' . 'cat_id'
                             ORDER BY 'id' DESC"
    ;
                            
         
    $query mysql_query($query);
        
         while ( 
    $row mysql_fetch_assoc($query) ) {
                   
    $posts[] = $row;
         }
        
         return 
    $posts;
    }

    function 
    get_categories($id null) {
         
    $categories = array();
        
         
    $query mysql_query("SELECT 'id', 'name' FROM 'categories' ");
        
         while ( 
    $row mysql_fetch_assoc($query) ) {
              
    $categories[] = $row;
         }
        
         return 
    $categories;
    }

    function 
    category_exists($field$value) {
         
    $field mysql_real_escape_string($field);
         
    $value mysql_real_ecape_string($value);
        
         
    $query mysql_query("SELECT COUNT(1) FROM 'categories' WHERE '{$field}' = '{$value}' ");
        
         return ( 
    mysql_result($query0) == '0' ) ? false true;

    }
    ?>
    index:

    PHP Code:
    <?php
    include_once('resources/init.php');

    $posts get_posts();
    ?>
    <!DOCTYPE html>

    <html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
      
        <!--[if lt IE 9]>
        <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
      
        <style>
        ul { list-style-type: none; }
        li { display: inline; margin-right: 20px; }
        </style>
      
        <title> My Blog </title>
        
         <?php
         
    foreach ( $posts as $post) {
             
         }
         
    ?>
    </head>

    <body>
        <nav>
            <ul>
                <li><a href="index.php"> Index </a></li>
                <li><a href="add_post.php"> Add a Post </a></li>
                <li><a href="add_category.php"> Add a Category </a></li>
                <li><a href="category_list.php"> Category List </a></li>
            </ul>
        </nav>
      
        <h1> Devin's Awesome Blog </h1>
       
       
    </body>
    </html>

  17. #17
    SitePoint Enthusiast
    Join Date
    Jan 2011
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks SpacePhoenix after posting it I quickly fixed it

  18. #18
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,019
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    The first error indicates that an sql query has failed, echo the query to see if anything looks out of place or is missing.

    The second error relates to the first as foreach was expecting an array but as the query that would populate the array failed it didn't get what it expected. Fixing the cause of the first error should automatically fix that error.
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  19. #19
    SitePoint Enthusiast
    Join Date
    Jan 2011
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I echoed out $row and $posts nothing shows up

  20. #20
    SitePoint Enthusiast
    Join Date
    Jan 2011
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have to update my version of php after that I will check for more errors


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
  •