Forum Moderators: coopster

Message Too Old, No Replies

creating dynamic content for a website

         

althofma

6:23 pm on Dec 21, 2003 (gmt 0)

10+ Year Member



What I want to create is a gallery of posts, perhaps text files in a single directory on my website, and allow the user to scroll through them with "back" and "next" links. I hate the idea of creating individual html files with the content posted in the body of the file. Is it possible to use php to post this content dynamically from a directory of txt files, with no hardcoding, so the previous or next post just appears on the page with no reloading? If so, can anybody direct me to a tutorial on how to accomplish this?

-max

WhosAWhata

3:37 am on Dec 22, 2003 (gmt 0)

10+ Year Member



i don't know of any specific tutorial, but you might want to try something like this:
name your text files as number.txt (ie 1.txt, 2.txt)
<?
if (($file == "") OR (!file_exists($file.txt))) {
$file = 1;
}
echo "<html><body bgcolor=blue><h1 align=center>File $file</h1><hr><pre>";
include("$file.txt");
$next = $file+1;
$prev = $file-1;
echo "</pre><hr><p align=\"center\">";
if (file_exists($prev.txt)) { echo "<a href=\"index.php?file=$prev\"><font color=\"#ffffff\">PREVIOUS</font></a> ¦ ";
} else {
echo <font color=\"#c0c0c0\">Previous</font> ¦ "; }
if (file_exists($next.txt)) { echo "<a href=\"index.php?file=$next\"><font color=\"#ffffff\">NEXT</font></a>";
} else {
echo <font color=\"#c0c0c0\">NEXT</font>"; }
echo "</p></body></html>";
?>

althofma

4:37 am on Dec 22, 2003 (gmt 0)

10+ Year Member



That certainly looks like the type of thing I'd want to use, but when I entered the code on my page (within a div), the page showed up blank white. I also tried writing a simple page and entering your code as such:

------------------------------------------
<html>
<body>
<?
if (($file == "") OR (!file_exists($file.txt))) {
$file = 1;
}
echo "<html><body bgcolor=blue><h1 align=center>File $file</h1><hr><pre>";
include("$file.txt");
$next = $file+1;
$prev = $file-1;
echo "</pre><hr><p align=\"center\">";
if (file_exists($prev.txt)) { echo "<a href=\"index.php?file=$prev\"><font color=\"#ffffff\">PREVIOUS</font></a> ¦ ";
} else {
echo <font color=\"#c0c0c0\">Previous</font> ¦ "; }
if (file_exists($next.txt)) { echo "<a href=\"index.php?file=$next\"><font color=\"#ffffff\">NEXT</font></a>";
} else {
echo <font color=\"#c0c0c0\">NEXT</font>"; }
echo "</p></body></html>";
?>
</body>
</html>

-------------------------------------------

I named the file fubar.php. I know my ISP runs php. Are there any errors in the code itself?

thanks,
Max

WhosAWhata

6:50 am on Dec 22, 2003 (gmt 0)

10+ Year Member



after changing a few amature mistakes and testing my code...here

<?
if (($file == "") OR (!file_exists("$file.txt"))) {
$file = 1;
}
echo "<html><body bgcolor=blue><h1 align=center>File $file</h1><hr><pre>";
include("$file.txt");
$next = $file+1;
$prev = $file-1;
echo "</pre><hr><p align=\"center\">";
if (file_exists("$prev.txt")) { echo "<a href=\"fubar.php?file=$prev\"><font color=\"#ffffff\">PREVIOUS</font></a> ¦ ";
} else {
echo "<font color=\"#c0c0c0\">Previous</font> ¦ "; }
if (file_exists("$next.txt")) { echo "<a href=\"fubar.php?file=$next\"><font color=\"#ffffff\">NEXT</font></a>";
} else {
echo "<font color=\"#c0c0c0\">NEXT</font>"; }
echo "</p></body></html>";
?>

you can add little tags like title or anything, but this should get you started let me know if u need any other modifications

althofma

2:08 pm on Dec 22, 2003 (gmt 0)

10+ Year Member



Wow, thanks a lot for the help. I got rid of most of the style elements (I'm using CSS) and put the navigation section on top of the content section, but other than that your actual php code is intact and it works like a dream.

-max

[edited by: jatar_k at 5:33 pm (utc) on Dec. 22, 2003]
[edit reason] no personal urls thanks [/edit]

althofma

2:15 pm on Dec 22, 2003 (gmt 0)

10+ Year Member



OK, spoke too quickly (well not exactly). There's just a minor issue with the $file count, I think. When you get to the page, 1.txt is already showing. When you click on "Next", it gives you 1.txt again. After that, a click will give you 2.txt, 3.txt, etc, etc. Is there any way to make it so it serves 2.txt the first time you click "Next"?

Longhaired Genius

4:33 pm on Dec 22, 2003 (gmt 0)

10+ Year Member



It works perfectly for me, althofma. Maybe you've made an inadvertent change to the script.

Anyway, this is good stuff. Thanks WhosAWhata. What I would like to do is include page title and description in the textfile and explode them onto the html page in the title tag and description meta data areas. Would that be difficult?

althofma

4:53 pm on Dec 22, 2003 (gmt 0)

10+ Year Member



Yes, the code works correctly. But try going to:
<snip>
You'll see that when the page loads, the first poem is already up . . . that's 1.txt. When you click on the "Next" link, 1.txt goes up again. I want to make it so that when you first click on the "Next" link, 2.txt goes up instead of just posting 1.txt again.

[edited by: jatar_k at 5:34 pm (utc) on Dec. 22, 2003]
[edit reason] no personal urls thanks [/edit]

WhosAWhata

6:29 pm on Dec 22, 2003 (gmt 0)

10+ Year Member



when i tried it on my server, i had 1.txt 2.txt 3.txt and it worked just fine
to add a title and description, you could do something like this.
in you text file:
<!--title Poem 1 - poem title endtitle-->
<!--desc a poem about whatever enddesc-->

and then change the code to something like this,
<?
if (($file == "") OR (!file_exists("$file.txt"))) { $file = 1; }
$grab = "$file.txt"; $read = fopen("$grab", "r"); do {
$data = fread($read, 8192);
if (strlen($data) == 0) { break; }
$contents .= $data;
} while(true);
fclose ($read);
echo "<html><body bgcolor=blue><h1 align=center>";
if(ereg("<!--title(.*)endtitle-->", $contents, $out)){
echo $out;
} else { echo "File $file"; }
echo "</h1>"; error_reporting(0);
do { $data = fread($read, 8192);
if (strlen($data) == 0) { break; }
$contents .= $data;
} while(true);
fclose ($read);
if(ereg("<!--desc(.*)enddesc-->", $contents, $out)){ echo "<p align=center>"; echo $out[1]; echo"</p>"; }
echo "<hr><pre>";
include("$file.txt");
$next = $file+1;
$prev = $file-1;
echo "</pre><hr><p align=\"center\">";
if (file_exists("$prev.txt")) { echo "<a href=\"fubar.php?file=$prev\"><font color=\"#ffffff\">PREVIOUS</font></a> ¦ ";
} else {
echo "<font color=\"#c0c0c0\">Previous</font> ¦ "; }
if (file_exists("$next.txt")) { echo "<a href=\"fubar.php?file=$next\"><font color=\"#ffffff\">NEXT</font></a>";
} else {
echo "<font color=\"#c0c0c0\">NEXT</font>"; }
echo "</p></body></html>";
?>

[1][edited by: WhosAWhata at 6:56 pm (utc) on Dec. 22, 2003]

Longhaired Genius

6:37 pm on Dec 22, 2003 (gmt 0)

10+ Year Member



Thanks a lot, WhosAWhata. I'll play with that tomorrow.

althofma

7:04 pm on Dec 22, 2003 (gmt 0)

10+ Year Member



Maybe it has something to do with how I edited your code, then. Here's what I used in my page:

-------------------------------------------------

<?php
$next = $file+1;
$prev = $file-1;
echo "</pre><p align=\"center\">";
if (file_exists("$prev.jpg")) { echo "<a href=\"index.php?file=$prev\">Previous</a> ¦ ";
} else {
echo "Previous ¦ "; }
if (file_exists("$next.jpg")) { echo "<a href=\"index.php?file=$next\">Next</a>";
} else {
echo "Next"; }
echo "</p></body></html>";
if (($file == "") OR (!file_exists("$file.jpg"))) {
$file = 1;
}
echo "<pre>";
include("$file.jpg");
?>

-------------------------------------------------
My objective was to put the links on top of the include function. Is there an error in the edited code that would explain why the page hangs up on 1.txt until "Next" is clicked a second time? BTW, thanks for all the help. I really appreciate it.

-max

althofma

7:07 pm on Dec 22, 2003 (gmt 0)

10+ Year Member



oops, here's the right code. don't know why .jpg got in there.

--------------------------------------------------------
<?php
$next = $file+1;
$prev = $file-1;
echo "</pre><p align=\"center\">";
if (file_exists("$prev.txt")) { echo "<a href=\"index.php?file=$prev\">Previous</a> ¦ ";
} else {
echo "Previous ¦ "; }
if (file_exists("$next.txt")) { echo "<a href=\"index.php?file=$next\">Next</a>";
} else {
echo "Next"; }
echo "</p></body></html>";
if (($file == "") OR (!file_exists("$file.txt"))) {
$file = 1;
}
echo "<pre>";
include("$file.txt");
?>

aevea

11:28 pm on Dec 22, 2003 (gmt 0)

10+ Year Member



I just wrote a similar code to post jpg's. if anyone can make suggestions, I'd love the advice...I comepletely new to this so I expect my code is pretty ugly. Also, what would be the best way to change the titles and alts?

<html>
<head></head>
<?php
$imgdir = "images/";
$imgend = ".jpg";
$next=$_SERVER['QUERY_STRING'];
if (($next >= 2) && (is_file($imgdir.$next.$imgend))) {
echo "<body class=\"type\"><p>&lt;&lt; <a href=\"type.php?";
echo ($next-1);
echo "\">back</a> ¦¦ <a href=\"type.php?";
echo ($next+1);
echo "\" name=\"next\" id=\"next\">next</a> &gt;&gt;</p><h1>Tales of the Typewriter</h1><img src=\"";
echo $imgdir.$next.$imgend;
echo"\" width=\"612\" alt=\"typed notes\" id=\"typeimg\" name=\"typeimg\" title=\"Notes typed by the audience at Grackle Mundy\" /></body></html>";
}
else {
echo
"<body class=\"type\">
<p>&lt;&lt; <a href=\"../index.htm\">index</a> ¦¦ <a href=\"type.php?2\" name= \"next\" id=\"next\">next</a> &gt;&gt;</p><h1>Tales of the Typewriter</h1>
<img src=\"images/1.jpg\" width=\"612\" alt=\"typed notes\" id=\"typeimg\" name=\"typeimg\" title=\"Notes typed by the audience at Grackle Mundy\" />
</body>
</html>";
}
?>

Thanks,
Adam

WhosAWhata

2:05 am on Dec 23, 2003 (gmt 0)

10+ Year Member



Here's your problem...you put the
if (($file == "") OR (!file_exists("$file.txt"))) {
$file = 1;
}

before
$next = $file+1;
$prev = $file-1;

the code should read
<?php
if (($file == "") OR (!file_exists("$file.txt"))) {
$file = 1;
}
$next = $file+1;
$prev = $file-1;
echo "</pre><p align=\"center\">";
if (file_exists("$prev.txt")) { echo "<a href=\"index.php?file=$prev\">Previous</a> ¦ ";
} else {
echo "Previous ¦ "; }
if (file_exists("$next.txt")) { echo "<a href=\"index.php?file=$next\">Next</a>";
} else {
echo "Next"; }
echo "</p></body></html>";
echo "<pre>";
include("$file.txt");
?>