Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    Dec 2005
    Posts
    35
    Thanks
    1
    Thanked 1 Time in 1 Post

    Possible Variable Variables problem

    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");

    PHP Code:
    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:

    PHP Code:
    ${"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

  • #2
    Regular Coder Deacon Frost's Avatar
    Join Date
    Feb 2008
    Location
    Between the Lines
    Posts
    279
    Thanks
    31
    Thanked 4 Times in 4 Posts
    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.

  • #3
    New Coder
    Join Date
    Dec 2005
    Posts
    35
    Thanks
    1
    Thanked 1 Time in 1 Post
    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.

  • #4
    Regular Coder Deacon Frost's Avatar
    Join Date
    Feb 2008
    Location
    Between the Lines
    Posts
    279
    Thanks
    31
    Thanked 4 Times in 4 Posts
    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.

  • #5
    New Coder
    Join Date
    Dec 2005
    Posts
    35
    Thanks
    1
    Thanked 1 Time in 1 Post
    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.

  • #6
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,079
    Thanks
    2
    Thanked 320 Times in 312 Posts
    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.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #7
    New Coder
    Join Date
    Dec 2005
    Posts
    35
    Thanks
    1
    Thanked 1 Time in 1 Post
    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):

    PHP Code:
    for ($i=1$i <= 10$i++) {

    $q$i $a$i 



  • #8
    New Coder
    Join Date
    Dec 2005
    Posts
    35
    Thanks
    1
    Thanked 1 Time in 1 Post
    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']

  • #9
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,079
    Thanks
    2
    Thanked 320 Times in 312 Posts
    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 Code:
    <?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...)
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #10
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,079
    Thanks
    2
    Thanked 320 Times in 312 Posts
    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.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #11
    New Coder
    Join Date
    Dec 2005
    Posts
    35
    Thanks
    1
    Thanked 1 Time in 1 Post
    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.

  • #12
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,079
    Thanks
    2
    Thanked 320 Times in 312 Posts
    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.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #13
    New Coder
    Join Date
    Dec 2005
    Posts
    35
    Thanks
    1
    Thanked 1 Time in 1 Post
    I'm now encountering a new problem, of a different nature. Heres my code:

    PHP 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:

    Code:
    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:

    Code:
    '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
    Last edited by InsaneRhino; 03-15-2008 at 05:35 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •