Inserting 31102 records from an existing table

Discussion in 'PHP' started by gilgalbiblewheel, Mar 24, 2011.

  1. #1
    I want to insert 31102 records into a db table from an html table.

    I wanted to make it dependent on the number of the page. When page=1 then it would insert the first set of records. When it's 2 the 2nd set. This way the browser won't freeze.

    How do you associate the $page with the for loop? So that the 1st page will insert the 1st set of records then the next page will insert the next set...

    $num=round(count($trInnerHTML[1])/32);
    
    echo "<span style=\"color: orange;\">".$num."</span><br />\n";
    
    
    
    $limit=($page+1)*$num;
    
    $tds = array();
    
    for($getTds=($page-1)*$num; $getTds<$limit; $getTds++){//counting the trs
    
        if($getTds!==0){
    
            preg_match_all("#<td.*>(.+)</td#Ui", $trInnerHTML[1][$getTds], $tdInnerHTML);
    
            //print_r($tdInnerHTML[1]);
    
            echo "<span style=\"color: red;\">".count($tdInnerHTML[1])."</span>";
    
            //echo "<br />\n";
    
            for($splitTds=0; $splitTds<count($tdInnerHTML[1]); $splitTds++){
    
                if($splitTds!==0){
    
                    $tds[] = addslashes($tdInnerHTML[1][$splitTds]);
    
                    echo "<span style=\"color: blue;\">".$tdInnerHTML[1][$splitTds]."</span><br /\n>";
    
                }
    
                if($splitTds==count($tdInnerHTML[1])-1){
    
                    $tdarr = implode("', '", $tds);
    
                    $sql = "INSERT INTO ".$dbTable3." (".$fieldarr.") VALUES ('".$tdarr."')";
    
                    echo "<span style=\"color: green;\">".$sql."</span><br /\n>";
    
                    mysql_query($sql) or die(mysql_error());
    
                    $tds = array();
    
                }
    
            }
    
        }
    
    }
    
    mysql_close($con);
    
    
    
    //then redirect them to the index
    
    $add=$page+1;
    
    echo "Location: create_bible.php?type=create&table=bible&page=".$add;
    
    header("Location: create_bible.php?type=create&table=bible&page=".$add);
    PHP:
     
    gilgalbiblewheel, Mar 24, 2011 IP
  2. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #2
    It's this area that I'm looking for clarification:
    	$start=$page*$num;
    	$limit=$page*$num;
    	$tds = array();
    	echo "page = ".$page;
    	for($getTds=$start; $getTds<$limit; $getTds++){//counting the trs
    PHP:
    You might see some changes from the code above since I'm working on this.
     
    gilgalbiblewheel, Mar 24, 2011 IP
  3. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #3
    Ok. Let me clarify further. Since
    31102/32=971.9375
    Rounded up to 972
    as mentioned in this formula:
    $num=round(count($trInnerHTML[1])/32);
    PHP:
    Each page should have 972 records to insert in the database table.
    But page 1 will insert records 1-972;
    page 2 will insert records 973-1945 (which is 973+972);
    page 3 will insert records 1946-2918 (which is 1946+972);
    and so on up to 31102.
     
    gilgalbiblewheel, Mar 24, 2011 IP
  4. bledileka

    bledileka Peon

    Messages:
    35
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    i think you dont have to complicate it, use a simple counter, when the counter=972, make it 0 and do flush() or ob_flush, and yur browser won't freeze. You can do it even for each insert if you like and see in real time what is going on !
     
    bledileka, Mar 24, 2011 IP