/    Sign up×
Community /Pin to ProfileBookmark

call PHP function

Hi

This is what I want…
I have a couple of .txt files:
1.txt
2.txt
3.txt

Now i want to make a website with tree buttons and when I click on the first the txt from 1.txt is shown on the screen. When I click on the second the text of 1.txt is still there en the text of 2.txt is shown above it. Can someone help me!!

I’m trying to do this by calling the PHP function from HTML without refreshing it.

This is what I have for now….

[code=php]<html>
<head>
<title>Update</title>
</head><body>

<a href=”” call inputText(“tekst1.txt”)>tekst1</a>
<a href=”” call inputText(“tekst2.txt”)>tekst2</a>

<?PHP
function inputText ($vileName) {
$filename = $vileName;
$text = “”;
$contents = @file($filename) or die(“No files in guestbook.”);

foreach ($contents as $line) {
$text .= $line;
}

$text = split(“<break>”, $text);
$text = array_reverse($text); # removing this line will put newest comments at the bottom
for ($i=0; $i<count($text)-1; $i++) {
echo “<div style=”border: gray 0px solid; padding: 0px; font-family: arial, sans-serif; background-color: #ffffff;”>”;
echo $text[$i];
echo “</div>”;
}
}
?>
</body>[/code]

Thanks in advance Zeb

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@ShrineDesignsJul 16.2004 — PHP isn't JavaScript, you can't call a php function in the same manner

with this script you will have to set your url's up with this in it: [b]?msg=1[/b] the number is the number in the files, ie. tekst1.txt and so forth[code=php]<?php
$file_pre = "tekst"; // file preffix
$file_ext = ".txt"; // file extension

if((isset($_GET['msg']) && !empty($_GET['msg'])) && is_int($_GET['msg']))
{
$files = array();

for($i = 1; $i <= $_GET['msg']; $i++)
{
$files[] = explode("<break>", @file_get_contents("$file_pre$i$file_ext"));
}
$files = array_reverse($files, 0);

foreach($files as $file)
{
$file = array_reverse($file, 0);

foreach($file as $text)
{
echo "<div style="border: 1px solid gray; padding: 0px; font-family: arial, sans-serif; background-color: #ffffff;">$text</div>n";
}
}
}
?>[/code]
Copy linkTweet thisAlerts:
@zebdaagauthorJul 16.2004 — this is what i have now but it doesn't show any text??

[URL=http://www.doriendenijs.nl/Pages/test1.php]test it here[/URL]

[code=php]<html>
<head>
<title>Update</title>
</head><body>


<a href="test1.php?msg=1">tekst1</a>
<a href="test1.php?msg=2">tekst2</a>

<?php
$file_pre = "tekst"; // file preffix
$file_ext = ".txt"; // file extension

if((isset($_GET['msg']) && !empty($_GET['msg'])) && is_int($_GET['msg']))
{
$files = array();

for($i = 1; $i <= $_GET['msg']; $i++)
{
$files[] = explode("<break>", @file_get_contents("$file_pre$i$file_ext"));
}
$files = array_reverse($files, 0);

foreach($files as $file)
{
$file = array_reverse($file, 0);

foreach($file as $text)
{
echo "<div style="border: 1px solid gray; padding: 0px; font-family: arial, sans-serif; background-color: #ffffff;">$text</div>n";
}
}
}
?>
</body>
</html>[/code]
Copy linkTweet thisAlerts:
@ShrineDesignsJul 17.2004 — remove this: [b] && is_int($_GET['msg'])[/b] and it will work
Copy linkTweet thisAlerts:
@zebdaagauthorJul 17.2004 — Thanks it works great!!

Ow wait a second it does work once but when I click

button 1 , 2 , 1

It doesn't show text 1 , 2 , 1

It only shows 1

And when you click the 2 button you get text 1 first..??

Instead of just text 2 and then when you click button 1 Text one above i t.??
Copy linkTweet thisAlerts:
@ShrineDesignsJul 17.2004 — [code=php]<?php
$file_pre = "tekst"; // file preffix
$file_ext = ".txt"; // file extension

if(isset($_GET['msg']) && !empty($_GET['msg']))
{
$files = explode("<break>", @file_get_contents("$file_pre{$_GET['msg']}$file_ext"));
$files = array_reverse($files, 0);

foreach($files as $text)
{
echo "<div style="border: 1px solid gray; padding: 0px; font-family: arial, sans-serif; background-color: #ffffff;">$text</div>n";
}
}
?>[/code]
Copy linkTweet thisAlerts:
@zebdaagauthorJul 17.2004 — sorry but it still isn't what I want

What happens now is when you click [b]button 1[/b] the text of [b]tekst1.txt[/b] is shown.

And when you click [b]button 2[/b] the text of [b]tekst2.txt[/b] is shown.


What I want is that all the text stays on screen when you click on a button so when you click [b]button 1[/b] the text of [b]tekst1.txt[/b] is shown. Then when you click [b]button 2[/b] the text of [b]tekst2.txt[/b] is shown above the [b]tekst1.txt[/b] text. When you click [b]button 2[/b] again you see the [b]tekst2.txt[/b] text twice above the [b]tekst1.txt[/b] text. And so on and so>

So the page remembers what and how many times a button is click and you can see this in all the text that is on the page.
Copy linkTweet thisAlerts:
@solavarJul 17.2004 — Step1. Create two files

File 1: text1.txt

File 2: text2.txt

Step2. Name this script below into showfile.php

Step3. Upload text1.txt, text2.txt and showfile.php to the same folder

Step4. Call up showfile.php in your browser.

[code=php]

<?php
session_start();

echo '
<a href="showfile.php?text=1">Show Text1</a>

<a href="showfile.php?text=2">Show Text2</a><br />';

if($_GET['text'])
{
$num = $_GET['text'];
$incfile = "text" .$num . ".txt";
$_SESSION['showfile'] .= "[BREAK]$incfile";
$showfile =$_SESSION['showfile'];

//$showfile has a complete memory of which link was clicked, in the order clicked.

// we must reverse the order if we want to display the latest INCLUDE first


// So, first explode the entire string into an array:
$show_array = explode("[BREAK]", $showfile);

// reverse it so we show latest first
$array_show = array_reverse($show_array);

// each element of the array is a filename. Let's show them all

foreach($array_show as $key => $value)
{
if($value)
{
include($value);
echo "<br />";
}
}
}
else
{
$_SESSION['showfile'] = "";
}



?>

[/code]
Copy linkTweet thisAlerts:
@zebdaagauthorJul 18.2004 — Hi

Thanks for your help, I did exactly what you said but it didn't work when I open the file in my browser I get a lot of errors..can you help me with this??

if you want you can[URL=http://www.doriendenijs.nl/Pages/showfile.php]check the errors here[/URL]

thanks in advance
Copy linkTweet thisAlerts:
@solavarJul 18.2004 — Looks like you've a sessions problem.

OK, I'll modify the script so it uses only $_POST.

In the meantime, see if you can sort out the sessions problem.

Have you got any other scripts which use sessions?

If you upload this little script, as it is, and call it in your browser, it should run without any problems unless there's something wrong with the sessions settings on your server.

sessiontest.php
[code=php]
/* Note that the opening php tag must be the very first character on the very first line of the page, and the rest of the script must simply be copied as is , otherwise you run the risk of 'headers already sent' errors */

<?php
session_start();
$_SESSION['text'] = "This is a test";
echo $_SESSION['test'];
?>
[/code]


I'll post the revised script soon.
Copy linkTweet thisAlerts:
@zebdaagauthorJul 18.2004 — Hi i tried to put the files on an other server and it worked great!! Thanks for your help!!
Copy linkTweet thisAlerts:
@solavarJul 18.2004 — Great to hear you've got it sorted.

Anyway, here is an alternative approach which doesn't use sessions. It shows how you can use a hidden field on a form to display stuff just as if you were using sessions.

Note: You require text1.txt and text2.txt, and this script to be saved as 'showfile.php', all in the same folder.

[code=php]

<form name="form1" method="post" action="showfile.php">
<input type="submit" name="text1" Value="Show Text1" />
<input type="submit" name="text2" Value="Show Text2" />

<?php
$allfiles = $_POST['allfiles'];

if($_POST['text1'])
{
$allfiles .= "[BREAK]text1.txt";
}
if($_POST['text2'])
{
$allfiles .= "[BREAK]text2.txt";
}
?>

<input type="hidden" name="allfiles" value="<?php echo $allfiles ?>" />
</form>

<?php
/*
Each time a button is clicked, its value is appended to the variable, $allfiles.
This variable is conveniently stored in a hidden field so that we can carry
it forward each time a button is clicked.

For our display, we want to show the latest first, so we shall reverse the variable's
contents. To make this easy for ourselves, we have inserted the word '[BREAK]' wherever a button was clicked.
Therefore all we need to do is to explode the whole thing into an array, then reverse the array, and display
our listing. */

$show_array = explode("[BREAK]", $allfiles);
$array_show = array_reverse($show_array);

// display the array contents...
foreach($array_show as $key => $value)
{
if($value)
{
include($value);
echo "<br />";
}
}

?>


[/code]
Copy linkTweet thisAlerts:
@zebdaagauthorJul 18.2004 — Hi thanks for all your help you have been a great help!!

I have a new question for you if you are interested if you go to this [URL=http://www.nearestneighbour.net/TESTkuenen/showfile.php]link[/URL] you see that it works like i asked so that's great but now i thought of something else.

When there is a new text shown on screen it appears on a new line. IS there a possibility to put the text just after the text that was just displayed??

I hope you understand what i mean otherwise i will try to explain it better.
×

Success!

Help @zebdaag spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...