how to call a external function in a specific division using php
Results 1 to 3 of 3

Thread: how to call a external function in a specific division using php

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    2

    how to call a external function in a specific division using php

    I have two files called index.php which is my main file and configFun.php file which includes all functions. I want to call a function function form configFun.php in a index.php but not anywhere it should be load in a specific div so how to implement that code.
    I know how to call external function but i don't understood how to call that function in a specific div.

  2. #2
    Junior Member
    Join Date
    Jan 2013
    Posts
    2
    This is the index.php file

    <?php require'config.php'; ?>
    <html><body>
    <p>Welcome to the homepage</p>
    <br>
    <hr>
    <a href="?run=About">About</a><br>
    <a href="?run=Contact">Contact</a> <br>
    <a href="?run=0">Refresh No run</a>
    <div class="dispArea">

    </div>
    </body></html>

    This is configFun.php file

    <?php
    if (isset($_GET['run'])) $linkchoice=$_GET['run'];
    else $linkchoice='';
    switch($linkchoice)
    {
    case 'About' :
    myAbout();
    break;
    case 'Contact' :
    myContact();
    break;
    }
    ?>
    <?php
    function myAbout(){
    echo 'we are just trying it for a demo.';
    }
    function myContact(){
    echo 'Thanx for cantacting us';
    }
    ?>


    I want to call functions in the"dispArea" div how to implement the code.

  3. #3
    Senior Member traq's Avatar
    Join Date
    Jun 2011
    Location
    so.Cal
    Posts
    949
    Please use [PHP][/PHP] BBCode tags and indent your code to make it more readable.

    I'm not exactly sure what you're asking, but, if I understand your question correctly, you want the result of your function to be printed in the .dispArea div?

    Turn your HTML into a template.

    index.php
    PHP Code:
    <?php 
    require 'config.php';

    /* 
    move all output to the *end* of your script (i.e., a "View", or "template" file).

    It is **not possible** to print 
        "<div class=dispArea></div>"
    to the browser and then, afterwards, add some content inside it.

    PHP works *first*, ON THE SERVER.  As soon as you print (echo, ?>, etc.) something, it's *gone*.
    HTML works *after*, ON THE USER'S BROWSER.  It has *no clue* that PHP ever existed.
    */
    config.php
    PHP Code:
    <?php

    // you should always use brackets to define blocks.
    // it will really help prevent/fix bugs.
    if( isset( $_GET['run'] ) ){
        
    $linkchoice=$_GET['run'];
    }else{
        
    $linkchoice=''
    }

    /*
    In your functions, don't use `echo`.
    Instead, `return` the text so it can be stored in a variable and inserted into your template.
    */
    function myAbout(){
        return 
    'We are just trying it for a demo.'

    function 
    myContact(){ 
        return 
    'Thanks for contacting us'
    }

    /*
    In your `switch`, assign your function's return value to a variable.
    Make sure you choose a unique variable name so it's not accidentally overwritten
    (you could also avoid this problem by using a class (object-oriented programming)).
    Here, I choose to prefix the variable with `$t_` (i.e., "template").
    */
    switch( $linkchoice ){
        case 
    'About' 
            
    $t_dispArea myAbout(); 
            break; 
        case 
    'Contact' 
            
    $t_dispArea myContact(); 
            break; 
    /* add a `default` case, in case `$linkchoice` is empty or doesn't match the above cases */
        
    default:
            
    $t_dispArea "Nothing to see here.";
            break;
    }

    /*
    When *all* of your content is ready, get your template.
    */
    require 'template_index.php';
    template_index.php
    PHP Code:
    <!doctype html>
    <html>
    <body>
        <p>Welcome to the homepage</p>
        <hr>
        <a href="?run=About">About</a><br> 
        <a href="?run=Contact">Contact</a> <br> 
        <a href="?run=0">Refresh No run</a>
        <div class="dispArea">
            <?= $t_dispArea ?>
        </div>
    </body>
    </html><?php
    /*
    If you are using <PHP 5.4 and shorttags are not enabled, you'll have to do this instead:

        <?php print $t_dispArea; ?>

    It does excactly the same thing.
    */

    // all done
    exit;
    Last edited by traq; 01-06-2013 at 01:08 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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