need HELP

Discussion in 'PHP' started by wayz1229, Nov 13, 2009.

  1. #1
    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();
    ?>
    
    Code (markup):
     
    wayz1229, Nov 13, 2009 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    require_once('config.php');
    for($i=1; isset($_POST['Question-'.$i]); $i++)
    {
        $a = $_POST['Question-'.$i];
        $q = $_POST['qid'.$i];
        $query = "UPDATE `answers` SET `account` = `account` + 1 WHERE `aid` = '$a' AND `qid` = '$q'";
        $result = mysql_query($query) or die("ERROR: $query. ".mysql_error());
    }
    mysql_close();
    PHP:
     
    JAY6390, Nov 13, 2009 IP
  3. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #3
    Yes, I can teach you.

    if I had this code:

    
    $notes="<br>This is line 1 of my notes.";
    echo $notes;
    $notes="<br>This is line 2 of my notes.";
    echo $notes;
    $notes="<br>This is line 3 of my notes.";
    echo $notes;
    $notes="<br>This is line 4 of my notes.";
    echo $notes;
    $notes="<br>This is line 5 of my notes.";
    echo $notes;
    
    PHP:

    I could put it into a loop like this:

    
    
    for ($counter=1; $counter<=5; ++$counter)
    {$notes="<br>This is line ".$counter." of my notes.";
    echo $notes;
    }
    
    
    PHP:

    If you have any specific questions please ask.
     
    plog, Nov 13, 2009 IP