Jump to content

Problem With Code, Suspecting It's An Issue With Passing Values


pegastep

Recommended Posts

Excuse my unintelligence, I'm fairly new to php, but I'm working on writing a page for a class and I'm having trouble getting it to work. Taking out all the unnecessary text, here's my code:

 

<?php
if(!isset($show))
{
$show="";
}
?>
<html><head><title>Lab</title></head>
<body>
<table>
<tr><td>
<h3>Menu</h3>
<ul>
<li><a href="lab.php?show=reagan">Ronald Reagan</a></li>
<li><a href="lab.php?show=kennedy">John F. Kennedy</a></li>
<li><a href="lab.php?show=roosevelt">Franklin Roosevelt</a></li>
<li><a href="lab.php?show=lincoln">Abraham Lincoln</a></li></ul>
</td><td class="content">
<?php
if ("reagan"== $show)
{
echo "<h3 class=\"topHeading\">Ronald Reagan</h3><br />";
echo "<img src=\"reagan.gif\" align=\"left\" /><p>Text1</p>";
}
else if ("kennedy" == $show)
{
echo "<h3 class=\"topHeading\">John F. Kennedy</h3><br />";
echo "<img src=\"kennedy.gif\" align=\"left\" /><p>Text2</p>";
}
else if ("roosevelt" == $show)
{
echo "<h3 class=\"topHeading\">Franklin Roosevelt</h3><br />";
echo "<img src=\"roosevelt.gif\" align=\"left\" /><p>Text3</p>";
}
else if ("lincoln" == $show)
{
echo "<h3 class=\"topHeading\">Abraham Lincoln</h3><br />";
echo "<img src=\"lincoln.gif\" align=\"left\" /><p>Text4</p>";
}
else
{
echo "<center><h1>Presidential Quotes</h1><br /><h3>Text5</h3>
?>
</td></tr>
</table>
</body>
</html>

The way this is supposed to operate is that the page displays "Text5" until one of the links is clicked. The link should assign a value to $show, and then the content cell will display Text1, Text2, Text3, or Text4, depending on which link was clicked. The only problem is, the content cell still shows "Text5," regardless of which links I click. I suspect this is a problem with passing the values via the link, but it could very well be something else that I'm missing. I've gone through this and I can't seem to find the problem. Does anybody see any particualrily noticable errors? Thank you for taking the time to read this.

Link to comment
Share on other sites

This has nothing to do with your assignment (and you probably shouldn't turn in code that works like this), but when you operate on a set of related data values, you should not write out repetitious code that only differs in the values each section contains. You should use a data structure of some kind (array, database table) -

 

<?php
$data['regan'] = array('title'=>'Ronald Reagan','image'=>'reagan.gif','content'=>'Text1');
$data['kennedy'] = array('title'=>'John F. Kennedy','image'=>'kennedy.gif','content'=>'Text2');
$data['roosevelt'] = array('title'=>'Franklin Roosevelt','image'=>'roosevelt.gif','content'=>'Text3');
$data['lincoln'] = array('title'=>'Abraham Lincoln','image'=>'lincoln.gif','content'=>'Text4');
?>
<html><head><title>Lab</title></head>
<body>
<table>
<tr><td>
<h3>Menu</h3>
<ul>
<?php
foreach($data as $key=>$arr){
   echo "<li><a href='lab.php?show=$key'>{$arr['title']}</a></li>";
}
?>
</ul>
</td><td class="content">
<?php
$show = isset($_GET['show']) ? strtolower(trim($_GET['show'])) : '';
if(isset($data[$show])){
   // show exists
   echo "<h3 class=\"topHeading\">{$data[$show]['title']}</h3><br />";
   echo "<img src=\"{$data[$show]['image']}\" align=\"left\" /><p>{$data[$show]['content']}</p>";
} else {
   // default
   echo "<center><h1>Presidential Quotes</h1><br /><h3>Text5</h3>";
}
?>
</td></tr>
</table>
</body>
</html>

Link to comment
Share on other sites

All I want to say

switch($_GET['show']) {
case 'reagan':
code .....
}

 

I tried it and that worked, thank you. However, I'd still like to be able to get to the bottom of why the if/else isn't working.

 

You need to use $_GET['show'] to access the variable from the URL. Being able to use $show directly is an old "feature" called register_globals which has since been removed as it generally causes more harm than good.

 

Check out the manual page on superglobals for some details about what variables to use and when.

 

So I'd type something like

if ("reagan" == $_GET['show'])

?

Link to comment
Share on other sites

I tried it and that worked, thank you. However, I'd still like to be able to get to the bottom of why the if/else isn't working.

 

 

 

So I'd type something like

if ("reagan" == $_GET['show'])

?

 

Using the code I typed up there, the page will work if I go right to lab.php?show=reagan or any of the other values. However, if I just go to lab.php, I get an "Unidentified Index: show" error on all of the lines where I used the $_GET['show']. Is there any way I can fix this?

Link to comment
Share on other sites

Yes. See the following line from my post above -

 

$show = isset($_GET['show']) ? strtolower(trim($_GET['show'])) : '';

 

This tests if $_GET['show'] is set and does some filtering/conditioning of the value and assigns the result to $show.

Edited by PFMaBiSmAd
Link to comment
Share on other sites

You can do something like this

<html>
<head><title>Lab</title></head>
<body>
<table>
<tr><td>
<h3>Menu</h3>
<ul>
<li><a href="lab.php?show=reagan">Ronald Reagan</a></li>
<li><a href="lab.php?show=kennedy">John F. Kennedy</a></li>
<li><a href="lab.php?show=roosevelt">Franklin Roosevelt</a></li>
<li><a href="lab.php?show=lincoln">Abraham Lincoln</a></li></ul>
</td><td class="content">

<?php
$default = "<center><h1>Presidential Quotes</h1><br /><h3>Text5</h3>";

if(isset($_GET['show']) && !empty($_GET['show'])) {

$president = null;

switch($_GET['show']) {
case 'reagan':
$president = "Ronald Reagan";
$text = "Text1";
break;
case 'kennedy':
$president = "John F. Kennedy";
$text = "Text2";
break;
case 'roosevelt':
$president = "Franklin Roosevelt";
$text = "Text3";
break;
case 'lincoln':
$president = "Abraham Lincoln";
$text = "Text4";
break;
default:
echo $default; // or whatever you want
break;
}

if(isset($president)) {
echo '<h3 class="topHeading">' . $president . '</h3><br />';
echo '<img src="' . $president . '.gif align="left" /><p>' . $text . '</p>';
}
}
else {
echo $default;
}
?>
</td></tr>
</table>
</body>
</html>

 

or like PFMaBISmAD, but with a little difference

<?php
$data['Reagan'] = array('surname' => 'Ronald ', 'content' => 'Text1');
$data['Kennedy'] = array('surname' => 'John F. ', 'content' => 'Text2');
$data['Roosevelt'] = array('surname' => 'Franklin ', 'content' => 'Text3');
$data['Lincoln'] = array('surname' => 'Abraham ', 'content' => 'Text4');

$show = (isset($_GET['show'])) ? trim($_GET['show']) : '';
$htmlTag = '';
?>
<html>
<head>
<title>Lab</title>
</head>
<body>
<table>
<tr>
<td>
<h3>Menu</h3>
<ul>
<?php
foreach($data as $k => $v){
echo "<li><a href='index.php?show=$k'>{$v['surname']}{$k}</a></li>";
if($show == $k) {
$htmlTag = "<h3 class=\"topHeading\">{$v['surname']}{$k}</h3><br />";
$htmlTag.= "<img src=\"{$k}.gif\" align=\"left\" /><p>{$v['content']}</p>";
}
}
?>
</ul>
</td>
<td class="content">
<?php echo (empty($htmlTag)) ? "<center><h1>Presidential Quotes</h1><br /><h3>Text5</h3>" : $htmlTag; ?>
</td>
</tr>
</table>
</body>
</html>

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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