Welcome Guest, Not a member yet? Register   Sign In
How to redirect using $this-session-userdata(country_territory)
#1

This is the coding in the Login Controller.

PHP Code:
$data = array('user_id' => $result->user_id,
'user_name' => $result->user_name,
'first_name' => $result->first_name,
'last_name' => $result->last_name,
'country_territory' => $result->country_territory); 

And this is the coding in the Controller that I've tried but PHP wont accept the variable in redirect.

PHP Code:
$country $this->session->userdata('country_territory');
redirect('countries/$country'); 

I want to redirect to the country that exists in the database rather than IP address.

Can anybody guide me?
Reply
#2

You can't use variables surrounded by single quotes.
Try this:
PHP Code:
redirect('countries/'$country); 

You'll also need a route, otherwise CI thinks the $country is a function in the countries controller.
By default, the first segment in a url is the controller, the second segment is the function.
Reply
#3

(09-22-2019, 10:44 AM)Wouter60 Wrote: You can't use variables surrounded by single quotes.
Try this:
PHP Code:
redirect('countries/'$country); 

You'll also need a route, otherwise CI thinks the $country is a function in the countries controller.
By default, the first segment in a url is the controller, the second segment is the function.

Thanks Wouter60, I changed your coding a little and without a route and everything appears to be working good now.

PHP Code:
redirect('/'$country); 
Reply
#4

You're welcome. Do I get it right that you have a different controller for each country?
Reply
#5

(09-23-2019, 07:43 AM)Wouter60 Wrote: You're welcome. Do I get it right that you have a different controller for each country?

Yes, later I will have but at present working on the first Controller, Afghanistan.

I've now been able to move on and starting to work on users files for them to establish their adverts but I'm at a lose on where to start. I haven't yet given it too much thought but you may be able to guide me in the right direction.

I have a folder in the root directory for all users in all the countries, and for storing all their information, including their adverts.
Reply
#6

Why not simply one controller that takes the country as a parameter? If you have a controller for each country, you'll probably have to write a lot of code over and over again. That's bad practice.
I would create a controller, e.g. "Adverts" and in that controller a function which accepts the country as parameter:

PHP Code:
public function show_adverts($country NULL)
{
  if (! $country) {
      show_error('You must include the country in the url!');
      die();
  }
  else {
      //get files form a subfolder for the given country and perform the actions you want.
  }


The url to call this function is:
Code:
https://yourwebsite.com/adverts/show_adverts/Afghanistan

If a user would include another country, all you need to do is handle that in the show_adverts function.
Reply
#7

(09-24-2019, 08:17 AM)Wouter60 Wrote: Why not simply one controller that takes the country as a parameter? If you have a controller for each country, you'll probably have to write a lot of code over and over again. That's bad practice.
I would create a controller, e.g. "Adverts" and in that controller a function which accepts the country as parameter:

PHP Code:
public function show_adverts($country NULL)
{
  if (! $country) {
      show_error('You must include the country in the url!');
      die();
  }
  else {
      //get files form a subfolder for the given country and perform the actions you want.
  }


The url to call this function is:
Code:
https://yourwebsite.com/adverts/show_adverts/Afghanistan

If a user would include another country, all you need to do is handle that in the show_adverts function.

Once I get Afghanistan working good I'll simply copy & paste that controller into the other countries controllers and use a text editor to change the name from Afghanistan to the name of the each country. I can't see my system working otherwise.

Thanks for the additional advice, I'll work on that in the next few days. I intend to download some program scripts from Hotscripts to see if I can get some other ideas.
Reply
#8

(This post was last modified: 09-26-2019, 09:06 AM by christaliise.)

(09-24-2019, 08:17 AM)Wouter60 Wrote: Why not simply one controller that takes the country as a parameter? If you have a controller for each country, you'll probably have to write a lot of code over and over again. That's bad practice.
I would create a controller, e.g. "Adverts" and in that controller a function which accepts the country as parameter:

PHP Code:
public function show_adverts($country NULL)
{
  if (! $country) {
      show_error('You must include the country in the url!');
      die();
  }
  else {
      //get files form a subfolder for the given country and perform the actions you want.
  }


The url to call this function is:
Code:
https://yourwebsite.com/adverts/show_adverts/Afghanistan

If a user would include another country, all you need to do is handle that in the show_adverts function.

How do I allow the User to create an advert, then connect the advert with the country?

I have the country controller separated by function classifieds which provides for the User to upload an advert, but I don't know how to connect the advert and or the User to those classifieds.

Im thinking if I create a controller say named "Adverts" and have a series of functions named 1, 2, 3, 4, 5, and so on, to match the user_id, and create an advert template within each function. Would that be appropriate?

Is there some way to auto generate the function names either by user_id or user_name?
Reply
#9

Don't create functions for every user.
Create one function that accepts the user_id as parameter.

Controller: Adverts.php

PHP Code:
public function create_advert($user)
{
  


To use this function: https://example.com/adverts/create_advert/3
(where 3 is the user_id).
Reply
#10

(09-26-2019, 12:30 PM)Wouter60 Wrote: Don't create functions for every user.
Create one function that accepts the user_id as parameter.

Controller: Adverts.php

PHP Code:
public function create_advert($user)
{
  


To use this function: https://example.com/adverts/create_advert/3
(where 3 is the user_id).

OK, I've created an Adverts Controller and several functions within, but I can see that I have a lot of learning to do.

Throughout the development I've used routes together with <a href="/createadvert">CreateAdvert</a> links rather than url inserts. Can you explain how I would automatically get the user_id within a link?

And before a Users creates an advert I want to give them an option of the advert layout, for example the "Description - Images - Ingredients - Details" to be in different places on the page. Where & how can I achieve that?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB