It is currently Sat Apr 27, 2024 9:06 pm



Reply to topic  [ 34 posts ]  Go to page 1, 2, 3  Next
php question (pig) 
Author Message
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16650
Location: On a slope
Reply with quote
Post php question (pig)
Piggie!

Ok, I took some time and learned some of the basics of php. I only have one question...

I know how to assign a variable via a web form. Is there anyway to assign a variable via an html hyperlink?

ie
<a href="index2.php" $flash = "1">Flash site</a>
<a href="index.php" $flash = "0">Nonflash site</a>

That way I can just carry a variable into the rest of my site and do an include based on the value of $flash (either flash menu or non-flash menu).

*edit*

ok, I did some digging, and it looks like I can pass variables as part of the URL. Erm... this is my understanding of what I should do... please, feedback (or code corrections) are welcome.

in the index.html
Code:
<A href="index2.php?flash=1">Flash Version</a>
&&& <A href="index2.php">NonFlash Version</a>


and then in index2.php (and every other page)...

Code:
<?php
if (isset($flash)) {
          include("flash.txt");
                }
   else {
          include("nonflash.txt");
        }
?>


initially I wanted to do an if ($flash =1), but in testing I realized that if the variable isn't set the test returns true. :/ How annoying. Anyway, any comments?

Of course, I could extrapolate this to the point of having only one actual .php file and including all my content depending on links you clicked. That kinda scares me for some reason.

*edit again*
If I set a variable via link like that, will that variable carry on to further pages, or would I have to set it in every link? Obviously the latter is a bad thing... if that's the case, is there a way to set a variable via hyperlink that's global? Would I have to set a cookie? I prefer to avoid cookies.

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Thu Oct 09, 2003 9:14 am
Profile WWW
Emperor
User avatar

Joined: Wed Apr 16, 2003 1:25 am
Posts: 2560
Reply with quote
Post 
I have other problem. I'm member of intranet on the my faculty where PHP works, but my *folder* is not on the root so, I cannot start anything php yet.

Once I steal some code and only generally difference is in first row:

#!/usr/local/bin/php

But that do not works for me.
So, what I should do to start damn php?

_________________
++


Fri Oct 10, 2003 2:16 am
Profile WWW
Duke
User avatar

Joined: Mon Mar 31, 2003 8:59 am
Posts: 1358
Location: right behind you
Reply with quote
Post 
OK, satis first.
There are different variable arrays that you are working with. All of your variables are accessible from variable arrays. Each array has a different scope. Right now it's not a big deal, but you learn to reference variables in terms of arrays instead of global variables. It is much safer. To access them from an array, you would use: $_ARRAY['variable'].

One array is stuff passed in the query string, either typed in by the user, or from a hyperlink, or from a form with it's method set to GET. These are called GET.

There are also POST, which are passed from a form that has it's method set to post.

There are SERVER variables (not even going to explain that one).

COOKIE vars are obvious too.

There are GLOBAL variables (which you are using whenever you type $var instead of $_POST['var'].

The last one is the most interesting, and those are SESSION variables.

Maintaining state between pages is fundamental problem wih the web. The easiest method is to use either cookies, or sessions. Sessions are basically temporary files on the server that can store any variables you want. If you want to store shopping cart items, session are awesome. If your need is much more temporary, then you might want to consider passing the vars in the URL automatically, or using auto generated form fields.

Let me give you an example. Let's say you want to have form broken up over 3 pages. The first page gets filled out, and then they hit submit. On the second page, you would check to make sure that they didn't leave anything out of the required fields. Then you could so something like this:
Code:
foreach($_POST AS $key => $value)
{
     echo("<input type=hidden name=\"$key\" value=\"$value\">\n");
}

Then, all of the $_POST from this page will put on the next page through hidden fields!

If you want to pass all of your GET data on certain pages, you could make your links like this:
=========================
fuck, outta time. I'll finish this when I get to work.


Fri Oct 10, 2003 6:32 am
Profile YIM WWW
Duke
User avatar

Joined: Mon Mar 31, 2003 8:59 am
Posts: 1358
Location: right behind you
Reply with quote
Post 
OK, GET data in the links...
Code:
unset($new_get);
foreach( $_GET AS $key => $value )
{
     $new_get .= "$key=$value&";
}
$new_get = substr($new_get, 0, -1);
echo("<a href=\"path/to/file.php?$new_get\">link</a>");


As far as your flash example, I would either use a session variable, or use a cookie. Cookies are actually the best option. I would create a file, and just include it on every pertinent page. Then, just check to see if the cookie exists. If it doesn't, include the nonflash.txt, etc etc.

As far as checking existance, I always use empty() or strlen(), and you have found the reason why! Something can be empty, or zero, and still unsset.

============================================
GFreeman,
You don't access PHP like would Perl, etc. All you have to do, is make sure your page is in htdocs (or deeper) and it ends with a .php extension. Then you start the code with <?php and end with ?>.


Fri Oct 10, 2003 7:51 am
Profile YIM WWW
Emperor
User avatar

Joined: Wed Apr 16, 2003 1:25 am
Posts: 2560
Reply with quote
Post 
Pig, I'm on some directory (sim. home/usr/mr02117/) and I cannot do anything out of that.

<?php?>

This is code i put here and it cause error.

What went wrong? Piggie?

_________________
++


Fri Oct 10, 2003 8:42 am
Profile WWW
Duke
User avatar

Joined: Mon Mar 31, 2003 8:59 am
Posts: 1358
Location: right behind you
Reply with quote
Post 
There's an error in your code. Can you post your code?


Fri Oct 10, 2003 8:44 am
Profile YIM WWW
Emperor
User avatar

Joined: Wed Apr 16, 2003 1:25 am
Posts: 2560
Reply with quote
Post 
Code is only:
----------------------------
<?php?>
----------------------------
.

?

_________________
++


Fri Oct 10, 2003 9:18 am
Profile WWW
Duke
User avatar

Joined: Mon Mar 31, 2003 8:59 am
Posts: 1358
Location: right behind you
Reply with quote
Post 
Your code has to go between the tags.

So if your page is called xe.php, and you wanted to the referring page, your page would look like this:

<?php
echo("You just came from " . $_SERVER['HTTP_REFERRER'] . "!");
?>

Try putting those 3 lines in, and see what you get.


Fri Oct 10, 2003 10:21 am
Profile YIM WWW
Emperor
User avatar

Joined: Wed Apr 16, 2003 1:25 am
Posts: 2560
Reply with quote
Post 
HEY! It works when I put #!/usr/local/bin/php on start.
Thanx Pig! Now I can start PHP.

_________________
++


Fri Oct 10, 2003 10:31 am
Profile WWW
Duke
User avatar

Joined: Mon Mar 31, 2003 8:59 am
Posts: 1358
Location: right behind you
Reply with quote
Post 
Apache must be configured strangely on that server. You should never have to put #!/usr/local/bin/php in your files.


Fri Oct 10, 2003 11:44 am
Profile YIM WWW
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16650
Location: On a slope
Reply with quote
Post 
I'll have to digest that slowly. I'll be back if I have any further questions.

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Fri Oct 10, 2003 4:37 pm
Profile WWW
Duke
User avatar

Joined: Mon Mar 31, 2003 8:59 am
Posts: 1358
Location: right behind you
Reply with quote
Post 
Let me know if anything I said was unclear, or requires further explanation.

Also, when I said:
Something can be empty, or zero, and still unsset.
I meant ...and still be set


Fri Oct 10, 2003 5:09 pm
Profile YIM WWW
Emperor
User avatar

Joined: Wed Apr 16, 2003 1:25 am
Posts: 2560
Reply with quote
Post 
Pig, PHP faster than CGI?

_________________
++


Wed Oct 15, 2003 10:31 am
Profile WWW
Duke
User avatar

Joined: Mon Mar 31, 2003 8:59 am
Posts: 1358
Location: right behind you
Reply with quote
Post 
In general, yes. PHP is faster and more efficient than most CGI languages. PHP is designed specifically for the web, where as most languages (eg: python, Perl) ended up there by accident.


Wed Oct 15, 2003 12:10 pm
Profile YIM WWW
Emperor
User avatar

Joined: Wed Apr 16, 2003 1:25 am
Posts: 2560
Reply with quote
Post 
Yes, tought that. Just want to be sure... :idea: it must be PHP interpreter already exist in the RAM and the just need to create another object... CGI goes in one part and, in general, PHP shall do 10000 requests faster than CGI regardless there works interpreting. :P

BTW, Satis, you haven't download the PHP manual (I shall remove it soon from there) or you was to lazy to read or search for 'em OR you found greater enjoy in the beer? :roll:

_________________
++


Fri Oct 17, 2003 12:25 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 34 posts ]  Go to page 1, 2, 3  Next

Who is online

Users browsing this forum: No registered users and 22 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware.