SitePoint Sponsor

User Tag List

Results 1 to 19 of 19

Hybrid View

  1. #1
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How Can I $_GET a variable?

    Notice: Undefined variable: rmenu in C:\wamp\www\nyhungry\restaurants\shoe1.php on line 76

    I know how to global GET an integer
    PHP Code:
    " . (int) $_GET['shoe'] 
    But not a variable, which in this case the rmenu value pass undefined being.

    How could I global get the $rmenu variable so I can use it's value.

    Thank you.
    Last edited by co.ador; May 13, 2009 at 20:21.

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Please restate your question in coherent English. An example of the problem would be useful as well.

    The error you have means you tried to use a variable $rmenu that was not defined before you used it. The solution is to not use variables which you haven't defined.

  3. #3
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It was defined before but the value of $rmenu does not pass

    PHP Code:
    while($rmenu mysql_fetch_array($regularshoe_set)){
      echo 
    "<li"
      if (
    $rmenu["id"] == $sel_shoe) {
      echo 
    " class=\"selected\""
      } 
      echo 
    "><a href=\"shoe1.php?menu=" urlencode($rmenu["id"]) ."\";>
    {$rmenu["shoename"]}</a></li>";  }
      }
     if (
    $rmenu["id"] == $sel_menu) { // this is line 76 
    If you note before line 76 the rmenu value is difined but so how it doesn't pass it to the $rmenu below it.

    Sorry about the incoherent English.

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    That variable only exists within the loop. Even if it existed outside the loop, $rmenu would simply hold false as that was the last return value of mysql_fetch_array() when there were no more rows in the result set.

  5. #5
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so It doesn't exist.

    I will abandon this possibility then.


    Thanks though!

  6. #6
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    That variable only exists within the loop.
    This statement is false. Loops run in the scope they are created in so all variables set in a loop do exist outside it.

    Quote Originally Posted by Dan Grossman View Post
    [...] $rmenu would simply hold false as that was the last return value of mysql_fetch_array() when there were no more rows in the result set.
    True, but it's got nothing to do with PHP throwing an undefined variable error. The variable is defined and set to FALSE.

    @co.ador: Please paste in more code (especially the code after problematic line).

    EDIT: After formatting your code correctly:

    PHP Code:
      while($rmenu mysql_fetch_array($regularshoe_set)) {
        echo 
    "<li"
      
        if (
    $rmenu["id"] == $sel_shoe) {
          echo 
    " class=\"selected\""
        } 
      
        echo 
    "><a href=\"shoe1.php?menu=" urlencode($rmenu["id"]) ."\";>{$rmenu["shoename"]}</a></li>";
      }
    // what block of code is this closing?

    if ($rmenu["id"] == $sel_menu) { // this is line 76 
    it is obvious that you are closing another block of code (after closing the loop) before line 76. Please paste more code before the lines you have pasted.

  7. #7
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Skundbad it is debuged, I debuged it for a while but still showing up the problem. I will show some more detail so you guys can see more details.

    after line 76 the fallowing bit of code
    PHP Code:
    if ($rmenu["id"] == $sel_menu) { 
    I am trying to place or arrange the bit of code below this paragraph, so it can open the subject and hold on to its submenus and avoid to close the submenus once the users click on a submenu. The problem is that when a user open or click on a subject it opens it's submenus but once users click on a submenu it closes everything and it return to show the subject only closing all the submenus below the subject. If users want to open another submenu other than the one opened again, the users have to click on the subject again to choose another submenu instead of leaving all of the submenus visible and available to the users regardless users clicking on a submenu or not. Is there a format or coding it could be arranged for that? The way it is arranged now, the subject always closes its submenus once a submenu is click on instead of leaving all the submenus visible whether it is clicked on or not.

    PHP Code:
     echo "<ul class=\"submenu2\">";
     while(
    $rmenu mysql_fetch_array($regularshoe_set)){
      echo 
    "<li"
      if (
    $rmenu["id"] == $sel_shoe) { 
      echo 
    " class=\"selected\"";
      } 
      echo 
    "><a href=\"shoe1.php?menu=" urlencode($rmenu["id"]) ."\";>{$rmenu["shoename"]}</a></li>";  }
    echo 
    "</ul>"

    decowski this is the rest of the code before the while loop and what the curly braces below closes is the If statement after the mysql_query, Thank you for the help.

    PHP Code:
    $regularshoe_set mysql_query($query$connection);
      if(!
    $regularshoe_set){
      die(
    "Database query failed:" mysql_error());} 
    if (
    $shoesubject["id"] == $sel_shoe['id'])  {   
    while(
    $rmenu mysql_fetch_array($regularshoe_set)) {
        echo 
    "<li"
      
        if (
    $rmenu["id"] == $sel_shoe) {
          echo 
    " class=\"selected\""
        } 
      
        echo 
    "><a href=\"shoe1.php?menu=" urlencode($rmenu["id"]) ."\";>{$rmenu["shoename"]}</a></li>";
      }
    //That carly brace is closing the first If then statement at the top of the code.

    if ($rmenu["id"] == $sel_menu) { // this is 
    line 76

  8. #8
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have a logical error in your code:

    PHP Code:
    if ($shoesubject["id"] == $sel_shoe['id'])  {
      while(
    $rmenu mysql_fetch_array($regularshoe_set)) {
      
    // [...]
      
    }
    }

    if (
    $rmenu["id"] == $sel_menu) { 
    So only if the first condition is met you set the $rmenu variable but you're trying to use it even if the condition is not met. You need to rethink what you're doing here.

    Sorry I can't help more but I can't understand the purpose of this code.

  9. #9
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if ($rmenu["id"] == $sel_shoe
    It is not sel_menu is $sel_shoe

  10. #10
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is irrelevant. I'm talking about $rmenu variable, because that's what PHP is complaining about being not set. Look again at the snippet:

    PHP Code:
    if ($shoesubject["id"] == $sel_shoe['id'])  {
      while(
    $rmenu mysql_fetch_array($regularshoe_set)) {
      
    // [...]
      
    }
    }

    if (
    $rmenu["id"] == $sel_menu) { 
    You only set $rmenu variable if the first condition is met but then try to use the variable outside the condition (thus it may be not set).
    Last edited by decowski; May 14, 2009 at 08:36.

  11. #11
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Define a variable before the loop, set its value within the loop, and after the loop is over, it will hold the value from the last row.

  12. #12
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok let me try difine a variable with that name before the loop starts
    PHP Code:
    $regularshoe_set mysql_query($query$connection);
      if(!
    $regularshoe_set){
      die(
    "Database query failed:" mysql_error());} 
      
    $rmenu mysql_fetch_array($regularshoe_set
    // I am not sure If I should define or call $rmenu with a mysql_fetch_array?
    //another thing is that before the while loop I have an If statement.
    //I am not sure if I should place this variable in between the if statement and the while loop thought it was better to locate it here. 
      
    echo "<ul class=\"submenu\">";
      if (
    $shoesubject["id"] == $sel_shoekind['id'])  {
    while(
    $rmenu mysql_fetch_array($regularshoe_set)){
      echo 
    "<li"
      if (
    $rmenu["id"] == $sel_shoe) {
      echo 
    " class=\"selected\""
      } 
      echo 
    "><a href=\"shoe1.php?menu=" urlencode($rmenu["id"]) ."\";>
    {$rmenu["shoename"]}</a></li>";  }
      }
     if (
    $rmenu["id"] == $sel_menu) { // this is line 76 

  13. #13
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Doing that will throw away the first row of the result set, since you advanced the row pointer twice before getting inside your loop by calling mysql_fetch_array twice.

    PHP Code:
    $lastrow = array();

    while (
    $rmenu mysql_fetch_array($regularshoe_set)) {
      
    // other code
      
    $lastrow $rmenu;
    }

    //now you can use $lastrow 

  14. #14
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you Dan Grossman!!!

    I will keep trying tomorrow morning

  15. #15
    SitePoint Addict skunkbad's Avatar
    Join Date
    Apr 2008
    Location
    Temecula, CA
    Posts
    278
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One thing you should know about variables is that they can be set, unset, and not set at all. Being set means that the variable has a value of some sort applied to it. Unsetting a variable makes the variable lose all value, and a variable without a value is not set.

    When debugging your php applications, you will see errors like the one you saw:

    Notice: Undefined variable: rmenu in C:\wamp\www\nyhungry\restaurants\shoe1.php on line 76

    and this error tells you that the rmenu variable was not set. If you thought you had set it, you were wrong.

    Sometimes, programatically speaking, you will not know if a variable is set or not, and in that case, you can check if it is set or not, and do what is necessary.

    Tonight, after reinstalling my operating system, wamp, and all of my programs, one of my applications wasn't working, and it was hard to track down, because of the complex nature of the framework I was using. In the end, my error came down to a problem similar to yours. My script was checking for a server variable - $_SERVER['HTTP_USER_AGENT'] - and it didn't exist. I used the php function isset() to fix my problem like this:

    Code:
    if(isset($_SERVER['HTTP_USER_AGENT'])){
    			$ie6 = "MSIE 6.0";  
    			$browser = $_SERVER['HTTP_USER_AGENT'];  
    			$browser = substr("$browser", 25, 8);
    			if($browser == $ie6){
    				$this->template->javascripts[] = 'js/supersleight-min.js';
    			}
    		}
    I just thought you might need to know this, because I had a hard time learning php, and you seem sort of new.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •