954,576 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to pass variables in php

I am a total noob at php and I am creating a basic wordpress plugin for a site.
I have a function

function My_Function_2() {
?>


Then I have an input inside a div coming from an options page:

<form method="post" action="options.php">
<?php wp_nonce_field('update-options'); ?>
Define Box height
<table width="350">
<tr valign="top">
<th width="200" scope="row">Enter Box Height</th>
<td width="50">
<input name="Box_height" type="text" id="Box_height"
value="<?php echo get_option('Box_height'); ?>" />
(ex. 400)</td>
</tr>
</table>

But when I try to $box_height to use in a url later on, it doesnt work.
If I set box_height to a value using an array it works. So I think I need to make the input variable into a Global Variable somehow?
Sorry if I havent made this very clear, im not very well experienced

Acids
Newbie Poster
9 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

I am a total noob at php and I am creating a basic wordpress plugin for a site. I have a function

function My_Function_2() {
?>

Then I have an input inside a div coming from an options page:

<form method="post" action="options.php">
<?php wp_nonce_field('update-options'); ?>
Define Box height
<table width="350">
<tr valign="top">
<th width="200" scope="row">Enter Box Height</th>
<td width="50">
<input name="Box_height" type="text" id="Box_height"
value="<?php echo get_option('Box_height'); ?>" />
(ex. 400)</td>
</tr>
</table>

But when I try to $box_height to use in a url later on, it doesnt work. If I set box_height to a value using an array it works. So I think I need to make the input variable into a Global Variable somehow? Sorry if I havent made this very clear, im not very well experienced

Where did you put the variable? Session? Cookie? Database? If you don't put the variable somewhere, the only page that can use it is the page that your form posted it to.

MagicMedia
Junior Poster
149 posts since Jul 2010
Reputation Points: 20
Solved Threads: 35
 

Thanks for your reply.

Wordpress stores the value in a database. I have a database field in the coding too. So if I put in a value and save it. I can come back the the page later and the value will still be there. It doesnt use cookies.
Are variables in functions global by default or do I have to set them that way?

Acids
Newbie Poster
9 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

use a seesion or cookie to do this.....

tomato.pgn
Posting Whiz in Training
262 posts since Mar 2011
Reputation Points: 4
Solved Threads: 32
 

But The settings for the height are to be done by an admin as they are settings for a plugins size. Cookies an sessions are for users only right?

Acids
Newbie Poster
9 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

Did you grab the variable from the database first? You need to ask the database for the data before you can use it. You have to grab it for every page that wants to use the variable.

MagicMedia
Junior Poster
149 posts since Jul 2010
Reputation Points: 20
Solved Threads: 35
 

ahh ok. Is there anyway that I can do this with a very short amount of code? Because I will be calling many variables from the database.
Thank you

Acids
Newbie Poster
9 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 
ahh ok. Is there anyway that I can do this with a very short amount of code? Because I will be calling many variables from the database. Thank you

That depends on your database. A few lines will usually do the trick.

// open mysql connection

$row = mysql_fetch_array(mysql_query('SELECT column FROM table'));
$column = $row['column'];
MagicMedia
Junior Poster
149 posts since Jul 2010
Reputation Points: 20
Solved Threads: 35
 

Thanks alot..
But I have messed something up and the Database I thought was created isnt actually there.
I used the code

<?php
/* Runs when plugin is activated */
register_activation_hook(__FILE__,'hello_world_install'); 

function hello_world_install() {
/* Creates new database field */
add_option("hello_world_data", 'Default', '', 'yes');
}


?>

Mine just isnt called hello world. Am I supposed to use this code as it is? Or am I supposed to remove the first section?

Acids
Newbie Poster
9 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 
Zero13
Practically a Master Poster
624 posts since Jan 2009
Reputation Points: 120
Solved Threads: 139
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: