开发者

Hide / Show divs from url string in php

A complete beginners question. I have a large number of divs (>80) on a page (page2.php) and what I would like to do is open page1.php, click on a link to open page2.php and show only one of these divs depending on which link was clicked.

I have a basic working version of this by adding an if else to the divs. I've only done this on 5 of the divs so far and it works but it also seems a fairly in-eloquent way of doing things.

Page 1:

开发者_JS百科
 <a href="page2.php?id=r0101">this is a link</a>

Page 2:

<?php 
$divID = $_GET['id'];
?>

<div id="r0101" <? if($divID == r0101): ?>class="show"<? else: ?>class="hidden"<? endif; ?> >

This then applies a css class to hide or show the div. Would it be possible to have a function or whatever at the top of the page that takes the id from the url, figures out that there is a div with that id also, show it and hide all the others? This is probably an easy thing to do but it has me stumped.

Any help greatly appreciated. Thanks.


Let alone the divs and work on css (as you relay on that to hide/show the divs). You can generate not only markup but css stylesheet too. Use a similar one (put it at the end of your head section). And let the browser do the work for you ;)

<style type="text/css">
    div {
        display: none;
    }

    div#<?php echo $_GET['id']; ?>:{
        display: block;
    }
</style>


$divs = array('r0101', 'r0102', 'r0103');
$divID = $_GET['id'];
foreach($divs as $div)
{
    echo '<div id="'.$div.'" class="';
    if ($div == $divID)
    {
      echo 'show';
    }
    else
    {
        echo 'hidden';
    }
    echo '">';
}

Assuming I have read the question correctly, you have a set of divs (r0101, r0102, etc.) and wish to show only one of these depending on the page you are on. The code above creates an array of these divs, loops through and creates the div. The class of the div is 'show' if the div matches the div from the page url, and 'hidden' otherwise.


First of all, you should consider a way of making your divs to be printed dynamically. Something like:

<?php
for($i = 1; $i <= 80; $i++):
?>
<div id="r<?php print $i; ?>">div contents</div>
<?php
endfor;
?>

Also, if you find a way of doing what's stated above, you can also do something like:

<?php
for($i = 1; $i <= 80; $i++):
    if($i == $_GET['id']){
        $class = 'show';
    } else {
        $class = 'hidden';
    }
?>
<div id="r<?php print $i; ?>" class="<?php print $class; ?>">div contents</div>
<?php
endfor;
?>

or

<?php
for($i = 1; $i <= 80; $i++):
    $class = ($i == $_GET['id']) ? 'show' : 'hidden';
?>
<div id="r<?php print $i; ?>" class="<?php print $class; ?>">div contents</div>
<?php
endfor;
?>

which is exactly the same but (using the ternary operator) spares a few lines and (some people think) it decreases readability.


If you want to make your download faster, you should output only the div you want to show. You could do something like this:

 $divs = array('r0101', 'r0102', 'r0103');
$divID = $_GET['id'];
foreach($divs as $div)
{
    if($div == $divID){ echo '<div> /* CONTENT HERE */ </div> };
}

Hope this helps.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜