Cant get customs functions work which are located in different file attached with inc

Discussion in 'PHP' started by swashata, Apr 3, 2010.

  1. #1
    Hi, I am trying to create a cms type of thing for my website template. For this I have used the following File structure...

    [​IMG]

    Now basically what I am doing is, using the file user_var.php to store custom information in array form, custom_func.php files to define all the custom functions which I am going to use on separate pages...

    On a page say contact.php first I am including two files first the user_var.php and then custom_func.php...

    Then I am using the custom functions to echo the page url, or the sidebar contents or all those htmls... For example, following code is there on the user_var.php

    //Main Website URLs and Title
    $website_title = "Swashata's Portfolio"; //This is the title of the Website. Would be used on all pages
    $website_url = 'http://www.itgwebtoolz.com/seatemplate'; //This is the exact URL of the root of your Website. If it is under a subfolder then write it like http://your.domain/subfolder
    $blog_url = 'http://www.intechgrity.com'; //The absolute URL of your Blog
    
    
    //The relatve address of the relevant pages.
    //For example, we use contact = 'contact.php' if the URL of the contact page is http://your.domain/contact.php
    $rel_page_url = array (
        'portfolio' => 'portfolio.php',
        'services' => 'services.php',
        'contact' => 'contact.php',
        'about' => 'about.php',
        'disclaimer' => 'disclaimer.php'
    );
    PHP:
    Now in custom_func.php file there are some functions defined as follows:
    //Load the default Navigation URLs
    function get_home_url() {
        echo "$website_url";
    }
    
    function get_portfolio_url() {
        echo "$website_url/$rel_page_url[portfolio]";
    }
    
    function get_services_url() {
        echo "$website_url/$rel_page_url[services]";
    }
    
    function get_blog_url() {
        echo "$blog_url";
    }
    
    function get_contact_url() {
        echo "$website_url/$rel_page_url[contact]";
    }
    
    function get_about_url() {
        echo "$website_url/$rel_page_url[about]";
    }
    
    function get_disclaimer_url() {
        echo "$website_url/$rel_page_url[disclaimer]";
    }
    PHP:
    On the header.php file, I have mixed up some HTML and PHP codes like this:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta name="description" content="<?php get_meta_description(); ?>" />
        <meta name="keywords" content="<?php get_meta_keywords(); ?>" />
        <script type="text/javascript" src="js/jquery.js"></script>
        <script type="text/javascript" src="js/functions.js"></script>
        <script type="text/javascript" src="js/easySlider1.7.js"></script>
        <title><?php get_page_title(); ?></title>
        <link type="text/css" href="css/main-frame.css" rel="stylesheet" />
        <link type="text/css" href="css/navigation.css" rel="stylesheet" />
        <link href="css/widgets.css" rel="stylesheet" type="text/css" />
        <link href="css/style.css" rel="stylesheet" type="text/css" />
        <link href="css/css3.css" rel="stylesheet" type="text/css" />
    </head>
    
    <body>
    <div id="wrap">
    	<div id="header-wrapper">
        	<div id="header">
            </div> <!-- End div#header -->
            <div id="nav-wrapper">
            	<div id="navigation">
                	<ul>
                        <li><a href="<?php get_home_url(); ?>" class="home"><span>Home</span></a></li>
                        <li><a href="<?php get_portfolio_url(); ?>" class="portfolio"><span>Portfolio</span></a></li>
                        <li><a href="<?php get_blog_url(); ?>" class="blog"><span>Blog</span></a></li>
                        <li><a href="<?php get_services_url(); ?>" class="services"><span>Services</span></a></li>
                        <li><a href="<?php get_contact_url(); ?>" class="contact"><span>Contact</span></a></li>
                    </ul>
                </div> <!-- End div#navigation -->
            </div> <!-- End div#nav-wrapper -->
            <div class="clear"></div>
    	</div> <!-- End div#header-wrapper -->
    HTML:
    Now finally the contact.php file which is a page of the website, is written like this:
    <?php
    //Include the custom_func.php file which contains all the definitions
    include_once dirname(_FILE_)."/sea_inc/user_var.php";
    include_once dirname(_FILE_).'/sea_inc/custom_func.php';
    
    //Get the header of the Document
    get_header_content();
    //Get the Featured Content Slider
    get_slider_content();
    ?>
    
            	<div id="main-content">
                	<div class="widget">
                        <div class="content">
                        <h2>Hi</h2>
                        <p>Blah</p>
                        </div>
                    </div> 
                </div> <!-- End div#main-content -->
                
    <?php
    //Get the Sidebar
    get_sidebar_content();
    //Get the Footer();
    get_footer_content();
    ?>
    PHP:
    But it is giving error everywhere! Like you can see in the image below:
    [​IMG]

    Or the live demo of the website: http://www.itgwebtoolz.com/seatemplate/contact.php

    Please tell me where am I going wrong with the PHP codes! Is there anything for the include thing? or is it that I just cant get the variables to pass through the header.php or sidebar.php files?
     
    swashata, Apr 3, 2010 IP
  2. swashata

    swashata Member

    Messages:
    86
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #2
    Mann it took me whole day to understand this:

    $var ="mystring";
    function echoer() {
    echo $var;
    }
    echoer();
    PHP:
    Will echo nothing!

    function echoer() {
    $var ="mystring";
    echo $var;
    }
    echoer();
    PHP:
    Will echo "mystring" ... Dat was the problem! All the variables were not loaded actually inside the functions... Is there any way to do that except passing arguments!
     
    swashata, Apr 4, 2010 IP
  3. Brandon.Add.On

    Brandon.Add.On Peon

    Messages:
    178
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yes, it's the scope of the variable. You can either do 3 things. First you can pass the variable as a parameter in the function, secondly you can declare it as global withing the function or thirdly you can write a VariableContextClass which is essentially an OO wrapper for variables needing in other classes and functions. You can get the value of the variable anywhere in your script by calling VariableContextWrapper::get('var') after you have it set.
     
    Brandon.Add.On, Apr 4, 2010 IP
  4. swashata

    swashata Member

    Messages:
    86
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #4
    Thanks... I have set the variable as global from inside every custom functions I have made...
    $var ="mystring";
    global $var;
    function echoer() {
    echo $var;
    }
    echoer();
    PHP:
    I was wondering if there is a way of declaring the variable to be global when defining it!!
     
    swashata, Apr 4, 2010 IP