Jump to content

Archived

This topic is now archived and is closed to further replies.

almightyegg

php in a mysql database

Recommended Posts

i want to be able to update links quickly and easily so id only have to change 1 page, (well 1 row of a database)

so i added code to all pages saying to fetch this row of a database and it won't read any php inside it :(

it will read html links but not the '<? echo "$mem[id]"; ?>' etc... any ideas? it will make my life so much simpler :D

Share this post


Link to post
Share on other sites
when you store PHP within a database and pull it out, you may need to run the eval() function on the string. this then evaluates the PHP in the string and runs that code.

Share this post


Link to post
Share on other sites
never done the eval() func before ???

something like:
<?
eval('$qlink[quicklinks]');
echo "$qlink[quicklinks]";
?>

??

Share this post


Link to post
Share on other sites
maybe you should show us what your $qlink['quicklinks'] might hold. eval() is a very touchy function, and should be used sparingly, so let's break it down a little further, and i'll see if i can help you with an optimal solution. i'll need a little more information, though:

1) what is the code in the database supposed to accomplish?

2) how are you using the PHP code you pull from the database on the page?

Share this post


Link to post
Share on other sites
it pulls out code for the quicklinks eg.
<a href="blah.html">link</a><----that comes out fine
<a href="link.php?<? echo "$mem[id]"; ?>">link2</a> <----that doesnt...


so basically i add :
<?
echo "qlink[quicklinks]";
?>
to every page and then i only have to edit the database when i change things...

i think that answers both questions???


Share this post


Link to post
Share on other sites
hmm... seems like you'd be better off using a sort of templating scheme (or else changing how you store the code in the DB). here's what i would suggest, in order of preference:
[code]
<?php
// first choice:
// create a template for your ID in the database:
// database entry: <a href="link.php?{MEM_ID}">link</a>
$qlink['quicklinks'] = preg_replace('|\{MEM_ID\}|', $mem['id'], $qlink['quicklinks']);
echo $qlink['quicklinks'];
?>
[/code]

basically, this option creates a template tag "{MEM_ID}" that would be in your quicklink in the database, and you would simply replace that tag with your query result.

option 2:
[code]
<?php
// database entry: echo "<a href=\"link.php?$mem[id]\">link</a>";
eval($qlink['quicklinks']);
?>
[/code]

notice how i actually would record the entire link as one echo statement in the database. this would allow me to run eval on the statement and output my result.

does this help some?

Share this post


Link to post
Share on other sites
what if i have 2 different php bits
$mem[id] and $mem[username]
would you change it to:
$qlink['quicklinks'] = preg_replace('|\{MEM_ID\}|', $mem['id'],  '|\{MEM_USERNAME}\|', $mem['username'], $qlink['quicklinks']);

Share this post


Link to post
Share on other sites
preg_replace() takes arrays as the arguments for the changes, so you'd do something like this:
[code]
<?php
$tags = array('|\{MEM_ID\}|', '\{MEM_USERNAME\}|');
$vals = array($mem['id'], $mem['username']);
$qlink['quicklinks'] = preg_replace($tags, $vals, $qlink['quicklinks']);
?>
[/code]

make sense?

Share this post


Link to post
Share on other sites
[quote author=almightyegg link=topic=112118.msg454967#msg454967 date=1161355540]
right now it shows all info plus {} around those bits we changed
[/quote]

??? i don't follow. can you post the string before and after the change? basically, do this:
[code]
<?php
echo "Before: $qlink[quicklinks]<br /><br />\n";
$qlink['quicklinks'] = preg_replace($tags, $vals, $qlink['quicklinks']);
echo "After: $qlink[quicklinks]<br />\n";
?>
[/code]

this way, i can see exactly what's being changed.

Share this post


Link to post
Share on other sites
Before: {MEM_USERNAME}'s House
Iridia Central
My Clutch
Mine Shaft
Portal
Battle
Arena
Message Board
My Account
Logout


After: {Lord of the Abyss}'s House
Iridia Central
My Clutch
Mine Shaft
Portal
Battle
Arena
Message Board
My Account
Logout

Share this post


Link to post
Share on other sites
that is strange... can you post your code for your preg_replace()? just want to double check your syntax on the patterns. if you'd rather, you could probably use str_replace() a little more easily:
[code]
<?php
$tags = array('{MEM_ID}', '{MEM_USERNAME}');
$vals = array($mem['id'], $mem['username']);
$qlink['quicklinks'] = str_replace($tags, $vals, $qlink['quicklinks']);
?>
[/code]

does that help?

Share this post


Link to post
Share on other sites
yay the str replace worked :D

my code before with preg :)
$tags = array('{MEM_ID}', '{MEM_USERNAME}', '{MEM_CLUTCH}');
$vals = array($mem['id'], $mem['username'], $mem[clutch]);
$qlink['quicklinks'] = preg_replace($tags, $vals, $qlink['quicklinks']);

thanks for the help :D

Share this post


Link to post
Share on other sites
just for reference, the reason your code with preg_match() wasn't working is because you needed to escape the brackets to get them to match:
[code]
<?php
$tags = array('|\{MEM_ID\}|', '|\{MEM_USERNAME\}|', '\{MEM_CLUTCH\}|');
?>
[/code]

glad you got it working!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.