html within php works, but php within html doesnt

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

    html within php works, but php within html doesnt

    Below are two different ways of writing the same script. The top one works
    but the bottom one displays nothing in the list. Can anyone see why?

    <?php
    echo "<select name='subcat' style='WIDTH: 95%'><option value=''>Select
    one</option>";
    while($noticia = mysql_fetch_arr ay($quer)) {
    echo "<option value='$noticia[TopicID]'>$noticia[Topic]</option>";
    }
    echo "</select>";
    ?>



    <select name="subcat" style="WIDTH: 95%">
    <option value="">Select one</option>
    <?php while($noticia = mysql_fetch_arr ay($quer)) { ?>
    <option value="<?php $noticia[TopicID]; ?>"><?php $noticia[Topic];
    ?></option>
    <?php } ?>
    </select>

    Ian


  • Andy Hassall

    #2
    Re: html within php works, but php within html doesnt

    On Sat, 02 Sep 2006 13:19:38 GMT, "mantrid" <ian.dandav@vir gin.netwrote:
    ><option value="<?php $noticia[TopicID]; ?>"><?php $noticia[Topic];
    >?></option>
    You haven't done anything in those PHP blocks; you need "print" or "echo".

    --
    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

    • mantrid

      #3
      Re: html within php works, but php within html doesnt

      DOOH!
      Knew it was somethin simple
      Cheers

      "Andy Hassall" <andy@andyh.co. ukwrote in message
      news:gh1jf2dur1 knluu2uf2mqj8r8 shgu778ne@4ax.c om...
      On Sat, 02 Sep 2006 13:19:38 GMT, "mantrid" <ian.dandav@vir gin.netwrote:
      >
      <option value="<?php $noticia[TopicID]; ?>"><?php $noticia[Topic];
      ?></option>
      >
      You haven't done anything in those PHP blocks; you need "print" or
      "echo".
      >
      --
      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

      • Paul

        #4
        Re: html within php works, but php within html doesnt


        mantrid wrote:
        Below are two different ways of writing the same script. The top one works
        but the bottom one displays nothing in the list. Can anyone see why?
        >
        <?php
        echo "<select name='subcat' style='WIDTH: 95%'><option value=''>Select
        one</option>";
        while($noticia = mysql_fetch_arr ay($quer)) {
        echo "<option value='$noticia[TopicID]'>$noticia[Topic]</option>";
        }
        echo "</select>";
        ?>
        >
        >
        >
        <select name="subcat" style="WIDTH: 95%">
        <option value="">Select one</option>
        <?php while($noticia = mysql_fetch_arr ay($quer)) { ?>
        <option value="<?php $noticia[TopicID]; ?>"><?php $noticia[Topic];
        ?></option>
        <?php } ?>
        </select>
        >
        Ian
        Because you're not actually doing anything with that code. HTML isn't a
        templating language, you can't just plug values in surrounded by php
        tags. you have to actually *do* something with the values. Replace the
        4th line with this:

        <option value="<?php echo $noticia[TopicID]; ?>"><?php echo
        $noticia[Topic]; ?></option>

        Cheers,
        Paul

        Comment

        • Phil

          #5
          Re: html within php works, but php within html doesnt

          mantrid wrote, On 3/09/06 1.19 a:
          echo "<option value='$noticia[TopicID]'>$noticia[Topic]</option>";
          The problem is in the way that you reference the $noticia array. In your
          first example, you a double-quoted string, this causes PHP to
          automatically grab the value from the array, but in your second example...
          <option value="<?php $noticia[TopicID]; ?>"><?php $noticia[Topic];
          You don't. You'll need to give it the key of the array as a string, like
          $noticia['TopicID']

          When using double-quoted strings, something like "$array[key]" will grab
          the variable $array['key'], unless you use curly-braces like
          "{$array['key']}".

          See:
          <http://nz.php.net/manual/en/language.types. string.php#lang uage.types.stri ng.parsing.simp le>

          -Phil

          Comment

          Working...