You are here:  » Get first 2 records


Get first 2 records

Submitted by Bay_Oz on Thu, 2012-12-13 21:31 in

I want to get first 2 records after dijitall.com, how can I do that?

Dijitall.com -> 179,92
Bizdehesapli ->183,92

Dijitall.com ->409,06
mormani.com ->419,25

CSV File :

"urunadi","firma","urunfiyati"
"Artes i703"," Dijitall.com","179,92 TL+6 TL kargo"
"Artes i703"," Bizdehesapli","183,92 TL+ kargo"
"Artes i703"," Webdenal.com","191,06 TL+10 TL kargo"
"Artes i703"," mormani.com","191,13 TL+ kargo"
"Artes i703"," alisveris.com","194,12 TL+7 TL kargo"
"Artes i703"," Telepanya","194,22 TL+ kargo"
"Artes i703"," Ucuzalıyorum","194,39 TL+ kargo"
"Artes i703"," Beyazesyam.com","194,71 TL+ kargo"
"Artes i9701ips"," Dijitall.com","409,06 TL+6 TL kargo"
"Artes i9701ips"," mormani.com","419,25 TLÜcretsiz Kargo"
"Artes i9701ips"," Bizdehesapli","419,44 TL+ kargo"
"Artes i9701ips"," Tekmarshop","420,79 TL+ kargo"
"Samsung Boracay AQ12TSMN"," Dijitall.com","877,00 TL+44 TL kargo"
"Samsung Boracay AQ12TSMN"," Beyaz Burada","878,00 TL+ kargo"
"Samsung Boracay AQ12TSMN"," TekÇarşı","882,24 TL+ kargo"
"Samsung Boracay AQ12TSMN"," İdeal Eşya","888,25 TL+ kargo"

Submitted by support on Fri, 2012-12-14 10:07

Hi,

The way to do this would be to have a global flag that you set to say "I'm interested in the next record", and a counter to keep track of how many interesting records you have found. The "interesting" records can then be loaded into a global array; and processed after the parse using foreach() and a function that behaves exactly as a normal myRecordHandler function.

Consider the following example:

<?php
  
require("MagicParser.php"):
  
$interested FALSE;
  
$records = array();
  
$counter 0;
  function 
myRecordHandler($record)
  {
    global 
$interested;
    global 
$records;
    global 
$counter;
    if (
$interested)
    {
      
$records[] = $record;
      
$interested FALSE;
      
$counter++;
      return (
$counter == 2);
    }
    if (
$record["firma"]==" Dijitall.com")
    {
      
$interested TRUE;
    }
  }
  
MagicParser_parse("filename.csv","myRecordHandler","csv|44|1|34");
  function 
myInterestedRecordHandler($record)
  {
    
print_r($record);
  }
  foreach(
$records as $record)
  {
    
myInterestedRecordHandler($record);
  }
?>

Hope this helps!
Cheers,
David
--
MagicParser.com

Submitted by Bay_Oz on Fri, 2012-12-14 11:03

First of all, you are the best support ever. You deserve every penny you get for what you do.

so I works great and I just need add dijitall.com record too in list as well.

For now output :

*it just giving first record after dijitall.com

Array
(
[urunadi] => Artes i703
[firma] => Büroteknik
[urunfiyati] => 180,34 TL+8 TL kargo
)
Array
(
[urunadi] => Samsung Boracay AQ12TSMN
[firma] => Beyaz Burada
[urunfiyati] => 878,00 TL+ kargo
)
Array
(
[urunadi] => Artes i9701ips
[firma] => mormani.com
[urunfiyati] => 419,20 TLÃœcretsiz Kargo
)

Submitted by support on Fri, 2012-12-14 12:05

Hi,

No problem - you can add each dijitall.com record to the array at this point:

    if ($record["firma"]==" Dijitall.com")
    {
      $interested = TRUE;
    }

...REPLACE with:

    if ($record["firma"]==" Dijitall.com")
    {
      $interested = TRUE;
      $records[] = $record;
    }

Cheers,
David.

Submitted by Bay_Oz on Tue, 2012-12-18 14:03

Hi David,

it works great!

I can get all the data but I only have one last problem that I could not solved.

$fiyat = $record["urunfiyati"];

as you know I am getting 2 records and I want to get $fiyat2-$fiyat1 in this foreach func, but I could not find how?

Output :

Apple iPhone 3GS 8GB--->889,20
Apple iPhone 3GS 8GB--->899,00
Sony Vaio Duo 11 SVD1121Q2EB--->3.025,00
Sony Vaio Duo 11 SVD1121Q2EB--->3.039,00
Artes i703--->180,32
Artes i703--->180,77

Thanks,

Submitted by support on Tue, 2012-12-18 15:40

Hi,

If you only want to display each first (Dijitall.com) record of the captured records, and to be able to show the price difference to the next, you could try something like:

  function myInterestedRecordHandler($record)
  {
    global $records;
    global $k;
    $fiyat1 = $record["urunfiyati"];
    $fiyat2 = $records[$k+1]["urunfiyati"];
    print ($fiyat2-$fiyat1);
  }
  foreach($records as $k => $record)
  {
    // skip every other one
    if ($k % 2) continue;
    myInterestedRecordHandler($record);
  }

This has quite complex dependencies, but I hope this points you in the right direction...

Cheers,
David
--
MagicParser.com