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
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):
@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
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):
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 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 / @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
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.