Variable question, yet another
| From: | King, Justin | Date: | Tue, 08 May 2001 18:20:49 +0000 |
| Subject: | Variable question, yet another | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-51951@lists.php.net to get a copy of this message | ||
Alright... here's my situation for those that didn't read my string of
messages yesterday.. (plus a bit more background information as to why
I'm doing this)
I'm writing an engine for a content managing system. Part of the system
is a menu builder. I use 2 tables, (this is a mysql database) menu and
menu_parts. They look like this
Table menu
ID : Integer; (auto increments, key)
Name : Varchar[128];
Type : Integer; (unused now)
Table menu_parts
ID : Integer;
Menu : Integer; (which menu this belongs to)
Order : Tinyint; (all menu items have incremental numbers)
Type : Tinyint;
Data : Text;
My code looks like this
[code]
$query="SELECT type,data FROM menu_parts WHERE menu='1' ORDER BY
m_order";
$result=mysql_query($query);
if($myrow=mysql_fetch_array($result))
{ do {
if($myrow["type"]==1)
eval("echo
\"".$myrow["data"]."\";");
elseif($myrow["type"]==2)
eval($myrow["data"]);
else
echo $myrow["data"];
} while($myrow=mysql_fetch_array($result));
}
[/code]
So depending on the menu_parts type, it will display the data field
differently.
If the type=0, it simple outputs the data field plain text.
If the type=1, it adds echo to both sides and outputs data.
If the type=2, it evaluates the code as is.
The problem I'm having, is with type 1. The main reason for this type
is say I want to send the user to a php script and pass parameters (I'm
going to use []'s so those of you with html email readers don't parse
it)
[a href="myscript.php?username={$userdata["username"]}"]Goto
MyScript.php[/a]
So when the line hits type 1, it evals like this
echo "[a href="myscript.php?username={$userdata["username"]}"]Goto
MyScript.php[/a]";
Of course this yields an error because of the "'s in the anchor tag.
But if I add slashes the evaluated code will be
echo "[a href=\"myscript.php?username={$userdata[\"username\"]}\"]Goto
MyScript.php[/a]";
Which will cause the $userdata["username"] variable to not process
properly. I thought about making type one do echo ''; instead of echo
""; but then if the user has a ' in their menu it won't function
properly.
I wish I could do this with regular expressions, but I'm not fluent with
them (and also doubt I could succeed in my task if I did).
I think I described my problem as thorough as I could have. Any help,
comments, etc would be greatly appreciated.
-Justin