If Statement Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bokke

    #1

    If Statement Problem

    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

    #2
    Re: If Statement Problem

    On 30 Oct 2006 10:06:04 -0800, "bokke" <micrest@gmail. comwrote:
    ><?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.u k :: http://www.andyh.co.uk
    http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

    Comment

    • bokke

      #3
      Re: If Statement Problem

      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

      Comment

      • Michael Fesser

        #4
        Re: If Statement Problem

        ..oO(bokke)
        >else
        >{
        echo '';
        >
        >}
        You can remove this.

        Micha

        Comment

        • Pedro Graca

          #5
          Re: If Statement Problem

          ["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*.

          Comment

          Working...