Jump to content

calling object from a class


narjis

Recommended Posts

I am calling an object from another class in the constructor of first class but when I call it in another function it does not access it's object. Here is the code.

<?php
/* This is controller.php used to control the flow of data between view.php and 
model.php. This file cotains all the controlling mechanism of the data 
It was created on 11th Feb 2011 By Narjis Fatima for an Open Source Project and
contains all the includes to the database etc.
*/
include ("view.php");
include ("functions.php");


$name = stripslashes($_GET['username']);
$email = stripslashes($_GET['email']);
//echo "in controller.php";
check_validate_input($name, $email);
$newUser = new controller($name, $email);
$action=$_GET['t'];
$newUser->display_view($action);


class controller
{
public $my_model;    //is an object of model class
public $user_info;//=array('id' , 'username' ,'role','email','interests','location', 'homepage');
    function __construct($name,$email)
    {
  
    $my_model = new model();
    $user_info =  $my_model->check_members($name,$email); echo "<pre>";
    print_r  ($user_info);

    }
    function __destruct()
    {
    }
    function display_view($token)
    {
    echo "In display function";
    echo "<pre>";
    print_r  ($user_info);
    if ($this->user_info == NULL)
    {
        if (($token=="register"))
        {
          show_registration_form();
        } 
        else{   
          show_mailing_form();  
        }
    }
    if ($this->user_info['role'] == "admin")
    {
         $myView = new view();
         $myView->view_admin_menu();
    }
     if ($this->user_info['role'] == "user")
    {
         $myView = new view();
         $myView->view_user_menu() ;
    }
    }//close of function display_view()
} 



/* name and email cominfg from index.php would be checked by the controoler class 
to be checked if it a vaild name and email. The database would be connected n the 
model.php*/
  
?>

The code for model.php is as follows

<?php
//This is model.php created on 10th Feb 2011  .It deals with daytabase connection
//
//By NArjis Fatima
//For the project of EDUForge an Open Source Software 
//include ("register.php");
class model{
public $dbh;
public $user_arr=array('username','password','email','location','interest','homepage');
//cunstructor of model class

//function to connect to database
function __construct(){
          try{
    $this->dbh = new PDO("mysql:host=localhost;dbname=phpfaqproject",'root','');
//echo "connected";
      }
  catch(PDOException $e){
  echo $e-> getMessage();
} 
}
public function check_members($username,$email)
{
   //get_connected();
   
   
   $sql = "SELECT * FROM account WHERE (username='" .$username ."' AND email='"
      .$email."')";
       $sth = $this->dbh->prepare($sql);
      $sth->execute();
  
    
    $result = $sth->fetch();
   /* echo('<pre>');        
    print_r($result);*/
    return ($result);
      
}
public function insert_data($user_info)
{
//$user_info = $user_in;
//$dbh;
try{
$dbh = new PDO("mysql:host=localhost;dbname=phpfaqproject",'root','');
//echo "connected";
}
catch(PDOException $e){
echo $e-> getMessage();
} 
         $dbh->exec("INSERT INTO account (username, userpassword, email, role, location, interest, homepage)
       VALUES ('" .$user_info['username']."','".$user_info['password'] 
      ."','".$user_info['email']."', 'user','".$user_info['location']."','".$user_info['interests']
      ."','".$user_info['homepage']."')");
}

}
?>

 

 

Please somebody help me. I am really stuck.

I am also sending my attachment.

 

[attachment deleted by admin]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.