Learning by doing; 2 country selects [Solved]

Hello all,
Thanks for being there for. I hope someone has the patience to help out a person new to PHP. I learn by doing. I have programmed in several languages a long time ago. Now I would like to be able to say I have created my own PHP application. But it’s not going so well. I have gone through the tutorial. I can manage articles and tags. But I don’t know how the information gets to the screen.
I have a new class “Countries” with ID, Name, and 6 integer values. I The table exists and the Class came out fully baked. I would like create a page with 2 selectors; ‘homecountry’ and ‘visitcountry’. When a user has selected each country, the respective 6 parameters should appear on the screen. I will then perform some calculation on each pair, comparing both countries on the respective value.
My “index.php” contains a table, and starts with;

<?php echo $this->Form->index($country); ?>

I have learned from the documentation that this is the better alternative to;
<form method='post' action='countries/index'>
Maybe I can get some points for that.Furthermore, it contains 2 selects. I have not yet found the right. I thought that the following code would display what was selected on the previous page, if anything, given that we have selected a country and clicked a button bring is to the same page but now with values for those tow parameters;
<?= $this->$homecountry->name ?>
<?= $this->$visitcountry->name ?>

And I thought that these parameters can ‘automagically’ be found if I instantiate them in CountriesController. My CountriesController contains the following function;

public function index() { $countries = $this->Paginator->paginate($this->Countries->find()); $this->set(compact('countries')); if (isset($_GET['homecountry']) && isset($_GET['visitcountry'])) { $homecountry = $_GET['homecountry']; $visitcountry = $_GET['visitcountry']; } else { $homecountry = 1; $visitcountry = 1; } }
I wanted to start out simple by saying that if not all country parapmeters are found, let’s just start by displaying the values for the country with ID = 1.
However, the response to the call /content/countries

Notice (8): Undefined variable: homecountry [APP/Template/Countries/index.ctp, line 39]

So the moment we want to display any parameters, I run into trouble. Also, I do get 2 selector boxes but with no content.
What am I doing wrong? Better yet, can anyone help me to learn how to do it right? Thanks in advance.

I will look at your post/troubles this evening.

  1. Read about php and cakephp3 a lot more. <?= $this->$homecountry->name ?> should be <?= $this->homecountry->name ?> - the correct synthax, howerver this would have no sense in your case , you want to do : <?= $homeCountry ?>
  2. Do not use $_GET anywhere in cake php, you have request object available in controller under $this->request.
  3. To pass variable from controller to the view you need to use of the sythaxes eg : $this->set(‘homeCountry’, $homeCountry);
  4. In a view $this-> refffers to the view not to the controller.
  5. Conventions …

Thanks for the help. I have now made it work. The call that works to look up homecountry is;
$homecountryname = $this->request->getData(‘Homecountry’);
$homecountry = $this->Countries->findByName($homecountryname)->first();
$this->set(‘homecountry’, $homecountry);

I had to first look up the selected country from the list in the page before. I then used the ‘request’ function as requested (POint #2). I needed a lot of debugging before I found the correct call that corresponds to a form of method POST. The cookbook is not clear about that. It probably assumes that people have programmed with PHP in combination with HTML forms before.
I am familiar with O-O languages and context-based formulas using ‘this’. I found this is implemented not totally consistently here. For instance, <?= $homeCountry ?> indeed works and is now part of my solution but it leaves implicit where this variable is taken from. For instance, if I have 2 functions that each set ‘homecountry’ as a globally available variable, what would happen then? If I were able to indicate where to place a variable and where to take the variable from, the current for ($this) or from anohter location, that would make more sense. In any case, I now have a fully functioning page, which is worth celebrating, and am on to more now.
If you have any specific points on number #5, @Marcel , I’d be happy to hear about those.