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.

Please help with If else code

Discussion in 'HTML & Website Design' started by locals, Nov 18, 2015.

  1. #1
    Okay I am back, can someone help me add an "Else" part to this "If" part? I am having trouble because it is made into 2 parts and I am a little confused as to where I can fit some code in.
    SEMrush
    
    <input type="checkbox" name="job-categories[<?php echo $i; ?>]" value="<?php echo $redux_demo['resume-industries'][$i]; ?>" style="float: left; width: auto;" <?php if(!empty($user_job_categories)) { if (in_array($redux_demo['resume-industries'][$i], $user_job_categories[0])) { ?> checked="checked" <?php } } ?> ><?php echo $redux_demo['resume-industries'][$i]; ?>
    
    Code (markup):
    The error I am getting is:
    
    Warning: in_array() expects parameter 2 to be array, string given in
    
    Code (markup):

    If the box is checked, everything is fine... if it is not fixed I get this error. I have also tried to add $strict = FALSE but that does not work.


    thanks again!
     
    locals, Nov 18, 2015 IP
    SEMrush
  2. sarahk

    sarahk iTamer Staff

    Messages:
    26,411
    Likes Received:
    3,942
    Best Answers:
    111
    Trophy Points:
    665
    #2
    first up, don't be afraid of white space. Html doesn't care if break up your code like this...

    <input type="checkbox" 
    name="job-categories[<?php echo $i; ?>]" 
    value="<?php echo $redux_demo['resume-industries'][$i]; ?>" 
    style="float: left; width: auto;" 
    <?php if(!empty($user_job_categories)) { 
        if (in_array($redux_demo['resume-industries'][$i], $user_job_categories[0])) { 
            ?> checked="checked" <?php 
        } 
    } ?> ><?php echo $redux_demo['resume-industries'][$i]; ?>
    Code (markup):
    but having said that I'd be inclined to rewrite the code so that the logic occurs before you start the output and then you're just breaking the html to output values - takes a few more lines but easier to read and to maintain eg

    <?php $checked = '';
    if (!empty($user_job_categories)) { 
        if (in_array($redux_demo['resume-industries'][$i], $user_job_categories[0])) { 
            $checked="checked";
        } 
    }
    ?><input type="checkbox" 
    name="job-categories[<?php echo $i; ?>]" 
    value="<?php echo $redux_demo['resume-industries'][$i]; ?>" 
    style="float: left; width: auto;" 
    checked="<?php echo $checked; ?>"><?php echo $redux_demo['resume-industries'][$i]; ?>
    Code (markup):
    That would make it clearer where your error is and I can see some issues in the job categories check.
    first you ask if it's empty, but then treat it as an array which implies the check should be
    if (count($user_job_categories)){
    Code (markup):
    and you are assuming that the first entry in $user_job_categories is also an array - without seeing your data I don't know if that's appropriate. Just based on what we've got here I'd have thought you'd be looking for the industry in $user_job_categories rather than $user_job_categories[0] - but remember, I don't know your data.
     
    sarahk, Nov 18, 2015 IP
  3. locals

    locals Well-Known Member

    Messages:
    1,677
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    153
    #3
    Hello, thank you for the explanation. I erased the [0] from the area you mentioned and when all areas are blank there is no error. However, if I check or uncheck a box and then click save and then refresh the page the box is not checked.

    The code for the page is a little long, but I have copied it below, THANK YOU!!! for your help thus far. I will re read your post and see if there is any information that I missed.
     
    locals, Nov 18, 2015 IP
  4. locals

    locals Well-Known Member

    Messages:
    1,677
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    153
    #4
    the code was too long, I sent it to you in a message, hopefully you can help me :D
     
    locals, Nov 18, 2015 IP
  5. locals

    locals Well-Known Member

    Messages:
    1,677
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    153
    #5
    Here is the code, I found out how to upload the large file. Thank you in advance
     

    Attached Files:

    locals, Nov 18, 2015 IP
  6. sarahk

    sarahk iTamer Staff

    Messages:
    26,411
    Likes Received:
    3,942
    Best Answers:
    111
    Trophy Points:
    665
    #6
    can you do a var_dump of $user_job_categories after this line:

    $user_job_categories = get_user_meta($td_user_id, 'user_job_categories_subcriptions');

    and of $redux_demo['resume-industries']

    That's a hideous template, the kind of thing that gives WP a bad name. I hope you are just editing it, and it's not your work.
     
    sarahk, Nov 18, 2015 IP
    locals likes this.
  7. kk5st

    kk5st Prominent Member

    Messages:
    3,499
    Likes Received:
    377
    Best Answers:
    29
    Trophy Points:
    335
    #7
    I saw this, "File size: 85.1 KB", and figured there were serious issues, i.e. piled high and deep. @sarahk, your suggestion to separate the logic and structure as much as possible is a good one. Even better, would be to make logic and structure orthogonal by using a true templating engine. There are those who argue that PHP is a templating language. If so, then so are javascript (server-side), Perl, Python and others including even Lisp. All they require is the appropriate Apache x_mod. A black box API means logic does not depend on structure nor vice versa. You simply export the variables to the API and the engine, such as Smarty, uses the values, or not, as needed. You may alter how either works or is organized without affecting the other. The front-end guy simply asks for the values he needs and need not concern himself with how it's derived.

    cheers,

    gary
     
    kk5st, Nov 18, 2015 IP
  8. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,387
    Likes Received:
    1,865
    Best Answers:
    245
    Trophy Points:
    515
    #8
    I got the same reaction from three simple characters: "wp_"

    So it's universally rubbish... and illustrates yet ANOTHER reason I can't stand dealing with turdpress -- they actively encourage the sloppy practice of willy-nilly opening and closing PHP even when you don't have to or when it makes it HARDER to deal with like around IF statements.

    I mean even just simple crap like this:

    	?>
    
    						<?php require_once(get_template_directory() . '/inc/BFI_Thumb.php'); ?>
    									
    						<?php  
    Code (markup):
    Is like... really? REALLY? REALLY?!? Seriously, WHISKEY TANGO FOXTROT!!!

    But that's why I'd throw whatever mess of a site that is in the trash and start over clean, that file alone proves that it's a disaster not even worth trying to save. Of course, that's turdpress' core audience... as evidenced by such idiotic halfwit nonsense gibberish HTML as:

    <span class="my-account-header-title" style="max-width: 460px;">
    						<h1 class="resume-section-title"><?php _e( 'My Account', 'themesdojo' ); ?></h1>
    						<h3 class="resume-section-subtitle"><?php _e( 'Welcome back,', 'themesdojo' ); ?><span><?php the_author_meta('display_name', $td_user_id); ?></span>...</h3>
    Code (markup):
    Which is another stunning example of "If you don't know what's wrong with that, you shouldn't be making websites".


    As to the offending snippet, not being familiar with what that $user_job_categories holds it would be very hard to diagnose, but I'd suspect that either your array index is wrong, or the other variable is wrong. In those situations I suggest doing a ...

    echo '<pre>', print_r($varaible), '</pre>';

    ... on each of those (the pre makes it easier to read in the page than calling print_r or var_dump does without it) variables to see what they actually are and what they hold.

    Of course, like most turdpress code even that snippet raises warning flags, what with no unique ID on the input or a label around what is quite obviously a label -- a warning sign that again, whoever came up with that doesn't know enough about HTML to be making websites.

    IF I were to try and rewrite JUST the initial snippet you shared... and I'm guessing WILDLY here.

    echo '
    	<input
    		type="checkbox"', (
    			!empty($user_job_categories) && 
    			in_array($redux_demo['resume-industries'][$i], $user_job_categories) ?
    			' checked' : ''
    		), '
    		name="job-categories[', $i, ']"
    		id="jobCategories_', $i, '"
    		value="', $redux_demo['resume-industries'][$i], '"
    	>
    	<label for="jobCategories_', $i, '">
    		', $redux_demo['resume-industries'][$i], '
    	</label>';
    Code (markup):
    Since there is NO legitimate reason to be using the style attribute there as that's the type of crap that has NO business in HTML in the first blasted place, re-arranging the order is harmless and makes the code simpler, comma delimits are faster than dropping in and out of PHP at the parser stage, you only had one condition not two so just use an inline evaluation (aka a "ternary operator" to use PHP's terminology)... and probably axe the ="checked" part since you obviously do NOT have valid XHTML structure either since that code is wrapping inline level tags like <span> around block level tags like <h1> and <h3>... which is HTML 5's broken style of doing things.
     
    deathshadow, Nov 18, 2015 IP
    locals likes this.
  9. locals

    locals Well-Known Member

    Messages:
    1,677
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    153
    #9
    Okay, so... I will probably get laughed at, but it is working. :D I had to hack the hack - I do not know what is making it work but at least I do not get any errors when there are no boxes checked :D

    I tried to blend my two inputs but it didn't work that well and then I tried to leave the label area, but I had duplicate boxes that had the redux text under them. So I erased the code that was with the site but when I clicked the boxes and saved them it did not save the check box.

    anyways I have this code in there now and it is working, but I know it is not the correct way.


    
        <input
            type="hidden"', (
                !empty($user_job_categories) && 
                in_array($redux_demo['resume-industries'][$i], $user_job_categories[0]) ?
                ' checked' : ''
            ), '
            name="job-categories[', $i, ']"
            id="jobCategories_', $i, '"
        >
    
    
    
    
    
                                            <input type="checkbox" name="job-categories[<?php echo $i; ?>]" value="<?php echo $redux_demo['resume-industries'][$i]; ?>" style="float: left; width: auto;" <?php if(!empty($user_job_categories)) { if (in_array($redux_demo['resume-industries'][$i], $user_job_categories[0])) { ?> checked="checked" <?php } } ?> ><?php echo $redux_demo['resume-industries'][$i]; ?>
    
    
    Code (markup):



    Also I agree, even though I hate to refer to it as TurdPress sometimes it does suck with its vulnerabilities.

    But for what I use it for there is not anything else that I have come across that is as easy to use to manage 185 websites and upload my content and find new themes and even edit - with my little ability - to make a "nice looking" design, it may not be coded the best but the visitors do not know that :D


    I really appreciate EVERYONES help to assist me with getting this working properly even though you might not care too much for the platform I am using, you have assisted me with getting it to work to the best of its ability
     
    locals, Nov 19, 2015 IP
  10. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,387
    Likes Received:
    1,865
    Best Answers:
    245
    Trophy Points:
    515
    #10
    See the other thread -- fixed this way PROBABLY actually broke the functionality of it. The variable you were checking either didn't exist or was returning incorrect values since there's a third parameter that makes it return an array if FALSE, and the default if you omit that parameter is TRUE.

    Right up until it's slow loading, chews on bandwidth and mobile battery for nothing, and starts to choke out the hosting with too many file requests, too much markup to parse, etc, etc -- much less the accessibility DISASTER the gibberish markup and broken methodologies can lead to.

    EVEN when bashing things, doing so is usually frustration out of TRYING to help in system that takes something simple and makes it complicated. Actual programmers are even more likely to complain when said system flat out ignores good practices while people claim it is somehow "easier" since from our perspective, it clearly is not.
     
    deathshadow, Nov 19, 2015 IP
  11. ketting00

    ketting00 Well-Known Member

    Messages:
    748
    Likes Received:
    27
    Best Answers:
    3
    Trophy Points:
    128
    #11
    Does this also has to do with data consumption. I've been experienced exceeding data usage threshold for two consecutive months recently.
    Now I'm surfing webs with EDGE speed. What a pathetic.
     
    ketting00, Nov 21, 2015 IP
  12. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,387
    Likes Received:
    1,865
    Best Answers:
    245
    Trophy Points:
    515
    #12
    Bingo... It's funny how you'll hear the people who see nothing wrong with such bloated messes saying "everyone has broadband" in utter and complete ignorance of the bandwidth crunch that's been coming for over a decade. We're operating on old infrastructure at the backbone level and NOBODY is investing in building more. That's why ISP's worldwide are looking at instituting bandwidth caps and overage charges -- something our friends in Canada are now experiencing, something people on mobile plans are MORE than aware of, and a painful reality of life in large sections of places like Australia.

    It's why a LOT of people in these places are taking to browsing with images disabled, browsing with scripting selectively blocked or disabled, and even going so far as to seeing which sites are useful/usable without CSS.

    Once you start being charged more for going over, dropped to dialup speeds for going over, or just plain getting cut off cold the whole "don't worry about file count" and "don't worry about file sizes" IDIOCY tends to come across as a lame excuse for sloppy development habits and outright ignorance on the part of site owners.

    ... and that's just client-side. Bandwidth limits server side? HELLO. Not everyone has an unlimited bandwidth hosting resulting in the fact that code bloat, failure to leverage cache properly, and generally just sleazing things together any old way tends to cost more in the long run than either doing the job right at the start, or hiring someone to dot the t's and cross the i's for you... or, uhm... wait, that's not right. :p

    Again why if an accessible bandwidth design can't be hot and flashy, you need to start speaking to the client's wallet and overcome that pesky "credit mentality".

    Generally speaking that's why sites that blow 256k in 9 files to deliver zero content images, one form element and 511 bytes of plaintext are just as big a steaming pile of /FAIL/ as the sites that waste a few megabytes in hundreds of files on delivering a dozen content images and 10k of plaintext. Either way you're pissing off users and tossing your wallet into the ocean.
     
    deathshadow, Nov 22, 2015 IP