Xtremer360 Posted May 9, 2011 Share Posted May 9, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/235886-adding-1-for-while-loop/ Share on other sites More sharing options...
Zane Posted May 9, 2011 Share Posted May 9, 2011 No, you're just telling it to echo $j + 1; Otherwise you would have added 5 to it. To increment it you need to call $j++; Quote Link to comment https://forums.phpfreaks.com/topic/235886-adding-1-for-while-loop/#findComment-1212581 Share on other sites More sharing options...
Xtremer360 Posted May 9, 2011 Author Share Posted May 9, 2011 <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 ; Quote Link to comment https://forums.phpfreaks.com/topic/235886-adding-1-for-while-loop/#findComment-1212582 Share on other sites More sharing options...
monkeytooth Posted May 9, 2011 Share Posted May 9, 2011 echo don't do math.. it echos.. why not echo $j; all by itself.. then at the end of the loop right before it repeats itself $j++; Quote Link to comment https://forums.phpfreaks.com/topic/235886-adding-1-for-while-loop/#findComment-1212585 Share on other sites More sharing options...
PFMaBiSmAd Posted May 9, 2011 Share Posted May 9, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/235886-adding-1-for-while-loop/#findComment-1212587 Share on other sites More sharing options...
PFMaBiSmAd Posted May 9, 2011 Share Posted May 9, 2011 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/235886-adding-1-for-while-loop/#findComment-1212605 Share on other sites More sharing options...
Xtremer360 Posted May 9, 2011 Author Share Posted May 9, 2011 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'; Quote Link to comment https://forums.phpfreaks.com/topic/235886-adding-1-for-while-loop/#findComment-1212621 Share on other sites More sharing options...
Xtremer360 Posted May 9, 2011 Author Share Posted May 9, 2011 Any ideas on how to achieve this? Quote Link to comment https://forums.phpfreaks.com/topic/235886-adding-1-for-while-loop/#findComment-1212842 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.