...

View Full Version : Getting and defining functions from hyperlink



matak
06-28-2007, 11:48 AM
Hey guys. I need some help here :)

I'm trying to build a text editor. But i don't know how to define function from hyperlink. I know how to use $_GET and that, but this is what i need.

Folders are like this

--includes
--files
-index.php

My index.php contains function that reads Files dir, and includes some functions to use in editor, that's write.php. Editor is also in includes folder, that is editor.php.

Now in index.php i generated my links with simple readdir found on php.net like this


if ($handle = opendir('files')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
#this is the problem part
echo "<a href=\"?edit=$file\">$file</a><br />";
}
}
closedir($handle);
}

Now i think that ?edit=$file can be used to call edit function which is defined in some other file like write.php

But i have this problem with editor.php, where i want to use my textarea to edit that file. It looks like this


<form method="POST">
<p><input id="title" name="title"></p>
<p><textarea name="tekst" wrap="virtual">
<?php #what to write here so that it uses that ?edit=$file link ?>
</textarea></p>
<p><input type="submit"></p>
</form>

I guess this could be solved with using sessions, but i don't think that that is necessary, and i would really like for u guys to help me figure this out.

Thanks

Fumigator
06-28-2007, 05:38 PM
The query string of the URL will be stored in the $_GET array for your use. So if you were to just add ?doyouliketacos=hellyes to any URL, PHP would provide that script a $_GET['doyouliketacos'] variable with value 'hellyes'.

TheShaner
06-28-2007, 06:44 PM
Let's see if I have this right:
1) You have index.php that lists your files in the files directory (folder)
2) write.php is in the files directory
3) When you click on the link yourdomain.com/index.php?edit=write.php (which is listed in #1), it should open editor.php and include the write.php file in editor.php's textarea.

If all that is correct, then you'll have this in editor.php:

<form method="POST">
<p><input id="title" name="title"></p>
<p><textarea name="tekst" wrap="virtual">
<?php include_once "files/" . $_GET['edit']; ?>
</textarea></p>
<p><input type="submit"></p>
</form>

HTH!

-Shane

matak
06-28-2007, 07:24 PM
@TheShaner

actualy my domain then looks yourdomain.com/index.php?edit=My%20Title.txt

and now i see the missing link, you see that is not just an editor but also a viewer of those text files, and i forgot the part where i have links to NEW, EDIT, DELETE, and SAVE functions. Sorry about that, couse i'm still thinking about the whole concept.

Basicly first part is the links part where i have PHP generated links to text files like this

<a href="index.php?show=My Title.txt">My Title</a>
<a href="index.php?show=My Title2.txt">My Title2</a>

In the middle content can be displayed with simple use of get. That is the part i forgot to mention.

Ok, now on the right corner i have Admin tools like NEW, EDIT, which are basicly

<a href="new.php">NEW</a>
<a href="edit.php?edit=My Title2.txt">EDIT</a>

So this is the tricky part, which i can't figure out.

I don't want my admin tools to be like that, but i want them to be generated from included script in includes folder. So that instead my EDIT calls another file and edits that My Title2.txt like shown above, i want it to be shown in the middle section of the index.php.

I only need guidance couse paths are bothering me since i've started this little script.. So let's say that in write.php i have functions like


function new($file){
#some php stuff to create new file and store it in [B]files[B] folder
}

and


function edit($file){
#some php stuff that edits the egzisting file
}

How can i call those functions just by including write.php in index.php.

This sounds really complicated, and maybe i'm just missing the obvious couse i haven't slept well these days.

Thanks

TheShaner
06-28-2007, 08:37 PM
In edit.php, you'll have:

include_once "files/write.php";
if (isset($_GET['edit'])) edit($_GET['edit']);
The above will call your edit function in write.php and pass the file that edit was set to.

-Shane

matak
06-29-2007, 05:13 AM
In edit.php, you'll have:

include_once "files/write.php";
if (isset($_GET['edit'])) edit($_GET['edit']);
The above will call your edit function in write.php and pass the file that edit was set to.

-Shane

Thank you



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum