Jump to content

[SOLVED] Tabs


zenix

Recommended Posts

Hi, I was experimenting and decided to try making tabs on a web page, made with php. I have it so the tabs initially display and the contents of tab one shows. When I click on tab 2 none of the contents for that tab show. If I click back on tab 1 there is no contents displayed. I've been wracking my head on this and can't see where my code is wrong. I'd really appreciate it if someone could take a look and help me out.

 

Code for tabs:

 

<?php
error_reporting(1);
$tabs =array();
function tabs_header()
{
?>
    <style type="text/css">
.tab
{
	border-bottom: 1px solid blue;
	text-align: center;
	font-family: arial, verdana;
}
.tab-active
{
	border-left: 1px solid black;
	border-top: 1px solid black;
	border-right: 1px solid black;
	text-align: center;
	font-family: arial, verdana;
	font-weight: bold;
}
.tab-content
{
	padding: 5px;
	border-left: 1px solid black;
	border-right: 1px solid black;
	border-bottom: 3px solid black;
}
</style>
    <?php
}
function tabs_start()
{
ob_start();
}
function endtab()
{
global $tabs;
$text = ob_get_clean();
$tabs[count($tabs)-1]['text']=$text;
ob_start();
}
function tab($title)
{
global $tabs;
if(count($tabs)>0)
endtab();
$tabs[] = array(title=>$title, text=>"test");
}
function tabs_end()
{
global $tabs;
endtab();
ob_end_clean();
$index =0;
if($_GET['tabindex'])
$index =$_GET['tabindex'];
?>
<table width="100%" cellspacing="0" cellpadding="0">
<tr>
<?php
$baseuri =$_SERVER['REQUEST_URI'];
$baseuri =preg_replace("/\?.*$/", "", $baseuri);

$curindex =0;
foreach($tabs as $tab)
{
$class ="tab";
if($index ==$curindex)
$class ="tab_active";
?>
<td class="<?php echo($class);?>">
<a href=<?php echo($baseuri."?tabindex=".$curindex);?>">
<?php echo($tab['title']);?>
</a></td>
<?php
$curindex +=1;
}
?>
</tr>
<tr><td class="tab-content" colspan="<?php echo(count($tabs)+1);?>">
<?php echo($tabs[$index]['text']);?>
</td></tr>
</table><?php
}
?>

 

Code for index page:

 

<?php
error_reporting (1);
include('tabs.php');
?>

<html><head><?php tabs_header();?></head>
<body>
<div style="width:600px;">
<?php tabs_start();
tab("tab one");
echo 'This is the first tab. I am just going to add a little text here to see if it makes any difference at all. ';
tab("tab two");
echo "This is the second tab, this is the tab that isn't showing anyting for me when I click on it.";
tabs_end();?>
</div>
</body></html>

Link to comment
https://forums.phpfreaks.com/topic/168394-solved-tabs/
Share on other sites

First time i've seen the output buffering used like this in a while...

 

Anyway, the problem is a tiny one. You've a rogue quote mark on this line:

 

<a href=<?php echo($baseuri."?tabindex=".$curindex);?>">

 

If you'd echoed out $_GET['tabindex'] or taken a look at the page your browser was going to, you'd probably have spotted that '...?tabindex=1" ' isn't quite right. Try:

 

<a href='<?php echo($baseuri."?tabindex=".$curindex);?>'>

 

 

Link to comment
https://forums.phpfreaks.com/topic/168394-solved-tabs/#findComment-888269
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.