Jump to content

papacostas

Members
  • Posts

    4
  • Joined

  • Last visited

    Never

Posts posted by papacostas

  1. Ok I have a problem which i assume is not that complicated,

    but it is for me... ;)

     

    I have a class called Display that makes calls to a database access class called DAO

    where all the SQL code is and the logic for fetching the data.

     

    I've always been instantiating this class for every function, (like this)

     

      require_once(classname)

      $DAO = new DAO();

     

    which is not very good. so now i want to figure out a way to just do it once

    in the beginning of the class and then access is whenever i want to

     

    i've played around with constructors, autoload and global variables without getting the result i want.

    what can i do?

     

    here is a snippet of the class i currently use,

    it works fine for fetching single row data but multiple records i will need to access the FetchArray class from the DAO

    which fetches the mysql array.

     

    any comments are greatly appreciated

     

     

    class Display {

     

    function getDBArray($function, $id) {

    $getArray = call_user_func(array($DAO, $function), $id);

    $return = $DAO->fetchArray($getArray);

    return $return;

    }

    function postDBArray($function, $values) {

    $postArray = call_user_func(array($DAO, $function), $values);

    }

            function getUserInfo($id){

    $user = $this->getDBArray('getUserInfo', $id);

    echo hello $user['name'] ;

    }

    }

  2. I have an OOP problem.

    Im trying to create a simple DB class structure but when it lists the

    result it turns in to a never ending loop, what am i doing wrong?

     

    the getEvents function in Display calls the getEvents function in the DAO class

    which performs the SQL query by calling the executeSQL function that returns the resultset

     

    what should i change for this to work?

     

    thank you

     

    class Display {

    function getEvents() {

    include 'inc/dao.php';

    $DAO=new DAO();

    while ($events = $DAO->getEvents()) {

    echo $events['headline'];

     

    }

    }

    }

     

    class DAO {

    private $SQL;

    function executeSQL($SQL) {

    require_once 'inc/db.php';

    $db = new DB();

    $db->opendb();

    $results= mysql_query($SQL);

    return mysql_fetch_array($results);

    }

    function getEvents () {

    $query = "select id, headline, startdate, desc_short, filename from events";

    $query = $this->executeSQL($query);

    return $query;

    }

    }

     

  3. im trying to plan and write a flexible and simple DAO for all my SQL.
    I want to be able have a class that decides what function to use based on how many objects im throwing at it.

    I recall writing classes like this in Java but i cant be able to make it work in PHP


    for instance

    function sql($variable) {
    generate sql + db fetch code

    }

    function sql($variable, $variable) {
    generate sql based on two variables + db fetch code

    }
    what i mean is the same function name but different amount of variables, im sure you get what im trying to do.

    i've made similar classes in the past but i always end up having a crapload of functions based on
    how many variables i need,

    does anyone have any thoughts about how to go about with this problem?
    I've read numerous of how to create DAO articles but I find them all overly complicated and not really
    dealing with what i need.

    how did set up your DAO class structure?
    any general thoughts on the subject?
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.