Page 1 of 1

Varible help

Posted: Fri Jul 23, 2004 2:51 am
by ek5932
On my website I want the users to be able to display information from my database without having to use php but varibles. I have no clue how to do this but im sure you need to set the varibles first and need some help in doing it. The users have the option to edit the code on the site and to display the information they can use tags like [posts] how would i do this?

thanks

Re: Varible help

Posted: Fri Jul 23, 2004 8:44 am
by hawleyjr
ek5932 wrote:I want the users to be able to display information from my database without having to use php but varibles.
You will not be able to make a database connection without some type of server side scripting (PHP,JSP,ASP) Another alternative could be to use Macromedia Flash, but based off of what your saying that solution may not be feasible.

Posted: Fri Jul 23, 2004 10:22 am
by evilmonkey
Okay, the only reason you'd need PHP is to get the info from the database. Before you can use your variables, they must be given values. In order for your variables to be given values, those values need to come from somwhere. BOOM, the database. Assuming you are using MySQL, you can do something like this:

Code: Select all

<?php
//establish a MySQL connection
$db = mysql_connect("localhost", "username", "password");
mysql_select_db("your_database_name", $db);

//set up a query (request to the database) for your info
$query = "SELECT list_fields_here_or _use_*_for_all_fields FROM yourtable WHERE " //set up a condition, such as $foo='bar'
//execute your request
$result = mysql_query($query, $db);
//now you have the result, but you need to be able to access it through a variable. You also need a loop in case there is more than one result:
while ($row=mysql_fetch_array($result)){
//here you can do whatever you want, my guess would be output the data.
//you can do it like this: (let's say you want a table)
?>
<table>
<tr>
<td><?php echo $row['your_field_name']; ?></td>
</tr>
<?php 
//the above will ctreate a table of all the values of one column gotten from the database.
}
?>
If you need more help, I suggest reading a PHP-MySQL tutorial, or better yet, book.

Good luck!

Posted: Fri Jul 23, 2004 5:14 pm
by ek5932
No I mean like set the php varible as some other sort of code.


eg $php_varible='[posts]'


then when displaying the information you just need to code [posts]

Posted: Fri Jul 23, 2004 5:19 pm
by hawleyjr
Try this:

Page 1 form:

Code: Select all

&lt;form name="form1" method="post" action=""&gt;
&lt;input type="text" name="textfield"&gt;
&lt;input type="checkbox" name="checkbox" value="checkbox"&gt;
&lt;input type="radio" name="radiobutton" value="radiobutton"&gt;
&lt;/form&gt;
Page 2:

Code: Select all

<?php
echo '<PRE>'; print_r($_POST); echo '</PRE>';

echo $_POST['textfield'].'<BR>';
echo $_POST['checkbox'].'<BR>';
echo $_POST['radiobutton'].'<BR>';

//The variables can also be accessed without the $_POST

echo $textfield.'<BR>';
echo $checkbox.'<BR>';
echo $radiobutton.'<BR>';


?>

Posted: Fri Jul 23, 2004 6:12 pm
by m3mn0n
The variables can also be accessed without the $_POST
Only with register_globals turned ON, it's OFF by default.

Posted: Fri Jul 23, 2004 6:18 pm
by ek5932
Grr no one seems to get what I mean. I have seen it on a site before. Basicly what I am trying to do is create a script where the user can edit the content of the page using a text field but instead of allowing them to post the php instead using my own language which just refers to the php. Eg if the user was to insert

<td>Username</td><td>Posts</td>
</tr><tr>
<td>[username]</td><td>[posts]</td>



[username] would be <? echo ("$username"); ?> but the users see it and edit it in my own sort of language.

Posted: Fri Jul 23, 2004 6:58 pm
by genetix
I understand what your trying to do but I would suggest your learn how to do it instead of asking the people here to do it for you. Correct me if I'm wrong but thats what it looks like.

I aint trying to be mean I'm just saying you would be better off learning how it is done.

Posted: Sat Jul 24, 2004 2:42 am
by feyd
ek5932, you need to look into template engines.. like Smarty, or uh, I can't think of some others at the moment. We've had a discussion or three about template engines here and potentially here.