Jump to content

Recommended Posts

I'm telling it to add 1 each time through the while loop but its not and not sure why.

 

<fieldset class="answerLeg">
        <legend>Edit Poll Answers</legend>
        <?php
        $j = 0;
        while($row2 = mysqli_fetch_array ( $pollAnswersResult, MYSQL_ASSOC )) { 
        ?>
            <div class="field required answers">
                <label for="answer<?php echo ($j + 1)?>">Answer #<?php echo ($j + 1)?></label><input type="text" class="text" name="answer<?php echo ($j + 1)?>" id="answer<?php echo ($j + 1)?>" title="Answer <?php echo ($j + 1)?>" value="<?php echo $row2['answer']; ?>"/>
                <span class="required-icon tooltip" title="Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. ">Required</span>      
            </div>
        <?php } ?>
    </fieldset>

Link to comment
https://forums.phpfreaks.com/topic/235886-adding-1-for-while-loop/
Share on other sites

<label for="answer<?php echo j++; ?>">Answer #<?php echo j++; ?></label><input type="text" class="text" name="answer<?php echo j++; ?>" id="answer<?php echo $j++; ?>" title="Answer <?php echo j++; ?>" value="<?php echo $row2['answer']; ?>"/>

 

Is giving me a php error saying that its unexpected T_INC expecting '' or ;

The problem causing the error is that there are no $ on the $j variables. I think if you would not write your code using so many opening and closing php tags that it would be easier for you to see problems like this.

 

You will also find that logically your code does not output what you expect. You would only want to increment $j once per loop so that your HTML inside the loop uses one value for the duration of each pass through the loop.

Assuming the code you posted is what you want to do -

        <?php
        $j = 0;
	while($row2 = mysqli_fetch_array ( $pollAnswersResult, MYSQL_ASSOC )) {
		$j++;
		echo "<div class='field required answers'>
                <label for='answer{$j}'>Answer #{$j}</label>
			<input type='text' class='text' name='answer{$j}' id='answer{$j}' title='Answer $j' value='{$row2['answer']}'/>
                <span class='required-icon tooltip' title='Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. '>Required</span>
            </div>";
	} ?>

I tried putting in all relevant code but the only thing I'm trying to figure out is when it gets to the foreach loop to have the ID of the answer from the pollAnswers table that way it doesn't over write the other answers in the table with that pollID. I'm thinking to add a hidden field with the answerID but then if I do that how would I send it with the dataString in the ajax request so that it makes sure it gets sent with the right answer.

 

<fieldset class="answerLeg">
        <legend>Edit Poll Answers</legend>
        <?php
        $j = 1;
        while($row2 = mysqli_fetch_array ( $pollAnswersResult, MYSQL_ASSOC )) { 
        ?>
            <div class="field required answers">
                <label for="answer<?php echo $j ?>">Answer #<?php echo $j ?></label><input type="text" class="text" name="answer<?php echo $j ?>" id="answer<?php echo $j ?>" title="Answer <?php echo $j ?>" value="<?php echo $row2['answer']; ?>"/>
                <span class="required-icon tooltip" title="Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. ">Required</span>      
            </div>
        <?php $j++; } ?>
    </fieldset>

 

<?php

// Include the database page
require ('../../inc/dbconfig.php');

$pollID = $_GET['id'];

$pollsQuery = "
    SELECT 
        polls.question, 
        polls.statusID,
        polls.numAnswers,
        DATE_FORMAT(polls.dateExpires, '%m/%d/%Y') AS dateExpires
    FROM 
        polls
    WHERE 
        polls.ID = '" . $pollID . "'";
$pollsResult = mysqli_query ( $dbc, $pollsQuery ); // Run The Query 
$row = mysqli_fetch_array ( $pollsResult, MYSQL_ASSOC );

$statusQuery = "
    SELECT 
        * 
    FROM 
        statuses";
$statusResult = mysqli_query ( $dbc, $statusQuery ); // Run The Query

$pollAnswers = "
    SELECT
        pollAnswers.ID,
        pollAnswers.answer
    FROM
        pollAnswers
    WHERE
        pollAnswers.pollID = '" . $pollID . "'"; 
$pollAnswersResult = mysqli_query ( $dbc, $pollAnswers ); // Run The Query 

?>

 

$answer = explode(',', $_POST['answersList']);
foreach ($answer as $answer) { 
            
            $answer = htmlspecialchars($answer);

            $query = "UPDATE `pollAnswers` 
                    SET `answer` = '".$answer."' WHERE `ID` = '".$pollID."'";
            mysqli_query($dbc, $query);
        }

 

var answersList = new Array;
            for(var i=0;i<numAnswers; i++) {
                var answerLabel = i + 1;
                var answers = $('input#answer'+answerLabel).val();
                answersList.push(answers);
            }
            
            var dataString = 'pollID=' + pollID + '&question=' + question + '&dateExpires=' + dateExpires + '&statusID=' + statusID + '&numAnswers=' + numAnswers + '&answersList=' + answersList + '&editPoll=True';

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.