Jump to content

[SOLVED] Building dynamic array key from array


xstevey_bx

Recommended Posts

$query = "SELECT * FROM users WHERE id=$id";
$result = mysql_query($query);
$num = mysql_num_rows($result);
$row = mysql_fetch_array ($result, MYSQL_NUM);

if ($num == 1) 
{
echo '<h3>Edit User</h3><form action="?id=news_comment_edit&num='.$id.'" method="post">';

$arr = array('id', 'username', 'password', 'email', 'forename', 'surname', 'country', 'state', 'postcode', 'dob', 'gender', 'registrationdate', 'lastlogin', 'code', 'activated');

foreach ($arr as $value) 
{
	echo '<p>'.$value.' : <input type="text" name="'.$value.'" size="25" maxlength="255" value="'. $row[{$value}] .'" /></p><br/>'; 
}

unset($value);
}

 

This is a snippet from a code from an edit information page. Im trying to build a form from a list of MySQL column names stored in an array.

 

The problem is in the line

 

echo '<p>'.$value.' : <input type="text" name="'.$value.'" size="25" maxlength="255" value="'. $row[{$value}] .'" /></p><br/>';

 

I want the

$row[{$value}]

to show the information stored in  the $row = mysql_fetch_array depending on what the $value in the foreach loop is

Link to comment
Share on other sites

Your not returning an associative array from mysql_fetch_array(). Either use mysql_fetch_assoc() (recommended) instead or use the MYSQL_ASSOC constant. eg;

 

$row = mysql_fetch_array ($result, MYSQL_ASSOC);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.