Page 1 of 1

How to post rows in forms?

Posted: Tue Jul 12, 2005 9:04 am
by Terriator
If I have a database containing a couple of races for a rpg and the race_name is set in one field in the table. How do I then make a form-input where each option represents a row in the table?

Thanks in advance..

Posted: Tue Jul 12, 2005 9:54 am
by pickle
So you want each race to be put in a pulldown box? This is the general idea:

Code: Select all

//get the data from the database
$results = returned from query getting each row

echo "<select>";

//loop through each row (race) and put it as an option
//in the select box
for each($row of result set)
{
  echo "<option>".$race stored in current row."</option>";
}
echo "</select>";

Posted: Mon Jul 18, 2005 8:07 am
by Terriator
hmmm...I didn't get it to work that way, but it now works using using this code;

Code: Select all

<SELECT NAME="village">

<?php
$village=mysql_query("SELECT * FROM villages");
while($r=mysql_fetch_array($village)){
 $nameofvillage=$r["name"];
 echo "<option>$nameofvillage</option>";
}
?>

</SELECT>
Is the other way better when you look at performance??...Because then I could use a little help i think (A)

Also, is it bad for the performance when you jump out and in from php?..I do it constantly without noticing >_> .

Posted: Mon Jul 18, 2005 8:12 am
by John Cartwright
Pickles example is what we call pseudo code, which is not actual php but gives you an idea if what is needed. Your example is fine and jumpung in and out of php shouldn't affect performance -- atleast enough to make a different.

Posted: Mon Jul 18, 2005 9:55 am
by pickle
If you're looking for as much performance as possible, there are 2 things:

1) Use single quotes (') rather than double quotes (") when you enter your query, and when you're accessing values in $r. If you use double quotes, the PHP parser has to check through that string for any possible variables that you've got stored in there. If you use single quotes, the parser just treats the string as a string, and doesn't try to evaluate it.

2) Don't put the name of the village in a variable, just to use it once in the next line. Change the next line to:

Code: Select all

echo &quote;&lt;option&gt;$r&#1111;name]&lt;/option&gt;&quote;;
OR

Code: Select all

echo '&lt;option&gt;'.$r&#1111;'name'].'&lt;/option&gt;'

Nothing you've done so far is wrong by any means, and in any script is just fine. If you want to eek out as much performance out of this as possible, though, make the changes.