Page 2 of 2 FirstFirst 12
Results 11 to 19 of 19

Thread: help with php if statement

  1. #11
    jmarcv's Avatar
    jmarcv is offline Cranky Coder
    Join Date
    Jan 2005
    Posts
    354

    Default

    Hey D! Hows the weather now that Matt is there!

    Lynne, you can also use $_REQUEST which will look at POST and GET and return either one.

  2. #12
    jmarcv's Avatar
    jmarcv is offline Cranky Coder
    Join Date
    Jan 2005
    Posts
    354

    Default

    > I basically want this:

    Sorry, but that code makes no sense. Both conditions are identical.

  3. #13
    rlhanson's Avatar
    rlhanson is offline Master Glow Jedi
    Join Date
    Aug 2007
    Location
    Chapman, Kansas
    Posts
    531

    Default

    Thanks for the response John-Marc,

    How is $cat1=$_POST['cat1'] which is "Animals"
    identical to
    $cat1=="recent"

    ?
    Thank you,
    Lynne Hanson
    RL Hanson-Online

  4. #14
    jmarcv's Avatar
    jmarcv is offline Cranky Coder
    Join Date
    Jan 2005
    Posts
    354

    Default

    Quote Originally Posted by rlhanson View Post
    Thanks for the response John-Marc,

    How is $cat1=$_POST['cat1'] which is "Animals"
    identical to
    $cat1=="recent"

    ?
    You mean, which MAY be animals....

    OK, I see what I missed - the comment line - well, when you see my 2 answers you are going to KICK yourself! LOL!

    First of all, == is a comparison operator, what you want is = which is a defining operator.

    Method 1
    Code:
    $cat1 = $_POST['cat1'];
    if (isSet($_POST['cat1'])){
    createGallery("prodimages/{$rs['pId']}/{$cat1}/","gallery/{$rs['pId']}/{$cat1}/");
     }else {
    //tell script that $cat1 is "recent"
    $cat1='recent';
     createGallery("prodimages/{$rs['pId']}/{$cat1}/","gallery/{$rs['pId']}/{$cat1}/",200);
       }
    Method 2
    Code:
    $cat1 = $_POST['cat1'];
    if (isSet($_POST['cat1'])){
    createGallery("prodimages/{$rs['pId']}/{$cat1}/","gallery/{$rs['pId']}/{$cat1}/");
     }else {
    //tell script that $cat1 is "recent"
     createGallery("prodimages/{$rs['pId']}/recent/","gallery/{$rs['pId']}/recent/",200);
       }
    ... and now, its obfuscation cleanup time!!!!
    Code:
    if (isSet($_POST['cat1'])){
     $cat1 = $_POST['cat1'];
    } else {
     $cat1 = 'recent';
    }
    createGallery("prodimages/{$rs['pId']}/{$cat1}/","gallery/{$rs['pId']}/{$cat1}/");

  5. #15
    rlhanson's Avatar
    rlhanson is offline Master Glow Jedi
    Join Date
    Aug 2007
    Location
    Chapman, Kansas
    Posts
    531

    Default

    ooooohhhhh! LOL Wow, thanks so much. I kept trying all these versions of "==" and could not get it to work!

    Thank you sooo much! That will help with a few things I am trying to accomplish.

    A couple other questions...
    When you receive the data like
    $cat1=$_POST['cat1'];

    does that always have to appear before the function or script you want to use it or can you create an external file that has all the posts and include it on each page?

    If I did want to include the file that has all of them would I have to make them global and make the entire page coded in php vs. going back and forth from html to php?

    And if each page has the
    $cat1=$_POST['cat1'];
    do I have to have a matching <input type="hidden"... to pass the same info to the next page?
    Thank you,
    Lynne Hanson
    RL Hanson-Online

  6. #16
    jmarcv's Avatar
    jmarcv is offline Cranky Coder
    Join Date
    Jan 2005
    Posts
    354

    Default

    >A couple other questions...
    >When you receive the data like
    >$cat1=$_POST['cat1'];
    >does that always have to appear before the function or
    >script you want to use it or can you create an
    >external file that has all the posts and include it on each page?
    Knowledge goes a long way.
    a POST global is a var that was included on a form with METHOD=POST, whether a checkbox, text, hidden, etc.
    a GET global is a var that was included on a form with METHOD=GET, or a var on a URL (ie: somewhere.com?cat1=arnimules&cat2=hat)
    $cat1 is a php var and is NOT global. If you have a function, it will NOT see php vars unless you make them global or pass them.
    $_POST['cat1'] is a global and will be seen anywhere.
    functions are order independant. I usually keep mine in functions.inc
    $cat1=$_POST['cat1'];
    This is duplicating a POST global to a php local var. You dont NEED to do it. Its mainly for readablilty.
    So you COULD just do this if you wanted:
    Code:
    createGallery("prodimages/{$rs['pId']}/{$_POST['cat1']}/","gallery/{$rs['pId']}/{$_POST['cat1']}/",200);
    But in YOUR case, since you want to supply a val if its blank, it makes sense to transfer and pass it.
    >If I did want to include the file that has all of them
    >would I have to make them global and make the entire page
    >coded in php vs. going back and forth from html to php?
    The usual esoteric Lynne-ism <g>
    I really dont know what this means.
    >And if each page has the
    >$cat1=$_POST['cat1'];
    >do I have to have a matching <input type="hidden"... to pass the same info to the next page?
    Yes, uhhh --- no, but ummm....
    OK, you want magic? I can see that. We all do. I should KNOW what you mean, even without the reference of what is going on in your brain.
    The computer should KNOW what window I am thinking of, I am looking at it!
    So - if you want the next page to have data, you need to pass it to it. As I specified, a POST comes from a form, so in order to send a POST to another page, yes, it needs to be a hidden, ... or a checkbox, text, etc.
    So why did I say 'No'? Because PHP has magic! And I think you may be wanting a little magic, so here we go.....
    PHP has something called sessions. To turn it on, this:
    session_start();
    needs to be at the very top of your script. Once you do that you turn on a NEW global - $_SESSION[]
    This is a SUPER global, because it sticks and is available for any page the user goes to that has session_start up top.
    So, consider this:
    $cat1=$_POST['cat1'];
    $_SESSION['cat1']=$_POST['cat1'];
    We put the post into the php var on the first line
    and session var in the second. Nothing else hanges. Yet.
    FYI: doing this puts the post into the php and session var in one shot
    $cat1=$_SESSION['cat1']=$_POST['cat1'];
    So now, do NOT put cat1 in a bunch of messy hidden html tags in a form and make a user submit.
    No, now, you can simply put a link on the page, and when the user clicks the link, if the new page (s)he goes to has the session start up top, then guess what???
    $cat1=$_SESSION['cat1'];
    $cat1 will magically have whatever the POST was on the last page the user was on! Whoa.... eh?
    To rewrite your IF to use sessions:
    Code:
    if (isSet($_POST['cat1'])){
     $cat1 = $_POST['cat1'];
    } else {
     $cat1 = 'recent';
    }
    // Store the modified value for any future pages
    $_SESSION['cat1']=$cat1;
    createGallery("prodimages/{$rs['pId']}/{$cat1}/","gallery/{$rs['pId']}/{$cat1}/");
    Chew on that for a bit.

  7. #17
    rlhanson's Avatar
    rlhanson is offline Master Glow Jedi
    Join Date
    Aug 2007
    Location
    Chapman, Kansas
    Posts
    531

    Default

    Lynne-ism == Read my mind. LOL

    I do already have a session started on each page, so that is one step closer to what you have said...

    I DO want magic!! lol So, I am c-h-e-w-i-n-g...

    thanks so much John-Marc!
    Thank you,
    Lynne Hanson
    RL Hanson-Online

  8. #18
    rlhanson's Avatar
    rlhanson is offline Master Glow Jedi
    Join Date
    Aug 2007
    Location
    Chapman, Kansas
    Posts
    531

    Default

    Warning: Unknown(): Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively. in Unknown on line 0
    I read that it's a security risk to have registered globals on... so will the session functionality not work with registered-gloabls off?

    This is what is on the top of each of my pages:

    PHP Code:
    <?php
    session_cache_limiter
    ('none');
    session_start();
    ob_start();
    ?>
    Thank you,
    Lynne Hanson
    RL Hanson-Online

  9. #19
    jmarcv's Avatar
    jmarcv is offline Cranky Coder
    Join Date
    Jan 2005
    Posts
    354

    Default

    Its just a warning and should not show up when you turn off error reporting.
    You can create a php.ini file and put this in it, then upload it where your scripts are at.
    session.bug_compat_warn = off

    I believe this will disable the message but still have globals off.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Referencing a PHP Class inside a PHP Include
    By rickpugh in forum Programming Talk
    Replies: 1
    Last Post: 01-22-2006, 08:30 AM

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14