Jump to content

Mixing PHP and HTML

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
ezcat

ezcat

    Learning Programmer

  • Members
  • PipPipPip
  • 39 posts
I'm stuck!

In the following code sample, I have 5 variables set to people's names.

I then "Echo" all the HTML forms tags.

For the forms "<option.. =" tag I have a loop to place the correct number of names in the form.

This works fine as long as I just place text in the form.

What I want to do is put the PHP variable $N1 - $N5 in the form.

I also need to increment the "value=" tag with the $i counter.

Any help is welcome,
Jon

<?php
  Echo '<html>';
  Echo '<body>';
  $N1 = "William H. Smith";
  $N2 = "Stanley E. Smith";
  $N3 = "Florence P. Smith";
  $N4 = "Jon F. Smith";
  $N5 = "Jon G. Smith";
  $Name_Count = 5;
   
  Echo '<form action="">';
  Echo '<select name="People" size="5">';
  $i=0;
  while ($i < $Name_Count) 
    {
     Echo '<option value="P1-5">one - five</option>';
     $i++;
     }
  Echo '</select>';
  Echo '<p>';
  Echo '<input type="submit" />';
  Echo '</p>';
  Echo '</form>';
  Echo '</body>';
  Echo '</html>';
?>

Edited by Jordan, 04 November 2008 - 02:10 PM.
Added php tags


#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Please use PHP tags when posting PHP code. What you want to do is use an array instead of variables.

<?php
  Echo '<html>';
  Echo '<body>';
  $arrayName = array("William H. Smith","Stanley E. Smith","Florence P. Smith","Jon F. Smith","Jon G. Smith");
   
  Echo '<form action="">';
  Echo '<select name="People" size="5">';
  $i=0;
foreach($arrayName as $name) {
     echo "<option value=\"P{$i}\">$name</option>";
     $i++;
}
  Echo '</select>';
  Echo '<p>';
  Echo '<input type="submit" />';
  Echo '</p>';
  Echo '</form>';
  Echo '</body>';
  Echo '</html>';
?> 

You can't use single quotes (') when you want to echo variable data ($name). The above example should work.

#3
ezcat

ezcat

    Learning Programmer

  • Members
  • PipPipPip
  • 39 posts
First, thank you so much for your help. Your example works just as I wanted.

And you are right, the names will be in a MySQL query array when I'm done.

I'm new to this forum so when you say "Please use PHP tags when posting PHP code", I'm not sure what that is. Is it the icon second from the right that looks like a sheet of paper with PHP on it? It tried it below to see what would happen.


echo "Did I do this right?';


Thank you sooooo much,
Jon

#4
Guest_Jordan_*

Guest_Jordan_*
  • Guests
You got it. The tags are [noparse]
// Code .....
and for generic code
....
and for HTML
....
[/noparse]. If you have any more questions just let us know.

#5
ezcat

ezcat

    Learning Programmer

  • Members
  • PipPipPip
  • 39 posts
I have the form working with the results of a SQL query in the form's list.

In the below example, the form has a hard coded "select ... size=5". I want to make the size = to the SQL query count.

I have the variable "$Array_Count" that I would like to use for size=.

I thought I could figure this out but not having much luck. No matter what I try, the form's list always looks like the size is set to 1. The following is part of the code where the size is set:


     Echo '<form action="">';

     Echo '<select name="People" size=5>';

     $iii=1;

     while($iii<=$Array_Count)

       {

         echo "<option value=\"P{$iii}\">$ArrayNames[$iii]</option>";

          $iii++;

       }

     Echo '</select>';


Thanks again for your help,
Jon

#6
Guest_Jordan_*

Guest_Jordan_*
  • Guests
You can do two things.

1) Use the mysql_num_rows (if using MySQL) or a similar function for your database.
2) Use count($arrayName) on your SQL results array. Both should return the same number which is a count of your rows.

#7
ezcat

ezcat

    Learning Programmer

  • Members
  • PipPipPip
  • 39 posts
Thanks for your reply, but that wasn't my question. I know the variable name I want to use "$Array_Count", I just can't get it to work on the line for size=.

Thanks,
Jon

#8
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
Echo '<select name="People" size="'.$Array_Count.'">'; 
easiest way.

a variable inside ' surrounded strings does not evaluate, but if the strings are using ", it does evaluate.

in this case, as you want to output " in the string, it's easier to concatenate with . to add in the variable.

also note that html wishes all parameters to be surrounded by ", even if most browsers do handle without any properly.

#9
ezcat

ezcat

    Learning Programmer

  • Members
  • PipPipPip
  • 39 posts
Thanks, it worked. I guess one can never have enough "'. .'". LOL. I was so close.

Jon