How to post rows in forms?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Terriator
Forum Commoner
Posts: 60
Joined: Mon Jul 04, 2005 12:46 pm

How to post rows in forms?

Post 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..
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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>";
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Terriator
Forum Commoner
Posts: 60
Joined: Mon Jul 04, 2005 12:46 pm

Post 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 >_> .
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply