Jump to content

getting the name of a variable (without the $)


vampke

Recommended Posts

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 by vampke
Link to comment
Share on other sites

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 by CPD
Link to comment
Share on other sites

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.

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.