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.

Help with If / Else Statment

Discussion in 'PHP' started by phplduser, Mar 5, 2020.

  1. #1
    Hi all,

    I have a section where people can suggest a number of social media links but if none is entered the code i'm using will then 'OutPut' the page url, What i'm looking to do is if no information is entered it is blank? The code i have -

                        <?php
                        if(trim($listing["fields"])!="")
                        {
                            $listing_fields = unserialize($listing["fields"]);
                         
                            if(is_array($listing_fields))
                            {
                                foreach($listing_fields as $key=>$value)
                                {
                                ?>
                    <li><a href="<?php echo $value;?>" target="_blank" rel="nofollow"><?php echo $key;?></a>
                             
                                <?php
                                }
                            }
                        }
                        ?>
    Code (markup):
    Any help would be very much appreciated as i only know a little bit of php.

    Mean statement or statment - sorry

    Best Regards
     
    Solved! View solution.
    phplduser, Mar 5, 2020 IP
  2. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #2
    Try this:

    <?php
    
    $urlOfPage= $_SERVER['HTTP_HOST']."/".$_SERVER['REQUEST_URI'] ."/?".$_SERVER['QUERY_STRING'] ;
    
    
    if(trim($listing["fields"])!=""){
    $listing_fields = unserialize($listing["fields"]);
    if(is_array($listing_fields)){
        foreach($listing_fields as $key=>$value){
        echo '<li><a href="'.$value.'" target="_blank" rel="nofollow">'.$key.'</a>';
        }//foreach ends
      }//array check ends
    }//main if ends
    else{
    echo '<a href="'.$urlOfPage.'"> This page </a>';
    }
    ?>
    Code (markup):
     
    Last edited by a moderator: Mar 5, 2020
    JEET, Mar 5, 2020 IP
    phplduser likes this.
  3. phplduser

    phplduser Member

    Messages:
    297
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    43
    #3
    @JEET

    Still the same issue, if no data entered, it lists the Field option and uses the page the code is on as the url.

    Many thanks
     
    phplduser, Mar 6, 2020 IP
  4. #4
    Oh ok, so you don't want the list displayed if $listing['fields'] is empty. Got it now.
    I was thinking you want page URL link displayed if nothing is provided...

    Can you show a sample of what is stored in $listing["fields"] if no social links are given?

    You can also try this:

    <?php
    if(trim($listing["fields"])!=""){
    $listing_fields = unserialize($listing["fields"]);
    if(is_array($listing_fields)){
        foreach($listing_fields as $key=>$value){
        if( strlen($value)>5 ){
        echo '<li><a href="'.$value.'" target="_blank" rel="nofollow">'.$key.'</a>';
        }//strlen($value) ends
        }//foreach ends
      }//array check ends
    }//main if ends
    Code (markup):
     
    Last edited by a moderator: Mar 6, 2020
    JEET, Mar 6, 2020 IP
    phplduser likes this.
  5. sarahk

    sarahk iTamer Staff

    Messages:
    28,494
    Likes Received:
    4,457
    Best Answers:
    123
    Trophy Points:
    665
    #5
    Here's my version

    
    //$a = ['facebook' => 'abc', 'twitter' => 'def'];
    $a = '';
    $listing['fields'] = serialize($a);
    
    $listing_fields = unserialize($listing["fields"]);
       
    if(is_array($listing_fields) && count($listing_fields)){
        foreach($listing_fields as $key=>$value){
            echo "<li><a href='{$value}' target='_blank' rel='nofollow'>{$key}</a>";
        }//foreach ends
    }//array check ends
    
    else{
    echo '<a href=""> This page </a>';
    }
    Code (php):
     
    sarahk, Mar 6, 2020 IP
    phplduser and JEET like this.
  6. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #6
    sarahk
    shouldn't this line:
    if(is_array($listing_fields) && count($listing_fields)){
    be like this:
    if(is_array($listing_fields) && count($listing_fields)>0 ){

    Thanks
     
    JEET, Mar 6, 2020 IP
  7. phplduser

    phplduser Member

    Messages:
    297
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    43
    #7
    @JEET / @sarahk

    Hi, ok i tried JEETS newly edited code and suggested code from sarahk (thank you both so much), JEETS Code is working now as i wanted it too, Did place your code in (sarahk) and it worked for removing 'Un-entered' data but still outputted the currant page url.

    I want to thank you both for taking the time to look and writing your code.

    Best Regards
    J
     
    phplduser, Mar 6, 2020 IP
    JEET likes this.
  8. sarahk

    sarahk iTamer Staff

    Messages:
    28,494
    Likes Received:
    4,457
    Best Answers:
    123
    Trophy Points:
    665
    #8
    count() works as a normal boolean if there's no comparison.
     
    sarahk, Mar 6, 2020 IP
    JEET likes this.
  9. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #9
    It's called empty(), use it.

    
    $trimFields = trim($listing['fields']);
    
    if (!empty($trimFields)) {
    	$listingFields = unserialize($trimFields);
    	if (is_array($listingFields) && !empty($listingFields)) {
    		foreach ($listingFields as $textContent => $uri) {
    			if (strlen($uri) > 5) echo '
    				<li><a href="', $uri, '" rel="nofollow" >',  $textContent, '</a></li>';
    		}
    	}
    }
    Code (markup):
    Oh and don't go shoving new windows down the users gullet. They want a new window they can middle click. target="_blank" was deprecated in 4 strict for a reason, and it being back in 5 is just more of the whatWG's mental-huffing-midgetry.
     
    Last edited: Mar 6, 2020
    deathshadow, Mar 6, 2020 IP
    JEET likes this.