4

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!";
?>
GrumpyCrouton
  • 8,486
  • 7
  • 32
  • 71
Mark
  • 85
  • 9
  • I think that's what he's asking - how to put code into a function, and maybe which coed – Mawg says reinstate Monica Aug 13 '18 at 14:42
  • 1
    @Mawg I hadn't read all the post sorry – Squalo Aug 13 '18 at 14:43
  • require_once is what you are looking for: http://php.net/manual/it/function.require-once.php – Simonluca Landi Aug 13 '18 at 14:44
  • Yea sorry, I am not very good at this, I am still trying to learn...my appologies! so that require once would place all the data on the page its being called from? – Mark Aug 13 '18 at 14:45
  • Require_once, require or include the file will work, and if its a class, instantiate this. – Ferrmolina Aug 13 '18 at 14:45
  • Like so... or – Mark Aug 13 '18 at 14:46
  • Yes, just ensure to use the right path. If fails , require and require_once stop and crash the code execution, include and include_once doesn't. – Ferrmolina Aug 13 '18 at 14:56
  • If you're looking for more information about this subject, I suggest you start with reading the official documents. [Such as the oop](http://php.net/manual/en/language.oop5.php) man page. It may give you a great place to start in knowing what to research. OOP has the benefit of offering readability aswell as cleanliness – IsThisJavascript Aug 13 '18 at 15:00

3 Answers3

1

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();
Nikita Leshchev
  • 1,784
  • 2
  • 14
  • 26
  • Great thank you very much, I will give this a try, I was kind of going in the right direction with wraping it in a function but limied by how to call it :-) – Mark Aug 13 '18 at 14:55
1

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

Ferrmolina
  • 2,737
  • 2
  • 30
  • 46
  • 1
    Hi Again, So I have tried the first approach by adding the require_once and then call the function, this gives me a php error of "PHP Parse error: syntax error, unexpected 'public' (T_PUBLIC)" I have read here...https://stackoverflow.com/questions/13341378/php-parse-error-syntax-error-unexpected-t-public that it is incorrect because it should be in a class? – Mark Aug 13 '18 at 15:08
0

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

AymDev
  • 6,626
  • 4
  • 29
  • 52
  • Awsome that works great, You need to "require" your file, where you want to use it. Here an example Working with classes: MyClass.php I had actually tried the second approach using MyClass but hadn't used the require_once at the top of the html :-) many many thanx for all of your help guys. – Mark Aug 13 '18 at 15:23
  • 1
    @Mark you can have multiple files (one for the database connections, one for your constants, etc), include them in 1 file (`master.php` for example), and then include this file on every page you need. As you may learn *Object Oriented Programing*, you will probably want to create an *autoloader* (something that will *include* your classes files automatically). I hope you'll enjoy learning PHP ;-) *(PS: if my question solved your issue, you can upvote and/or accept it)* – AymDev Aug 13 '18 at 15:30
  • That is axactly what I am trying to learn. I want to learn Object orientation, I am trying my best, But I have certain learning disabilities, however, I am also not the kind of person who says I cant :-), so I am trying! All the great feedback that you all have given me is putting me in the right direction...Untill now I have created a Session.php, SessionData.php and a GetSessionData.php. I would like to place all my SQl Strings into one place as well. Where can I say that your answer is the correct one? because I cant find it on the page doh :-) – Mark Aug 13 '18 at 16:34
  • 1
    @Mark I recommend you to often browse StackOverflow and the php.net documentation, this will help you a lot ! To accept my answer: left to the answers you can see the score (you can vote up/down), and just under the score you can see a grey check. By clicking on it you set the accepted answer of your question. – AymDev Aug 13 '18 at 17:20
  • I may have made a booboo in my questions/feedback and the places they need to be in (If so, my apologies!) Your advice is sound and helps me progress immensely thnx. But to clarify the awnsers so everyone is happy :-) The "Working with classes:" Part "is" how I have setup my Class:Method So that helped me understand how the class/methods works! And using the "include" method (If this is the correct terminology!) has helped me initiate the GetSessionData.php So again to all thank you very much for each piece of info! – Mark Aug 14 '18 at 02:29