Welcome Guest, Not a member yet? Register   Sign In
If statements in Controller?
#1

[eluser]industrial[/eluser]
No need to say that i am new @ CI development. You have probably figured that put by my title Smile

Can i use any kind of if/else statements in a controller?

Like following example, based on the shoe-sandal id example shown in user guide (http://ellislab.com/codeigniter/user-gui...passinguri )

Here comes the code:

Code:
<?php
class Products extends Controller {

    if (empty($sandals) && empty($id))
    {    
        echo('in else');
    }
    
    else
    {
        function shoes($sandals, $id)
        {
            echo $sandals;
            echo $id;
        }    
    }
    
}
?>


Thanks a lot!
#2

[eluser]jedd[/eluser]
Hi industrial, and welcome to the CI forums.

While I know this is a demo bit of code, I'll throw in a couple of other suggestions too.

First, you'd probably do this instead:

Code:
function foo ()  {
    if (empty($sandals) && empty($id))
    {    
        echo('in else');
    }
    else
    {
        $this->shoes ($sandals, $id);
    }
}

function shoes($sandals, $id)  {
    {
        echo $sandals;
        echo $id;
    }
}

Keep working through example code - there's plenty out there.

The code you posted .. you'd generally put all that in a function, rather than directly into the class as you've shown there.

Don't use closing ?> tags - they'll bite you later. PHP doesn't need them, anyway.
#3

[eluser]industrial[/eluser]
Hi Jedd! Thanks a lot for your kind words.

You suggest ny other words that I shall put this code in the current controller, is that right?

From my (tiny) experience, uri's are only passed and executed by the controller? Correct?
#4

[eluser]jedd[/eluser]
Yes - I was just extending what you'd written in your first bit of code, within the controller.

And yes, again - controllers take the URL information (well, a generalisation, but it'll do now). Re-read the section in the user guide on MVC and specifically controllers, and keep on playing!
#5

[eluser]industrial[/eluser]
[quote author="jedd" date="1256085432"]Yes - I was just extending what you'd written in your first bit of code, within the controller.

And yes, again - controllers take the URL information (well, a generalisation, but it'll do now). Re-read the section in the user guide on MVC and specifically controllers, and keep on playing![/quote]

Hi again Jedd,

Took your advice and read through the user guide together with a book and some additional tutorials to seek further advice and examples regarding this. However I am still unsure of how to make it work.

Here's the current version of my "products" controller. I think that i have implemented it correcly.

Code:
<?php
class Products extends Controller {

    function index()
    {
        echo 'in index';    
        
        function shoes($sandals, $id)
        {
            if (!empty($sandals) OR !empty($sandals))
            {
            echo $sandals;
            echo $id;
            }
        }
        
         if (!empty($sandals) OR !empty($id))
         {
            $this->shoes ($sandals, $id);    
         }
        
         else
         {
            echo 'in else';
         }    
    
    }
    
}

Now when i Access either products/shoes/sandals/123 or products/sandals/123 (that i preferably would like to use in the future) i get a 404 back.

By visiting just /products/ i run into both echoes, and get both "in index" and "in else" printed out.

What is the best way to verify if a specific URI segment exist, and if so, pass it into an function?

Thanks!
#6

[eluser]jedd[/eluser]
Okay, two things.

First, start using a constructor - as per the template code shown in the manual for controllers. Otherwise it'll bite you really soon when you start using CI classes.

Second, and it's what is causing your problem here - index() should not be the parent to the other functions or methods you are using there.

index() typically is empty or remaps back to the main or default function you have in your controller - simply because of the ambiguity of taking parameters to index() versus using a different method name (they both look the same in the URL).

Now, when you hit the url products/shoes/sandals/123 - it's looking for Products controller (which it can find) and then the shoes method (which it can't). So, 'lift up' shoes a level, so that it's peer of the index() method.
#7

[eluser]industrial[/eluser]
[quote author="jedd" date="1256162606"]Okay, two things.

First, start using a constructor - as per the template code shown in the manual for controllers. Otherwise it'll bite you really soon when you start using CI classes.

Second, and it's what is causing your problem here - index() should not be the parent to the other functions or methods you are using there.

index() typically is empty or remaps back to the main or default function you have in your controller - simply because of the ambiguity of taking parameters to index() versus using a different method name (they both look the same in the URL).

Now, when you hit the url products/shoes/sandals/123 - it's looking for Products controller (which it can find) and then the shoes method (which it can't). So, 'lift up' shoes a level, so that it's peer of the index() method.[/quote]


Thanks again Jedd! You really are a great guy!

My basic idea for using empty to check the variables, was to make the function idiot safe. If either of the variables werent posted, we could control the result instead of posting an error.

What happens now is that I get "Missing argument 2 for Prod:Confusedhoes()" + "Undefined variable: id" when url "prod/shoes/sandals/" is called, is just fully understable, but how can i get around it?

Do I already have one foot inside the remapping feature to make this happen, as shown in this article: http://www.anmsaiful.net/blog/php/codeig...nefit.html ?

Thanks for your time!
#8

[eluser]jedd[/eluser]
Read other people's code - there's no better way to learn (well, other than writing your own code). But yeah, picking up someone's code and analysing it, assuming you pick a smart person's code to start with, is an excellent learning experience.

There's no shortage of good demonstration code around, too. I hasten to add that my code is not, by any stretch of the imagination, good coode.

Quote:What happens now is that I get "Missing argument 2 for Prod:Confusedhoes()" + "Undefined variable: id" when url "prod/shoes/sandals/" is called, is just fully understable, but how can i get around it?

This is why you have default parameter values in your methods.

Consider .. somewhere in your Products controller ...
Code:
function shoes ( $type = FALSE , $id = FALSE )  {
     if (! $type )
          $type = 'sandals';   // Alternatively you'd set $type to a config entry you have for the default type or just die() here
     if (! $id )
          $id = 1;   // Alternatively you'd set $id to a config entry you have for the default id or just die() here
     }

Note that you can have your parameters in your method declaration set to, say : ($type = 'sandals', $id='1'). You'd do this if you don't care if wrong or absent information was in your URL, and were happy with a hard-coded default value. It's likely that an id of '1' doesn't match the category of sandals, though, so that probably isn't the best approach.

Don't take this the wrong way, but if you're having trouble with these things at the moment, you might want to put off remapping until you're more comfortable with the basic patterns.
#9

[eluser]industrial[/eluser]
[quote author="jedd" date="1256178286"]Read other people's code - there's no better way to learn (well, other than writing your own code). But yeah, picking up someone's code and analysing it, assuming you pick a smart person's code to start with, is an excellent learning experience.

There's no shortage of good demonstration code around, too. I hasten to add that my code is not, by any stretch of the imagination, good coode.

Quote:What happens now is that I get "Missing argument 2 for Prod:Confusedhoes()" + "Undefined variable: id" when url "prod/shoes/sandals/" is called, is just fully understable, but how can i get around it?

This is why you have default parameter values in your methods.

Consider .. somewhere in your Products controller ...
Code:
function shoes ( $type = FALSE , $id = FALSE )  {
     if (! $type )
          $type = 'sandals';   // Alternatively you'd set $type to a config entry you have for the default type or just die() here
     if (! $id )
          $id = 1;   // Alternatively you'd set $id to a config entry you have for the default id or just die() here
     }

Note that you can have your parameters in your method declaration set to, say : ($type = 'sandals', $id='1'). You'd do this if you don't care if wrong or absent information was in your URL, and were happy with a hard-coded default value. It's likely that an id of '1' doesn't match the category of sandals, though, so that probably isn't the best approach.

Don't take this the wrong way, but if you're having trouble with these things at the moment, you might want to put off remapping until you're more comfortable with the basic patterns.[/quote]

Yes sir! Have been going through sample applications and snippets to get more knowledge, but where can I find more info about this specific feature regarding giving funcion variables an initial FALSE value?

Tried to google "default parameter values" but without too much luck.

No offence taken regarding the remapping thing. I was quite certain of an existing and easy way to do what I wanted without leaving the basic features in CI. Many thanks for guiding me into the correct path!
#10

[eluser]jedd[/eluser]
[quote author="industrial" date="1256231582"]
... but where can I find more info about this specific feature regarding giving funcion variables an initial FALSE value?
[/quote]

You can apply any default value using that syntax. It means that if you call the function using a URL that doesn't include one or more of the parameters its expecting, it will use the default values instead.

I tried to find where this is described in the CI manual, but could not .. which is a touch weird. In any case, it's fairly straightforward, and you can easily play with it with a couple of echos in one of your functions, then throwing different URLs into your browser.




Theme © iAndrew 2016 - Forum software by © MyBB