...

View Full Version : Possible Variable Variables problem



InsaneRhino
03-15-2008, 12:58 PM
Hey there.

I have a form on, lets call it "page1" that uses the following code to get questions ffrom a database. Heres the code:

<?php
$sql=mysql_query("SELECT * FROM tbl_ethicalquestions");


while($row = mysql_fetch_assoc($sql)) {
?>

<tr>
<td width=65%>
<font class="text">
Q<?php echo($row['qid']) ?>. <?php echo($row['text']) ?>
</font>
</td>
<td width="15%">
<input type="radio" name="q<?php echo($row['qid']) ?>" value="yes"> <font class="text">Yes</font><BR>
<input type="radio" name="q<?php echo($row['qid']) ?>" value="no"> <font class="text">No</font><BR>
<input type="radio" name="q<?php echo($row['qid']) ?>" value="na" checked="checked"> <font class="text">Not Known</font>
</td>
<td width="20%">
<input size="30" name="q<?php echo($row['qid']) ?>comment" value="<Enter a comment if applicable>">
</td>
</tr>

<?php
}

That generates the questions and gives them a form input name that would look like "q1", "q2", "q1comment", "q2comment" etc. Now Im having trouble with the submitting of the form. On a different page ("page2") I am trying to gather all the variables created on the previous page.

My logic tells me to create a FOR loop using mysql_num_rows() as a count. But I cant seem to then create the variables for the questions. I get code close to:


${"q_$i"} = $_POST['

But I get stuck at that point. I have never used variable variables before up until about 10 minutes ago.

Any help would be a god send. Im not sure if a for loop is the right approach but to me it seems like the most logical way of doing it, but im not 100% sure if its the best/easier way.

Cheers
Ryan

Deacon Frost
03-15-2008, 01:25 PM
A variable cannot start with a (.

:
Variable Naming Rules
A variable name must start with a letter or an underscore "_"
A variable name can only contain alpha-numeric characters and underscores (a-Z, 0-9, and _ )
A variable name should not contain spaces. If a variable name is more than one word, it should be separated with underscore ($my_string), or with capitalization ($myString)

That's probably where you're running into most of your problems.

so when you pull it'd be:

$q_qid = $_POST["$row['qid']"];


My advice would in the page1, re-variable $row['qid'] == $qid. That way when you pull it'll just be: $q_qid = $_POST['$qid'];

Hope that helps get you on the right track.

InsaneRhino
03-15-2008, 01:34 PM
The ${"q_$i"} is a variable variable, aka a variable with its name as another variable. So like in my situation you need a variable to have its name defined with another variable you can do. I believe ${"q_$i"} would come out as $q# depending on what $i is. so it SHOULD look like $q1, then $q2 etc. Thats how i would like it to work, and how I have read it from php.net

After re thinking, im thinkmaybe a while loop would be better. Going to give that a shot.

Deacon Frost
03-15-2008, 01:47 PM
Oh, haha, I hate being taught wrong XD! Sorry.

Well...

${"q_$i"}

From what I read...

Even through all the comments, you need a $ before that as well (shooting in the dark), which would make it $${'q_$i"}????? But then that'd be one, and two variables, and three?, well, that's whatchoo wanted :P.

InsaneRhino
03-15-2008, 02:05 PM
Yes you are right the $$ is needed. I havent yet even tried the code as Im sure that it wouldnt work correctly. I will try a while later on tonight.

CFMaBiSmAd
03-15-2008, 03:53 PM
Don't use variable variables for sequentially named variables. Variable variables are 3 times slower than using an array, so, variable variables will make your code and page loading times slower than if you just use an array.

Also, it takes more complicated code to create and use variable variables than just using an array.

InsaneRhino
03-15-2008, 04:16 PM
Im really not sure how to do this. So if I rephrase the issue im having maybe somebody can help out.

Basically I need code that generates a set number of variables with sequencial names. So it need to generate $q1, $q2, $q3 etc and then give them an assigned value based on another set of sequencial variables (that also needs to be generated), ie $a1, $a2, $a3 etc so that.

$q1 = $a1
$q2 = $a2
$q3 = $a3
etc

For example (I think this code is wrong):



for ($i=1; $i <= 10; $i++) {

$q$i = $a$i

}

InsaneRhino
03-15-2008, 04:18 PM
In that case CFMaBiSmAd how would I automatically insert data into the next array of a variable? And also how would I call the variable $_POST with a sequential array? like $_POST['$i']

CFMaBiSmAd
03-15-2008, 04:44 PM
Each question on your form has a set of radio buttons and a text field (if it had a type="text" parameter) for a comment.

The radio and text fields need to be a separate array (you are currently using the same name "q", so any comment value will overwrite any radio button value) using the $row['qid'] as the array index.

To start with, lets give the radio buttons a name like "radio" and the comment field a name like "comment", so your form code becomes -


<?php
while($row = mysql_fetch_assoc($sql)) {
?>

<tr>
<td width=65%>
<font class="text">
Q<?php echo($row['qid']) ?>. <?php echo($row['text']) ?>
</font>
</td>
<td width="15%">
<input type="radio" name="radio[<?php echo($row['qid']) ?>]" value="yes"> <font class="text">Yes</font><BR>
<input type="radio" name="radio[<?php echo($row['qid']) ?>]" value="no"> <font class="text">No</font><BR>
<input type="radio" name="radio[<?php echo($row['qid']) ?>]" value="na" checked="checked"> <font class="text">Not Known</font>
</td>
<td width="20%">
<input type="text" size="30" name="comment[<?php echo($row['qid']) ?>]" value="<Enter a comment if applicable>">
</td>
</tr>

<?php
}

This will result in two arrays - $_POST['radio'][] and $_POST['comment'][] where the index [] will be each successive $row['qid']

You can then just loop through all the values and access each radio value and comment. There is no need to copy the values to separately named variables, just use the $_POST['radio'][] and $_POST['comment'][] variables directly (once you have verified what they contain and make them safe from harmful content to avoid sql injection...)

CFMaBiSmAd
03-15-2008, 04:52 PM
If you need code to compare the given answers with the actual answers, you would need to show what your data table looks like, but a query to retrieve the answers with the $row['qid'] and then a simple while() loop fetch each answer and compare it with the value in each $_POST['radio'][$row['qid']] would be about it.

InsaneRhino
03-15-2008, 04:53 PM
So using $_POST['radio'][] and $_POST['comment'][].

To call the array would it be $_POST['radio'][$i]?The form is a questionnaire so its not comparing any data just storing it. I can do all the major parts its just this sort of array-upon-array or variable-upon-variable thing that i need to sort out.

CFMaBiSmAd
03-15-2008, 04:57 PM
I added a post above that indicates how you would loop and compare the given answers with the correct ones from the database.

If you know the range of qid values, then you could use a for loop and access each value the way you just posted.

InsaneRhino
03-15-2008, 06:23 PM
I'm now encountering a new problem, of a different nature. Heres my code:


$count=mysql_num_rows($sql);

for ($i=1; $i <= $count; $i++) {

$q[$i] = $_POST['q'.$i];
$c[$i] = $_POST['comment'.$i];

$sql = "UPDATE tbl_ethicalanswers SET answer = " . $q[$i] . ", comment = " . $c[$i] . " WHERE qid = " . $i . " && userid = " . $id;

mysql_query ($sql) or die ('Could not update user. Please try again or contact an administrator quoting error: <BR><BR>' . mysql_error() '<BR><BR>Iteration' . $i);

}

Its giving me the error:


Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/totalre1/public_html/project/ethical_question.php on line 48



EDIT

Doh! The problem was:


'Could not update user. Please try again or contact an administrator quoting error: <BR><BR>' . mysql_error() '<BR><BR>Iteration' .

It didnt have a dot (.) to concenate the "mysql_error()". Silly me :P



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum