Jump to content

Question PHP OOP Login video


rayus

Recommended Posts

I was very pleased with the PHP login videos, excelent!

 

I'am trying to extend the application with a simple user model and user controller and view.

My goal is a simple display of a query in the user view displaying some information.

 

In my controller all the properties and methods are availabl but how do i display them in the view:

My user class contains the following:

<?php

/*
User Class
Deal with user tasks tasks and sites
*/


class Users{




function __construct()
{

                         #global $Database;
                   }

function getSites(){

                     // get all sites
                     global $Database;

	// Query database
		$result = $Database->query("SELECT SiteCode FROM site ORDER BY SiteCode ASC");
		 $sites = $result->fetch_array(MYSQLI_ASSOC);

                                                      return $sites;

		// free result set.
		$result->close();
}

               function getUsers(){

                   global $Database;

                   // get all Users

                   $result = $Database->query("Select * FROM user ORDER BY Displayname ");
                   return $result;

                   // free result set
                    $result->close();   



               } 



                function getUsersPerSite(){

                   // Users per site matched on city 
                   // error handeling
                    global $Database;

                         // query database


                   $result = $Database->query( "SELECT * FROM user INNER JOIN site ON user.l = site.City ORDER BY SiteCode");

                   if( $result->num_rows != 0)
                   {

                       return $result;

                   }
                   else{

                   echo  "no results";  

                   }



                   // free result set
                    $result->close();   


               }

               function getLastUpdated(){

                   // display last updated database import Active directory
                    global $Database;
                   $result = $Database->query( "SELECT DISTINCT creationdate FROM user");
                   $date = $result->fetch_row();

                   return $date;

                   // free result set
                    $result->close();   

               }

               function getStatusSite($siteCode,$status){

                      //Gegevens op halen

                   global $Database;
                  $result = $Database->query( "SELECT * FROM user INNER JOIN site ON user.l = site.City WHERE SiteCode = '$siteCode' and status= '$status'");


                      if( $result->num_rows != 0){

                          return   $result;

                      }

                  else {

                           #echo "No users for this status on site " . $siteCode . "<br/>";
                          return false;
                  }
               }




}


?>



 

And my controler contains:

<?php
include ('includes/database.php');
include("includes/init.php");

// check authorization
if ($Auth->checkLoginStatus() == FALSE)
{
$Template->setAlert('Unauthorized!', 'error');
$Template->redirect('login.php');
}
else
{



      //Meta data
        $LastUpdate = $Users->getLastUpdated();
        $TotalSites= $Users->getSites();




   //load sites and meta data
      $Sites = $Users->getSites();
      $LastUpdate ->getLastUpdated();

      $Template->load("views/v_admr.php");
}


?>

 

The view needs to display the $Sites in a table, but thats not an issue. I recieve errors about the template model? But how does it fit in?

Link to comment
Share on other sites

Fatal error: Cannot redeclare class Template in D:\wamp\www\admr\models\m_template.php on line 9

 

included the model in init.php like the auth model.

If i understand correct. all the business logic needs to be in de model. So I only have to instantiate the class in the controler? But how does the view know about the class. It has only acces to the template class.

Link to comment
Share on other sites

What code is in your m_template.php? It sounds like you have an issue around line 9. Or perhaps you have two classes that both use the same "Template" as the class name? It doesn't sound like this is an error that's related to the Users model at all.

 

Correct - the view doesn't know anything about the Users class. However, it doesn't need to. The controller should create that Users object and take care of calling any of the Users object's methods. If you want to pass data into the controller from the model, that's what the Template's getData() and setData() functions are for. You can use setData() to pass data into the view, and getData() can be used within the view to display that data.

Link to comment
Share on other sites

The template class only contains default code from the example. And line 9 is the first line of the class {. Don't thing there is the problem. I have to use the 2 methods you mentioned.

 

 

 

 

Then I have to change the setDatamethod to except an array or object? like:

 

In the controler:

$Template->setData("Sites_data",$Users->getSites());

 

In the view:

$result = $Template->getData("Sites_data");

Loop trough the properties

Link to comment
Share on other sites

In regards to the Template error, do you have multiple instances of this template class? One in admr\models\m_template.php and another m_template.php somewhere else? The error message indicates that you have already created a Template class and you can't create another. I'm not sure quite how I can help you with this... it's always a bit harder to troubleshoot when you're dealing with a full project with lots of files rather than just a file or two. That said, if you zip up the project (I don't need a dump of the database) and email it to ben [at] falkencreative.com I can take a look.

 

I have to use the 2 methods you mentioned.

Yes - assuming you don't want to rewrite the Template class. I'm sure there are other ways to pass data between the controller and view, but you'd have to write those yourself.

 

I don't think you need to modify the getData/setData functions to accept arrays -- that should work fine without any changes.

 

If you want to avoid PHP code in your view, you could create a string within the controller that will hold the table, and do all the processing/looping through the results of the $Users->getSites() within the controller. Once the table is created and held within that temporary variable, you pass the variable off to the view. Something like

 

$table = '<table cellpadding="0" cellspacing="0">';
$sites = $Users->getSites();
// start loop
   // for each loop, add to the $table variable
   $table .= '...additional code here...';
// end loop
$table .= '</table>';
$Template->setData("Sites_data", $table);

Otherwise, you could (like you started above) do all the looping/creating the table within the view.

Link to comment
Share on other sites

Thanks for the offer and hints so far but I got some what further:

I changed the setData Methode in the Template:

 

function setData($name, $value){

 

//set data

#$this->data[$name] = htmlentities($value, ENT_QUOTES);

$this->data[$name] =$value;

 

}

I think I all most got the required data in the view now. I will try to change the PHP in the view.

Link to comment
Share on other sites

Ah, ok, I see what you mean now regarding setData(). Actually, the htmlentities() function is there as a security measure -- for example, if you are using setData() to grab the input into a form and you want to make sure that it is safe. A better alternative might be something like this (which I actually did in my CMS series, which is based on the OOP Login series:

 

function setData($name, $value, $security = true){

  if ($security == TRUE)
  {
     $this->data[$name] = htmlentities($value, ENT_QUOTES);
  }
  else
  {
     $this->data[$name] = $value;
  }
}

You can then disable the htmlentities security like this (it defaults to "true", unless you set the third perimeter:

 

setData('name', 'value', false);

Link to comment
Share on other sites

Spot on thats what I was looking for!

Ah, ok, I see what you mean now regarding setData(). Actually, the htmlentities() function is there as a security measure -- for example, if you are using setData() to grab the input into a form and you want to make sure that it is safe. A better alternative might be something like this (which I actually did in my CMS series, which is based on the OOP Login series:

 

function setData($name, $value, $security = true){

  if ($security == TRUE)
  {
     $this->data[$name] = htmlentities($value, ENT_QUOTES);
  }
  else
  {
     $this->data[$name] = $value;
  }
}

You can then disable the htmlentities security like this (it defaults to "true", unless you set the third perimeter:

 

setData('name', 'value', false);

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...