
Newbie: How to generate multiple web pages with one PHP script
Hello, this is my first on this list. I have a photography website and I
want to gradually convert it to PHP. I'm new to PHP, but I do have some
Perl knowledge and I am an expert in Bash-scripting (linux). Up till now
I used a script to generate a list of nearly identical html-files like
photo/01.html, photo/02.html, photo/03.html etc.. Every page constists
of a picture with caption an previous/next buttons. The photo's are
numbered 01.jpg, 02.jpg etc. The only varying content in the pages is
the size (horizontal/vertical) of the pictures and the captions. The
whole thing acts as a slide show. I want to write a PHP script which can
replace the list of HTML files, is such a thing possible? TIA, Max.
This is the site what it's about (not a plug!! I only want technical
information) http://www.maccusfoto.nl/
This is the Bash-script I use to generate the pages:
#!/bin/bash
clear
# Check for configuration file
if [ ! -f ./genpages.conf ]; then
echo -e "Configuration file not found! Aborting..."
exit 1
else
echo -e "Reading configuration file..."
sleep 1
. ./genpages.conf
echo -e "Done..."
fi
# Check for base directory
if [ ! -d "${ROOTDIR}" ]; then
echo -e "${ROOTDIR} Not found! Edit configuration file. Aborting..."
sleep 2
exit 1
fi
# Set direcory level
case ${DIRLEVEL} in
1) DIRLEVEL="../";;
2) DIRLEVEL="../../";;
3) DIRLEVEL="../../../";;
esac
LOOPCOUNT="0"
until [ "${LOOPCOUNT}" -ge "${PAGES}" ]; do
# Count the pages and set the variables
LOOPCOUNT="`expr ${LOOPCOUNT} + 1`"
if [ "${LOOPCOUNT}" -lt "10" ]; then
if [ "${LOOPCOUNT}" -eq "9" ]; then
NEXT="$(expr ${LOOPCOUNT} + 1)"
else
NEXT="0$(expr ${LOOPCOUNT} + 1)"
fi
LOOPCOUNT="0${LOOPCOUNT}"
PREV="0$(expr ${LOOPCOUNT} - 1)"
elif [ "${LOOPCOUNT}" -eq "10" ]; then
NEXT="$(expr ${LOOPCOUNT} + 1)"
PREV="0$(expr ${LOOPCOUNT} - 1)"
elif [ "${LOOPCOUNT}" -gt "10" ]; then
NEXT="$(expr ${LOOPCOUNT} + 1)"
PREV="$(expr ${LOOPCOUNT} - 1)"
fi
[ "${PREV}" = "00" ] && PREV="${DIRLEVEL}${STARTPAGE}"
[ "${LOOPCOUNT}" = "${PAGES}" ] && NEXT="${DIRLEVEL}${STARTPAGE}"
# Check for image files
if [ ! -f "${ROOTDIR}/${SUBDIR}/images/${LOOPCOUNT}.jpg" ]; then
echo "No image(s) found in ${ROOTDIR}/${SUBDIR}/images/"
# Create subdirectory
if [ ! -d "${ROOTDIR}/${SUBDIR}" ]; then
mkdir -p "${ROOTDIR}/${SUBDIR}/images/orig"
echo -e "Created ${ROOTDIR}/${SUBDIR}/images. Add your images."
fi
echo -e "Aborting..."
sleep 2
exit 1
fi
ORIENTATION="`identify ${ROOTDIR}/${SUBDIR}/images/${LOOPCOUNT}.jpg
| cut -d " " -f 3`"
WIDTH="`echo ${ORIENTATION} | cut -d "x" -f 1`"
HEIGHT="`echo ${ORIENTATION} | cut -d "x" -f 2`"
CAPTION="`grep "CAPTION_${LOOPCOUNT}=" ./genpages.conf | sed
"s|CAPTION_${LOOPCOUNT}=|| ; s|[\\\"]||g"`"
# Write the HTML pages
echo "Writing ${ROOTDIR}/${SUBDIR}/${LOOPCOUNT}.html"
echo -e "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0
Transitional//EN\"
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\">
<head>
<title>${GENERAL_TITLE} - Photo ${LOOPCOUNT}</title>
<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />
<meta http-equiv=\"refresh\" content=\"15;URL=${NEXT}.html\" />
<meta name=\"author\" content=\"Fotografie & Webdesign:
Maxim Heijndijk\" />
<meta name=\"copyright\" content=\"Fotografie & Webdesign:
Maxim Heijndijk\" />
<link rel=\"stylesheet\" type=\"text/css\"
href=\"${DIRLEVEL}../index.css\" />
</head>
<body oncontextmenu=\"return false\">
<table summary=\"${GENERAL_TITLE} - Photo ${LOOPCOUNT}\"
align=\"center\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\"
width=\"100%\" class=\"caption\">
<tr>
<td align=\"right\" valign=\"bottom\" width=\"10%\">
<a href=\"${PREV}.html\"><img alt=\"Previous\"
src=\"${DIRLEVEL}images/prev.gif\" border=\"0\" width=\"22\"
height=\"24\" /></a>
</td>
<td align=\"center\" valign=\"bottom\" width=\"80%\">
<a href=\"${NEXT}.html\"><img alt=\"${IMG_ALT}\"
src=\"images/${LOOPCOUNT}.jpg\" border=\"0\" width=\"${WIDTH}\"
height=\"${HEIGHT}\" /></a>
</td>
<td align=\"left\" valign=\"bottom\" width=\"10%\">
<a href=\"${NEXT}.html\"><img alt=\"Next\"
src=\"${DIRLEVEL}images/next.gif\" border=\"0\" width=\"22\"
height=\"24\" /></a>
</td>
</tr>
<tr>
<td align=\"center\" nowrap=\"nowrap\" valign=\"top\" width=\"100%\"
height=\"10%\" colspan=\"3\">
${CAPTION}
</td>
</tr>
</table>
</body>
</html>" > "${ROOTDIR}/${SUBDIR}/${LOOPCOUNT}.html"
done
# Backup the configuration
cp -f "./genpages.conf" "${ROOTDIR}/${SUBDIR}/genpages.conf"
exit 0
Re: Newbie: How to generate multiple web pages with one PHP script
In article <45b2547a$0$55713$dbd43001 [at] news.wanadoo.nl>, Maxim Heijndijk
<maccus_nospam [at] orange.nl> wrote:
> . ./genpages.conf
What does this configuration file look like? Could you post a few
lines? Everything else is a breeze to cover in PHP.
BTW... Instead of using `expr` you can use the following syntax in bash
and save a call to an external operation:
NEXT=$(($LOOPCOUNT+1))
You also have the option of coding your UNTIL loop:
until [ "${LOOPCOUNT}" -ge "${PAGES}" ]; do
like this:
for ((i=0;i<=$PAGES;i++)); do
# content...
done
And for the HTML bit, you could have used HEREDOC syntax so that you
didn't have to escape all the sub-quotes:
cat <<EOF > "${ROOTDIR}/${SUBDIR}/${LOOPCOUNT}.html"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=utf-8"/>
<title>This is a template</title>
</head>
<body>
<!-- content -->
</body>
</html>
EOF
--
Koncept <<
"The snake that cannot shed its skin perishes. So do the spirits who are
prevented from changing their opinions; they cease to be a spirit." -Nietzsche
Re: Newbie: How to generate multiple web pages with one PHP script
I didn't look through all that code you posted, but from your
description, what you're basicly asking for is a way to display a
dynamic page (with $_GET[] variables) as a static page. You can do a
google search for 'converting a dynamic url to a static url' or
something similar, but it's pretty simple, as long as you can make
..htaccess files.
http://www.google.com/search?q=converting+a+dynamic+url+to+a +static+url
Rather than rewrite what many, many others have said on their own
websites, I would suggest simply doing that google search, there are
dozens of great resources to explain how to easily convert dynamic
links to static ones. But once your htaccess file is set up properly,
all you have to do is create a php script that takes the same
parameters you're trying to pass in the static url and outputs what you
want. If you're still stuck, feel free to contact me.
Re: Newbie: How to generate multiple web pages with one PHP script
Koncept schreef:
> In article <45b2547a$0$55713$dbd43001 [at] news.wanadoo.nl>, Maxim Heijndijk
> <maccus_nospam [at] orange.nl> wrote:
>
>> . ./genpages.conf
>
> What does this configuration file look like? Could you post a few
> lines? Everything else is a breeze to cover in PHP.
>
> BTW... Instead of using `expr` you can use the following syntax in bash
> and save a call to an external operation:
>
> NEXT=$(($LOOPCOUNT+1))
>
> You also have the option of coding your UNTIL loop:
>
> until [ "${LOOPCOUNT}" -ge "${PAGES}" ]; do
>
> like this:
>
> for ((i=0;i<=$PAGES;i++)); do
> # content...
> done
>
> And for the HTML bit, you could have used HEREDOC syntax so that you
> didn't have to escape all the sub-quotes:
>
> cat <<EOF > "${ROOTDIR}/${SUBDIR}/${LOOPCOUNT}.html"
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
> <head>
> <meta http-equiv="Content-Type" content="text/html;
> charset=utf-8"/>
> <title>This is a template</title>
> </head>
> <body>
> <!-- content -->
> </body>
> </html>
> EOF
OK Thanx for the Bash tips... This is a config file:
ROOTDIR="/home/max/Development/current/website/sites/www.mac cusfoto.nl/photo"
SUBDIR="travel/spain"
DIRLEVEL="2"
GENERAL_TITLE="Spain"
PAGES="47"
STARTPAGE="travel"
IMG_ALT="© Maxim Heijndijk"
CAPTION_01="MONTSERRAT"
CAPTION_02="MONTSERRAT - Wedding"
CAPTION_03=""MONTSERRAT
CAPTION_04="BARCELONA - Tibidabo"
CAPTION_05="EL MASNOU - Fireworks, Ple de Riure del Masnou"
CAPTION_06="EL MASNOU"
Re: Newbie: How to generate multiple web pages with one PHP script
> OK Thanx for the Bash tips... This is a config file:
>
> ROOTDIR="/home/max/Development/current/website/sites/www.mac cusfoto.nl/photo"
> SUBDIR="travel/spain"
> DIRLEVEL="2"
> GENERAL_TITLE="Spain"
> PAGES="47"
> STARTPAGE="travel"
> IMG_ALT="© Maxim Heijndijk"
>
> CAPTION_01="MONTSERRAT"
> CAPTION_02="MONTSERRAT - Wedding"
> CAPTION_03=""MONTSERRAT
> CAPTION_04="BARCELONA - Tibidabo"
> CAPTION_05="EL MASNOU - Fireworks, Ple de Riure del Masnou"
> CAPTION_06="EL MASNOU"
Something like this should work for you. I hope I got the logic correct
given your specs. I have to admit that I didn't really take a lot of
time to figure out exactly what you were doing with the shell script:
<?php
// Config options
$rootDir =
"/home/max/Development/current/website/sites/www.maccusfoto. nl/photo";
$subDir = "travel/spain";
$generalTitle = "Spain";
$startPage = "travel";
$imgAlt = "© Maxim Heijndijk";
$dirLevel = 2;
$pages = 47;
$captions = array(
'MONTSERRAT',
'MONTSERRAT - Wedding',
'MONTSERRAT',
'BARCELONA - Tibidabo',
'EL MASNOU - Fireworks, Ple de Riure del Masnou',
'EL MASNOU'
);
// check for root directory
if(!is_dir($rootDir)){
trigger_error("{$rootDir} not found. Check config.", E_USER_ERROR);
}
// create path structure
$dirPath=str_repeat("../",$dirLevel);
// Get the page request from $_GET
if(isset($_GET['page'])&&is_numeric($_GET['page'])){
$_GP=$_GET['page']>0?$_GET['page']:1;
} else{$_GP=1;}
// Assign next, previous and current variables
if($_GP>1&&$_GP<$pages){
list($p,$n,$c)=array($_GP-1,$_GP+1,$_GP);
} elseif($_GP==1){
list($p,$n,$c)=array($pages,2,1);
} elseif($_GP==$pages){
list($p,$n,$c)=array($_GP-1,1,$_GP);
}
// Prefix variables with "0" if value is less than 10
// since that seems to be the naming convention here.
$preName=$p<10?"0{$p}":$p;
$curName=$c<10?"0{$c}":$c;
$nxtName=$n<10?"0{$n}":$n;
// Check for the the source image...
if(!is_file($curImg="{$rootDir}/{$subDir}/{$curName}.jpg")){
trigger_error("Missing image file $curImg",E_USER_ERROR);
}
// Get image properties
list($x,$y,$type,$attr)=getimagesize($curImg);
// Get the photo caption for this image
$caption=$captions[$c-1];
// Send template to browser
echo <<<HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>{$generalTitle} - Photo {$curName}</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="refresh"
content="15;URL={$_SERVER['PHP_SELF']}?page={$n}" />
<meta name="author" content="Fotografie & Webdesign: Maxim
Heijndijk" />
<meta name="copyright" content="Fotografie & Webdesign: Maxim
Heijndijk" />
<link rel="stylesheet" type="text/css" href="{$dirPath}../index.css"
/>
</head>
<body oncontextmenu="return false">
<table summary="{$generalTitle} - Photo {$curName}"
align="center" cellspacing="0" cellpadding="0"
border="0" width="100%" class="caption">
<tr>
<!-- Previous -->
<td align="right" valign="bottom" width="10%">
<a href="{$_SERVER['PHP_SELF']}?page={$p}"><img
alt="Previous" src="{$dirPath}images/prev.gif"
border="0" width="22" height="24" /></a>
</td>
<!-- Current -->
<td align="center" valign="bottom" width="80%">
<a href="{$_SERVER['PHP_SELF']}?page={$n}"><img
alt="{$imgAlt}" src="images/{$curName}.jpg"
border="0" {$attr} /></a>
</td>
<!-- Next -->
<td align="left" valign="bottom" width="10%">
<a href="{$_SERVER['PHP_SELF']}?page={$n}"><img
alt="Next" src="{$dirPath}images/next.gif"
border="0" width="22" height="24" /></a>
</td>
</tr>
<tr>
<td align="center" nowrap="nowrap" valign="top"
width="100%" height="10%" colspan="3">
{$caption}
</td>
</tr>
</table>
</body>
</html>
HTML;
?>
--
Koncept <<
"The snake that cannot shed its skin perishes. So do the spirits who are
prevented from changing their opinions; they cease to be a spirit." -Nietzsche
Re: Newbie: How to generate multiple web pages with one PHP script
Koncept schreef:
> Something like this should work for you. I hope I got the logic correct
> given your specs. I have to admit that I didn't really take a lot of
> time to figure out exactly what you were doing with the shell script:
Thank you so much! I already figured out some code, and it seems that
i'm on the right track, it looks a lot like the code you posted. I think
I will be able to tackle this one. Thanx again.
BTW, about the bash expr tip, I get this error if I use
NEXT=$(($LOOPCOUNT+1))
Writing /home/max/www/sites/www.maccusfoto.nl/photo/bands/01.html
Writing /home/max/www/sites/www.maccusfoto.nl/photo/bands/02.html
Writing /home/max/www/sites/www.maccusfoto.nl/photo/bands/03.html
Writing /home/max/www/sites/www.maccusfoto.nl/photo/bands/04.html
Writing /home/max/www/sites/www.maccusfoto.nl/photo/bands/05.html
Writing /home/max/www/sites/www.maccusfoto.nl/photo/bands/06.html
Writing /home/max/www/sites/www.maccusfoto.nl/photo/bands/07.html
../website-genpages: line 63: 08: value too great for base (error token
is "08")
Re: Newbie: How to generate multiple web pages with one PHP script
In article <45b4aca3$0$62666$dbd4b001 [at] news.wanadoo.nl>, Maxim Heijndijk
<maccus_nospam [at] orange.nl> wrote:
> NEXT=$(($LOOPCOUNT+1))
Not sure why that's occurring for you. Try this in a shell:
[prompt]$ a=10
[prompt]$ b=5
[prompt]$ echo $(($a+$b))
Does that echo 15 for you?
--
Koncept <<
"The snake that cannot shed its skin perishes. So do the spirits who are
prevented from changing their opinions; they cease to be a spirit." -Nietzsche
Re: Newbie: How to generate multiple web pages with one PHP script
Koncept schreef:
> In article <45b4aca3$0$62666$dbd4b001 [at] news.wanadoo.nl>, Maxim Heijndijk
> <maccus_nospam [at] orange.nl> wrote:
>
>> NEXT=$(($LOOPCOUNT+1))
>
> Not sure why that's occurring for you. Try this in a shell:
>
> [prompt]$ a=10
> [prompt]$ b=5
> [prompt]$ echo $(($a+$b))
>
> Does that echo 15 for you?
Yes.
Re: Newbie: How to generate multiple web pages with one PHP script
In article <45b62912$0$287$dbd49001 [at] news.wanadoo.nl>, Maxim Heijndijk
<maccus_nospam [at] orange.nl> wrote:
> Yes.
Something else in your code doing it then ;-)
--
Koncept <<
"The snake that cannot shed its skin perishes. So do the spirits who are
prevented from changing their opinions; they cease to be a spirit." -Nietzsche
Re: Newbie: How to generate multiple web pages with one PHP script
Koncept schreef:
> Something like this should work for you. I hope I got the logic correct
> given your specs. I have to admit that I didn't really take a lot of
> time to figure out exactly what you were doing with the shell script:
Well, I now have modified the script so the end and startpages are there
too. Also the script reads the captions and the variables from
includefiles. But I want to simplify my site even more:
At the moment, the directory structure is as follows:
index.html
photo/rroma/index.php
photo/rroma/captions.txt
photo/rroma/vars.inc
photo/travel/spain/index.php
photo/travel/spain/captions.txt
photo/travel/spain/vars.inc
links in index.html point to the index.php files. But the index.php
files are exactly the same, as they read vars and captions from textfiles.
What I want is an index.html with links that point to a single index.php
file. The links in the html file should contain querystrings that will
be passed to the phpscript so that the script knows from which directory
to get the captions and vars. so the structure will look like this:
index.html
index.php
photo/rroma/captions.txt
photo/rroma/vars.inc
photo/travel/spain/captions.txt
photo/travel/spain/vars.inc
How do I do that??
This is the script with include files up to now:
## index.php
<?php
// Read config from file
include("vars.inc");
// Create path structure
$dirPath=str_repeat("../",$dirLevel);
// Get the page request from $_GET
if(isset($_GET['page'])&&is_numeric($_GET['page'])){
$_GP=$_GET['page']>0?$_GET['page']:1;
}else{
$_GP=1;
}
// Assign next, previous and current variables
if($_GP>1&&$_GP<$pages){
list($p,$n,$c)=array($_GP-1,$_GP+1,$_GP);
}elseif($_GP==1){
list($p,$n,$c)=array($pages,2,1);
}elseif($_GP==$pages){
list($p,$n,$c)=array($_GP-1,1,$_GP);
}
// Prefix variables with "0" if value is less than 10
$preName=$p<10?"0{$p}":$p;
$curName=$c<10?"0{$c}":$c;
$nxtName=$n<10?"0{$n}":$n;
// First and last page must point to $startPage
if($startPage!==0&$c==$pages){
$rootDir = dirname($_SERVER['PHP_SELF']);
$next="{$rootDir}/${dirPath}${startPage}";
}else{
$next="{$_SERVER['PHP_SELF']}?page={$n}";
}
if($startPage!==0&$c==1){
$rootDir = dirname($_SERVER['PHP_SELF']);
$prev="{$rootDir}/${dirPath}${startPage}";
}else{
$prev="{$_SERVER['PHP_SELF']}?page={$p}";
}
// Check for the the source image...
if(!is_file($curImg="{$jpgDir}/images/{$curName}.jpg")){
trigger_error("Missing image file $curImg",E_USER_ERROR);
}
// Get image properties
list($x,$y,$type,$attr)=getimagesize($curImg);
// Get the photo caption for this image
$caption=$captions[$c-1];
// Send template to browser
echo <<<HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>{$generalTitle} - Photo {$curName}</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="refresh" content="15;URL={$next}" />
<meta name="author" content="Fotografie & Webdesign: Maxim
Heijndijk" />
<meta name="copyright" content="Fotografie & Webdesign: Maxim
Heijndijk" />
<link rel="stylesheet" type="text/css" href="{$dirPath}../index.css" />
</head>
<body oncontextmenu="return false">
<table summary="{$generalTitle} - Photo {$curName}" align="center"
cellspacing="0" cellpadding="0" border="0" width="100%" class="caption">
<tr>
<!-- Previous -->
<td align="right" valign="bottom" width="10%">
<a href="{$prev}"><img alt="Previous"
src="{$dirPath}images/prev.gif" border="0" width="22" height="24" /></a>
</td>
<!-- Current -->
<td align="center" valign="bottom" width="80%">
<a href="{$next}"><img alt="{$imgAlt}" src="images/{$curName}.jpg"
border="0" {$attr} /></a>
</td>
<!-- Next -->
<td align="left" valign="bottom" width="10%">
<a href="{$next}"><img alt="Next" src="{$dirPath}images/next.gif"
border="0" width="22" height="24" /></a>
</td>
</tr>
<tr>
<td align="center" nowrap="nowrap" valign="top" width="100%"
height="10%" colspan="3">
{$caption}
</td>
</tr>
</table>
</body>
</html>
HTML;
?>
## captions.txt
MONTSERRAT
MONTSERRAT - Wedding
MONTSERRAT
BARCELONA - Tibidabo
EL MASNOU - Fireworks, Ple de Riure del Masnou
## vars.inc
<?php
$jpgDir = dirname($_SERVER['SCRIPT_FILENAME']);
$subDir = "travel/spain";
$dirLevel = "2";
$generalTitle = "Spain";
$startPage = "travel.html";
$imgAlt = "© Maxim Heijndijk";
$captions = [at] file("captions.txt");
$pages = "47";
?>
PHP » alt.php » Newbie: How to generate multiple web pages with one PHP script