How to get data in for loop

I run a for loop but it fetch only 22 page data as there is near about 800 data.
Any wordpress developer can check this what is wrong in this code that I would not able to get data of all page thanks in advance. If you feel my code is not clear please ask me for understanding.

$arr = array();
$counter = 1;
for($i=0;$i<1450;$i+=50)
{
    $i;
    $page = "http://www.cardkingdom.com/catalog/view?page=".$counter++."";
    $all_page = file_get_contents($page);
    preg_match_all
    ('/http://www.cardkingdom.com/catalog/item/([0-9]+)/', $all_page,$match2);
    $arr[] = $match2[0];
}
$arrs = count($arr);
for($i=0;$i<$arrs;$i++)
{
    echo "Page no: ".$i;
    for($j=0;$j<50;$j++){
        echo '<div class="container">';
        //  get product detail on url basis
        $input  = file_get_contents($arr[$i][$j]);
        preg_match('/http://www.cardkingdom.com//product_images
                /(d+)/(d+)/(d+)/(d)/(d+)/(d+)/([0-
                9_]+)standard(.w+)|http://www.cardkingdom.com/media/images/products
                /standard/([0-9]+[_]+[0-
                9]+).jpg|http://www.cardkingdom.com/media/images/products/max/([0-9]+
                [_]+[0-9]+).jpg/', $input , $proimg);
        // for print itmes
        if(isset($proimg[0])){
            echo $postGuid = $proimg[0]; echo "<br>";
        }
        //   End
        //    get products title,description
        preg_match
        ('/<td valign=.*?>s+?<b>(.*?)<br><span style=.*?>(.*?)</span></b><br><br>s+?              (.*?|.*+n+.*?)<BR>|<td valign=.*? width=.*?>s+?<b>(.*?)</b>s+?<br>s+?<a href="(.*?)">(.*?)</a>s+<br><br>s+?(.*?<br>.*?<br>|.*?<br>.*?<BR>s+.*?<br>)|<td valign=.*? width=.*?>s+?<b>(.*?)</b>s+?<br>s+?<a href="(.*?)">(.*?)</a>s*<br><br>s+?(.*?<br>.*?<BR>s+?.*?<BR>)|<td valign=.*?>s+?<b>(.*?)<br><span style=.*?>(.*?)</span></b><br><br>s+?([a-zA-zs.'<br>a-z0-9<br>]*)</span>/',$input ,    $match_title);
        //echo "<pre>";
        if((isset($match_title[1]))  && ($match_title[1]!='')) {
            echo $posTitle = htmlspecialchars($match_title[1]); echo "<br>";
        }
        if((isset($match_title[12])) && ($match_title[12]!='')){
            echo $posTitle = htmlspecialchars($match_title[12]); echo "<br>";
        }
        if((isset($match_title[2]))  && ($match_title[2]!='')) {
            echo $postContent = $match_title[2]; echo "<br>";
        }
        if((isset($match_title[3]))  && ($match_title[3]!='')) {
            echo $postContent = $match_title[3]; echo "<br>";
        }
        if((isset($match_title[4]))  && ($match_title[4]!='')) {
            echo $posTitle = $match_title[4]; echo "<br>";
        }
        if((isset($match_title[8]))  && ($match_title[8]!='')) {
            echo $posTitle = $match_title[8]; echo "<br>";
        }
        if((isset($match_title[5]))  && ($match_title[5]!='')) {
            echo $postContent = $match_title[5];"<br>";
        }
        if((isset($match_title[6]))  && ($match_title[6]!='')) {
            echo $postContent = $match_title[6]; echo "<br>";
        }
        if((isset($match_title[7]))  && ($match_title[7]!='')) {
            echo $postContent = $match_title[7]; echo "<br>";
        }
        if((isset($match_title[9]))  && ($match_title[9]!='')) {
            echo $postContent = $match_title[9]; echo "<br>";
        }
        if((isset($match_title[10])) && ($match_title[10]!='')){
            echo $postContent = $match_title[10]; echo "<br>";
        }
        if((isset($match_title[11])) && ($match_title[11]!='')){
            echo $postContent = $match_title[11]; echo "<br>";
        }
        if((isset($match_title[13])) && ($match_title[13]!='')){
            echo $postContent = $match_title[13]; echo "<br>";
        }
        if((isset($match_title[9]))  && ($match_title[9]!='')) {
            echo $postContent = $match_title[9]; echo "<br>";
        }
        if((isset($match_title[14])) && ($match_title[14]!='')){
            echo $postContent = $match_title[14]; echo "<br>";
        }
        //$match_arr[] = $match_title[0];
        //  price of products
        preg_match('/<br><br><span style=.*?>(.*?)</span><br><br>s+?Price:(s*$(d+.d+))s+?<br><br>/' , $input ,$match_price);
        //echo "<pre>";
        if(isset($match_price[3])){
            echo $productprice =$match_price[3]; echo "<br>";
        }
        // stock of products
        preg_match('/<i>(([0-9]+)/' , $input, $stock );
        //echo "<pre>";
        if(isset($stock[1])){
            echo $availstock = $stock[1];echo "<br>";
        }
        echo "<br><br>";
        // database query to insert products in woocommerce
        $prefix = $wpdb->prefix;
        $seLastPosts = "SELECT max(ID) as maxid FROM ".$prefix."posts";
        $getLastId   = $wpdb->get_results($seLastPosts);
        /* $lastIdposts = $getLastId[0]->maxid;   NOTE : Last ID of wp_posts */
        //  check if record exsist is databse and update database
        $resCheck   = $wpdb->get_results(
                "SELECT COUNT(*) AS counter FROM ".$prefix."posts
                WHERE post_title='".mysql_real_escape_string($posTitle)."'");
        echo $count = $resCheck[0]->counter;
        // result check for guid
        $resCheck_guid   = $wpdb->get_results("SELECT guid FROM ".$prefix."posts
                WHERE  guid='".mysql_real_escape_string($proimg[0])."'");
        $guid_noduplacate = $resCheck_guid[0]->guid;
        //echo $count = $resCheck[0]->guid;
        // count product if > 0 then record is not       inserted into database
        // insert query in databse
        if($count > 0 ){
            // set update query if record exist in table
            $update = $wpdb->update(
                    $prefix."posts",array("post_content" => $postContent,
                            "post_status" =>      "publish",
                            "post_type" => "product"),array("post_title" => $posTitle));
            $select = $wpdb->get_results(
                    "SELECT ID AS productsId FROM ".$prefix."posts
                    WHERE  post_title='".$posTitle."'");
            $updateImg  = $wpdb->update($prefix."posts",array("guid" => $postGuid),
                    array("post_parent" => $select[0]->productsId ));
            $postmetaIns = $wpdb->Update($prefix."postmeta",array("
                    meta_value" => $productprice),array("
                            post_id" => $select[0]->productsId,"meta_key" => "_price"));
            $postmetaIns2 = $wpdb->update($prefix."postmeta",array(
                    "meta_value" => $availstock),array("post_id" => $select[0]->productsId,
                            "meta_key" => "_stock"));
            //$img = ABSPATH.'wp-content/uploads/2014/07/'.$proimg[7].'standard'.$proimg[8];
            ///file_put_contents($img,$input);
            // Add Featured Image to Post
            //$image_url  = 'http://s.wordpress.org/style/images/wp-header-logo.png';
            // Define the image URL here
            /*  $upload_dir = wp_upload_dir(); // Set upload folder
            $image_data = file_get_contents($proimg[0]);
            // Get image data
            $filename   = basename($proimg[0]); // Create image file name
            // Check folder permission and define file location
            if( wp_mkdir_p( $upload_dir['path'] ) ) {
            $file = $upload_dir['path'] . '/' . $filename;
            } else {
            $file = $upload_dir['basedir'] . '/' . $filename;
            }
            // Create the image  file on the server
            file_put_contents( $file, $image_data );
            // Check image file type
            $wp_filetype = wp_check_filetype( $filename, null );
            // Set attachment data
            $attachment = array(
                    'post_mime_type' => $wp_filetype['type'],
                    'post_title'     => sanitize_file_name( $filename ),
                    'post_content'   => '',
                    'post_status'    => 'inherit'
            );
            // Create the attachment
            $attach_id = wp_insert_attachment( $attachment, $file, $post_id );
            // Include image.php
            require_once(ABSPATH . 'wp-admin/includes/image.php');
            // Define attachment metadata
            $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
            // Assign metadata to attachment
            wp_update_attachment_metadata( $attach_id, $attach_data );
            // And finally assign featured image to post
            set_post_thumbnail( $post_id, $attach_id ); */
            //  End here
        }
        if($count <1)
        {
            // if record not exist insert New record
            /*$insert = $wpdb->insert( $prefix."posts", array("post_title" => $posTitle,
                    "post_content" => $postContent,
                    "post_status" => "publish","post_type" =>"product")); */
            // Register Post Data
            $post = array();
            $post['post_status']   = 'publish';
            $post['post_type']     = 'product'; // can be a CPT too
            $post['post_title']    = mysql_real_escape_string($posTitle);
            $post['post_content']  = $postContent;
            $post['post_author']   = 1;
            // Create Post
            $post_id = wp_insert_post( $post );
            // select products ID
            $select = $wpdb->get_results("SELECT ID AS productsId FROM ".$prefix."posts
                    WHERE  post_title='".$posTitle."'");
            //echo $select[0]->productsId; die();
            // insert Image in pots and product attribute meta posts table
            //$insertImg = $wpdb->insert($prefix."posts",array("post_status" =>"inherit",
            "post_type" => "attachment",
            "guid" => $postGuid,"post_parent" => $select[0]->productsId ));
            $select[0]->productsId;
            // Add Featured Image to Post
            $image_url  = $proimg[0]; // Define the image URL here
            $upload_dir = wp_upload_dir(); // Set upload folde r
            $image_data = file_get_contents($image_url); // Get image data
            $filename   = basename($image_url); // Create image file name
            // Check folder permission and define file location
            if( wp_mkdir_p( $upload_dir['path'] ) ) {
                $file = $upload_dir['path'] . '/' . $filename;
            } else {
                $file = $upload_dir['basedir'] . '/' . $filename;
            }
            // Create the image  file on the server
            file_put_contents( $file, $image_data );
            // Check image file type
            $wp_filetype = wp_check_filetype( $filename, null );
            // Set attachment da ta
            $attachment = array(
                    'post_mime_type' => $wp_filetype['type'],
                    'post_title'     => sanitize_file_name( $filename ),
                    'post_content'   => '',
                    'post_status'    => 'inherit'
            );
            // Create the attachment
            if(!$guid_noduplacate){
                $attach_id = wp_insert_attachment( $attachment, $file, $post_id );
            }
            // Include image.php
            require_once(ABSPATH . 'wp-admin/includes/image.php');
            // Define attachment metadata
            $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
            // Assign metadata to attachment
            wp_update_attachment_metadata( $attach_id, $attach_data );
            // And finally assign featured image to post
            set_post_thumbnail( $post_id, $attach_id );
            add_post_meta( $post_id, '_thumbnail_id', $attach_id, true );
            add_post_meta( $post_id, '_price', $productprice, true );
            add_post_meta( $post_id, '_stock', $availstock, true );
            //  End here
            /*  $imgId = $wpdb->get_results("SELECT ID AS imageId FROM ".$prefix."posts
             WHERE post_parent=".$select[0]->productsId."");
            //echo "hello".$imgId[0]->imageId;
            // Entry INTO postmeta table of products attribute
            // price,stock and _thumbnail_id
            $postmetaIns = $wpdb->insert($prefix."postmeta",array(
                    "post_id" => $select[0]->productsId,
                    "meta_key" => "_thumbnail_id","meta_value" => $imgId[0]->imageId));
            $postmetaIns = $wpdb->insert($prefix."postmeta",array("
                    post_id" => $select[0]- >productsId,"meta_key" => "_price",
                    "meta_value" => $productprice));
            $postmetaIns = $wpdb->insert($prefix."postmeta",array(
                    "post_id" => $select[0]->productsId,"meta_key" => "_stock",
                    "meta_value" => $availstock)); */
        }
        echo "</div>";
    }
}

Related posts

Leave a Reply

1 comment

  1. this is how i would do it, in your initial for loop you increment counter but it will never reach more than 29 because 1450 / 50 (the value you increment your for loop) = 29, your counter reaches max value 29 so you will only get 29 pages to file_get_contents from. for the preg match, i am unsure what you want to achieve from it as i do not have a sample of the page, but you will definitely have ALL the results in $match2 (i don’t think this is something you would want). my advice is to debug your code slowly, aka add a var_dump for most of your vars inside each of your for loops and a quick exit/break so you know what kind of data you are actually looking at. it is common practice to escape the slashes but you could just as well use a different regexp separator like % or # so your code is more readable. hope this helps

    $arr = array();
    for($i=0;$i<1450;$i++)
    {
        $page = "http://www.cardkingdom.com/catalog/view?page=".$i;
        $all_page = file_get_contents($page);
        preg_match_all('%http://www.cardkingdom.com/catalog/item/([0-9]+)%ism', $all_page, $match2);
        $arr[] = $match2[0];
    }