TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   $_GET error (http://www.talkphp.com/absolute-beginners/4848-_get-error.html)

Tanax 08-14-2009 12:23 PM

$_GET error
 
Hi, I have a really weird error...

phpfile.php
PHP Code:


$year 
$_GET['year'];
        
        if(
is_numeric($year) || $year == 'current')
        {
        
            
$file 'subs/economy/' $year;
            
            if(
file_exists($temp->getFile($file))) 
            {
            
                
$temp->view($file);
                
            }
            
            else
            {
            
                
$data['msg'] = 'Sidan finns ej';
                
$temp->view('error'$data);
            
            }
            
        }
        
        elseif(!isset(
$year))
        {
        
            
$year 'current';
            
$temp->view('subs/economy/' $year);
        
        }
        
        else
        {
        
            
$data['msg'] = 'Sidan finns ej';
            
$temp->view('error'$data);
        
        } 

Viewing phpfile.php?year=2009 brings up the template subs/economy/2009.php. Viewing 2008.. well you get the point.

Problem is though, that if I'm just viewing phpfile.php it should set $year = 'current', but it gives me an undefined index error. Any ideas how I can set so if they give no parameter in the url, it should set the parameter to 'current'(it doesn't have to DISPLAY that in the url though)?

Salathe 08-14-2009 12:53 PM

You need to check if the year was provided, if not use current as a default.
PHP Code:

$year = isset($_GET['year']) ? $_GET['year'] : 'current'


Tanax 08-14-2009 02:36 PM

Yea, I tried like this:
PHP Code:

if(isset($_GET['year'])) $year $_GET['year']; 

But that didn't work. Your code worked however. So thanks!

adamdecaf 08-14-2009 04:40 PM

Quote:

Originally Posted by Tanax (Post 27759)
Yea, I tried like this:
PHP Code:

if(isset($_GET['year'])) $year $_GET['year']; 

But that didn't work. Your code worked however. So thanks!

His worked because you were not setting a default value if none was found, so PHP was using a[n] ''. <-- {Blank Space}

Tanax 08-14-2009 07:44 PM

Ooh.. right.. thanks :-)


All times are GMT. The time now is 09:58 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0