Varible help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ek5932
Forum Commoner
Posts: 25
Joined: Mon Jun 28, 2004 5:55 pm

Varible help

Post 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
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Re: Varible help

Post 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.
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post 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!
ek5932
Forum Commoner
Posts: 25
Joined: Mon Jun 28, 2004 5:55 pm

Post 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]
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post 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>';


?>
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

The variables can also be accessed without the $_POST
Only with register_globals turned ON, it's OFF by default.
ek5932
Forum Commoner
Posts: 25
Joined: Mon Jun 28, 2004 5:55 pm

Post 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.
User avatar
genetix
Forum Contributor
Posts: 115
Joined: Fri Aug 01, 2003 7:40 pm
Location: Sask, Regina
Contact:

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Post Reply