For Programmers: Free Programming Magazines  


Home > Archive > PHP Programming > October 2006 > If Statement Problem









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author If Statement Problem
bokke

2006-10-30, 7:05 pm

Hi,

I have a page that has several stories that run on it from a mysql
database. Right now I use this code:
<img src="Images/NewsPics/<?php echo $row["id"];?>.jpg" border="1"> to
display the image.

BUT I have added a column to the database with 1 or 2 (1=has pic, 2=no
pic). I would like to use an IF STATEMENT - so that:
<?php
if (<?php echo $row["PicNumber"];?> == "2") {
echo "";
} else {
echo "<img src="Images/NewsPics/<?php echo $row["id"];?>.jpg"
border="1">";
}
?>

But I can't seem to embed the <?php echo $row["PicNumber"];?> within
the PHP statement.

What am I doing wrong here? Any help would be much appreciated


Thanks

Andy Hassall

2006-10-30, 7:05 pm

On 30 Oct 2006 10:06:04 -0800, "bokke" <micrest@gmail.com> wrote:

><?php
>if (<?php echo $row["PicNumber"];?> == "2") {
>echo "";
>} else {
>echo "<img src="Images/NewsPics/<?php echo $row["id"];?>.jpg"
>border="1">";
>}
>?>
>
>But I can't seem to embed the <?php echo $row["PicNumber"];?> within
>the PHP statement.
>
>What am I doing wrong here? Any help would be much appreciated


You're already in PHP, so you don't need another <?php tag; you don't want to
be trying to print PHP code into other PHP code, it really just does not work
like that.

Surely you mean:

<?php
if ($row['PicNumber'] == '2')
{
echo '';
}
else
{
echo '<img src="Images/NewsPics/' . $row['id'] . '.jpg" border="1">';
}
?>

--
Andy Hassall :: andy@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
bokke

2006-10-30, 7:05 pm

Hi Andy,

I had tried that before without success - but it gave me another idea -
and it worked ...

<?php
if ($row['picture'] == '1')
{
echo '<img src="Images/NewsPics/' . $row['id'] . '.jpg"
border="1">';
}

else
{
echo '';

}

?>

for some reason the other way round fails ...

thanks a lot for the help

Michael Fesser

2006-10-30, 7:05 pm

..oO(bokke)

>else
>{
> echo '';
>
>}


You can remove this.

Micha
Pedro Graca

2006-10-30, 7:05 pm

["Followup-To:" header set to comp.lang.php.]
bokke wrote:
> I have a page that has several stories that run on it from a mysql
> database. Right now I use this code:
> <img src="Images/NewsPics/<?php echo $row["id"];?>.jpg" border="1">

|-------- HTML --------||------- PHP --------||--- HTML ----|


> to display the image.
>
> BUT I have added a column to the database with 1 or 2 (1=has pic, 2=no
> pic). I would like to use an IF STATEMENT


The only part of your code above that is PHP if the bit with the
filename. You need to make the PHP bigger.

<?php
echo '<img src="Images/NewsPics/'; // previous HTML
echo $row["id"]; // previous PHP
echo '.jpg" border="1">'; // previous HTML
?>

> <?php
> if (<?php echo $row["PicNumber"];?> == "2") {
> echo "";
> } else {
> echo "<img src="Images/NewsPics/<?php echo $row["id"];?>.jpg"
> border="1">";
> }
> ?>


and make your IF STATEMENT encompass all of the image

<?php // This snippet is incomplete. It does not 'work'

if ( /* something here */ ) {

echo '<img src="Images/NewsPics/';
echo $row["id"];
echo '.jpg" border="1">';

}
?>

> But I can't seem to embed the <?php echo $row["PicNumber"];?> within
> the PHP statement.


You can't embed PHP within PHP. All the code above is PHP right now.
There is no need to "reenter" PHP mode.

<?php

if ($row["PicNumber"] == 1) {

echo '<img src="Images/NewsPics/';
echo $row["id"];
echo '.jpg" border="1">';

} else {
// no <img ...> written to the browser
}
?>

> Thanks


You're very welcome. Hope this helps.

--
I (almost) never check the dodgeit address.
If you *really* need to mail me, use the address in the Reply-To
header with a message in *plain* *text* *without* *attachments*.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com