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.<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>
Could someone tell me what I'm doing wrong?
Moderator: General Moderators
Could someone tell me what I'm doing wrong?
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
I think your problem is here:
try removing all the dollar signs:
Mac
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>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>- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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.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?
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>Code: Select all
if ($variable1 == 1)
// and
elseif ($variable1 == 2)
// and
elseif ($variable == 3)
// and
elseif ($variable2 == 4)
// and
elseif ($variable2 == 5)Mac
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.
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>
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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:
now if you want to pass the value of $comic, as well as the value of $funct, you can do something like:
BTW, doing:
is a shorter way of writing:
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
Code: Select all
<?php $comic = 3; ?>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>Code: Select all
<?php $funct = (!empty($_GET['funct'])) ? $_GET['funct'] : 5; ?>Code: Select all
<?php
if (!empty($_GET['funct'])) {
$funct = $_GET['funct'];
} else {
$funct = 5;
}
?>viewtopic.php?t=511
Mac
php4.1 should be doing fine with that code.
Some of the closing php-tags have not been displayed in Twigletmac's code (tricky
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 againis the error I get.Parse error: parse error in test.php on line 18
<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>
there are still some closing tags missing but this shouldn't cause exactly that error.
Did you try something simpler first?
something like
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>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>?><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>