vampke Posted January 3, 2013 Share Posted January 3, 2013 (edited) My original question was supposed to be "how can I get a variable's name without the $", but then I found a topic on stackoverflow about this where most people are saying this is not the way to do it, so I would like to rephrase the question to "what is the best way to handle the below situation". I have a script where users can go and modify the result later. The script contains mainly checkboxes. The code: function outRow($question, $answer){ return "<input type=\"checkbox\" id=\"fieldnamewithoutthestring\" name=\"fieldnamewithoutthestring\" value=\"1\" />".($answer==1?$checked:$unchecked).$question; } $qry = "SELECT * FROM mehtable WHERE id=$id"; $qry_result = mysql_query($qry) or die(mysql_error()); $info = mysql_fetch_array($qry_result); $firstfield = $info['firstfield']; $secondfield = $info['secondfield']; $out = outRow("the result of the first field=", $firstfield); $out .= outRow("second field's result=", $secondfield); echo $out; Above code is simplified. I need the fieldname so I can update the db table accordingly. I have specifically given the variables the same name as the corresponding db fieldnames. What would be the right way to handle this? edit: obviously the $checked and $unchecked are correctly defined but not shown here. Edited January 3, 2013 by vampke Quote Link to comment https://forums.phpfreaks.com/topic/272644-getting-the-name-of-a-variable-without-the/ Share on other sites More sharing options...
Muddy_Funster Posted January 3, 2013 Share Posted January 3, 2013 I'm affraid I don't understand your question, what are you trying to do exactly? Quote Link to comment https://forums.phpfreaks.com/topic/272644-getting-the-name-of-a-variable-without-the/#findComment-1402970 Share on other sites More sharing options...
cpd Posted January 3, 2013 Share Posted January 3, 2013 (edited) Assuming your database is normalised: Use the primary key of your answers table to update the relevant row. Setup your HTML so it is parsed into an array when posted to the http server. Correct the in-line if statement such that its inside the input tag so it can be checked if they previously checked it. return "<input type=\"checkbox\" name=\"question[" . $primaryKey . "]\" " . ($answer == 1 ? "checked=\"checked\"" : "") . " /> " . $question; You can access the field by doing: foreach($_POST['question'] as $primaryKey => $value) { // Code omitted } You need to update all the IDs passed as POST data with 1, then update every other record relating to said user with a 0 - this could all be combined in a single query. One final point: in your DOM there should be no single tag with the same ID as another tag hence why I removed the ID from the input tag. Edited January 3, 2013 by CPD Quote Link to comment https://forums.phpfreaks.com/topic/272644-getting-the-name-of-a-variable-without-the/#findComment-1402971 Share on other sites More sharing options...
Christian F. Posted January 6, 2013 Share Posted January 6, 2013 I am, quite honestly, baffled by what set of circumstances prompted this question. As far as I can see the only time you're working with variables without knowing their names in advance, is when you're using variable variables. Something which you should never do in the first place, and is a sure sign of really messed up logic/code. In any case, even when you're working with variable variables you have the actual name of the variable stored. So there would be no need to use any of the methods mentioned on StackOverflow to find the name. That said, I do think that CPD's answer is what you're looking for. Quote Link to comment https://forums.phpfreaks.com/topic/272644-getting-the-name-of-a-variable-without-the/#findComment-1403769 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.