You are here:  » Field is not being displayed.


Field is not being displayed.

Submitted by formmailer on Sat, 2008-05-17 16:19 in

Hi David,

I try to parse the following feed: http://www.zwedenweb.com/feed/cache.php?id=Kras&cachetime=1440 and everyting goes well except for the last field: ADDITIONAL/FIELD@9-VALUE. This field isn't being displayed.

I use the following code:

<?php
 
include_once("/var/www/vhosts/zwedenweb.com/httpdocs/feed/kras_feed.php"); 
  
$count 0;
  foreach (
$records as $record) {
    echo 
'<div id="full_description_image"><img src="'.$record["IMAGEURL"].'" height="130"><br><font size="-2">'.$record["NAME"].'</font></div>';
  echo 
'<b><a href="'.$record["PRODUCTURL"].'" target="_blank">'.$record["NAME"].' ('.$record["ADDITIONAL/FIELD@9-VALUE"].')</b> <br><i>'.$record["ADDITIONAL/FIELD-VALUE"].'</i></a><p>';
  echo 
$record["DESCRIPTION"].' <p>';
  echo 
'<i>Vanaf '.$record["PRICE"].''.$record["PRICE-CURRENCY"].' p.p.</i> <a href="'.$record["PRODUCTURL"].'" target="_blank">(Lees meer...)</a><p></p>';
  
$count++;
  if (
$count == 26) break;
  }
?>

In my server log I can find the following:
[Sat May 17 17:45:24 2008] [error] [client 81.170.241.43] PHP Notice: Undefined index: ADDITIONAL/FIELD@9-VALUE in /var/www/vhosts/zwedenweb.com/httpdocs/feed/kras_all.php on line 9

Can you tell me what's wrong here?

Thanks in advance!

Kind regards,
Jasper

Submitted by support on Sat, 2008-05-17 17:05

Hi Jasper,

This is occurring because not every record has as many as 9 "ADDITIONAL/FIELD-VALUE" fields. I entered your URL into the demo page, and it showed only 7 additional fields in record 2.

The way to handle this without generating the notice is to use isset(); for example:

  echo '<b><a href="'.$record["PRODUCTURL"].'" target="_blank">'.$record["NAME"].' '.(isset($record["ADDITIONAL/FIELD@9-VALUE"])?'('.$record["ADDITIONAL/FIELD@9-VALUE"].')':'').'</b> <br><i>'.$record["ADDITIONAL/FIELD-VALUE"].'</i></a><p>';

Cheers,
David.

Submitted by formmailer on Sat, 2008-05-17 18:08

Hi David,

Thanks for your reply. Your example doesn't generate the notice, but the values still aren't displayed.

I now noticed that they have a fieldname, called reisduuralgemeen, but sometimes this value is on place 9 sometimes on place 8 and sometimes on place 7.
Is there a way to make sure that the field value of reisduuralgemeen is displayed, no matter how many fields they have?

Thanks again.

/Jasper

Submitted by support on Sat, 2008-05-17 18:19

Hello Jasper,

What I would do is parse out the additional fields into their own array; so that you can then reference them by the fieldname; regardless of what position they are in the original feed.

Here's an example myRecordHandler containing the code to do this - cut the appropriate code out and paste it into your record handler:

<?php
  
function myRecordHandler($record)
  {
    
$additionalFields = array();
    foreach(
$record as $k => $v)
    {
      if ((
strpos($k,"ADDITIONAL/FIELD")===0) && strpos($k,"NAME"))
      {
        
$name $v;
      }
      if ((
strpos($k,"ADDITIONAL/FIELD")===0) && strpos($k,"VALUE"))
      {
        
$value $v;
      }
      if (
$name && $value)
      {
        
$additionalFields[$name] = $value;
        
$name "";
        
$value "";
      }
    }
    
// from here you can access the reisduuralgemeen as $additionalFields["reisduuralgemeen"]
    // for example:
    
print $additionalFields["reisduuralgemeen"];
  }
?>

Hope this helps!
Cheers,
David.

Submitted by formmailer on Sat, 2008-05-17 18:42

I tried this, but it doesn't work. Probably my recordhandler is wrong now (it contained already some other stuff):

<?php
  
function myRecordHandler($record)
  {
    global 
$bestemming;
    if (isset(
$bestemming)) {
    global 
$dest_exact;
    if (
$dest_exact == "Y") {
       global 
$dest_field;
      if (
$record[$dest_field] <> $bestemming) return;  // ignore this record (just return) if the field does not contain the destination value
      
} else {
      global 
$dest_field;
      if (!
strpos($record[$dest_field], $bestemming)) return;  // ignore this record (just return) if the field does not contain the destination value
      
}
    }
    
$additionalFields = array();
    foreach(
$record as $k => $v)
    {
      if ((
strpos($k,"ADDITIONAL/FIELD")===0) && strpos($k,"NAME"))
      {
        
$name $v;
      }
      if ((
strpos($k,"ADDITIONAL/FIELD")===0) && strpos($k,"VALUE"))
      {
        
$value $v;
      }
      if (
$name && $value)
      {
        
$additionalFields[$name] = $value;
        
$name "";
        
$value "";
      }
  global 
$records;
  
$records[] = $record;
  }
?>

I use this to display this in the target page:

<?php
  
echo 'Reisduur: '.$additionalFields["reisduuralgemeen"].' dagen<p>';
?>

Submitted by support on Sat, 2008-05-17 18:46

Hello Jasper,

I see - if you are loading the record into a global array to use later; then you will need to do it slightly differently.

The easiest thing to do is to add the $additionalFields array to $record:

<?php
  
function myRecordHandler($record)
  {
    global 
$bestemming;
    if (isset(
$bestemming)) {
    global 
$dest_exact;
    if (
$dest_exact == "Y") {
       global 
$dest_field;
      if (
$record[$dest_field] <> $bestemming) return;  // ignore this record (just return) if the field does not contain the destination value
      
} else {
      global 
$dest_field;
      if (!
strpos($record[$dest_field], $bestemming)) return;  // ignore this record (just return) if the field does not contain the destination value
      
}
    }
    
$additionalFields = array();
    foreach(
$record as $k => $v)
    {
      if ((
strpos($k,"ADDITIONAL/FIELD")===0) && strpos($k,"NAME"))
      {
        
$name $v;
      }
      if ((
strpos($k,"ADDITIONAL/FIELD")===0) && strpos($k,"VALUE"))
      {
        
$value $v;
      }
      if (
$name && $value)
      {
        
$additionalFields[$name] = $value;
        
$name "";
        
$value "";
      }
  
$record["additionalFields"] = $additionalFields;
  global 
$records;
  
$records[] = $record;
  }
?>

Then in your target page; where I presume you have a loop to go through each $records as $record, use:

<?php
  
echo 'Reisduur: '.$record["additionalFields"]["reisduuralgemeen"].' dagen<p>';
?>

Hope this helps!
Cheers,
David.

Submitted by formmailer on Sat, 2008-05-17 22:00

Hi David,

Thanks again. I used your code, but it didn't work. :-(
I figured there was missing a } . I added it to the end of the file, but now things got only worse:
Only the first record is now being displayed, but 26 times and the fields ADDITIONAL/FIELD-VALUE (subtitel) and ADDITIONAL/FIELD@x-VALUE (reisduuralgemeen) are not being displayed.

I shall now post all the code I use:

Target page:

<?php
    
//Voeg artikelen in
    
require("/var/www/vhosts/zwedenweb.com/httpdocs/feed/MagicParser.php"); //Required for feeds
    
require("/var/www/vhosts/zwedenweb.com/httpdocs/feed/functions_new.inc"); //Required for feeds
    //Start feed met bestemming
    
$bestemming "reizen/Zweden";
    
$dest_field "CATEGORIES/CATEGORY@1-PATH";
    
$dest_exact "Y";
    include(
"/var/www/vhosts/zwedenweb.com/httpdocs/feed/kras_all.php");
    unset (
$bestemming);
    unset (
$dest_field);
    unset (
$dest_exact);
    
//Einde feed met bestemming
  
?>

kras_all.php:

<?php
 
include_once("/var/www/vhosts/zwedenweb.com/httpdocs/feed/kras_feed.php"); 
  
$count 0;
  foreach (
$records as $record) {
  echo 
'<div id="full_description_image"><img src="'.$record["IMAGEURL"].'" height="130"><br><font size="-2">'.$record["NAME"].'</font></div>';
  echo 
'<b><a href="'.$record["PRODUCTURL"].'" target="_blank">'.$record["NAME"].' ';
  echo 
'</b> <br><i>'.$record["additionalFields"]["subtitel"].'</i></a><p>';
  echo 
$record["DESCRIPTION"].' <p>';
  echo 
'Reisduur: '.$record["additionalFields"]["reisduuralgemeen"].' dagen<p>';
  echo 
'<i>Vanaf '.$record["PRICE"].''.$record["PRICE-CURRENCY"].' p.p.</i> <a href="'.$record["PRODUCTURL"].'" target="_blank">(Lees meer...)</a><p></p>';
  
$count++;
  if (
$count == 26) break;
  }
?>

functions_new.inc:

<?php
  
function myRecordHandler($record)
  {
    global 
$bestemming;
    if (isset(
$bestemming)) {
    global 
$dest_exact;
    if (
$dest_exact == "Y") {
       global 
$dest_field;
      if (
$record[$dest_field] <> $bestemming) return;  // ignore this record (just return) if the field does not contain the destination value
      
} else {
      global 
$dest_field;
      if (!
strpos($record[$dest_field], $bestemming)) return;  // ignore this record (just return) if the field does not contain the destination value
      
}
    }
    
$additionalFields = array();
    foreach(
$record as $k => $v)
    {
      if ((
strpos($k,"ADDITIONAL/FIELD")===0) && strpos($k,"NAME"))
      {
        
$name $v;
      }
      if ((
strpos($k,"ADDITIONAL/FIELD")===0) && strpos($k,"VALUE"))
      {
        
$value $v;
      }
      if (
$name && $value)
      {
        
$additionalFields[$name] = $value;
        
$name "";
        
$value "";
      }
  
$record["additionalFields"] = $additionalFields;
  global 
$records;
  
$records[] = $record;
  }
}
?>

kras_feed.php

<?php
  
//require("MagicParser.php");
  
$records = array(); // global array of records to pick from later
  
MagicParser_parse("http://www.zwedenweb.com/feed/cache.php?id=Kras&cachetime=1440","myRecordHandler","xml|PRODUCTS/PRODUCT/");
  
//shuffle($records);
  // now you pick off as many random records as you require....
?>

I hope these files help you to find, what I am doing wrong.

Thanks again!

/Jasper

Submitted by support on Sat, 2008-05-17 22:04

Hello Jasper,

It looks wrong in the myRecordHandler function... Try this:

<?php
  
function myRecordHandler($record)
  {
    global 
$bestemming;
    if (isset(
$bestemming)) {
    global 
$dest_exact;
    if (
$dest_exact == "Y") {
       global 
$dest_field;
      if (
$record[$dest_field] <> $bestemming) return;  // ignore this record (just return) if the field does not contain the destination value
      
} else {
      global 
$dest_field;
      if (!
strpos($record[$dest_field], $bestemming)) return;  // ignore this record (just return) if the field does not contain the destination value
      
}
    }
    
$additionalFields = array();
    foreach(
$record as $k => $v)
    {
      if ((
strpos($k,"ADDITIONAL/FIELD")===0) && strpos($k,"NAME"))
      {
        
$name $v;
      }
      if ((
strpos($k,"ADDITIONAL/FIELD")===0) && strpos($k,"VALUE"))
      {
        
$value $v;
      }
      if (
$name && $value)
      {
        
$additionalFields[$name] = $value;
        
$name "";
        
$value "";
      }
    }
    
$record["additionalFields"] = $additionalFields;
    global 
$records;
    
$records[] = $record;
  }
?>

The last 3 lines were one } too-high up in the code, so the field would not have been set correctly, and it would have made $record worse!

Cheers,
David.

Submitted by formmailer on Sat, 2008-05-17 22:08

That did the trick, David.

Thanks for the great support!!

/Jasper

Submitted by formmailer on Tue, 2008-05-27 07:40

Hi David,

I used the above solution for several more feeds without any problems. But now I have a feed where this doesn't seem to work and I can't find out why.
The additional feeds "stars" and "stad" (city) are not being displayed.

Feed: http://www.zwedenweb.com/feed/cache.php?id=CityZapper&cachetime=1440

Recordhandler:

<?php
  
function myRecordHandler($record)
  {
    global 
$bestemming;
    if (isset(
$bestemming)) {
    global 
$dest_exact;
    if (
$dest_exact == "Y") {
       global 
$dest_field;
      if (
$record[$dest_field] <> $bestemming) return;  // ignore this record (just return) if the field does not contain the destination value
      
} else {
      global 
$dest_field;
      if (!
stristr($record[$dest_field], $bestemming)) return;  // ignore this record (just return) if the field does not contain the destination value
      
}
    }
    
$additionalFields = array();
    foreach(
$record as $k => $v)
    {
      if ((
strpos($k,"ADDITIONAL/FIELD")===0) && strpos($k,"NAME"))
      {
        
$name $v;
      }
      if ((
strpos($k,"ADDITIONAL/FIELD")===0) && strpos($k,"VALUE"))
      {
        
$value $v;
      }
      if (
$name && $value)
      {
        
$additionalFields[$name] = $value;
        
$name "";
        
$value "";
      }
    }
    
$record["additionalFields"] = $additionalFields;
    global 
$records;
    
$records[] = $record;
  }
?>

Output:

<?php
 
include_once("/var/www/vhosts/zwedenweb.com/httpdocs/feed/cityzapper_feed.php"); 
  
$count 0;
  foreach (
$records as $record) {
  
//if ($count == 0 or $count == 4 or $count == 8 or $count == 12 or $count == 16 or $count == 20 or $count == 24)
  //{
    
echo '<div id="full_description_image"><img src="'.$record["IMAGEURL"].'"><br><font size="-2">'.$record["NAME"].'</font></div>';
  
//}
  
echo '<b><a href="'.$record["PRODUCTURL"].'" target="_blank">'.$record["NAME"];
  echo 
$record["additionalFields"]["stars"].', '.$record["additionalFields"]["stad"].'</b></a><p>';
  echo 
$record["DESCRIPTION"].' <p>';
  echo 
'<i>Vanaf '.$record["PRICE"].''.$record["PRICE-CURRENCY"].' p.p.p.n.</i> <a href="'.$record["PRODUCTURL"].'" target="_blank">(Lees meer...)</a><p></p>';
  
$count++;
  if (isset(
$maxcount)) {
    if (
$count == $maxcount) break;
  }
  if (
$count == 26) break;
  }
?>

The output is included in: http://www.zwedenweb.com/content/meer_hotels.php

Do you have any ideas?

/Jasper

Submitted by support on Tue, 2008-05-27 08:31

Hi Jasper,

This particular feed is not using the attributes -NAME and -VALUE, instead, you are looking for (in $record):

ADDITIONAL/FIELD@1

and

ADDITIONAL/FIELD@1-NAME

So in place of the following code:

      if ((strpos($k,"ADDITIONAL/FIELD")===0) && strpos($k,"NAME"))
      {
        $name = $v;
      }
      if ((strpos($k,"ADDITIONAL/FIELD")===0) && strpos($k,"VALUE"))
      {
        $value = $v;
      }

...try this version

      if ((strpos($k,"ADDITIONAL/FIELD")===0) && strpos($k,"NAME"))
      {
        $name = $v;
      }
      elseif (strpos($k,"ADDITIONAL/FIELD")===0)
      {
        $value = $v;
      }

That should do the trick!

Cheers,
David.