开发者

PHP: If $_GET then bold it

开发者 https://www.devze.com 2023-01-19 08:31 出处:网络
echo \'<a href=\"?view=new&days=2\">2</a> • <a href=\"?view=new&days=4\">4</a> • <a href=\"?view=new&days=7\">7</a> • <a href=\"?view=new&days
echo '<a href="?view=new&days=2">2</a> • <a href="?view=new&days=4">4</a> • <a href="?view=new&days=7">7</a> • <a href="?view=new&days=14">14</a> • <a href="?view=new&days=30">30</a> days';

I have this as a "submenu", for showing newest members. You can choose for how many within days you want to see the newest users on the site.

Its much repeating myself, the only thing that is changing is days=30 in the link, and "30" as linktext.

Now I would like to check if $_GET["days"] == 2, then bold it, <strong>2</strong>.

I could 开发者_StackOverflowdo this myself, but then it would be just duplicating it all, and in every link do the $_GET["days"] == 1, $_GET["days"] == 2, $_GET["days"] == 3..

Isnt there a smarter way to do this without repeating myself all the time?


You could do this:

$days = isset($_GET['days']) ? $_GET['days'] : 2;
$daysArray = array(2, 4, 7, 14, 30);
foreach ($daysArray as &$val) {
    if ($days == $val) {
        $val = '<a href="?view=new&amp;days='.$val.'"><strong>'.$val.'</strong></a></b>';
    } else {
        $val = '<a href="?view=new&amp;days='.$val.'">'.$val.'</a>';
    }
}
echo implode(' • ', $daysArray);

Here the conditional operator cond ? expr1 : expr2 is used to assign either the value of $_GET['days'] or 2 to $days depending on whether isset($_GET['days']) is true or false. With using &$val in foreach, $val is a reference to the actual value in $daysArray so that changing $val inside the foreach will also change the value that it references in $daysArray. That means after the foreach loop $daysArray is an array of links that are then put together using  •  as separator.

This might not be the most concise solution but it fulfills the requirement. As other already mentioned, you might consider using an unordered list instead and display it as a single line list with the bullet separators using CSS instead:

ul {
    margin: 0;
    padding: 0;
}
ul > li {
    list-style-type: none;
    float: left;
}
ul > li + li:before {
    content: '•';
    margin: 0 0.35em;
}

In fact, I would prefer that too.


With an array ?

$days = isset($_GET['days']) ? intval($_GET['days']) : 2;

foreach(array(2,4,7,14,30) as $day) {
echo '<a href="?view=new&days='.$day.'">'.($day===$days ? '<strong>'.$day.'</strong>' : $day).'</a>';
}


I would have an array with all the values:

$days = array(2, 4, 7, 14, 30);

and create the list like so:

<ul>
<?php foreach($days as $day): ?>
    <li>
    <a href="?view=new&days=<?php echo $day ?>">
        <?php echo (isset($_GET['days']) && $_GET['days'] == $day) "<strong>$day</strong>" : $day; ?>
    </a>
    </li>
<?php endforeach;?>
</ul>


here is mine 5 cents.
everyone here is talking of MVC but almost no one using it in their examples.

first we have to prepare all required data.

<?
$day_vars = array(2, 4, 7, 14, 30);

if (!isset($_GET['days'])) $_GET['days'] = 2;
$key  = array_search($_GET['days'], $day_vars);
$days = $day_vars[$key];
?>

and then output in using a template
(partly based on Ben's good suggestion)

<ul>
<? foreach($day_vars as $day): ?>
  <li>
   <a href="?view=new&days=<?=$day?>"<? if($days == $day): ?> class="active"<? endif ?>>
    <?=$day?>
   </a>
  </li>
<? endforeach ?>
</ul>

as for the • character, all appearance should be made using CSS features.


make a loop :

$days = array(2, 4, 7, 30);
foreach($days as $day){
  $label = $_GET['days'] == $day ? '<strong>'.$day.'</strong>' : $day;
  echo '<a href="?view=new&days='.$day.'">'.$label.'</a>';
}

Plus you should use a CSS class instead of using strong tag :

$days = array(2, 4, 7, 30);
foreach($days as $day){
  $attr = $_GET['days'] == $day ? ' class="active"' : '';
  echo '<a href="?view=new&days='.$day.'"'.$attr.'>'.$day.'</a>';
}

CSS should be like this:

<style type="text/css">
 .active{ font-weight: bold}
</style>

This way you will later be able to change the appearance of the active link without modifying your PHP code

0

精彩评论

暂无评论...
验证码 换一张
取 消