Page 1 of 1
Using Template
Posted: Fri Dec 22, 2006 5:09 am
by dibyendrah
Dear all,
I have created a template marc_record.tpl which is as follows :
Code: Select all
<?php
$marc_template = <<<EOD
=LDR 00000nam 2200000Ia 4500
=008 YYMMDD?DAT1\\\\CNT\\\\\\\\\\\000\0\LNG\d
=020 \\$a{ISBN} :$c{PRICE}
=100 1\$a{AUTHOR_SURNAME, AUTHOR_FORE_NAME}
=245 10$a{BOOK_NAME}/$c{AUTHOR}
=250 \\$a{EDITION}
=260 \\$a{PUBLISHERADD} : $b{PUBLISHER},$c{DATED}
=300 \\$a\{PAGENO} ; $c{SIZE}
=500 \\$a{REMARKS}
=653 1\$a{SUBJECT}
=852 41$aMadan Puraskar Pustakalaya$ePatanDhoka Lalitpur,Nepal$j{SN}
EOD;
?>
Is the design of the template correct for using ?
My table design for the simple book archive looks like this.
Code: Select all
CREATE TABLE "tblmpp" (
"id" int(10) unsigned NOT NULL auto_increment,
"sn" varchar(255) default NULL,
"book" varchar(255) default NULL,
"author" varchar(255) default NULL,
"publisher" varchar(255) default NULL,
"publisheradd" varchar(255) default NULL,
"subject" varchar(255) default NULL,
"pageno" varchar(255) default NULL,
"size" varchar(255) default NULL,
"edition" varchar(255) default NULL,
"press" varchar(255) default NULL,
"pressadd" varchar(255) default NULL,
"price" varchar(255) default NULL,
"dated" varchar(255) default NULL,
"isbn" varchar(13) default NULL,
"remark" text,
PRIMARY KEY ("id")
) ENGINE=MyISAM AUTO_INCREMENT=23548 DEFAULT CHARSET=utf8
I want to replace each of the database field values into the template and generate marc standard records.
Please suggest.
Posted: Fri Dec 22, 2006 5:39 am
by Kieran Huggins
From:
http://www.php.net/manual/en/function.str-replace.php
PHP Manual wrote:As of PHP 4.0.5, every parameter in str_replace() can be an array.
Sounds like a good match, since you can fetch a row from the database as an assoc. array.
Code: Select all
foreach($row as $key=>$val) $find[]='{'.$key.'}';
$output = str_ireplace($find,$row,$template);
Cheers,
Kieran
Posted: Fri Dec 22, 2006 8:34 am
by dibyendrah
Thank you Kieran . I'll try that.
Posted: Sun Dec 24, 2006 12:22 am
by dibyendrah
I have made a small script to write a contents of a database into .mrk file. It works but I cannot write to file using utf-8 encoding.
Code: Select all
<?php
include("db.php");
include("marc.php");
$connection = sql_db_connect(HOST, USER, PASS, DB, PORT);
$sql = "select * from tblmpp limit 0, 10";
$rs = mysql_query($sql) or die(mysql_error());
$x = 1;
while($row = mysql_fetch_array($rs)){
$sn = $row['sn'];
$book = $row['book'];
$author = $row['author'];
$publisher = $row['publisher'];
$publisheradd = $row['publisheradd'];
$price = $row['price'];
$isbn = $row['isbn'];
$subject = $row['subject'];
$remarks = $row['remarks'];
$pageno = $row['pageno'];
$size = $row['size'];
$dated = $row['dated'];
$edition = $row['edition'];
$file_name = $x.".mrk";
$hnd = fopen($file_name, "w+");
$marctemplate = str_replace('{SN}', $sn, $marctemplate);
$marctemplate = str_replace('{BOOK}', $book, $marctemplate);
$marctemplate = str_replace('{AUTHOR}', $author, $marctemplate);
$marctemplate = str_replace('{PUBLISHER}', $publisher, $marctemplate);
$marctemplate = str_replace('{PUBLISHERADD}', publisheradd, $marctemplate);
$marctemplate = str_replace('{PRICE}', $price, $marctemplate);
$marctemplate = str_replace('{ISBN}', $isbn, $marctemplate);
$marctemplate = str_replace('{REMARKS}', $remarks, $marctemplate);
$marctemplate = str_replace('{DATED}', $dated, $marctemplate);
$marctemplate = str_replace('{EDITION}', $edition, $marctemplate);
$marctemplate = str_replace('{SUBJECT}', $subject, $marctemplate);
$marctemplate = str_replace('{PAGENO}', $pageno, $marctemplate);
$marctemplate = str_replace('{SIZE}', $size, $marctemplate);
file_put_contents($file_name, $marctemplate);
$x++;
}
?>
Can we create a file with utf-8 encoding ?
Please suggest.
Posted: Sun Dec 24, 2006 1:00 am
by dibyendrah
I added a UTF-8 header on the file but what utf8 functions do I need to use before using this header.
utf8_encode or utf_decode functions ?
Code: Select all
$marctemplate = "\xEF\xBB\xBF".$marctemplate;
Any suggestions ?
Posted: Sun Dec 24, 2006 1:23 am
by dibyendrah
Code: Select all
$marctemplate = <<<EOD
=LDR 00000nam 2200000Ia 4500
=008 YYMMDD?DAT1\\\\CNT\\\\\\\\\\\000\0\LNG\d
=020 \$a{ISBN} :\$c{PRICE}
=100 1\$a{AUTHOR_SURNAME, AUTHOR_FIRST_NAME}
=245 10\$a{BOOK}/\$c{AUTHOR}
=250 \$a{EDITION}
=260 \$a{PUBLISHERADD} : \$b{PUBLISHER},\$c{DATED}
=300 \$a\{PAGENO} ; \$c{SIZE}
=500 \$a{REMARKS}
=653 1\$a{SUBJECT}
=852 41\$aMadan Puraskar Pustakalaya\$ePatanDhoka Lalitpur,Nepal\$j{SN}
EOD;
In this template, $a,$b,$c are not actually a variable but are the mark standard which should not be replaced. Only the string constant in uppercase within the curly braces must be replaced.
A modified code is given below. Hoping to get your suggestions soon.
Code: Select all
<?php
header('Content-Type: text/html; charset=utf-8');
include("db.php");
//ini_set("default_charset", "utf-8");
$connection = sql_db_connect(HOST, USER, PASS, DB, PORT);
$sql = "select * from tblmpp";
$rs = mysql_query($sql) or die(mysql_error());
$x = 1;
while($row = mysql_fetch_array($rs)){
$sn = $row['sn'];
$book = $row['book'];
print $book."\n";
$author = $row['author'];
$author_array = explode(" ", $author);
$author_fname = $author_array[0];
unset($author_array[0]);
$author_lname = implode(" ", $author_array);
$publisher = $row['publisher'];
$publisheradd = $row['publisheradd'];
$price = $row['price'];
$isbn = $row['isbn'];
$subject = $row['subject'];
$remarks = $row['remarks'];
$pageno = $row['pageno'];
$size = $row['size'];
$dated = $row['dated'];
$edition = $row['edition'];
$file_name = $x.".mrk";
$hnd = fopen($file_name, "w+");
include("marc.tpl");
$marctemplate = "\xEF\xBB\xBF".$marctemplate;
$marctemplate = str_replace('{SN}', $sn, $marctemplate);
$marctemplate = str_replace('{BOOK}', $book, $marctemplate);
$marctemplate = str_replace('{AUTHOR}', $author, $marctemplate);
$marctemplate = str_replace('{AUTHOR}', $author, $marctemplate);
$marctemplate = str_replace('AUTHOR_SURNAME', $author_lname, $marctemplate);
$marctemplate = str_replace('AUTHOR_FIRST_NAME', $author_fname, $marctemplate);
$marctemplate = str_replace('{PUBLISHER}', $publisher, $marctemplate);
$marctemplate = str_replace('{PUBLISHERADD}', $publisheradd, $marctemplate);
$marctemplate = str_replace('{PRICE}', $price, $marctemplate);
$marctemplate = str_replace('{ISBN}', $isbn, $marctemplate);
$marctemplate = str_replace('{REMARKS}', $remarks, $marctemplate);
$marctemplate = str_replace('{DATED}', $dated, $marctemplate);
$marctemplate = str_replace('{EDITION}', $edition, $marctemplate);
$marctemplate = str_replace('{SUBJECT}', $subject, $marctemplate);
$marctemplate = str_replace('{PAGENO}', $pageno, $marctemplate);
$marctemplate = str_replace('{SIZE}', $size, $marctemplate);
file_put_contents($file_name, $marctemplate);
$x++;
fclose($hnd);
}
?>
Posted: Sun Dec 24, 2006 2:37 am
by Kieran Huggins
Someone in the manual suggested using mb_convert_encoding() instead. In fact, the mbstring functions are for multi-byte character encoding. You can't use some of the regular string functions, you'll need to use these instead.
According to Cal Henderson (in his excellent O'Reilly book "Building Scalable Web Sites") you should leave the Byte-Order-Mark out altogether with UTF-8, since it causes problems and UTF-8 is compatible with Latin-1.
If you're going to be doing a lot of unicode work, you may want to get the book I mentioned. It's just awesome.
Cheers,
Kieran
Posted: Sun Dec 24, 2006 4:43 am
by dibyendrah
Thanks Kieran for your valuable suggestions. But I have already converted the output strings to UTF-8 through
I didn't have to convert the encoding into UTF-8 using mbstring.
Posted: Sun Dec 24, 2006 6:02 am
by Kieran Huggins
Sorry - I wasn't very clear: the BOM (\xEF\xBB\xBF) makes browsers unhappy.
Code: Select all
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
is enough for most browsers, I think. the header equivalent you're sending is probably enough as well. In fact, I think the BOM is rendered in Firefox
Also, I can't help but notice you're doing a lot manually... How about:
Code: Select all
header('Content-Type: text/html; charset=utf-8');
include("db.php");
$connection = sql_db_connect(HOST, USER, PASS, DB, PORT);
$sql = "select * from tblmpp";
$rs = mysql_query($sql) or die(mysql_error());
// don't include the same file a zillion times - you only need it once
$marctemplate = <<<EOD
=LDR 00000nam 2200000Ia 4500
=008 YYMMDD?DAT1\\\\CNT\\\\\\\\\\\000\0\LNG\d
=020 \$a{ISBN} :\$c{PRICE}
=100 1\$a{AUTHOR_SURNAME, AUTHOR_FIRST_NAME}
=245 10\$a{BOOK}/\$c{AUTHOR}
=250 \$a{EDITION}
=260 \$a{PUBLISHERADD} : \$b{PUBLISHER},\$c{DATED}
=300 \$a\{PAGENO} ; \$c{SIZE}
=500 \$a{REMARKS}
=653 1\$a{SUBJECT}
=852 41\$aMadan Puraskar Pustakalaya\$ePatanDhoka Lalitpur,Nepal\$j{SN}
EOD;
$x='0';
while($row = mysql_fetch_array($rs)){
print $row['book']."\n";
// easier than what you were doing
list($row['AUTHOR_FIRST_NAME'],$row['AUTHOR_SURNAME']) = explode(' ',$row['author'],'2');
// make a list of tokens to replace:
foreach($row as $key=>$val) $find[]='{'.$key.'}';
// str_ireplace is a case insensitive version of str_replace:
$output = str_ireplace($find,$row,$marctemplate);
// you don't need to use fopen - check the manual.
// The x++ will return the original value for x, then increment it.:
file_put_contents($x++.'.mrk', $output);
}
// have a kit-kat
You'll need to test it, as I don't have your data.
Cheers,
Kieran
Posted: Mon Dec 25, 2006 12:03 am
by dibyendrah
Thanks once again Kieran for your valuable suggestion and your clean code. Let me try your code and let you know if that worked or not.
Posted: Mon Dec 25, 2006 4:20 am
by dibyendrah
Hello Kieran,
I tried your code and I think output from your code needs some modification as it didn't replaced author surname, first name, remarks. Others are just fine.
Output from your code is
Code: Select all
=LDR 00000nam 2200000Ia 4500
=008 YYMMDD?DAT1\\\\CNT\\\\\\\\\\\000\0\LNG\d
=020 $a :$c
=100 1$a{AUTHOR_SURNAME, AUTHOR_FIRST_NAME}
=245 10$aकेही कहानी/$cभेशप्रसाद शर्मा
=250 $aपहिलो
=260 $aनेपाल : $b,$c२००९
=300 $a\५० ; $c१/१६ ड.क्रा<200c>.
=500 $a{REMARKS}
=653 1$aकथाहरु
=852 41$aMadan Puraskar Pustakalaya$ePatanDhoka Lalitpur,Nepal$j३
My previous output was :
Code: Select all
=LDR 00000nam 2200000Ia 4500
=008 YYMMDD?DAT1\\\\CNT\\\\\\\\\\\000\0\LNG\d
=020 $a :$c
=100 1$a{शर्मा, भेशप्रसाद}
=245 10$aकेही कहानी/$cभेशप्रसाद शर्मा
=250 $aपहिलो
=260 $aनेपाल : $b,$c२००९
=300 $a\५० ; $c१/१६ ड.क्रा<200c>.
=500 $a
=653 1$aकथाहरु
=852 41$aMadan Puraskar Pustakalaya$ePatanDhoka Lalitpur,Nepal$j३
I'll try to modify as per needed and post here in this thread.
Posted: Mon Dec 25, 2006 4:22 am
by dibyendrah
As we are storing records of Nepali books the output is in Nepali language.
Posted: Thu Dec 28, 2006 3:01 am
by Kieran Huggins
That's a nice looking language!
surname, first name can be fixed by modifying the tamplate:
Code: Select all
=100 1$a{AUTHOR_SURNAME}, {AUTHOR_FIRST_NAME}
As far as I can tell, remarks
should work! Make sure the database field name is correct.. you can use a print_r($row) before you do the str_ireplace() to help see what you're actually replacing.
Posted: Thu Dec 28, 2006 9:43 am
by dibyendrah
Oh Yes. I didn't notice that. That will surely fix the problem.