Mark
Mark

Reputation: 95

How to call a function in PHP

I have been following some courses on how to create a session object, which has worked out fine, If I place the complete code onto a PHP file, all works great!

What I would like to do is place this in another module (PHP file) and just use one line (or equivalent) to do this, such as GetSessiondata();

<?php
$SqlCon = new DBConnect("Databaselocation","Database","Usr","Pss");                     
$UserDataSet = $SqlCon->GetUserList("SELECT * FROM Users");

echo "<br /><br />";
echo "<br /><br />";

if ($UserDataSet)          
{               
    echo "<table>" . "<thead>" ;
    echo "<tr><th scope=\"col\">" . 'Usr' . "</th>";
    echo "<th scope=\"col\">" . 'Lvl' . "</th></tr></thead><tbody>";                
    foreach($UserDataSet as $data)
    {                                  
        echo "<td>" .$data->GetUsrName()."</td>" ;
        echo "<td>" .$data->GetUsrLevel()."</td></tr>" ;                                       
    }
    echo "<tfoot><tr><th scope=\"row\" colspan=\"2\">" . 'Total Users = 2' . "</th></tr></tfoot>";
    echo "</tbody>" . "</table>" ;  
}
else
    echo "Nothing Found in DB!";
?>

Upvotes: 4

Views: 185

Answers (3)

Ferrmolina
Ferrmolina

Reputation: 2771

You need to "require" your file, where you want to use it.

Here an example

Working with classes:

Whatever.php

class Whatever {
  public function __construct() {
    // Ever when the code is instantiated, this will be called too!
  }
  public function myMethod() {
    echo 'hello';
  }
}

index.php

require_once('./Whatever.php');
$whatever = new Whatever();
$whatever->myMethod();

Without classes:

functions.php:

function whatever(){ echo 'hello'; }

index.php:

require_once('./functions.php');
whatever();

Read more:

Require: http://php.net/manual/es/function.require.php

Require_once: http://php.net/manual/es/function.require-once.php

Upvotes: 1

Nikita Leshchev
Nikita Leshchev

Reputation: 1844

My advise is to split this refactoring process into 2 steps:

1.Wrap your code into function:

function someFunctionName() {
    $SqlCon = new DBConnect("Databaselocation","Database","Usr","Pss");                     
    $UserDataSet = $SqlCon->GetUserList("SELECT * FROM Users");

    echo "<br /><br />";
    echo "<br /><br />";

    if ($UserDataSet)          
    {               
        echo "<table>" . "<thead>" ;
        echo "<tr><th scope=\"col\">" . 'Usr' . "</th>";
        echo "<th scope=\"col\">" . 'Lvl' . "</th></tr></thead><tbody>";                
        foreach($UserDataSet as $data)
        {                                  
           echo "<td>" .$data->GetUsrName()."</td>" ;
           echo "<td>" .$data->GetUsrLevel()."</td></tr>" ;                                    
        }
        echo "<tfoot><tr><th scope=\"row\" colspan=\"2\">" . 'Total Users = 2' . "</th></tr></tfoot>";
        echo "</tbody>" . "</table>" ;  
    }
    else
        echo "Nothing Found in DB!";
}

// and call your function
someFunctionName();

2.Create another file, let's say functions.php, in the same dir and move function into it. Now you can require this file inside your php page:

require_once 'functions.php';

// and call your function
someFunctionName();

Upvotes: 1

AymDev
AymDev

Reputation: 7474

I think you are looking for include

Save your file, and then you can include it in another file as:

include 'my_file.php';

You can also use:

Read the documentation for further explanations or see this question

Upvotes: 0

Related Questions