Jump to content

php in a mysql database


almightyegg

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
Link to comment
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?
Link to comment
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???


Link to comment
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?
Link to comment
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?
Link to comment
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.
Link to comment
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?
Link to comment
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
Link to comment
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!
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.