SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    622
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    $Qry and function

    hi all

    i have this on my product.php
    PHP Code:
    <?
    if(($categy == 1) && ($dealr == 2))
        {
        
    $qry="select * from men";
        
    clothes();
        }
    if((
    $categy == 1) && ($dealr == 3))
        {
        
    $qry="select * from women";
        
    clothes();
        }
    ?>
    and this in my functions.php

    PHP Code:
    <?
    function clothes()
    {
        echo 
    '<form action="">
        <select>
        <option>SELECT</option>'
    ;
        
    $result=mysql_query($qry);
        while(
    $row=mysql_fetch_array($result))
        {
        
    // long code of 20 lines;
        
    }
        echo  
    '</select></form>';
    }
    ?>
    If i put the "$qry" inside the function then i dont get mysql_fetch_array error but only one query works (means i get result for either men or women based on qry inserted in function).

    If i dont put "$qry" inside function then i get mysql_fetch_array error.

    vineet

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,127
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    You will need to pass the $qry to the function. Otherwise, you would need to use global $qry; but I don't usually recommend that.
    PHP Code:
    <? 
    if(($categy == 1) && ($dealr == 2)) 
        { 
        
    $qry="select * from men"
        
    clothes($qry); 
        } 
    if((
    $categy == 1) && ($dealr == 3)) 
        { 
        
    $qry="select * from women"
        
    clothes($qry); 
        } 

    function 
    clothes($qry

        echo 
    '<form action=""> 
        <select> 
        <option>SELECT</option>'

        
    $result=mysql_query($qry); 
        while(
    $row=mysql_fetch_array($result)) 
        { 
        
    // long code of 20 lines; 
        

        echo  
    '</select></form>'

    ?>

  3. #3
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    622
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    hi cp

    thanks.
    It works perfect.

    I have another question :

    Suppose on top of my "product page" i have these variables
    PHP Code:
    $one $_REQUEST['one'];
    $two $_REQUEST['two'];
    $three $_REQUEST['three']; 
    and i want echo their values inside the function, then do i have to make them all global

    PHP Code:
    function clothes($qry

        echo 
    '<form action=""> 
        <select> 
        <option>SELECT</option>'

        
    $result=mysql_query($qry); 
        while(
    $row=mysql_fetch_array($result)) 
        { 
           echo 
    $one// echoes nothing 
           
    echo $two// echoes nothing 
           
    echo $three// echoes nothing

        // long code of 20 lines; 
        

        echo  
    '</select></form>'

    ?> 
    if i repeat those variables with $request on top of function or inside function then those values get echoes fine.

    PHP Code:
    function clothes($qry

    $one $_REQUEST['one'];
    $two $_REQUEST['two'];
    $three $_REQUEST['three'];
        echo 
    '<form action=""> 
        <select> 
        <option>SELECT</option>'

        
    $result=mysql_query($qry); 
        while(
    $row=mysql_fetch_array($result)) 
        { 
           echo 
    $one// echoes one fine
           
    echo $two// echoes two fine
           
    echo $three// echoes three fine

        // long code of 20 lines; 
        

        echo  
    '</select></form>'

    ?> 

    vineet

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,127
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    $_REQUEST is already global, so you can just utilize that within your function. Or similar to how you passed $qry, you can pass $one, $two, and $three into your function as well.
    PHP Code:
    <?
    $one 
    $_REQUEST['one']; 
    $two $_REQUEST['two']; 
    $three $_REQUEST['three']; 
    clothes($qry$one$two$three);

    function 
    clothes($qry$one$two$three)  
    {  
        echo 
    '<form action="">  
        <select>  
        <option>SELECT</option>'
    ;  
        
    $result=mysql_query($qry);  
        while(
    $row=mysql_fetch_array($result))  
        {  
           echo 
    $one// echoes one fine 
           
    echo $two// echoes two fine 
           
    echo $three// echoes three fine 

        // long code of 20 lines;  
        
    }  
        echo  
    '</select></form>';  
    }  
    ?>

  5. #5
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    622
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    hi cp

    yes, this also works perfect.

    Are values fetched from database also come under global

    PHP Code:
    $qry "select name from producttable";
    $result mysql_query($qry);
    $row mysql_fetch_array($result);
    $name $row['name']; 
    Will $name work as global

    vineet

  6. #6
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,127
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    No, the only automatic globals are the super-globals $_REQUST, $_POST, $_GET, $_SESSION, $_COOKIE, $_SERVER, etc. Everything else, is not a global, and only has local scope.

  7. #7
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    622
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    hi cp

    local scope means it will available only on that page on which its called ?

    Actually i had a variable on top of my product page that is called from the database

    Code:
    $name = $row['name'];
    and when i called "$name" inside the function on product page, it echoed the value absolutely fine.

    If i will call this function on any other page then i think "$name" will echo nothing. i havent tried yet.

    Thats why i asked if variable from database is also global or not.

    vineet

  8. #8
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,127
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vinpkl View Post
    hi cp

    local scope means it will available only on that page on which its called ?
    http://php.net/manual/en/language.variables.scope.php

    Actually i had a variable on top of my product page that is called from the database

    Code:
    $name = $row['name'];
    and when i called "$name" inside the function on product page, it echoed the value absolutely fine.

    If i will call this function on any other page then i think "$name" will echo nothing. i havent tried yet.

    Thats why i asked if variable from database is also global or not.

    vineet
    You're going to have to show me that code so I can see if it fits the description or not, as I'm not 100% certain I understand the code layout you ran to be able to answer why it would have echo'd $name.

  9. #9
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    622
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    hi cp

    this is my product.php
    PHP Code:
    <?php
    $qry 
    "select name from producttable";
    $result mysql_query($qry);
    $row mysql_fetch_array($result);
    $name $row['name'];  
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>products</title>
    </head>
    <body>
    <?php
        
    if(($categy == 1) && ($dealr == 2)) 
        {
        
    $qry="select * from men";
        
    clothes($qry$name);
        }
    ?>
    </body>
    </html>
    this is functions.php
    PHP Code:
    <?
    function clothes($qry$name)  
    {  
        echo 
    '<form action="">  
        <select>  
        <option>SELECT</option>'
    ;  
        
    $result=mysql_query($qry);  
        while(
    $row=mysql_fetch_array($result))  
        {  
           echo 
    $name// echoes product name
        // long code of 20 lines;  
        
    }  
        echo  
    '</select></form>';  
    }  
    ?>
    vineet

  10. #10
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,127
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    You passed the variable to the function, so that is why the function has access. Otherwise, it would not have had access to it, so passing it was the right decision.


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
  •