简体   繁体   中英

Get all but one loop

I'm making a function called get_other_sites. But i think there is a better or faster way than I'm doing right now.

function get_other_sites($curent_site){
    if ($curent_site == 'site1.com'){
        echo"
            <a href='site2.com'>site2.com</a>
            <a href='site3.com'>site3.com</a>
            <a href='site4.com'>site4.com</a>
            <a href='site5.com'>site5.com</a>
            ";
    }
    else if ($curent_site == 'site2.com'){
        echo"
            <a href='site1.com'>site1.com</a>
            <a href='site3.com'>site3.com</a>
            <a href='site4.com'>site4.com</a>
            <a href='site5.com'>site5.com</a>
            ";
    }
    else if ($curent_site == 'site3.com'){
        echo"
            <a href='site1.com'>site1.com</a>
            <a href='site2.com'>site2.com</a>
            <a href='site4.com'>site4.com</a>
            <a href='site5.com'>site5.com</a>
            ";
    }
}

For this question I made it a little easyer to read but the list of sites is a lot longer. Does somebody know a better way to do this whit a lot lesser text?

thanks in advanced.

$current_site="def.com"; // testing
$sites=array("abc.com","def.com","ghi.com","jkl.com");
foreach($sites as $site){
     if($current_site!=$site)
       echo "<a href='$site'>$site</a>";
}

It's that simple :P

Fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM