You are here:  » How to add one or more values ?


How to add one or more values ?

Submitted by toulouse on Thu, 2009-09-17 15:29 in

Hello,

I import in my database several flux.

function myRecordHandler($record) {
$sql = "INSERT INTO `rssitems` SET
title = '".mysql_real_escape_string( $record["TITLE"] )."',
description = '".mysql_real_escape_string( $record["DESCRIPTION"] )."'";
mysql_query($sql);
}
// we might want to empty the table before we start
$sql = "TRUNCATE `cms_rss_items`";
mysql_query($sql);
$feeds = array( 'http://www....link1.xml',
'http://www....link2.xml');
foreach($feeds as $url) {
MagicParser_parse($url,"myRecordHandler","xml|RSS/CHANNEL/ITEM/");
}

But my problem, it is that I must add a category for each entry which belongs to flux.

Also, I wonder whether it is possible to do this work with Magic Parser ?

For example :

$feeds = array( 'http://www.flux1x;l' = cat1,
'http://www.flux2.xml = cat2');

You have a suggestion ?

Thank you very much for your assistance.

Mick

Submitted by support on Thu, 2009-09-17 16:26

Hello Mick,

Sure - you could do this using global variables to pass the category name through to myRecordHandler. For example;

function myRecordHandler($record) {
global $cat;
// now you can use $cat from the $feeds array here in myRecordHandler
$sql = "INSERT INTO `rssitems` SET
title = '".mysql_real_escape_string( $record["TITLE"] )."',
description = '".mysql_real_escape_string( $record["DESCRIPTION"] )."'";
mysql_query($sql);
}
// we might want to empty the table before we start
$sql = "TRUNCATE `cms_rss_items`";
mysql_query($sql);
$feeds = array(
  'cat1' => 'http://www....link1.xml',
  'cat2' => 'http://www....link2.xml'
);
foreach($feeds as $cat => $url)
{
  MagicParser_parse($url,"myRecordHandler","xml|RSS/CHANNEL/ITEM/");
}

Hope this helps!
Cheers,
David.

Submitted by toulouse on Thu, 2009-09-17 17:07

Hello David,

It is perfect ! That functions marvelously well !

Just for information.

I solved my problem of encoding UTF-8 like this (_utf8) :
title = _utf8'".mysql_real_escape_string( strip_tags($record["TITLE"]) )."',

Perhaps that can help of other users of MagicParser.

Here the link which helped me :
http://forums.mysql.com/read.php?103,242754,245910#msg-245910

Thanks again. :)

Mick

Submitted by toulouse on Fri, 2009-09-18 12:31

Hello David,

I tried to add extra information, but that does not function.

Here my approach :

$site0 = array();
$site0 ['cat'] ='cat1';
$site0 ['url'] ='http://www..../rss.xml';
$site0 ['info'] = 'INFO...';
$site1 = array();
$site1 ['cat'] ='cat2';
$site1 ['url'] ='http://www.....com/rss.xml';
$site1 ['info'] = 'INFO...';
$feed = array ($site0, $site1);
foreach($feed as $url) {
MagicParser_parse($url['url'],"myRecordHandler","xml|RSS/CHANNEL/ITEM/");
}

function myRecordHandler($record) {
$sql = "INSERT INTO `cms_rss_items` SET
title = '".mysql_real_escape_string( $record["TITLE"] )."',
description = '".mysql_real_escape_string( $record["DESCRIPTION"]) )."',
info = '".mysql_real_escape_string( $url['info'] )."',
category = '".mysql_real_escape_string( $url['cat'] )."'";
mysql_query($sql);
}

The entries are well recorded, but I do not manage to record the new values (info and cat) in my table.

My approach does not seem to be correct.

You think that there is a solution ?

Thank you for your patience !

Mick

Submitted by support on Fri, 2009-09-18 12:36

Hi Mick,

I think you just need to declare $url as global in myRecordHandler() and that should be it... e.g:

function myRecordHandler($record) {
global $url;
$sql = "INSERT INTO `cms_rss_items` SET
title = '".mysql_real_escape_string( $record["TITLE"] )."',
description = '".mysql_real_escape_string( $record["DESCRIPTION"]) )."',
info = '".mysql_real_escape_string( $url['info'] )."',
category = '".mysql_real_escape_string( $url['cat'] )."'";
mysql_query($sql);
}

Cheers!
David.