firehawk777 Posted February 8, 2011 Share Posted February 8, 2011 Here's a question for all you great php programmers. What I want to do is use a variable username as the name of a select input of a form. I.E echo"<select name='$user'>"; Of course this code doesn't work though I feel sure that there must be a way to do this. Anyone know it? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted February 8, 2011 Share Posted February 8, 2011 You can use a variable for that, but why do you want to do it that way? Quote Link to comment Share on other sites More sharing options...
firehawk777 Posted February 8, 2011 Author Share Posted February 8, 2011 So as to why its hard to explain but here goes. I am basically embedding one form inside another. First I call an array of users from the database and make a list that displays on the page. I have a check box for each user for deleting the user in the list that is posted to an array. Now I want to also have the option to be able to change the users level from the same list so I want to use the variable $user (username) as the name of each select input then check if that username is posted and update the level of the user from that. If you still don't understand heres the complete code minus some of the css function ViewUsers($theaction) { $query = "SELECT * FROM users"; $result=mysql_query($query) or die("Invalid Query : ".mysql_error()); $row=mysql_fetch_array($result); $num=mysql_numrows($result); echo"<div class='post'>"; if ($num > 0){ echo "<h2>Users: $num</h2>"; }else{ echo "<h2>Users: No Users</h2>"; } if(!mysql_num_rows($result)) die("No users found."); $i=0; echo"<ul>"; echo"<form name='levelform' method='post' action='$theaction'>"; while ($i < $num) { $id=mysql_result($result,$i,"id"); $user=mysql_result($result,$i,"user"); $email=mysql_result($result,$i,"email"); $rquery = "SELECT * FROM profiles WHERE pid='$id'"; $rresult=mysql_query($rquery) or die("Invalid Query : ".mysql_error()); $level=mysql_result($rresult,0,"level"); echo"<li>"; echo"<table border='0' width ='500'>"; echo"<tr>"; echo"<td width='100'>"; echo"<div id='meta' STYLE='color: #FF0000;'> User</div> </td><td STYLE='color: #FF0000;'>$user"; echo"</td></tr>"; echo"<tr><td>"; echo"<div id='meta'>Email</div> </td><td>$email"; echo"</td>"; echo"</tr><tr><td>"; echo "<input type='checkbox' value='$id' name='userdelete[]' />"; echo "Delete:</td><td>$user"; echo"</td></tr>"; echo"<tr><td>"; echo"<select name='"; echo"$user"; echo"'>"; if ($level == 1){ echo"<option value='1' selected='selected'>1</option>"; }else{ echo"<option value='1'>1</option>"; } if ($level == 2){ echo"<option value='2' selected='selected'>2</option>"; }else{ echo"<option value='2'>2</option>"; } if ($level == 3){ echo"<option value='3' selected='selected'>3</option>"; }else{ echo"<option value='3'>3</option>"; } if ($level == 4){ echo"<option value='4' selected='selected'>4</option>"; }else{ echo"<option value='4'>4</option>"; } echo"</select>"; echo"</table>"; echo"</li>"; $i++; } echo"</ul>"; echo"<input type='hidden' name='level'value='level' />"; echo"<input type='submit' size='50' name='deleteuser' value='delete users' />"; echo "</form>"; echo"</div>"; } So if you know how I make the $user variable the select name I would be very thankful! Quote Link to comment Share on other sites More sharing options...
Bradley99 Posted February 8, 2011 Share Posted February 8, 2011 So you'd be using it for like an AdminCP kinda thing? You wan't the users listed? But rather than in a table or something, in a select? Quote Link to comment Share on other sites More sharing options...
firehawk777 Posted February 8, 2011 Author Share Posted February 8, 2011 Actually no thats not it. I don't want to be able to select the users or I would be using the 'value' rather than the 'name'. If you look at the code you'll see what I mean though the technique would be the same. Quote Link to comment Share on other sites More sharing options...
Bradley99 Posted February 8, 2011 Share Posted February 8, 2011 So... $user=mysql_query("SELECT * FROM users WHERE level='standard/mod/admin etc' ORDER BY id DESC LIMIT 100"); Then your... echo"<select name='$user'>"; Quote Link to comment Share on other sites More sharing options...
firehawk777 Posted February 8, 2011 Author Share Posted February 8, 2011 Sorry I didn't get that Bradley99 Quote Link to comment Share on other sites More sharing options...
Bradley99 Posted February 8, 2011 Share Posted February 8, 2011 Sorry, I completely didn't see the post with all of your code in, give me a minute to take a look. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.