almightyegg Posted October 20, 2006 Share Posted October 20, 2006 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 Quote Link to comment Share on other sites More sharing options...
obsidian Posted October 20, 2006 Share Posted October 20, 2006 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. Quote Link to comment Share on other sites More sharing options...
almightyegg Posted October 20, 2006 Author Share Posted October 20, 2006 never done the eval() func before ???something like:<?eval('$qlink[quicklinks]');echo "$qlink[quicklinks]";?>?? Quote Link to comment Share on other sites More sharing options...
obsidian Posted October 20, 2006 Share Posted October 20, 2006 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? Quote Link to comment Share on other sites More sharing options...
almightyegg Posted October 20, 2006 Author Share Posted October 20, 2006 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??? Quote Link to comment Share on other sites More sharing options...
obsidian Posted October 20, 2006 Share Posted October 20, 2006 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? Quote Link to comment Share on other sites More sharing options...
almightyegg Posted October 20, 2006 Author Share Posted October 20, 2006 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']); Quote Link to comment Share on other sites More sharing options...
obsidian Posted October 20, 2006 Share Posted October 20, 2006 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? Quote Link to comment Share on other sites More sharing options...
almightyegg Posted October 20, 2006 Author Share Posted October 20, 2006 right now it shows all info plus {} around those bits we changed Quote Link to comment Share on other sites More sharing options...
obsidian Posted October 20, 2006 Share Posted October 20, 2006 [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]<?phpecho "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. Quote Link to comment Share on other sites More sharing options...
almightyegg Posted October 20, 2006 Author Share Posted October 20, 2006 Before: {MEM_USERNAME}'s HouseIridia CentralMy ClutchMine ShaftPortalBattleArenaMessage BoardMy AccountLogoutAfter: {Lord of the Abyss}'s HouseIridia CentralMy ClutchMine ShaftPortalBattleArenaMessage BoardMy AccountLogout Quote Link to comment Share on other sites More sharing options...
obsidian Posted October 20, 2006 Share Posted October 20, 2006 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? Quote Link to comment Share on other sites More sharing options...
almightyegg Posted October 20, 2006 Author Share Posted October 20, 2006 yay the str replace worked :Dmy 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 Quote Link to comment Share on other sites More sharing options...
obsidian Posted October 20, 2006 Share Posted October 20, 2006 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.