Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What am I doing wrong? Foreach.... 1

Status
Not open for further replies.

JRBeltman

IS-IT--Management
Joined
Feb 5, 2004
Messages
290
Location
NL
Hi all,
I know the statement I am using can be done much more efficient, but I just can't get it right!

Code:
  $conn = db_connect();
  $query = "select * from options";
  $result = mysql_query($query);
  $character = mysql_fetch_array($result);

lots of bla...

And the statement:
Code:
for($i=0; $character = mysql_fetch_object($result); $i++)
{
  foreach($character as $name => $value)
  {
    echo "<OPTION VALUE='$value'>$value</OPTION>";
  }
}

more bla.....

JR
As a wise man once said: To build the house you need the stone.
Back to the Basics!
 
What are you trying to accomplish?

Why are you using a "for" statement, don't you want to do:
Code:
while ($character = mysql_fetch_assoc($result))
    foreach($character as $name => $value)
        echo "<OPTION VALUE='$value'>$value</OPTION>";

Ken
 
Code:
$conn = db_connect();
$query = "select * from options";
$result = mysql_query($query);

while ($row = mysql_fetch_array($result)){
  $character = $row["CHARACTER"]; //assuming that's the fieldname
  $value = $row["VALUE"]; //assuming that's the field name
  $option_list .= "<option value=\"$value\">$value</option>";
}
echo $option_list;


[cheers]
Cheers!
Laura
 
Thanks!
But I want to be able to print the key and value.
I know this is a modification on my original question,

but can this be done?

ie
Key = Username
Value = JR
Key = Password
Value = secret
Key = Address
Value = come around

Etc.

JR
As a wise man once said: To build the house you need the stone.
Back to the Basics!
 
kenrbnsn (TechnicalUser)
has actuakky solved this.

Thanks


JR
As a wise man once said: To build the house you need the stone.
Back to the Basics!
 
Then something like:

while ($row = mysql_fetch_assoc($result))
{
foreach ($row as $key => $value)
{
.
.
.
}
}




Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top