By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,521 Members | 1,460 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,521 IT Pros & Developers. It's quick & easy.

mysql related question

P: n/a
Hi group,

just wondering:

I have a database, containing the following fields:

name
address
zip
email

and a template for my page, containing the following tags:

[[name]] [[address]] [[zip]] [[email]]

Would there be a quick way to replace those tags with the data read from the
database without doing this:

$line=mysql_get_row($res) ; // provided we already have connection and data
selected
$html_to_show=preg_replace("/\[\[name]]/",$line[0],$html_to_show);
$html_to_show=preg_replace("/\[\[address]]/",$line[1],$html_to_show);
$html_to_show=preg_replace("/\[\[zip]]/",$line[2],$html_to_show);
$html_to_show=preg_replace("/\[\[email]]/",$line[3],$html_to_show);

(never mind the preg here or any typo's. this is a quick question)

Reason for me to ask, though code above works:
I need to create something dynamic. I would like to scan and replace every
instance in my template, looking like a tag ( [[some_tag_name]] ) by
the value of it's corresponding fieldname from my table.
I am really wondering how, but there's got to be an easier way to do this.
Currently I have 61 fields in one table and that kinda works towards RSI if
I have to code it all like this.
Anyone with a good suggestion, apart from reading the manual? (I am already
doing that)

Thank you very much in advance,

Michel

PS: sorry for double posting this.
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
maybe instead of building your own template system (based on preg_replace)
try to use some simple (!) existing one: try Xipe
(PEAR::HTML_temnplate_Xipe). It works this way that you don't operate on a
bunch of $template->setValue($someValue), but simply auto-input the values
of the template-variables with existsing-ones in php. This means that you
can use php-arrays as well in the template.

i.e. in the template file you can have:

(...SOME HTML...)<strong><{$_myArray['name']}</strong>(...SOME HTML...)

and on the php code BEFORE compiling this template you have to create an
array $_myArray with at last one value $_myArray['name']. This can be done
ie with:

while ($row = mysql_fetch_rows($result)) {

$_myArray['name'] = $row[0];
// etc.
}

cheers
andrew
Hi group,

just wondering:

I have a database, containing the following fields:

name
address
zip
email

and a template for my page, containing the following tags:

[[name]] [[address]] [[zip]] [[email]]

Would there be a quick way to replace those tags with the data read from
the database without doing this:

$line=mysql_get_row($res) ; // provided we already have connection and
data selected
$html_to_show=preg_replace("/\[\[name]]/",$line[0],$html_to_show);
$html_to_show=preg_replace("/\[\[address]]/",$line[1],$html_to_show);
$html_to_show=preg_replace("/\[\[zip]]/",$line[2],$html_to_show);
$html_to_show=preg_replace("/\[\[email]]/",$line[3],$html_to_show);

(never mind the preg here or any typo's. this is a quick question)

Reason for me to ask, though code above works:
I need to create something dynamic. I would like to scan and replace every
instance in my template, looking like a tag ( [[some_tag_name]] ) by
the value of it's corresponding fieldname from my table.
I am really wondering how, but there's got to be an easier way to do this.
Currently I have 61 fields in one table and that kinda works towards RSI
if I have to code it all like this.
Anyone with a good suggestion, apart from reading the manual? (I am
already doing that)

Thank you very much in advance,

Michel

PS: sorry for double posting this.


Jul 17 '05 #2

P: n/a
Hi dude, thanks for responding.
trick though is that the time I need to start using these classes, is as
good as the time it takes to do this myself, probably, with some info.
Latter, though sticks in my mem and I would have actually learned a deal.

Thanks for pointing me to this solution though. never hurts to see more
resources.

Mich

"Andrzej Bednarczyk" <an*****@kreo9.NOSPAM.com> wrote in message
news:40******@news.home.net.pl...
maybe instead of building your own template system (based on preg_replace)
try to use some simple (!) existing one: try Xipe
(PEAR::HTML_temnplate_Xipe). It works this way that you don't operate on a
bunch of $template->setValue($someValue), but simply auto-input the values
of the template-variables with existsing-ones in php. This means that you
can use php-arrays as well in the template.

i.e. in the template file you can have:

(...SOME HTML...)<strong><{$_myArray['name']}</strong>(...SOME HTML...)

and on the php code BEFORE compiling this template you have to create an
array $_myArray with at last one value $_myArray['name']. This can be done
ie with:

while ($row = mysql_fetch_rows($result)) {

$_myArray['name'] = $row[0];
// etc.
}

cheers
andrew
Hi group,

just wondering:

I have a database, containing the following fields:

name
address
zip
email

and a template for my page, containing the following tags:

[[name]] [[address]] [[zip]] [[email]]

Would there be a quick way to replace those tags with the data read from
the database without doing this:

$line=mysql_get_row($res) ; // provided we already have connection and
data selected
$html_to_show=preg_replace("/\[\[name]]/",$line[0],$html_to_show);
$html_to_show=preg_replace("/\[\[address]]/",$line[1],$html_to_show);
$html_to_show=preg_replace("/\[\[zip]]/",$line[2],$html_to_show);
$html_to_show=preg_replace("/\[\[email]]/",$line[3],$html_to_show);

(never mind the preg here or any typo's. this is a quick question)

Reason for me to ask, though code above works:
I need to create something dynamic. I would like to scan and replace every instance in my template, looking like a tag ( [[some_tag_name]] ) by the value of it's corresponding fieldname from my table.
I am really wondering how, but there's got to be an easier way to do this. Currently I have 61 fields in one table and that kinda works towards RSI
if I have to code it all like this.
Anyone with a good suggestion, apart from reading the manual? (I am
already doing that)

Thank you very much in advance,

Michel

PS: sorry for double posting this.

Jul 17 '05 #3

P: n/a
I use something like the following:
$pageoutput = preg_replace('/\[\[\s*(.*?)\s*\]\]>/ie',"get_replacement('\\1')",
$template);
// I think the "]" needs escaped? ie "\]" above?
// you just had "]"... I could be wrong
function get_replacement($what)
{
/*
obviously here is where you would lookup "what"
in your database or whatever
*/
return $what;
}
Jul 17 '05 #4

P: n/a
Aye Brad,

Thanks for your input. The brackets " ] " do not need to be pre-ed by \,
only the left brackets do " [ " needs to be "\[".....

I will have a look at what you're suggesting and get back with results.....

Mich

"Brad Kent" <bk***********@yahoo.com> wrote in message
news:7a*************************@posting.google.co m...
I use something like the following:
$pageoutput = preg_replace('/\[\[\s*(.*?)\s*\]\]>/ie',"get_replacement('\\1')", $template);
// I think the "]" needs escaped? ie "\]" above?
// you just had "]"... I could be wrong
function get_replacement($what)
{
/*
obviously here is where you would lookup "what"
in your database or whatever
*/
return $what;
}

Jul 17 '05 #5

P: n/a

"michel" <no@spam.please> wrote in message
news:ca**********@news.cistron.nl...
Hi group,

just wondering:

I have a database, containing the following fields:

name
address
zip
email

and a template for my page, containing the following tags:

[[name]] [[address]] [[zip]] [[email]]

Would there be a quick way to replace those tags with the data read from the database without doing this:

$line=mysql_get_row($res) ; // provided we already have connection and data selected
$html_to_show=preg_replace("/\[\[name]]/",$line[0],$html_to_show);
$html_to_show=preg_replace("/\[\[address]]/",$line[1],$html_to_show);
$html_to_show=preg_replace("/\[\[zip]]/",$line[2],$html_to_show);
$html_to_show=preg_replace("/\[\[email]]/",$line[3],$html_to_show);

(never mind the preg here or any typo's. this is a quick question)

Reason for me to ask, though code above works:
I need to create something dynamic. I would like to scan and replace every
instance in my template, looking like a tag ( [[some_tag_name]] ) by
the value of it's corresponding fieldname from my table.
I am really wondering how, but there's got to be an easier way to do this.
Currently I have 61 fields in one table and that kinda works towards RSI if I have to code it all like this.
Anyone with a good suggestion, apart from reading the manual? (I am already doing that)


strtr() is probably the best function to use in this case.

$row = mysql_fetch_assoc($res);
foreach($row as $name => $value) {
$tr["[[{$name}]]" = htmlspecialchars($value);
}
$html_to_show = strtr($html_to_show, $tr);
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.