Page 1 of 1

Variable Nomenclature In a Loop

Posted: Wed Jun 03, 2009 10:46 am
by lmg
I am running a page which will have a number of questions, and I would like each question's answer and comment to be uniquely defined and written to a database on the next page. Here is the code I have for the 'collecting data' page:

php

Code: Select all

    
<?php 
    $title="Self Audit";
    include("header.php");
    
    //connect to the database to get all of the questions for this section. 
    $username="user";
    $password="pass";
    $database="db";
 
    //connect and select the database
    @mysql_connect(localhost,$username,$password) or die("Cannot connect to database");
    @mysql_select_db($database) or die( "Unable to select database");
 
    $query="SELECT questions FROM question WHERE question_id BETWEEN 0 AND 3";
    $result=mysql_query($query);
//}
?>
 
</html>
<body>
<form method="post" action="readWriteDB2.php">
 
<a href="session.php"><font face="Arial">Return to Main Menu</font></a><br><br>
<br>
<font face="Arial"><b>Section 1</b></font><br><br><br>
<table width=100% border=0 cellspacing=0 cellpadding=5 width=616>
<?php
//put all other info on page
for($i=0; $i < mysql_numrows($result); $i++)
    echo "<tr>
    <td width=60%><font face='Arial'>".mysql_result($result, $i)."<br><br>
        <INPUT TYPE='radio' NAME='group.$i.' VALUE='1'><a href='#Definitions' title='Click for more information.'>C1</a>
        &nbsp; &nbsp;
        <INPUT TYPE='radio' NAME='group.$i.' VALUE='2'><a href='#Definitions' title='Click for more information.'>C2</a>
        &nbsp; &nbsp;
        <INPUT TYPE='radio' NAME='group.$i.' VALUE='3'><a href='#Definitions' title='Click for more information.'>C3</a>
        &nbsp; &nbsp;
        <INPUT TYPE='radio' NAME='group.$i.' VALUE='4'><a href='#Definitions' title='Click for more information.'>C4</a>
        &nbsp; &nbsp;
        <INPUT TYPE='radio' NAME='group.$i.' VALUE='5'><a href='#Definitions' title='Click for more information.'>C5</a><br><br>
        <font face='Arial'>Comments:<br>
        <TEXTAREA COLS='75' name='comment.$i.'></TEXTAREA>
    <td width=40%><table border=1 cellspacing=0 cellpadding=0 width=100% bgcolor=#FF6600>
        <td><font face='Arial'>More information here.</td></tr></table></td>
    </tr>";
?>
 
</table>
 
<!--End of survey-->
 
<br>
<input type="submit" name="continue" value="Continue">
<br>
<br>
<br>
 
<!--Definitions Table-->
 
<a name=Definitions><font face="Arial"><b>Definitions</b></a>
 
<table border=1 cellspacing=0 cellpadding=10 width=100%>
<tr>
<td align=center><font face="Arial"><b>C1</b></td>
<td><font face="Arial">Choice 1</td>
<td><font face="Arial">definition here</td>
</tr>
 
<tr>
<td align=center><font face="Arial"><b>C2</b></td>
<td><font face="Arial">Choice 2</td>
<td><font face="Arial">definition here</td>
</tr>
 
<tr>
<td align=center><font face="Arial"><b>C3</b></td>
<td><font face="Arial">choice 3</td>
<td><font face="Arial">definition here</td>
</tr>
 
<tr>
<td align=center><font face="Arial"><b>C4</b></td>
<td><font face="Arial">choice 4</td>
<td><font face="Arial">definition here</td>
</tr>
 
<tr>
<td align=center><font face="Arial"><b>C5</b></td>
<td><font face="Arial">choice 4</td>
<td><font face="Arial">definition here</td>
</tr>
</table>
 
<!--End Definitions Table-->
 
<br>
 
</form>
</div>
</body>
</html>
 
<?php
    include("footer.html");
?>
When I get to the next page, echo $_POST will not work for any of the variables. As such, I am pretty sure it is something to do with how I am naming things on the previous page.

Here is the second page code:

php

Code: Select all

<?php
    //set the username, password, and database name
    $username="user";
    $password="pass";
    $database="db";
 
    //connect and select the database
    @mysql_connect(localhost,$username,$password) or die("Cannot connect to database");
    @mysql_select_db($database) or die( "Unable to select database");
 
    //query the database for all audits for the user. If none are found, audit number is 1. Otherwise, audit
        //number is (largest audit number) + 1.
    $query = "INSERT INTO answer VALUES (1, '$_POST[group1]', '$_POST[group2]', '$_POST[group3]', '$_POST[comment1]', 
        '$_POST[comment2]' , '$_POST[comment3]')";
    //query the database
    mysql_query($query);
 
    mysql_close();
 
    //header('Location: get_aPie.php');
//}
 
function error($qNum, $header){
    if($header==true){
        $title="Error!";
        include("header.php");
        echo "<FONT FACE='arial'>You left question number ".$qNum." blank. Please go back and fix your error.<br>";
        $header=false;
    }
    else
        echo "<FONT FACE='arial'>You left question number ".$qNum." blank. Please go back and fix your error.<br>";
    return $header;
}
 
?>
 
<html>
<a href="get_aPie.php">Get a pie!</a><br>
Answers:<br>a1:<?php echo $_POST["group1"]; ?><br>
a2: <?php echo $_POST['group2']; ?><br>
a3: <?php echo $_POST['group3']; ?><br>
c1: <?php echo $_POST['comment1']; ?><br>
c2: <?php echo $_POST['comment2']; ?><br>
c3: <?php echo $_POST['comment3']; ?><br>
</html>

Re: Variable Nomenclature In a Loop

Posted: Wed Jun 03, 2009 11:29 am
by Reviresco
You're missing the brackets to start and end the "for" loop:

Code: Select all

for($i=0; $i < mysql_numrows($result); $i++) {
echo "stuff here with $i variable";
}
 
Also, get rid of the dots here:

Code: Select all

NAME='group.$i.'
Change to:

Code: Select all

NAME='group$i'
And also, the "for" loop needs to start at one, not zero, if your first group is group1.

Re: Variable Nomenclature In a Loop

Posted: Wed Jun 03, 2009 11:43 am
by lmg
Is PHP really that picky? My for loop is only one line long.

I made some minor changes (changed variables as suggested) and it appears to be working now. Thanks so much!

Re: Variable Nomenclature In a Loop

Posted: Wed Jun 03, 2009 12:03 pm
by Reviresco
Yes, with one line you can get away no brackets (I didn't notice -- looked like multiple lines).

Re: Variable Nomenclature In a Loop

Posted: Wed Jun 03, 2009 12:46 pm
by lmg
It was a rather large chunk of code - I went for readability :)