1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Why doesn't !="" && !="" work?

Discussion in 'PHP' started by misohoni, Mar 11, 2014.

  1. #1
    PHP is a bit complex! Been spending ages trying to figure this out where:

    <? if ($_GET["setID"] != "" && $_GET["modelID"] != "") { ?>
    Code (markup):
    Hope anyone can help?
     
    Solved! View solution.
    misohoni, Mar 11, 2014 IP
  2. ObliqueSolutions

    ObliqueSolutions Greenhorn

    Messages:
    6
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    13
    #2
    !empty($_GET[])
     
    ObliqueSolutions, Mar 11, 2014 IP
  3. misohoni

    misohoni Notable Member

    Messages:
    1,717
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    200
    #3
    I get this: Fatal error: Cannot use [] for reading

    What I want to do is allow a view for (index.php) ?setID=x AND ?modelID=x only...
     
    misohoni, Mar 11, 2014 IP
  4. thomasdgr

    thomasdgr Active Member

    Messages:
    167
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #4
    Try
    <? if (isset($_GET["setID"]) && isset($_GET["modelID"])) { ?>
    PHP:
     
    thomasdgr, Mar 11, 2014 IP
  5. pentaxial

    pentaxial Active Member

    Messages:
    101
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    78
    #5
    1. <? if ( ($_GET["setID"] != "" ) && ($_GET["modelID"] != "") ) { ?>
    Please check it and let me know
     
    pentaxial, Mar 11, 2014 IP
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #6
    It can be set AND empty, you should check for BOTH... you also should be using "empty", not ==""

    	if (
    		isset($_GET['setID']) && !empty($_GET['setID']) &&
    		isset($_GET['modelID']) && !empty($_GET['modelID'])
    	) {
    Code (markup):
    Again, they can be set and empty, so you have to check BOTH. (I really wish they had a check for both as a single operation)

    ... and for ghu's sake, stop opening and closing PHP on every line like that :D
     
    deathshadow, Mar 11, 2014 IP
  7. misohoni

    misohoni Notable Member

    Messages:
    1,717
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    200
    #7
    Nope none of them work for displaying it for setID and modelID. This works in that it doesn't display it for the categories page...not sure what's going on:
    <? if ($_GET["categoryID"] == "") { ?>
    Code (markup):
     
    misohoni, Mar 11, 2014 IP
  8. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #8
    Uhm... what EXACTLY are you actually trying to do? I have the feeling you aren't showing us enough code or explaining what it is you are actually trying to do....

    Should that even be an AND? You realize that && would require all conditions to be true to run the code inside the {}, right? Should they be separate IF for testing two different values? Should it be an OR? (unlikely in my mind)... Could we see the form your getData is coming from?

    I think we just need to see more to figure out what you are trying to do.
     
    deathshadow, Mar 11, 2014 IP
  9. misohoni

    misohoni Notable Member

    Messages:
    1,717
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    200
    #9
    Sorry guys, yes you're right.
    Here's the exact code below (apologies for opening/closing the tags so much...).

    The get call is all coming from one page, i.e.:
    index.php?setID=SETNAME
    index.php?categoryID=CATEGORY

    I'd only want the code below to be live on the setID and CategoryID pages only, which is currently:
    (* <? if ($_GET["categoryID"] == "") { ?> is what I'm looking to change *)

    <? if ($_GET["categoryID"] == "") { ?>
    <div id="secondarynav">
    <? include("inc-breadcrumbs.php"); ?>
    <? if ($_GET["setID"] != "") { ?>
    <span class="socialstuff"><a href="/"><img src="images/love-add.png" alt="test" /></a> <a href="/"><img src="images/love.png" alt="test" /></a> <a href="/"><img src="images/Facebook.png" alt="test" /></a> <a href="https://www.pinterest.com/join/?next=/pin/create/button/%3F"><img src="images/Path.png" alt="test" /></a> <img src="images/Reddit.png" alt="test" /> <a href="/"><img src="images/StumbleUpon.png" alt="test" /></a> <a href="/"><img src="images/Twitter.png" alt="test" /></a></span>
    <? } ?>
    </div>
    <? } ?>
    Code (markup):
     
    misohoni, Mar 11, 2014 IP
  10. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #10
    Well, that's kind-of a confusing mess thanks to all that malfing opening and closing PHP for no good reason, but translating it to this:

    if (isset($_GET['categoryId']) && !empty($_GET['categoryId'])) {
    
    	echo '
    		<div id="secondarynav">';
    		
    	include("inc-breadcrumbs.php");
    	
    	if (isset($_GET['setID']) && !empty($_GET['setId']) {
    		echo '
    			<span class="socialstuff">
    				<a href="/">
    					<img src="images/love-add.png" alt="test" />
    				</a>
    				<a href="/">
    					<img src="images/love.png" alt="test" />
    				</a>
    				<a href="/">
    					<img src="images/Facebook.png" alt="test" />
    				</a>
    				<a href="https://www.pinterest.com/join/?next=/pin/create/button/%3F">
    					<img src="images/Path.png" alt="test" />
    				</a>
    				<img src="images/Reddit.png" alt="test" />
    				<a href="/">
    					<img src="images/StumbleUpon.png" alt="test" />
    				</a>
    				<a href="/">
    					<img src="images/Twitter.png" alt="test" />
    				</a>
    			</span>';
    	}
    			
    	echo '
    		<!-- #secondaryNav --></div>';
    	
    }
    Code (markup):
    Should be what you are trying to do -- assuming you only want setID showing when categoryID is also true. Really some formatting and easing up on the endless pointless <? ?> for nothing would REALLY help make your logic clearer.
     
    deathshadow, Mar 11, 2014 IP
  11. misohoni

    misohoni Notable Member

    Messages:
    1,717
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    200
    #11
    I got an error, so I'm thinking this line should be:
    if (isset($_GET['setID']) && !empty($_GET['setID'])) {
    Code (markup):
    - But still didn't work, the <div id="secondarynav">xx</div> stuff isn't on any pages now...
     
    misohoni, Mar 11, 2014 IP
  12. misohoni

    misohoni Notable Member

    Messages:
    1,717
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    200
    #12
    Ok I saw the problem categoryId = categoryID works now.

    However, how do I include setID on this line as this doesn't work for the setID, categoryID and modelID pages:
    <? if (isset($_GET['categoryID']) && !empty($_GET['setID']) && isset($_GET['setID']) && !empty($_GET['categoryID']) && isset($_GET['modelID']) && !empty($_GET['modelID'])) {
    Code (markup):
     
    misohoni, Mar 11, 2014 IP
  13. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #13
    Are you sure you want all of those conditions to be true at the same time? Any one of them fails, none of them will run...
     
    deathshadow, Mar 11, 2014 IP
  14. misohoni

    misohoni Notable Member

    Messages:
    1,717
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    200
    #14
    Yep..I'd like it to be for those conditions only or should I use ! for the non-conditions which are:
    index.php
    index.php?site=about
     
    misohoni, Mar 11, 2014 IP
  15. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #15
    Again, I'm just really not clear on what you are even asking... and not certain you understood what I asked either. Need to see more of it as in what are you actually trying to do here?
     
    deathshadow, Mar 11, 2014 IP
  16. misohoni

    misohoni Notable Member

    Messages:
    1,717
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    200
    #16
    From what I understand:
    (isset($_GET['categoryId']) - Show if there's ?categoryID=SET
    && !empty($_GET['categoryId']) - Show if there's nothing set for ?categoryID.

    So I'd like to include setID and modelID conditions in that line, so it shows the code on these pages with setID and modelID on...
     
    misohoni, Mar 11, 2014 IP
  17. #17
    THIS:
    (isset($_GET['categoryID']) && !empty($_GET['setID']) && isset($_GET['setID']) && !empty($_GET['categoryID']) && isset($_GET['modelID']) && !empty($_GET['modelID']))

    Will only work if ALL of them are SET at the same time -- I think that's what you are missing here. As such whatever you are testing for would only work if for example:

    ?categoryID=something&setID=somethingElse&modelID=someOtherBlastedThing

    ALL of them AT ONCE. That would work out to true. This:

    ?categoryID=something&setID=somethingElse

    Would be false. As would:

    ?categoryID=something&modelID=someOtherBlastedThing

    and:

    ?setID=somethingElse&modelID=someOtherBlastedThing

    None of those would pass the test. Is that what you are trying to do? I have the feeling you either should be testing for 'or' instead. (|| instead of &&) or you should be breaking that into multiple separate conditions.

    Again, you aren't showing us enough of what you are doing with the result or where it's coming from for a reasonable answer to be given.
     
    deathshadow, Mar 11, 2014 IP
  18. misohoni

    misohoni Notable Member

    Messages:
    1,717
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    200
    #18
    Sorry for the headache! Yep it's || I should of used instead of && - thanks everyone for taking time out on this.
     
    misohoni, Mar 11, 2014 IP
  19. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #19
    Just be sure to pair your isset and empty with && when doing that. That's one reason I like to format as multiple lines to make it easier to follow, and pair them by variable instead of operation.

    if (
    	(isset($_GET['categoryID']) && !empty($_GET['categoryID'])) ||
    	(isset($_GET['setID']) && !empty($_GET['setID'])) ||
    	(isset($_GET['modelID']) && !empty($_GET['modelID']))
    ) {
    Code (markup):
    Since you want to make sure it's set (so you can work with them) and not empty... and beware that inside your routine you still can't be sure if they are set or not, which is why what you are doing inside that condition is important too. Also important is evaluating the && before each ||, hence the extra parenthesis to imply specificity / evaluation order.
     
    Last edited: Mar 11, 2014
    deathshadow, Mar 11, 2014 IP
    misohoni likes this.
  20. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #20
    oh, and when you get this deep into parenthesis and logic, an editor that highlights matching pairs of brackets and parenthesis can REALLY help. One of the few editor "aids" I actually like.
     
    deathshadow, Mar 12, 2014 IP
    misohoni likes this.