0

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:

 <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.

2
  • This would be much easier and simpler to do using jquery / javascript, and you could keep everything on a single page. Commented Nov 23, 2010 at 14:07
  • It would be but I can't rely on javascript for where this going to end up, it's a very restrictive environment unfortunately. Commented Nov 23, 2010 at 14:12

4 Answers 4

2

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>
Sign up to request clarification or add additional context in comments.

3 Comments

This is awesome! very simply and effective. Thanks for sorting this! :) and thanks for all the other replies also.
That's a great idea! However I'm starting to think that OP's main problem is not coding an if for each of the divs. Writing divs>80 content by hand seems really weird.
It's boring more than weird but there are no databases available and javascript isn't likely to be allowed. I've no other choice but to write it all out old skool fashion.
1
$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.

4 Comments

Ok, I think I follow this but I have specific unique content in each div so I'm not sure I could use this, could I? It would create all the divs but wouldn't allow for all the content? (There is a lot of content)
Where is the content coming from? You could include it in the array.
I have to copy it all in from InDesign documents, the documents are up to 20 A4 pages long, I figured putting this all into an array would be as much work as just placing it on the page?
I'd go with the css solution above by Eineki, seems far more elegant.
0

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.

2 Comments

Thanks for the reply. Would there be a way to move the div with the print commands outside of the loop? As with the other solution posted here I need to put unique custom content in each div and there is lots of it.
Where does your content come from? If you're writing down all your content (which is weird!!!), you could as well write if conditions to all your divs... Otherwise I would suggest something like ` $contents = array(0=>'content div id 0', 1=> 'content div id 1');` and replace at the code above where it says div contents by <?php print $contents[$i]; ?>
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.