how to use php using for array $i getting it to repeat 10 times

Discussion in 'PHP' started by xbat, Jan 17, 2014.

  1. #1
    I have the insert part that inserts a single line. However I would be interested in getting it to repeat into the database 10 rows in a row.

    Example
    what I am getting

    a b c
    d f e
    e f e

    what I want to do ...

    a b c
    a b c
    a b c
    a b c
    a b c
    a b c
    a b c
    a b c
    a b c
    d f e
    d f e
    d f e
    d f e
    d f e
    d f e
    d f e
    d f e
    d f e
    e f e
    e f e
    e f e
    e f e
    e f e
    e f e
    e f e
    e f e
    e f e
    e f e

    My code ( I tired for inside a for but no luck.. ) Any pointers or suggestion is appreciated.

    for($i=0;$i<count($this);$i++) {
       
       // $n = 1;
    //while ($n <= 10) {
     
       
    
         echo $h=$this[$i];
       
         echo$i= $fly[$i];
     
    
    
      $q = "INSERT INTO start (`yes`, `so`) VALUES ('$h', '$i')";
      $r = mysqli_query ($dbconnect, $q) or die ("Update query failed : " . mysql_error());   }
    PHP:
     
    Solved! View solution.
    xbat, Jan 17, 2014 IP
  2. #2
    First of all, you shouldn't use un-prepared queries, but ok - as for the question, you should be able to do this by doing something like this:
    
    for ($i = 0; $i < count($this); $i++) {
      $h = $this[$i];
      $i = $fly[$i];
      for ($c = 1; $c <= 10; $c++) {
       $q = "INSERT INTO start (`yes`, `so`) VALUES ('$h', '$i')";
      $r = mysqli_query ($dbconnect, $q) or die ("Update query failed : ". mysql_error());
    }
    }
    
    PHP:
     
    PoPSiCLe, Jan 17, 2014 IP
  3. xbat

    xbat Well-Known Member

    Messages:
    326
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    105
    #3

    You are fantastic.. Thank you I was so close but yet so horribly far. I had the for but with $i and in the wrong spot .. Yea I know about the prepared.. This is just for something I am running inside.
     
    xbat, Jan 17, 2014 IP