Could someone tell me what I'm doing wrong?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

marshie
Forum Newbie
Posts: 14
Joined: Thu May 01, 2003 10:04 pm

Could someone tell me what I'm doing wrong?

Post by marshie »

<HTML>
<TABLE ALIGN="left" BORDER="0" CELLPADDING="10" CELLSPACING="0">
<TR>
<TD>Test1</TD>
</TR>
<TR>
<TD>Test2</TD>
</TR>
</TABLE>


<?PHP
$variable1 = 1;
$variable2 = 3;
$variable3 = 1;

if ($variable1 == 1)
{
print "<IMG SRC='http://members.lycos.co.uk/comicsforpuberty/";
print "$variable2";
print ".jpg'>";
}

elseif ($variable1 == 2)
{
$variable2 = $variable2 - $variable3;

if ($variable2 < 1)
{
$variable2 = 1;
}

print "<IMG SRC='http://members.lycos.co.uk/comicsforpuberty/";
print "$variable2";
print ".jpg'>";
}


elseif ($variable == 3)
{
$variable2 = $variable2 + $variable3;

if ($variable2 > 3)
{
$variable2 = 3;
}

print "<IMG SRC='http://members.lycos.co.uk/comicsforpuberty/";
print "$variable2";
print ".jpg'>";
}

elseif ($variable2 == 4)
{
$variable2 = 3;
print "<IMG SRC='http://members.lycos.co.uk/comicsforpuberty/";
print "$variable2";
print ".jpg'>";
}

elseif ($variable2 == 5)
{
$variable2 = 1;
print "<IMG SRC='http://members.lycos.co.uk/comicsforpuberty/1.jpg'>";
}
?>

<A HREF="test.php?$variable1=2">back</A>
<A HREF="test.php?$variable1=3">forward</A>
<A HREF="test.php?$variable1=4">latest</A>
<A HREF="test.php?$variable1=5">first</A>
</HTML>
No matter which link I click on, 3.jpg is always being displayed. This is my first time programming in PHP, so forgive me if the problem is something stupid and obvious. :)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

I think your problem is here:

Code: Select all

<A HREF="test.php?$variable1=2">back</A> 
<A HREF="test.php?$variable1=3">forward</A> 
<A HREF="test.php?$variable1=4">latest</A> 
<A HREF="test.php?$variable1=5">first</A>
try removing all the dollar signs:

Code: Select all

<A HREF="test.php?variable1=2">back</A> 
<A HREF="test.php?variable1=3">forward</A> 
<A HREF="test.php?variable1=4">latest</A> 
<A HREF="test.php?variable1=5">first</A>
Mac
marshie
Forum Newbie
Posts: 14
Joined: Thu May 01, 2003 10:04 pm

Post by marshie »

Nope, didn't work. It still only displays 3.jpg. You can find the page here so you can see what it looks like.

While I'm on it though, do you ever need to put the dollar sign in the URL when you're changing a variable?[/url]
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

marshie wrote:While I'm on it though, do you ever need to put the dollar sign in the URL when you're changing a variable?
Variable names in the URL shouldn't have a dollar sign, so you need to have ?var1=2&var2=3 not ?$var1=1&$var2=3.

Try this code, it was changing the image for me:

Code: Select all

<html> 
<body>
<table align="left" border="0" cellpadding="10" cellspacing="0"> 
<tr> 
	<td>Test1</td> 
</tr> 
<tr> 
	<td>Test2</td> 
</tr> 
</table> 


<?php 

$variable1 = (!empty($_GET['variable1'])) ? $_GET['variable1'] : 1;
$variable2 = 3; 
$variable3 = 1; 

if ($variable1 == 2) { 
	$variable2 = $variable2 - $variable3; 
	if ($variable2 < 1) { 
		$variable2 = 1; 
	} 
} elseif ($variable1 == 3) { 
	$variable2 = $variable2 + $variable3; 
	if ($variable2 > 3) { 
		$variable2 = 3; 
	} 
} elseif ($variable1 == 4) { 
	$variable2 = 3; 
} elseif ($variable1 == 5) { 
	$variable2 = 1; 
}

echo '<img src="http://members.lycos.co.uk/comicsforpuberty/'.$variable2.'.jpg">'; 

?> 

<a href="test.php?variable1=2">back</a> 
<a href="test.php?variable1=3">forward</a> 
<a href="test.php?variable1=4">latest</a> 
<a href="test.php?variable1=5">first</a> 

</body>
</html>
You weren't being consistent with which variable you were testing for - you had:

Code: Select all

if ($variable1 == 1) 
// and
elseif ($variable1 == 2) 
// and
elseif ($variable == 3) 
// and
elseif ($variable2 == 4) 
// and
elseif ($variable2 == 5)
so one of the things I did was make sure that $variable1 was being tested each time. I also put the img tag text at the very end as it's the same throughout so will make life easier if you only have to edit one instance of it.

Mac
marshie
Forum Newbie
Posts: 14
Joined: Thu May 01, 2003 10:04 pm

Post by marshie »

I tried your code, and it worked to an extent. It would display 3.jpg, then when you hit Back, it would display 2.jpg, but then it wouldn't go back any farther. It would keep displaying 2.jpg. First and Latest work fine. But, if I use First to display 1.jpg, then hit forward, it goes to 3.jpg.

Near as I can tell, this is what's happening:

variable1 = 0
variable2 = 3

You hit Back.

variable1 = 2, so reduce variable2 by one.
variable2 = 2

Hit back again, and it starts over. variable2 is now equal to 3 again, just like when the page is first loaded.

variable1 = 2, so reduce variable2 bu one.
variable2 = 2

Then 2.jpg is displayed. I updated my own code, but got the exact same results. Here's what I have now.
<HTML>
<TABLE ALIGN="left" BORDER="0" CELLPADDING="10" CELLSPACING="0">
<TR>
<TD>Test1</TD>
</TR>
<TR>
<TD>Test2</TD>
</TR>
</TABLE>

<?

$comic = 3;

if ($funct == 2)
{
$comic = $comic - 1;

if ($comic < 1)
{
$comic = 1;
}
}

if ($funct == 3)
{
$comic = $comic + 1;

if ($comic > 3)
{
$comic = 3;
}
}

if ($funct == 4)
{
$comic = 3;
}

elseif ($funct == 5)
{
$comic = 1;
}

echo '<img src="http://members.lycos.co.uk/comicsforpub ... omic.'.jpg">';

?>

<A HREF="test.php?funct=2">back</A>
<A HREF="test.php?funct=3">forward</A>
<A HREF="test.php?funct=4">latest</A>
<A HREF="test.php?funct=5">first</A>
</HTML>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

In the code you have, $comic always has a starting value of 3 so the code isn't working as you expect. Basically each time the page is refreshed (i.e. you click one of the links) the value of $variable2 is lost and reset to 3 because of this bit of code:

Code: Select all

<?php $comic = 3; ?>
now if you want to pass the value of $comic, as well as the value of $funct, you can do something like:

Code: Select all

<html> 
<body>
<table align="left" border="0" cellpadding="10" cellspacing="0"> 
<tr> 
	<td>Test1</td> 
</tr> 
<tr> 
	<td>Test2</td> 
</tr> 
</table> 

<?php 

$funct = (!empty($_GET['funct'])) ? $_GET['funct'] : 5;
$comic = (!empty($_GET['comic'])) ? $_GET['comic'] : 3;

if ($funct == 2) { 
	$comic = $comic - 1; 
	if ($comic < 1) { 
		$comic = 1; 
	}
// use elseif not a separate if for this
} elseif ($funct == 3) { 
	$comic = $comic + 1; 
	if ($comic > 3) { 
		$comic = 3; 
	} 
} elseif ($funct == 4) { 
	$comic = 3; 
} elseif ($funct == 5) { 
	$comic = 1; 
} 

echo '<img src="http://members.lycos.co.uk/comicsforpuberty/'.$comic.'.jpg">'; 

?> 

<a href="test.php?funct=2&comic=<?php echo $comic; ?>">back</a> 
<a href="test.php?funct=3&comic=<?php echo $comic; ?>">forward</a> 
<a href="test.php?funct=4&comic=<?php echo $comic; ?>">latest</a> 
<a href="test.php?funct=5&comic=<?php echo $comic; ?>">first</a> 
</body>
</html>
BTW, doing:

Code: Select all

<?php $funct = (!empty($_GET['funct'])) ? $_GET['funct'] : 5; ?>
is a shorter way of writing:

Code: Select all

<?php
if (!empty($_GET['funct'])) {
    $funct = $_GET['funct'];
} else {
    $funct = 5;
}
?>
and what it does is get the value of funct from the query string in the URL if it's there and if not sets it to a default value of 5 (or whatever you like). The reason why $_GET['funct'] is used instead of just $funct is because that method of accessing variables from the query string is deprecated and it's a good idea if just learning PHP to get to grips with $_GET, $_POST et. al:
viewtopic.php?t=511

Mac
marshie
Forum Newbie
Posts: 14
Joined: Thu May 01, 2003 10:04 pm

Post by marshie »

I c/ped your code exactly as you typed it, but it gives me a parse error on line 18. I think I know why too. Tripod.co.uk has PHP version 4.1.0. How could I do what you did in 4.1?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

php4.1 should be doing fine with that code.
Some of the closing php-tags have not been displayed in Twigletmac's code (tricky

Code: Select all

blocks here). Now they do. You might try again
marshie
Forum Newbie
Posts: 14
Joined: Thu May 01, 2003 10:04 pm

Post by marshie »

Still giving the parse error. If it helps, it's on PHP4u 2.2.

Maybe it's because the site's on a co.uk server?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

shouldn't matter
can you post your current code
and the error you get (please mark the line in question) ?
marshie
Forum Newbie
Posts: 14
Joined: Thu May 01, 2003 10:04 pm

Post by marshie »

Parse error: parse error in test.php on line 18
is the error I get.
<html>
<body>
<table align="left" border="0" cellpadding="10" cellspacing="0">
<tr>
   <td>Test1</td>
</tr>
<tr>
   <td>Test2</td>
</tr>
</table>

<?php

$funct = (!empty($_GET['funct'])) ? $_GET['funct'] : 5;
$comic = (!empty($_GET['comic'])) ? $_GET['comic'] : 3;

if ($funct == 2) {
   $comic = $comic - 1;
   if ($comic < 1) {
      $comic = 1;
   }
// use elseif not a separate if for this
} elseif ($funct == 3) {
   $comic = $comic + 1;
   if ($comic > 3) {
      $comic = 3;
   }
} elseif ($funct == 4) {
   $comic = 3;
} elseif ($funct == 5) {
   $comic = 1;
}

echo '<img src="http://members.lycos.co.uk/comicsforpub ... omic.'.jpg">';



<a href="test.php?funct=2&comic=<?php echo $comic; ">back</a>
<a href="test.php?funct=3&comic=<?php echo $comic; ">forward</a>
<a href="test.php?funct=4&comic=<?php echo $comic; ">latest</a>
<a href="test.php?funct=5&comic=<?php echo $comic; ">first</a>
</body>
</html>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

there are still some closing tags missing but this shouldn't cause exactly that error.
Did you try something simpler first?
something like

Code: Select all

<html>
	<body>
		<pre>GET: <?php print_r($_GET); ?></pre>
		<pre>POST: <?php print_r($_POST); ?></pre>
		<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>?testget=val">
			<input type="hidden" name="testpost" value="val" />
			<input type="submit" />
		</form>
		<hr />
		<?php phpinfo(); ?>
	</body>
</html>
marshie
Forum Newbie
Posts: 14
Joined: Thu May 01, 2003 10:04 pm

Post by marshie »

The first thing I got with that was:
GET: Array
(
)

      

POST: Array
(
)
then the PHP info. Then after clicking the Submit button,
GET: Array
(
[testget] => val
)

      

POST: Array
(
[testpost] => val
)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

the part where the error occured as standalone script

Code: Select all

<html><body><?php
$funct = (!empty($_GET['funct'])) ? $_GET['funct'] : 5;
$comic = (!empty($_GET['comic'])) ? $_GET['comic'] : 3;

if ($funct == 2) {
	$comic = $comic - 1;
	if ($comic < 1) {
		$comic = 1;
	}
}
echo $comic, '<br /><a href="', $_SERVER['PHP_SELF'], '?comic=', $comic, '&funct=2">click</a>';
?></body></html>
marshie
Forum Newbie
Posts: 14
Joined: Thu May 01, 2003 10:04 pm

Post by marshie »

<html><body><?php
$funct = (!empty($_GET['funct'])) ? $_GET['funct'] : 5;
$comic = (!empty($_GET['comic'])) ? $_GET['comic'] : 3;

if ($funct == 2) {
   $comic = $comic - 1;
   if ($comic < 1) {
      $comic = 1;
   }
}
echo $comic, '<br /><a href="', $_SERVER['PHP_SELF'], '?comic=', $comic, '&funct=2">click</a>';
?></body></html>
?>
Post Reply