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();
?>

Your first place you should go for any question related to PHP is the PHP documentation: http://php.net/for

Try the following:

<?php
require_once('config.php');
    
    for ($i=1;$i<11;$i++) {
    $aid='aid'.$i;
    $$aid=$_POST['Question-'.$i];
    
    $qid='qid'.$i;
    $$qid=$_POST['qid'.$i];
	
    $query = 'UPDATE answers SET acount = acount + 1 WHERE aid = "'.mysql_real_escape_string($$qid).'" AND qid = "'.mysql_real_escape_string($$qid).'"';
    mysql_query($query) or die("ERROR: $query. ".mysql_error());    
    }
    mysql_close();
?>

I haven't tested it for bugs and yes those variables with two dollar signs should be correct. Also you can take a look at my loops tutorial as additional reference.

Try the following:

<?php
require_once('config.php');
    
    for ($i=1;$i<11;$i++) {
    $aid='aid'.$i;
    $$aid=$_POST['Question-'.$i];
    
    $qid='qid'.$i;
    $$qid=$_POST['qid'.$i];
	
    $query = 'UPDATE answers SET acount = acount + 1 WHERE aid = "'.mysql_real_escape_string($$aid).'" AND qid = "'.mysql_real_escape_string($$qid).'"';
    mysql_query($query) or die("ERROR: $query. ".mysql_error());    
    }
    mysql_close();
?>

I haven't tested it for bugs and yes those variables with two dollar signs should be correct. Also you can take a look at my loops tutorial as additional reference.

It works perfectly .. thank you cwarn23.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.