Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2007
    Posts
    122
    Plugin Contributions
    0

    Default Some code help please

    This is part of an import routine I have been writing.
    PHP Code:
    $row 1;
    $handle fopen("http://www.supplierurl.com/feed/feed6.csv""r");
    $new_products="";
    $existing_products="";
    $to_remove_products="";
    $instock 0;
    $outstock 0;
    $totalproducts 0;
    $newproducts 0;
    $deletedproducts 0;
    mysql_query("update products set products_status=99 ");
    while ((
    $data fgetcsv($handle1000",")) !== false) {
        
    $num count($data);
     
    //
        
    $row++;
        if(
    $row>2){
        for (
    $c 0$c $num$c++) {
            if (
    $c == 0$pname_orig str_replace('"''' $data[$c]);
            if (
    $c == 2$pid str_replace('"''' $data[$c]);
            if (
    $c == 4$cat str_replace('"''' $data[$c]);
            if (
    $c == 5$subcat str_replace('"''' $data[$c]);
            if (
    $c == 7$prod_description_orig str_replace('"''' $data[$c]);
            if (
    $c == 11$price str_replace('"''' $data[$c]);
            if (
    $c == 8)$img str_replace('"''' $data[$c]);
            if (
    $c == 9)$img2 str_replace('"''' $data[$c]);// Get 2nd image file name
            
    if ($c == 14) {
      
    $stock str_replace('"''' $data[$c]);
      if(
    $stock=='True'){ $q=100;$instock++;} else {$q=0$outstock++;}
      }
            if (
    $c == 15)$weight str_replace('"''' $data[$c]);
        } 
    Most of the time this works, but sometimes it does not pick the feed file up properly, and sees the file as empty. This clears all my products and ends up placing lots of empty rows in the database.

    Can someone help me with the code required to check that the file is present, and check it is not empty. If there is a problem with the file, I would rather it did not run the the import, and just send an email saying import file error.

    Any help would be appreciated.

    Thanks
    Laurie

  2. #2
    Join Date
    Jan 2004
    Posts
    66,443
    Plugin Contributions
    279

    Default Re: Some code help please

    Try wrapping it in an if/else condition, perhaps akin to this:
    Code:
     
    $row = 1;
    $handle = fopen("http://www.supplierurl.com/feed/feed6.csv", "r");
    if ($handle) {
      $new_products="";
      $existing_products="";
      $to_remove_products="";
      $instock = 0;
      $outstock = 0;
      $totalproducts = 0;
      $newproducts = 0;
      $deletedproducts = 0;
      mysql_query("update products set products_status=99 ");
      while (($data = fgetcsv($handle, 1000, ",")) !== false) {
          $num = count($data);
     //
          $row++;
          if($row>2){
          for ($c = 0; $c < $num; $c++) {
              if ($c == 0) $pname_orig = str_replace('"', '' , $data[$c]);
              if ($c == 2) $pid = str_replace('"', '' , $data[$c]);
              if ($c == 4) $cat = str_replace('"', '' , $data[$c]);
              if ($c == 5) $subcat = str_replace('"', '' , $data[$c]);
              if ($c == 7) $prod_description_orig = str_replace('"', '' , $data[$c]);
              if ($c == 11) $price = str_replace('"', '' , $data[$c]);
              if ($c == 8)$img = str_replace('"', '' , $data[$c]);
              if ($c == 9)$img2 = str_replace('"', '' , $data[$c]);// Get 2nd image file name
              if ($c == 14) {
        $stock = str_replace('"', '' , $data[$c]);
        if($stock=='True'){ $q=100;$instock++;} else {$q=0; $outstock++;}
        }
            if ($c == 15)$weight = str_replace('"', '' , $data[$c]);
      }
    } else {
     // send email here, or simply display some sort of message indicating that no data was found 
    }
    .

    Zen Cart - putting the dream of business ownership within reach of anyone!
    Donate to: DrByte directly or to the Zen Cart team as a whole

    Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
    Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

  3. #3
    Join Date
    Jun 2007
    Posts
    122
    Plugin Contributions
    0

    Default Re: Some code help please

    Hi DR Byte

    Thanks for that.

    Tried to find some info on what $handle is about. Will that pickup if the file is not found or if it is an empty file ?

    Please could you explain a bit, so I can understand.

    Regards
    Laurie

  4. #4
    Join Date
    Jan 2004
    Posts
    66,443
    Plugin Contributions
    279

    Default Re: Some code help please

    PHP.net documentation on fopen(): http://www.php.net/manual/en/function.fopen.php
    .

    Zen Cart - putting the dream of business ownership within reach of anyone!
    Donate to: DrByte directly or to the Zen Cart team as a whole

    Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
    Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

 

 

Similar Threads

  1. Help!Can anyone help me to add some code?
    By johnny20111 in forum General Questions
    Replies: 0
    Last Post: 27 Jan 2011, 02:55 PM
  2. Some code help
    By jcojr72 in forum General Questions
    Replies: 4
    Last Post: 14 Jan 2011, 03:02 AM
  3. Can anyone help with some code in invoices.php?
    By DaveS in forum General Questions
    Replies: 0
    Last Post: 18 Jan 2007, 04:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg