Jump to content

Recommended Posts

can anyone teach me how to do loop for the script below.. thanks in advance..

 

<?php
require_once('config.php');

$aid1=$_POST['Question-1'];
$aid2=$_POST['Question-2'];
$aid3=$_POST['Question-3'];
$aid4=$_POST['Question-4'];
$aid5=$_POST['Question-5'];
$aid6=$_POST['Question-6'];
$aid7=$_POST['Question-7'];
$aid8=$_POST['Question-8'];
$aid9=$_POST['Question-9'];
$aid10=$_POST['Question-10'];

$qid1=$_POST['qid1'];
$qid2=$_POST['qid2'];
$qid3=$_POST['qid3'];
$qid4=$_POST['qid4'];
$qid5=$_POST['qid5'];
$qid6=$_POST['qid6'];
$qid7=$_POST['qid7'];
$qid8=$_POST['qid8'];
$qid9=$_POST['qid9'];
$qid10=$_POST['qid10'];

   
    // update vote counter
$query = "UPDATE answers SET acount = acount + 1 WHERE aid = '$aid1' AND qid = '$qid1'";
    $result = mysql_query($query) or die("ERROR: $query. ".mysql_error());

$query = "UPDATE answers SET acount = acount + 1 WHERE aid = '$aid2' AND qid = '$qid2'";
    $result = mysql_query($query) or die("ERROR: $query. ".mysql_error());

$query = "UPDATE answers SET acount = acount + 1 WHERE aid = '$aid3' AND qid = '$qid3'";
    $result = mysql_query($query) or die("ERROR: $query. ".mysql_error());

$query = "UPDATE answers SET acount = acount + 1 WHERE aid = '$aid4' AND qid = '$qid4'";
    $result = mysql_query($query) or die("ERROR: $query. ".mysql_error());

$query = "UPDATE answers SET acount = acount + 1 WHERE aid = '$aid5' AND qid = '$qid5'";
    $result = mysql_query($query) or die("ERROR: $query. ".mysql_error());

$query = "UPDATE answers SET acount = acount + 1 WHERE aid = '$aid6' AND qid = '$qid6'";
    $result = mysql_query($query) or die("ERROR: $query. ".mysql_error());

$query = "UPDATE answers SET acount = acount + 1 WHERE aid = '$aid7' AND qid = '$qid7'";
    $result = mysql_query($query) or die("ERROR: $query. ".mysql_error());

$query = "UPDATE answers SET acount = acount + 1 WHERE aid = '$aid8' AND qid = '$qid8'";
    $result = mysql_query($query) or die("ERROR: $query. ".mysql_error());

$query = "UPDATE answers SET acount = acount + 1 WHERE aid = '$aid9' AND qid = '$qid9'";
    $result = mysql_query($query) or die("ERROR: $query. ".mysql_error());

$query = "UPDATE answers SET acount = acount + 1 WHERE aid = '$aid10' AND qid = '$qid10'";
    $result = mysql_query($query) or die("ERROR: $query. ".mysql_error());

    // close connection
    mysql_close();
?>

Link to comment
https://forums.phpfreaks.com/topic/181409-need-help/
Share on other sites

I would, instead of having inviduvidual post variables like you do, make an HTML array, so your form should resemble

<!-- questions -->
<input type="text".... name="Question[]" />
<input type="text".... name="Question[]" />
etc

<!-- question ids (assuming they are hidden fields? -->
<input type="hidden" name="qid[]" />
<input type="hidden" name="qid[]" />
etc

and then you could just loop through each question and corresponding id like so

foreach($_POST['Question'] as $key=>$value){
$aid = $value;
$qid = $_POST['qid'][$key];

$query = "UPDATE ...... WHERE aid= '$aid' AND qid='$qid'";
//do query
}

Link to comment
https://forums.phpfreaks.com/topic/181409-need-help/#findComment-956960
Share on other sites

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.