Connecting Tech Pros Worldwide Help | Site Map

HTML within PHP

Member
 
Join Date: Sep 2007
Posts: 40
#1: Sep 24 '07
Hi everyone,

Forgive my newbie PHP skills, but I'm trying to learn.

I've got a recordset on my detail page that can hold up to 5 possible colors for an item. For example, let's say Color1=blue, Color2=red, Color3=white. Color4 and Color5 are null values.

I obviously only want to populate the Available Colors listbox with non-null values. I'm having trouble with my php code, and was hoping someone could tell me why it doesn't work:
Expand|Select|Wrap|Line Numbers
  1. <form id="form1" name="form1" method="post" action="">
  2.   <label>Available Colors
  3.   <select name="Colors" id="Colors">
  4.  
  5.   <?php
  6.   if (strlen($row_rs_assoc_colors['color1'])>0) {
  7.   <option>'$row_rs_assoc_colors['color1']'</option>
  8.   } ?>
  9.  
  10.   </select>
  11.   </label>
  12. </form>
  13.  
Can I execute php within the html I have above? Or is there a better way to do this?

Additionally, I'd like to show the Available Colors drop-down list ONLY if an item has Color1 populated in the assoc_colors table. Meaning that sometimes an item won't come in any colors at all, and therefore I don't need to see the listbox. But I guess that's another question...

Thanks in advance for any help!
-Vozz
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,746
#2: Sep 24 '07

re: HTML within PHP


Hi Vozz. Welcome to The Scripts!

The only real problem with your script is at line 7.

You can't put HTML tags directly into the PHP code. You must echo or print the HTML to the output as a string

Try changing line 7 into something like this:
Expand|Select|Wrap|Line Numbers
  1. # Any of these lines will work.
  2. echo "<option>". $row_rs_assoc_colors['color1'] ."</option>";
  3. echo '<option>'. $row_rs_assoc_colors['color1'] .'</option>';
  4. echo "<option> {$row_rs_assoc_colors['color1']}</option>";
  5. echo "<option>$row_rs_assoc_colors[color1]</option>";
  6.  
Reply