VeggieBoards banner
1 - 17 of 17 Posts

·
Banned
Joined
·
10,763 Posts
Discussion Starter · #1 ·
Oh darn. I see how to get a variable value from an html form, into a new file. You do or you could use get instead of post. Then the variables will be available in file2.php. But how do I get these same variable, and any new variables I created in file2, file3 -- without using an html form, just a php function or whatever?

If there is an html markup code, that you can use to get a variable from an html file to be available for use in another file, a php file, you would think that there would be a way to do this with a php function, too.

I looked at lots of functions on php.net, but I can't find one that will do the job. Don't tell me I have to open up an sql database and put value into a table. It seems like a lot of computing power to be using, to do to make a value from one php script, available in another.
 

·
Registered
Joined
·
1,182 Posts
Information sent to a PHP script is only available to that script at the time you ran it unless you store it somewhere. I recommend looking into cookies and sessions.

~Wonder
 

·
Banned
Joined
·
10,763 Posts
Discussion Starter · #3 ·
Actually, I finally figured out how to get a variable from one script to another, without using an html form. You just put them in an using a question mark after the file name.

Like this:

]click here"

You can put more than one variable after the question mark, using and ampersand I think it is.
 

·
Registered
Joined
·
1,182 Posts
That's the get method, and yes, use an & between variables. Be careful you don't put store sensitive information such as passwords in the query. Search engines, among other things, have a tendency to find out.

~Wonder
 

·
Banned
Joined
·
10,763 Posts
Discussion Starter · #5 ·
I don't understand why they didn't put a function in php which allows you to pass variables to a new file that you open from a previous file, without having them be displayed in the address box.? There is a method to do this in html - html forms with method POST - so why did they neglect to do this in php? You can pass variables to an include(d) file or require(d) file. They are passed automatically, without you having to do anything. Just not to a new file.

I wonder if there might be some javascript that can be used to get variables into a new php file, when you click on a link to the new file from the old php file. Although I would rather do everything in a single program.

Maybe the easiest thing to do is to just create a mysql database and put things into mysql tables, instead of using php variables?
 

·
Banned
Joined
·
10,763 Posts
Discussion Starter · #7 ·
I thought sessions were a method of storing information on the client computer. That seems a rather unnecessarily elaborate way to pass a variable from one part of a program to another, when the 2 parts of the program are in different files. MySQL, in contrast, is on the server.

Here is what I want to do. I want to first put all 88 pitches on a piano into an array. Then, in the same php file, i want to display the array. This would be when the visitor is asked "would you like to see a chart showing the frequency of all the notes on an (equally tempered) piano." Then I want to ask the visitor, "would you like, now, to see a chart showing the beat rates that are produced by their harmonics, when 2 notes, a specific number of half steps apart, are played together. For example lets do fifths, lets show note 1 and 8, note 2 and 9, note 3 and 10, etcetera, all the way up to notes 81 and 88. The chart would show the pitch of the low note, the high note, the 3rd harmonic of the low note, the second harmonic of the high note, and the difference in frequency (the beat rate) when the 3rd harmonic of the lower note is subtracted from the 2d harmonic of the upper note. I would do the same thing, show another page, for fourths, for notes 1 and 6, notes 2 and 7... notes 83 and 88. Also for thirds, minor thirds, etcetera. Click on the link to fifths, fourths, thirds... etcetera." For each interval, I would like a link that takes the user to a new php file, that produces the selected chart.

For whatever new new file the user selcts, I don't want to recalculate the pitch of note 1 and 8, 2 and 9, 3 and 10, etcetera. I just want to calculate the 3rd harmonic of note 1 and the second harmonic of note 8, and the difference, on one line, on the next line I would like to calculate the third harmonic of note 2 and the second harmonic of note 9, and the difference, etcetera. A line for each interval. But I want to pick up the values of the fundamental from the array, instead of wasting time re-doing the calculation. Worse yet, would be wasting time sending the array to the the visitor's computer, and picking up the values from there. There is no need to pass the value to the client, then back again to the server, when everything can be done on the server. It is just a matter of accessing the array. Yes, if I make the chart in file2 an "include" of file1, the array is automatically available in file2. But I don't want to re-display file1; i just want to display file2.

Yes I could put everything in one file, and use an if-then control structure, or switch, case1, case2.... to display only the chart that needs to be displayed. But I like to put my separate programming subroutines into separate chunks in separate files, for both ease of understanding, and ease of editing. For example if I decide I don't want to have minor thirds any more, I don't have to find the correct subroutine in a long file; I just remove the whole minor third file. Later, if I have time, I can remove the link to it, in the show me 88 pitches file.

I have already made an Excel spreadsheet to do this, and a (single) basic program that does this. Now, learning php, I want to do the same thing in php. In Excel, I have each beatrate chart on a separate "sheet." I gave the array elements names, and I refer to the names, instead of re-calculating the pitch of any note. I know that in Lotus 1-2-3 I could have done the same thing, or I could have put each sheet in a different file, and linked the cells in each beatrate file to the approprite cells in the pitch file. I presume I can do the same thing with Excel. That is, you can refer not only to a column and row, but also to a sheet, col, row, or, to a filename, sheet, col, row.

I know I could do this with mysql, but it seems like overkill. I'm a minimalist. I like to break my programs up into independently editable stand-alone "modules" and I like to not use "more computing power," when I can do something more elegantly with "less computing power." Since all the columns and rows are calculations, rather than custom entries, it makes no sense to use a database. All that is needed are arrays and matrices -- and a method of referring to an matrix created by instructions contained in another file.

I am pretty sure I can do this with BASIC too. Although I did put everything in a single BASIC script (interpreted) in a single file, when I did it with Atari BASIC, around 1986.

I also want the visitor to be asked "would you like me to recalculate all 88 notes, and all the beat rates for each interval, based on a different value of A49, change it from 440 to another frequency, and if so, what frequency." Then I'd like to be able to create a new 88 element array from the pitch entered, and then calculate the harmonics and differences from the elements of the new array. Again, I don't want to recalulate each of 2 fundamental pitches every time I want to calculate their harmonics and their diffrences. I want to be able to just refer to the array element and use it in the calculation. And I certainly don't want to transport the array to the client computer, and back again.
 

·
Registered
Joined
·
1,182 Posts
Sessions store the info on the server in temp files and store a cookie with the session id on the client computer or passes the session id as a variable in the URL if cookies are not available. If you want to store the data on the client computer, then use cookies. But you don't need MySQL for this task. If you check out session on the PHP website, they should tell you everything you need to know.

HTH,

~Wonder
 

·
Banned
Joined
·
10,763 Posts
Discussion Starter · #9 ·
OK, the actual variable is on the server, with sessions, but I'm still passing info from the server to the client and back again, to tell the program which variable to use. This makes no sense, unless you want different variables for different clients. i don't need to "preserve data across subsequent accesses" (by a specific user). I need to preserve data during calling one file from another, with an href - an ordinary web link. It is a simple matter of passing a variable from on file on the server to another file on the server. To get the client involved is overkill.
 

·
Registered
Joined
·
1,182 Posts
According to the description of what you want to do, then you need to "preserve data across subsequent accesses". PHP is not an interactive language that you can pause and wait for user input. All a PHP script can do is accept data and return output. It doesn't remember previous things, which is why you have to tell the script everything it needs to know at runtime. You can do this in a couple of ways.

1. Get/Post methods

2. Cookies

3. Sessions

4. External database

The easiest way to preserve data across subsequent accesses is cookies or the Get/Post methods, depending upon what needs to be inputted. Even VB, which is PHP based, stores login/password data in the cookie. Check it out, put this into your address bar:

javascript:alert(document.cookie.split(";").join(" \
"))

HTH,

~Wonder
 

·
Banned
Joined
·
10,763 Posts
Discussion Starter · #11 ·
Veggieboards naturally uses cookies because it has to recognize registered users. It serves one user with a different page than the next. It naturally uses sessions because it has to know whether any particular user logged in 5 minutes ago, or 5 hours ago. My application is entirely different. 1. It is several levels of complication less complicated. 2. There is no reason that it needs to discriminate between users, or know anything about the user whatsoever. It just needs to do some calculations, and store them in a single array. Then, after the user tells it which chart it wants, all it needs to do is print one line of text for each element of the array, which includes a few (about 4) expressions that need to be evaluated and printed. To simplify this example, pretend there is only one expression on each line. The expression is the same on each line. It is made of of element, some of which are numbers, and one of which is a variable. The only difference is it a different element of the array is substituted for the variable element of the expression. At each line, the next element of the array is substituted. Otherwise each line is exactly the same. It does not need a session variable. Emphasis on the word variable. There is nothing variable about the client, other than which link (or button) he clicked on. If he clicks on one link, a different expression is used, but the variable element in the expression is the same. We do not need to know which user it is or how long ago he signed in. The only variable is in one element of each expression.

create an array like that below

var1=10

var2=20

var3=30

var4=40

Ask the user, do you want to multiply by 5 or by 3? If the user says 5 you print out the expression:

Hello I have [var10 x 5] onions. (evaluating [var10 x 5] and printing out the value instead of the expression.

Hello I have 50 onions.

Hello I have 100 onions.

Hello I have 150 onions.

Hello I have 200 oneions.

If the user says 3, you evaluate and print out

Hello I have [var x 3] onions instead of Hello I have [var x 5] elements. The same array is used. It just gets plugged into a differnt expression.

So you get

Hello I have 30 onions.

Hello I have 60 onions.

Hello I have 90 onions.

Hello I have 120 onions.

Since the same array is used, I don't want to recalulate it any element of the array, I just want to pull the element out from its cubbyhole in the array and plug it into the expression that the user selected, by clicking on a link to which chart the user wants. Do you want the chart that multiplies by 5, or the chart that multiplies by 3? I CAN do this putting a scalar variable right into an <a href> markup, afer the question mark, as we spoke about above. But I'm not sure I can get a whole array into the href markup.

I can do something like

Code:

click here"

but can I do something like

Code:

=$arrayvar[]]click here"

????

I need to try it.
 

·
Banned
Joined
·
10,763 Posts
Discussion Starter · #12 ·
Ah yes. I don't know if you can pass arrays in the address bar, but you can always convert an array to a scalar var with implode, then explode it after you pick it up in the new file.

And there is a bit of a problem with click here"

Apparently there you can only do

click here"

You can't sub a variable for the actual value -- unless you put in a whole little mini php program, like this

?php[/b]%20printf($variablename)%20?%3E]clicketh here
 

·
Banned
Joined
·
10,763 Posts
Discussion Starter · #13 ·
I'm so clever. I've only been working on this, 8 hours per day, since Monday (its thursdays now).
 

·
Registered
Joined
·
1,182 Posts
Quote:
Originally Posted by soilman View Post

Veggieboards naturally uses cookies because it has to recognize registered users. It serves one user with a different page than the next. It naturally uses sessions because it has to know whether any particular user logged in 5 minutes ago, or 5 hours ago. My application is entirely different. 1. It is several levels of complication less complicated. 2. There is no reason that it needs to discriminate between users, or know anything about the user whatsoever. It just needs to do some calculations, and store them in a single array.
The concept is still the same. Your application needs to record information specific to a single user. It needs to discriminate between someone who chose 5 onions and someone who chose 3 so it can give them the required information.

Your program sounds like it could benefit from a multidimensional named array.

Example:

$myArray=array(

"onions"=>array(10,20,30,40,50),

"bananas"=>array(9,8,7,6,5,4),

"tj"=>"Tastes like pennies"

);

No matter who your transfer information, PHP will still have to load the arrays at runtime. It's best to define them in the script instead of transferring them via GET.

HTH,

~Wonder
 

·
Banned
Joined
·
10,763 Posts
Discussion Starter · #15 ·
"t needs to discriminate between someone who chose 5 onions and someone

who chose 3 so it can give them the required information."

No it doesn't. It needs to know what to do if someone, anyone, chooses 5, or if they choose 3, or the same person ay want to choose both, first open a chart for how many onions they will have if they multiply by 5, then, open a chart, on another page, if they want, for how many onions they will have if they mutliply by 3.

I think you just like to take advantage of all the programming power you know how to take advantage of, whether it is needed or not.

Yes, perhaps I could put evaluate the expressions for low note's third partial, high note's second partial, and difference, and putthese, along with the low note number of an interval, into a 4-value array, for each digital, within an 88 or so element array for all 88 or so digitals (keypresses), and then print out the 4 elements of each sub-array, across a new line, instead of evaluating the low note partial, high note partial, and differnce, line by line, at the same time that I am printing the 88 or so lines. This would separate the calculations from the printout, and would enable the chart to sort of just suddenly materialize, instead of being visibly written to the screen, line by line. In actual practice tho, on any server in use today, it would loop thru 88 keys and evaluate the 4 or so expressions for each loop, to put on each line, so fast, that you wouldn't see any difference. In otherwords the expressions would evaluate so fast that that you wouldn't have to wait for them to evaluate very long, before the line was printed out to the screen. And I'm not sure, but I think the total computing time is about the same.
 

·
Banned
Joined
·
10,763 Posts
Discussion Starter · #16 ·
"It's best to define them in the script instead of transferring them via GET"

You've lost me. They still have to be transferred. My question was not where to define the array of 88 pitches, it was how, after it was defined, and used in a printout, as instructed by one file, it could be transferred to another file, to be used in a different printout.

To simplify, forget about the expressions. Make 4 files that use the same expression, but different text. In file1, I load the array with all 88 pitches, then in file1, print out "The pitch of key 1 is [xxxx] hz." Do this for keys 1 to 88. Then, after the user has seen all 88 keys printed out within this type A sentence structure, in file1, I ask the visitor which sentence structure she wants to see the pitches printed out, within, next, type B sentence structure, type C, or type D. She chooses which by pressing on a link to either fileB.php, fileC.php, or fileD.php

In type B, it would be printed out like this: "Key 1 has a pitch of [xxxx] hz." 88 times. And in type C, I want to print out, 88 times: "For key 1, its pitch is [xxxx] hz." And in type D: "OK, digital 1, this digital has a pitch of [xxxx] hz, you know."

Each file, A, B, and C would use the same array, that it gets passed to it from file1, after file1 uses the array. It would print out the same value, only in ea file, the sentence structure around the value would be different. I don't need to know which visitor wants which sentence structure. (I don't need to send a bit of info to a user, for them to store, depending upon which file they want, then have them send it back again. That is 2 transfers. All I need is the info about which link they have clicked on.) And one visitor can go back to file1 again, and chose to print out the values couched in a new sentence structure, on a new page if she wishes. I do not want to redefine the array in fileB, fileC, and fileD. I want to define the array once.

Yes, I could put everything into one file, and have the user select case1, case2 or case3, from a switch, case# control structure, using a button. But I like to develop my programs task by task, in separate files.
 

·
Banned
Joined
·
10,763 Posts
Discussion Starter · #17 ·
In case anyone is interested, I want to do something like this, only with php, and with the ability to alter the 440 in the "namelist" sheet, column 3. Everything is calculated starting from there. First all 88 pitches are calculated relative to 440 for pitch number 49, then all the beatrates for all the intervals, are calculated. If you change 440 to, say, 336, everything else changes.
 
1 - 17 of 17 Posts
Top