Jump to content

passing variables to model in view


Go to solution Solved by requinix,

Recommended Posts

I have this controller:

class papersController extends Controller
{
	//public $paper;
	public function index($StudylevelId='',$SubjectId='',$PaperId='',$name='')
	{
		echo 'StudylevelId is'. $StudylevelId;
		require MODEL.'paper_view.php';
		//$Paper new PaperView($StudylevelId,$SubjectId,$PaperId);
		// Retrieve data from the model
//$data = $this->model->getData();// Pass the data to the view
//$this->view->render('route_view', ['data' => $data]);
		$this->view('papers\index',new PaperView($StudylevelId,$SubjectId,$PaperId));
		$this->view->render();
		
	}

i need to pass the three variables $StudylevelId,$SubjectId,$PaperId in index function() to the paperview model

// Handles paper details
class PaperView
{
	// Public variables to be used in fsa template
	public $mPaper;
	public $mPaperId;
	// Private stuff
	private $_mStudylevelId;
	private $_mSubjectId;
	private $_mPaperId;
	// Class constructor
	public function __construct($StudylevelId,$SubjectId,$PaperId)
	{
		// Variable initialization
		// Get StudylevelId from query string casting it to int $paginate = new Pag(35);
		if ($StudylevelId<>'')
			$this->_mStudylevelId = (int)$StudylevelId;
		// Get SubjectId from query string casting it to int
		if ($SubjectId<>'')
		$this->_mSubjectId = (int)$SubjectId;

		if ($PaperId<>'')//$_GET['ProductId']))
			$this->mPaperId = (int)$PaperId;
		//$_GET['ProductId'];
		else
			trigger_error('PaperId not set');
	}

how can i access the data from paperview model in the actual view below.the echo statement works fine but the variables cannot be accessed in the view page for further use

	<section class="bg" >
			<?php //require MODEL.'paper_view.php';
	
				/*$className = str_replace(' ', '',
				ucfirst(str_replace('_', ' ',
				substr(basename(__FILE__), 0, strrpos(basename(__FILE__), '.')))));*/
	
				// Create presentation object
				//$Papersdetail=new PaperView($StudylevelId,$SubjectId,$PaperId);
				//$Papersdetail = new $className();
			?>
								   
    <!-- Quiz Box -->
    <div class="quiz">
        <header>
            <div class="title"><?=$data->mPaperId?><h2> PAPER 1</h2></div>

i can't acess $StudylevelId,$SubjectId,$PaperId in the above code. help out

Link to comment
https://forums.phpfreaks.com/topic/321812-passing-variables-to-model-in-view/
Share on other sites

You can't access $StudylevelId and such because those variables do not exist. You should use what's in $data.

You can't access $data->_mStudylevelId because it's private. Either make the variables public, like mPaperId is, or find some other way to make those values available outside of the class.

1 hour ago, requinix said:

You can't access $StudylevelId and such because those variables do not exist. You should use what's in $data.

You can't access $data->_mStudylevelId because it's private. Either make the variables public, like mPaperId is, or find some other way to make those values available outside of the class.

I can actually see this, when i render the view page.It is there in the paperscontroller as seen with the echo statement there. it does display at the top of the view page.My issue is that i cannot access this inside the paperview class that needs to use it.

1 minute ago, shadd said:

I can actually see this, when i render the view page.It is there in the paperscontroller as seen with the echo statement there. it does display at the top of the view page.My issue is that i cannot access this inside the paperview class that needs to use it. even when i made the mpaperId public,i couldn't displat it in the view template

 

3 hours ago, requinix said:

Post the code you tried, both for the class and for the view.

// Handles paper details
class PaperView
{
	// Public variables to be used in fsa template
	public $mPaper;
	public $mPaperId;
	// Private stuff
	private $_mStudylevelId;
	private $_mSubjectId;
	private $_mPaperId;
	// Class constructor
	public function __construct($StudylevelId,$SubjectId,$PaperId)
	{
		// Variable initialization
		// Get StudylevelId from query string casting it to int $paginate = new Pag(35);
		if ($StudylevelId<>'')
			$this->_mStudylevelId = (int)$StudylevelId;
		// Get SubjectId from query string casting it to int
		if ($SubjectId<>'')
		$this->_mSubjectId = (int)$SubjectId;

		if ($PaperId<>'')//$_GET['ProductId']))
			$this->mPaperId = (int)$PaperId;
		//$_GET['ProductId'];
		else
			trigger_error('PaperId not set');
	}
}

that is the class i used to try to capture the mpaperid variable from the papercontroller class below

class papersController extends Controller
{
	//public $paper;
	public function index($StudylevelId='',$SubjectId='',$PaperId='',$name='')
	{
		echo 'PaperId is'. $PaperId;//check wether variables are there
		require MODEL.'paper_view.php';
		//$Paper new PaperView($StudylevelId,$SubjectId,$PaperId);
		// Retrieve data from the model
		$this->view('papers\index',new PaperView($StudylevelId,$SubjectId,$PaperId));
		$this->view->render();
		
	}

the view is here

	<section class="bg" >
			<?php //require MODEL.'paper_view.php';
	
				/*$className = str_replace(' ', '',
				ucfirst(str_replace('_', ' ',
				substr(basename(__FILE__), 0, strrpos(basename(__FILE__), '.')))));*/
	
				// Create presentation object
				//$Papersdetail=new PaperView($StudylevelId,$SubjectId,$PaperId);
				//$Papersdetail = new $className();
			?>
								   
    <!-- Quiz Box -->
    <div class="quiz_box">
        <header>
            <div class="title"><?=mPaperId?></div>
          </header>
      </div>

iam not getting the variable displayed above.how can i do that

  • Solution

If you're going to work with PHP then I suggest you learn the fundamentals of PHP. That's going to be really important for you to be able to write code that actually works.

When you have a grasp on it, spend some time learning about how your MVC framework works. About how views work, and about this $data variable it apparently sets up in the view, and about how to use view classes to pass data.

I say that because I can see (1) you did not make anything public, and (2) changing $data->mPaperId to just mPaperId actually made your problem worse. What you should (as far as I can see) be doing is leaving mPaper and mPaperId alone, make your mStudylevelId and friends public (and please rename them to not have an underscore), and then using $data in the view to reference those properties. But that could be hard to understand if you don't have a basic knowledge of PHP or of the framework you need to use.

Posted (edited)
1 hour ago, requinix said:

If you're going to work with PHP then I suggest you learn the fundamentals of PHP. That's going to be really important for you to be able to write code that actually works.

When you have a grasp on it, spend some time learning about how your MVC framework works. About how views work, and about this $data variable it apparently sets up in the view, and about how to use view classes to pass data.

I say that because I can see (1) you did not make anything public, and (2) changing $data->mPaperId to just mPaperId actually made your problem worse. What you should (as far as I can see) be doing is leaving mPaper and mPaperId alone, make your mStudylevelId and friends public (and please rename them to not have an underscore), and then using $data in the view to reference those properties. But that could be hard to understand if you don't have a basic knowledge of PHP or of the framework you need to use.

Iam very sorry Sir. Iam writing an MVC FROM SCRATCH.

When iam in the paper controller class,the variables $StudylevelId,$SubjectId,$PaperId, are available with in that scope.

how can I avail them to the model class Paperview

class PaperView
{
    // Public variables to be used in fsa template
    public $mPaper;
    public $mPaperId;
    // Private stuff
    private $_mStudylevelId;
    private $_mSubjectId;
    private $_mPaperId;
    // Class constructor
    public function __construct($StudylevelId,$SubjectId,$PaperId)
    {
        // Variable initialization
        // Get StudylevelId
        if ($StudylevelId<>'')
            $this->_mStudylevelId = (int)$StudylevelId;
        // Get SubjectId from query string casting it to int
        if ($SubjectId<>'')
        $this->_mSubjectId = (int)$SubjectId;

        if ($PaperId<>'')//$_GET['ProductId']))
            $this->mPaperId = (int)$PaperId;
        //$_GET['ProductId'];
        else
            trigger_error('PaperId not set');
    }

}

which is called in the view file below:

Yes i have made them public in the papercontroller class,but now how do i access

them here where they are required

    <section class="bg" >
            <?php require MODEL.'paper_view.php';
    
                
                // Create presentation object
                $Papersdetail=new PaperView($StudylevelId,$SubjectId,$PaperId);
            ?>
                                   
    <!-- Quiz Box -->
    <div class="quiz_box">
        <header>
            <div class="title"><?=$Papersdetail->mPaperId?><h2></header>

i hope i have tried explaining better

Edited by shadd
more clarity
Posted (edited)
7 hours ago, shadd said:

Iam very sorry Sir. Iam writing an MVC FROM SCRATCH.

When iam in the paper controller class,the variables $StudylevelId,$SubjectId,$PaperId, are available with in that scope.

how can I avail them to the model class Paperview

class PaperView
{
    // Public variables to be used in fsa template
    public $mPaper;
    public $mPaperId;
    // Private stuff
    private $_mStudylevelId;
    private $_mSubjectId;
    private $_mPaperId;
    // Class constructor
    public function __construct($StudylevelId,$SubjectId,$PaperId)
    {
        // Variable initialization
        // Get StudylevelId
        if ($StudylevelId<>'')
            $this->_mStudylevelId = (int)$StudylevelId;
        // Get SubjectId from query string casting it to int
        if ($SubjectId<>'')
        $this->_mSubjectId = (int)$SubjectId;

        if ($PaperId<>'')//$_GET['ProductId']))
            $this->mPaperId = (int)$PaperId;
        //$_GET['ProductId'];
        else
            trigger_error('PaperId not set');
    }

}

which is called in the view file below:

Yes i have made them public in the papercontroller class,but now how do i access

them here where they are required

    <section class="bg" >
            <?php require MODEL.'paper_view.php';
    
                
                // Create presentation object
                $Papersdetail=new PaperView($StudylevelId,$SubjectId,$PaperId);
            ?>
                                   
    <!-- Quiz Box -->
    <div class="quiz_box">
        <header>
            <div class="title"><?=$Papersdetail->mPaperId?><h2></header>

i hope i have tried explaining better

Thanks Admin,i had to go back and re-read php and figured out how to get the data.

Edited by shadd
spelling

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.