7 Replies - 11825 Views - Last Post: 24 December 2008 - 02:33 PM Rate Topic: -----

#1 d.buckner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 04-December 08

PHP tabs

Posted 24 December 2008 - 10:48 AM

I am trying to learn how to make simple tabs in PHP, I have most of the funtionality working however the tabs do not change when clicked

<html>
<head>


<title>Tabs</title>
<style>
.link{
  font-family:tahoma;
  font-size:11px;
  font-weight:bold;

  color:white;
  text-decoration:none;
}
</style>
</head>
 
<body>
<?php
  $id=1;
 
  $links=array("Home","Link1","Link2","Link3","Link4");

 
  $total_links=count($links);

?>
  <table cellpadding="0px" cellspacing="0">

	<tr>
	  <td></td>
<?php
	for($i=0;$i<$total_links;$i++){ 
	  if($i+1==$id){
?>
	  <td style="padding-left:1px">

  <table cellpadding="0" cellspacing="0">
	<tr>

	  <td><img src="images/selectedLeft.gif" /></td>
	  <td style="background-image:url(images/selectedCenter.gif);" class="link"><?php echo $links[$i] ?></td>

	  <td><img src="images/selectedRight.gif" /></td>
	</tr>

  </table>
	  </td>
	  <?php } else { ?>
	  <td style="padding-left:1px">
  <table cellpadding="0" cellspacing="0">

	<tr>
	  <td><img src="images/normalLeft.gif" /></td>

	  <td style="background-image:url(images/normalCenter.gif);"><a href="?id=<?php echo $i+1; ?>" class="link"><?php echo $links[$i]; ?></a></td>

	  <td><img src="images/normalRight.gif" /></td>
	</tr>

  </table>
	  </td>
	  <?php } // else end
 
	  }  //for end
	?>
	  <td></td>
	</tr>

	<tr>
	  <td colspan="<?php echo $total_links+2?>" bgcolor="#6363ff"></td>
	</tr>

	<tr>
	  <td colspan="<?php echo $total_links+2?>" style="border:1px #1895D5 solid;">
		<h1 style="color:#000000">link #<?php echo $id?> goes here ...</h1>

	  </td>
	</tr>
  </table>
</body>
</html>



What did I miss?

Is This A Good Question/Topic? 0
  • +

Replies To: PHP tabs

#2 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

Re: PHP tabs

Posted 24 December 2008 - 10:51 AM

Because $id is set to 1 and is never changed anywhere. Perhaps make use of $_REQUEST or $_GET to set $id to whatever the page you're on?

This post has been edited by snoj: 24 December 2008 - 10:53 AM

Was This Post Helpful? 0
  • +
  • -

#3 mocker  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 50
  • View blog
  • Posts: 466
  • Joined: 14-October 07

Re: PHP tabs

Posted 24 December 2008 - 10:57 AM

the code for what snoj said would be
$id = $_GET['id'];



Also, if you are not deadset on making your own tab system, try checking out Yahoo's tab library : http://developer.yah...frommarkup.html
It is a javascript driven way of making tabs, and your php just has to output the appropriate content into the correct div
Was This Post Helpful? 0
  • +
  • -

#4 d.buckner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 04-December 08

Re: PHP tabs

Posted 24 December 2008 - 11:02 AM

View Postmocker, on 24 Dec, 2008 - 09:57 AM, said:

the code for what snoj said would be
$id = $_GET['id'];



Also, if you are not deadset on making your own tab system, try checking out Yahoo's tab library : http://developer.yah...frommarkup.html
It is a javascript driven way of making tabs, and your php just has to output the appropriate content into the correct div



I have tried both the $_REQUEST, and $_GET and get an error: Undefined index: id
Was This Post Helpful? 0
  • +
  • -

#5 d.buckner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 04-December 08

Re: PHP tabs

Posted 24 December 2008 - 11:27 AM

I got it! Thanks for all of your help, my problem was solved by adding the id in the address:

http://localhost/tabs.php?id=1

Thank you all againg for your assistance.
Was This Post Helpful? 0
  • +
  • -

#6 mocker  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 50
  • View blog
  • Posts: 466
  • Joined: 14-October 07

Re: PHP tabs

Posted 24 December 2008 - 11:41 AM

If you want it to default to id 1 when no id is specified you can use
$id = ($_GET['id']) ? $_GET['id'] : 1;


That way you don't have to worry about always linking to the page with id=1
Was This Post Helpful? 0
  • +
  • -

#7 d.buckner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 04-December 08

Re: PHP tabs

Posted 24 December 2008 - 12:16 PM

View Postmocker, on 24 Dec, 2008 - 10:41 AM, said:

If you want it to default to id 1 when no id is specified you can use
$id = ($_GET['id']) ? $_GET['id'] : 1;


That way you don't have to worry about always linking to the page with id=1



Thanks for the info, however for some reason my webserver is not executing ternary operators. I was able to make it work using the IF...ELSE:

if (empty($_GET['id']))
	{
	  $id = 1;
	}
  else
	{
	  $id = $_GET['id'];
	}



Thanks again for all your help!!!
Was This Post Helpful? 0
  • +
  • -

#8 ~NJ  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 8
  • View blog
  • Posts: 52
  • Joined: 22-December 08

Re: PHP tabs

Posted 24 December 2008 - 02:33 PM

Since you never defined $id's value from a _GET command, you've almost certainly got register_globals on. This is a MAJOR security issue and I hope you're not considering using your server as a production server.

Register globals allows anyone to set variable values through the URL, much like you just did.

I suggest changing register_globals to off immediately and changing the following line:
  $id=1;



To:
$id = $_GET['id'];



You can use the if else statements that you mentioned above to automatically set it to 1 if no ID is selected.

Then you'd simply use an anchor tag and supply the ID as follows:
<!-- The following anchor tag would tag you to tab "3" -->
<a href="page.php?id=3">Tab 3</a>


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1