By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,077 Members | 1,237 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,077 IT Pros & Developers. It's quick & easy.

Problem with if statement. Please help!

P: n/a
Hi there,

If you look at the code below, you will see that I am using a template
in order to display some photos on my website.
I also have "previous" and "next" buttons/link which increment the
PhotoId in my table so you can easily navigate.

I only have 35 rows in this particular table, but when you get to the
35th picture, clicking on next takes you to 36 which does not exist.
Same when you are on id 1 and click previous.

I am trying to prevent this with my poor excuse of an "if" statement,
but it doesn't seem to do the trick.

Please advise.

Regards,
Ciprian
<html>
<head>
<?php
$db = mysql_connect("localhost","user","pass");
mysql_select_db("mydb",$db);
$request = "SELECT * FROM photos WHERE PhotoId=".$PhotoId;
$result = mysql_query ($request,$db);
$photos =mysql_fetch_object($result);
mysql_free_result($result);
?>
<title>Photos 2004 - <?php echo $photos->PhotoDesc ?> </title>
</head>

<body>
<table width="600" border="0" cellspacing="0" cellpadding="2"
align="center">
<tr>
<td align="center">
<img src="<?php echo $photos->PhotoFileName ?>" alt="<?php echo
$photos->PhotoDesc ?>" title="<?php echo $photos->PhotoDesc ?>"
border="0">
</td>
</tr>
<tr>
<td align='center'>
<?php echo $photos->PhotoDesc ?>
</td>
</tr>
<tr>
<td align='center'>
<a href='index.php?PhotoId=<?php echo $photos->PhotoId-1
?>'>Previous</a> | <a href="index.php?PhotoId=1">Index</a> | <a
href='index.php?PhotoId=<?php echo $photos->PhotoId+1 ?>'>Next</a>
</td>
</tr>
</table>
<?php
if (&PhotoId > 35)
print "1";
?>
</body>
</html>
Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Ciprian Ilie wrote:
Hi there,

If you look at the code below, you will see that I am using a template
in order to display some photos on my website.
I also have "previous" and "next" buttons/link which increment the
PhotoId in my table so you can easily navigate.

I only have 35 rows in this particular table, but when you get to the
35th picture, clicking on next takes you to 36 which does not exist.
Same when you are on id 1 and click previous.

I am trying to prevent this with my poor excuse of an "if" statement,
but it doesn't seem to do the trick.

Please advise.

Regards,
Ciprian
<html>
<head>
<?php
$db = mysql_connect("localhost","user","pass");
mysql_select_db("mydb",$db);
$request = "SELECT * FROM photos WHERE PhotoId=".$PhotoId;
$result = mysql_query ($request,$db);
$photos =mysql_fetch_object($result);
mysql_free_result($result);
?>
<title>Photos 2004 - <?php echo $photos->PhotoDesc ?> </title>
</head>

<body>
<table width="600" border="0" cellspacing="0" cellpadding="2"
align="center">
<tr>
<td align="center">
<img src="<?php echo $photos->PhotoFileName ?>" alt="<?php echo
$photos->PhotoDesc ?>" title="<?php echo $photos->PhotoDesc ?>"
border="0">
</td>
</tr>
<tr>
<td align='center'>
<?php echo $photos->PhotoDesc ?>
</td>
</tr>


You have to put your if() inside the <tr> for navigation

<tr>
<td align='center'>
<?php
// print "Previous" link if PhotoID>1
if ($PhotoID>1) { ?>
<a href='index.php?PhotoId=<?php echo $photos->PhotoId-1; ?>'>Previous</a>
<?php } ?>
| <a href="index.php?PhotoId=1">Index</a> |
<?php
// print "Next" link if PhotoID<35 (really should not be a constant here)
if ($PhotoID<35) { ?>
<a href='index.php?PhotoId=<?php echo $photos->PhotoId+1 ?>'>Next</a>
<?php } ?>
</td>
</tr>
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #2

P: n/a
Hi Pedro,

Thanks a lot for your help, but it still does not look right.
The "previous" link is missing alltogether, regardless of the fact the
PhotoId is bigger than 1, and the "Next" link is always present even
if the PhotoId is 35 or 36 etc

Any advice? I could really do with getting this sorted

Regards,
Ciprian
Jul 17 '05 #3

P: n/a
Ciprian Ilie wrote:
The "previous" link is missing alltogether, regardless of the fact the
PhotoId is bigger than 1, and the "Next" link is always present even
if the PhotoId is 35 or 36 etc


Maybe there's some confusion here with 'standard' variables and
objects ???

is it $PhotoID or $Photo->PhotoID?
I'm not used to using objects. When I see too many '->' I get all
nervous. Maybe I said something that led you astray. If I did, I am
truly sorry.

Please repost your relevant code, and I'm sure you'll be helped to
understand why it doesn't work the way you want.
PS: don't assume we all know what this is about.
Keep some of the previous post (what is related to the new one)
to let us in on what you're talking about [I had to repull the
old posts to understand what you were talking about] :)
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #4

P: n/a
Try this modified code. Hope it will work (Not Tested)

As you are using PhotoId which i think will be a autoincrement field
so you can not rely on its continuity, as some rows might get deleted
with time.

So, you are required to fetch the previous and next ids from database.

There are better and more efficient solutions, which you can try, but
this is the easiest i guess.

[SNIP]

<html>
<head>
<?php
$db = mysql_connect("localhost","user","pass");
mysql_select_db("mydb",$db);
$request = "SELECT PhotoId FROM photos WHERE PhotoId < '$PhotoId'
ORDER BY PhotoId DESC";
if($row =mysql_fetch_object(mysql_query ($request,$db)))
$prevId = $cntrow->PhotoId;
else
$prevId = '';

$request = "SELECT PhotoId FROM photos WHERE PhotoId > '$PhotoId'
ORDER BY PhotoId";
if($row =mysql_fetch_object(mysql_query ($request,$db)))
$nextId = $cntrow->PhotoId;
else
$nextId = '';

$request = "SELECT * FROM photos WHERE PhotoId=".$PhotoId;
$result = mysql_query ($request,$db);
$photos =mysql_fetch_object($result);
mysql_free_result($result);

?>
<title>Photos 2004 - <?php echo $photos->PhotoDesc ?> </title>
</head>

<body>
<table width="600" border="0" cellspacing="0" cellpadding="2"
align="center">
<tr>
<td align="center">
<img src="<?php echo $photos->PhotoFileName ?>" alt="<?php echo
$photos->PhotoDesc ?>" title="<?php echo $photos->PhotoDesc ?>"
border="0">
</td>
</tr>
<tr>
<td align='center'>
<?php echo $photos->PhotoDesc ?>
</td>
</tr>
<tr>
<td align='center'>
<?php
if(!empty($prevId))
{
?>
<a href='index.php?PhotoId=<?php echo $prevId?>'>Previous</a> |
<?
}
?>
<a href="index.php?PhotoId=<?php echo $photos->PhotoId?>">Index</a>
<?php
if(!empty($nextId))
{
?>
| <a href='index.php?PhotoId=<?php echo $nextId?>'>Next</a>
<?
}
?>

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

[/SNIP]
Pedro Graca <he****@hotpop.com> wrote in message news:<c0*************@ID-203069.news.uni-berlin.de>...
Ciprian Ilie wrote:
The "previous" link is missing alltogether, regardless of the fact the
PhotoId is bigger than 1, and the "Next" link is always present even
if the PhotoId is 35 or 36 etc


Maybe there's some confusion here with 'standard' variables and
objects ???

is it $PhotoID or $Photo->PhotoID?
I'm not used to using objects. When I see too many '->' I get all
nervous. Maybe I said something that led you astray. If I did, I am
truly sorry.

Please repost your relevant code, and I'm sure you'll be helped to
understand why it doesn't work the way you want.
PS: don't assume we all know what this is about.
Keep some of the previous post (what is related to the new one)
to let us in on what you're talking about [I had to repull the
old posts to understand what you were talking about] :)

Jul 17 '05 #5

P: n/a
Thanks again for all your help, but this does not work.
Would have liked to get some useful advise of where am I going wrong
rather than code which is not working and I haven't got a clue how to
debug

Cheers
Jul 17 '05 #6

P: n/a
Ciprian Ilie wrote:
Thanks again for all your help, but this does not work.
Would have liked to get some useful advise of where am I going wrong
rather than code which is not working and I haven't got a clue how to
debug


As I said before, you need to test for 1 and 35 individually right
before printing (or not) the 'Previous' and 'Next' link.

So, if you're at PhotoID 19 you want to display

<link to Photo 18> | <index> | <link to Photo 20>

but if you're viewing Photo 1 you want that to be

<index> | <link to Photo 2>

and similarly for Photo 35

<link to Photo 34> | <index>

It's easy to identify the parts that need special treatment, namely the
"<link to previous Photo> | " and the " | <link to next Photo>"
So your pseudo code could be something like

if (ok to print link to previous photo) print "<previous link> | "
print "<index>"
if (ok to print link to next photo) print " | <next link>"

Now you just have to come up with a working way of saying that in PHP :)
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.