Small problem with variable being assigned

Discussion in 'PHP' started by j_o, Jun 1, 2011.

  1. #1
    Hi Everyone I have a quick question regarding this section of my code.

    
    <?php find_selected_page(); //Returns selected page or subject?>
    <?php
    if (isset($_GET['page'])){
    	$type = "page";
    	$fieldname = $sel_page;
    	if(intval($_GET['page']) == 0 ){
    		redirect_to("content.php");	
    	}
    }
    if (isset($_GET['guide_page'])){
    	$type = "guide_page";
    	$fieldname = $guide_pages;
    	if(intval($_GET['guide_page']) == 0 ){
    		redirect_to("content.php");	
    	}
    }
    
    PHP:
    As you can see depending on the value of the GET variable the variable $fieldname is being set. When the GET var is "page" I am having no problems. However for some reason when the GET var is "guide_page" and it reaches this line

    $fieldname = $guide_pages;
    PHP:
    The variable $fieldname is not being set. I did some troubleshooting which led me to this part of the code. I then did print_r($guide_pages) which provided the expected results. I then did print_r($fieldname) and got nothing.

    I might have just made a small error in the code, but I can't find it. I also checked my find_selected_page() function which is working properly.

    Any help is appreciated, thanks in advance.
     
    j_o, Jun 1, 2011 IP
  2. littlejohn199

    littlejohn199 Peon

    Messages:
    42
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    where is variable $guide_pages set? what does it contain?

    do you do print_r for $fieldname right after "$fieldname = $guide_pages" ?
     
    littlejohn199, Jun 1, 2011 IP
    j_o likes this.
  3. j_o

    j_o Well-Known Member

    Messages:
    516
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    113
    #3
    $guide_pages is a global variable that is set in the find_selected_page() function I made. Here is the code in that function where it is set:

    
    elseif(isset($_GET['guide_page'])){
    			$guide_subjects = get_guide_page_by_id($_GET['guide_page']);
    			$sel_subject = NULL;
    			$sel_page = NULL;
    			$guide_pages = NULL;
    }
    
    PHP:
    when i did print_r I did it after $fieldname was set.
     
    j_o, Jun 1, 2011 IP
  4. j_o

    j_o Well-Known Member

    Messages:
    516
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    113
    #4
    Nevermind, I found the error...its in the code I just posted above. i had the wrong variable set to NULL as you can see. Of course I realize this after I ask for help....thanks though, you question let me to finding the error.
     
    j_o, Jun 1, 2011 IP
  5. mike30

    mike30 Well-Known Member

    Messages:
    887
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    140
    #5
    You are getting nothing because you are "Setting" the "$guide_pages" to NULL in here:
    
    elseif(isset($_GET['guide_page'])){
                $guide_subjects = get_guide_page_by_id($_GET['guide_page']);
                $sel_subject = NULL;
                $sel_page = NULL;
                $guide_pages = NULL;
    }
    
    
    PHP:
    also, are you sure that you had set it to "global" ??? I don't see it on the code above.

    ~Mike
     
    mike30, Jun 1, 2011 IP
  6. j_o

    j_o Well-Known Member

    Messages:
    516
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    113
    #6
    Thanks mike, as said above I found the error, and yes it was global I just didnt include that code.
     
    j_o, Jun 1, 2011 IP
  7. DomainerHelper

    DomainerHelper Well-Known Member

    Messages:
    445
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    100
    #7
    If you use !empty() in the place of isset(), you can check the variables for being set and for having a null value:) Just thought I would toss that in there too.
     
    DomainerHelper, Jun 1, 2011 IP