Forums

PHP

This topic is locked

PHP function($var)

Posted 13 Aug 2001 23:59:05
1
has voted
13 Aug 2001 23:59:05 Dog 4 Ages posted:
Hi guys,

I guess this isn't a hard one for the more php-experienced among us, but here's the problem I'm dealing with for the moment.

I want to use a function, just like you use them with Javascript, but now with PHP/MySQL because I'm probably gonna need it in a lot of html pages
Here's the script:
<HTML>
<HEAD>
<SCRIPT language="PHP">
function counter($filename){
$connect = mysql_connect("servername<img src=../images/dmxzone/forum/icon_smile_tongue.gif border=0 align=middle>ort", "login", "pass"
or die ("Unable to connect database! retry later";
mysql_select_db ("dbaseName",$connect);
$sqlresult = mysql_query("SELECT cels FROM tabelname WHERE file='$filename'"
or die ("error in query";
$result=mysql_result($sqlresult,0);
echo"$result and ";
print("script is done";
}
&lt;/SCRIPT&gt;

&lt;/HEAD&gt;
&lt;BODY onLoad='counter("example.zip";'&gt;
&lt;p&gt;Some text&lt;p&gt;
&lt;/BODY&gt;
&lt;/HTML&gt;

Right now, it doesn't work like I wish it did.

I'll appreciate it if someone could give me a little help with this one, I know I'm close, just not there yet.

Thanx in advance

<u>Warning ! Dead end Topic <img src=../images/flfreaks/forum/icon_smile.gif border=0 align=middle></u>

Replies

Replied 14 Aug 2001 00:19:35
14 Aug 2001 00:19:35 Tim Green replied:
The problem you are facing is the fact that you are trying to mix two separate technologies.

It isn't possible to activate a PHP Function via a Javascript event like this, as PHP is processed before it gets to the web browser, and JavaScript is processed after it gets there.

What does this mean to you ? Well, put simply, if you know the value of the filename for each page then what you can do is something like this :-

&lt;HTML&gt;
&lt;HEAD&gt;
&lt;?php
$filename = "example.zip";
$connect = mysql_connect("servernameort", "login", "pass"
or die ("Unable to connect database! retry later";
mysql_select_db ("dbaseName",$connect);
$sqlresult = mysql_query("SELECT cels FROM tabelname WHERE file='$filename'"
or die ("error in query";
$result=mysql_result($sqlresult,0);
?&gt;
&lt;/HEAD&gt;
&lt;BODY&gt;
&lt;?php $result; ?&gt; and script is done.
&lt;p&gt;Some text&lt;p&gt;
&lt;/BODY&gt;
&lt;/HTML&gt;

However, you should be aware that this isn't the UD way of doing things. UD & PHAkT employ a much better way of accessing your data by creating a recordset.

I won't go into this here, but suffice to say that you should always remember that PHP is not an event driven language like JavaScript.

Hope this helps



Tim Green

Extension & PHP TalkZone Manager
<font size=1>-------------------------------------------
<i>Please read the Forum FAQ before posting
a question to this TalkZone.</i>
-------------------------------------------
www.UDzone.com : A dynamic Dreamweaver,
Ultradev and Fireworks site for developers
by developers.
-------------------------------------------</font id=size1>

Edited by - rawveg on 08/14/2001 00:20:29
Replied 14 Aug 2001 03:36:03
14 Aug 2001 03:36:03 Dog 4 Ages replied:
Aha, that makes sence.

Now I'm thinking of it, could it be possible to let PHP generate the complete page, from &lt;HEAD&gt; to &lt;/HEAD&gt;, and let the links pass the filename in a GET or POST methode?
Like in &lt;a href="pagegenerator.php?filename=example.zip"&gt;click&lt;/a&gt; or use it in a frameset like in framesource=pagegenerator.php ?

Then I'll be able to pass those filenames to the script before the page is created, or not? or are there any side-effects a should be aware of?<font face='Verdana'></font id='Verdana'>

<u>Warning ! Dead end Topic <img src=../images/flfreaks/forum/icon_smile.gif border=0 align=middle></u>
Replied 14 Aug 2001 10:36:11
14 Aug 2001 10:36:11 Tim Green replied:
There are a number of ways that you get PHP to generate the information for you.

For example, you can quite easily pass the filename in a GET variable using something like :-

&lt;a href="pagegenerator.php?filename=&lt;?php echo $HTTP_GET_VARS["theFilename"]; ?&gt;"&gt;click&lt;/a&gt;

The main side effect is really that you can possibly trigger download managers etc if the final GET variable contains a downloadable file, so I would be more likely to pass the file extension as a seperate value :-

&lt;a href="pagegenerator.php?filename=&lt;?php echo $HTTP_GET_VARS["theFilename"].".".$HTTP_GET_VARS["theFileExtension"]; ?&gt;"&gt;click&lt;/a&gt;

It really is a matter of experimenting !

Tim Green

Extension & PHP TalkZone Manager
<font size=1>-------------------------------------------
<i>Please read the Forum FAQ before posting
a question to this TalkZone.</i>
-------------------------------------------
www.UDzone.com : A dynamic Dreamweaver,
Ultradev and Fireworks site for developers
by developers.
-------------------------------------------</font id=size1>

Reply to this topic