You are here:  » strpos question


strpos question

Submitted by formmailer on Sun, 2007-10-21 12:31 in

Hi David,

Sorry to bother you again. I have a problem that's not really Magic Parser related, but I hope you can help me out anyway.

<?php
  
if (empty($bestemming)) { $bestemming="a";} //Just to make sure something is displayed when no destination is mentioned
  
$records = array(); // global array of records to pick from later
  
function myRecordHandler($record)
  {
    if (!
strpos($record["CATEGORIES/CATEGORY-NAME"], $bestemming))  return; // ignore this record (just return) if the field does not contain the destination value
    
global $records;
    
$records[] = $record;
  }
?>

This isn't working for some reason, but when I don't use a variable in the strpos, it does work:

<?php
  
if (empty($bestemming)) { $bestemming="a";} //Just to make sure something is displayed when no destination is mentioned
  
$records = array(); // global array of records to pick from later
  
function myRecordHandler($record)
  {
    if (!
strpos($record["CATEGORIES/CATEGORY-NAME"], London))  return; // ignore this record (just return) if the field does not contain the destination value
    
global $records;
    
$records[] = $record;
  }
?>

Do you have any idea why the script doesn't work with the variable?

Thanks in advance!

/Jasper

Submitted by support on Sun, 2007-10-21 13:08

Hi Jasper,

You need to declare $bestemming as global inside your myRecordHandler function...

<?php
  
if (empty($bestemming)) { $bestemming="a";} //Just to make sure something is displayed when no destination is mentioned
  
$records = array(); // global array of records to pick from later
  
function myRecordHandler($record)
  {
    global 
$bestemming;
    if (!
strpos($record["CATEGORIES/CATEGORY-NAME"], $bestemming))  return; // ignore this record (just return) if the field does not contain the destination value
    
global $records;
    
$records[] = $record;
  }
?>

That should do the trick...

Cheers,
David.

Submitted by formmailer on Mon, 2007-10-22 11:01

Wow... would you believe I spend hours on this issue... :-(
I declared $bestemming everywhere as a global... except within myRecordHandler.

Thanks a lot!

/Jasper

Submitted by formmailer on Mon, 2007-10-22 17:58

It's me again... :-)

Probabably I am trying to use a very complex structure, so if things can be done better and/or easier let me know.

I have a file that I use include in a page (in fact on multiple pages). I only change the $bestemming variable in order to get different destinations.
Since I use one include file that contains the myRecordHandler function, I need to set the XML field that contains the destination as well (eg. CATEGORIES/CATEGORY-NAME).

<?php
$bestemming 
"Oostenrijk";
$dest_field "\$record[\"CATEGORIES/CATEGORY-NAME\"]";
include(
"../includes/cityzapper.php");
unset (
$bestemming);
unset (
$dest_field);
?>

The second file (that shouldn't change when asking for a new destination) is:

<?php
  $records 
= array(); // global array of records to pick from later
  
MagicParser_parse("http://www.bijnavakantie.nl/feed/cache.php?id=CityZapper&cachetime=1440","myRecordHandler","xml|PRODUCTS/PRODUCT/");
  
shuffle($records);
  
// now you pick off as many random records as you require....
  
$count 0;
  foreach (
$records as $record) {
  echo 
'<h4><a href="'.$record["PRODUCTURL"].'" target="_blank">'.$record["NAME"].''.$record["ADDITIONAL/FIELD-VALUE"].', '.$record["ADDITIONAL/FIELD@2-VALUE"].'</a></h4>';
  echo 
'Vanaf '.$record["PRICE"].''.$record["PRICE-CURRENCY"].' p.p.';
  
$count++;
  if (
$count == 5) break;
  }
?>

Finally, the include file with the myRecordHandler function is:

<?php
  
function myRecordHandler($record)
  {
    if (!empty(
$bestemming)) {
      global 
$bestemming;
      global 
$dest_field;
      if (!
strpos($dest_field$bestemming))  return; // ignore this record (just return) if the field does not contain the destination value
    
}
  global 
$records;
  
$records[] = $record;
  }
?>

But now the problem is that the strpos() fails again. It does work fine when I replace it with $record["CATEGORIES/CATEGORY-NAME"], but in that case I should create a seperate function for every feed (because the "destination field" in the XML is different for each feed.

Can you tell me what I am doing wrong here?

I hope that the above makes at least some sense and that you understand what I am trying to achieve.

/Jasper

Submitted by support on Mon, 2007-10-22 19:21

Hi Jasper,

The way to do this is;

Instead of

$dest_field = "\$record[\"CATEGORIES/CATEGORY-NAME\"]";

...just use this:

$dest_field = "CATEGORIES/CATEGORY-NAME";

And then in your record handler function, instead of:

if (!strpos($dest_field, $bestemming)) return;

...use:

if (!strpos($record[$dest_field], $bestemming)) return;

That should do the trick!
Cheers,
David.