Jump to content

Recommended Posts

I am trying to do something where I have a bunch of HTML files containing articles, and includes to place them in one page.

 

IE the page is index.php and theres articles 1.html through 50.html

 

How do I use links to display a certain article with next and previous article buttons and such? I think includes are what I need?

How do I get this to work? I believe it won't work because the include won't do a variable.

There's something else wrong I'm not aware of also.

 

<html>
<body>
<?php

if (isset($_GET['a'])) {
      $a = $_GET['a'];
}
?> 

If this works, text appears here:<br />
<?php include("$a.html");?>
</body>
</html>

YAY! It works! I think I'm starting to get the hang of this crazy language.  ::)

 

<html>
<body>
<?php

if (isset($_GET['a']) && is_numeric ($_GET['a'])) {
      $a = $_GET['a'];
}
else {
echo "Sorry, you must have followed a dead link. Please return to the home page.";
}
if (isset($a))
{
echo "If this works, variable value here: ";
echo $a;
echo "And included page here: ";
include("$a.html");
}
?>
</body>
</html>

 

Although, there is one problem. For example, if I'm displaying 1.html, it will output:

If this works, variable value here: 1And included page here: 1.html content

How do I make it output like this?

If this works, variable value here:

1

And included page here:

1.html content

 

 

Thanks! Last question and I should (knock on wood) know enough for my web page.

 

A in my examples stands for an article number.

 

At the bottom I will have a next and previous link. Previous will be $p and next will be $n. So along these lines:

<?php
if ($a>1) {
$p = $a-1;
}
$n = $a+1;
?>

 

How do I use these values to make links? Will this work?

<?php
if ($a>1) {
$p = $a-1;
echo '<a href="$p.html">Previous</a><br />';
}
$n = $a+1;
echo '<a href="$n.html">Next</a><br />';
?>

Got the right idea, but since you are using single quotes, all variables inside are seen as they are not as the variable. You need to come out of the single quotes to get the value.

 

<?php
if ($a>1) {
$p = $a-1;
echo '<a href="'.$p.'.html">Previous</a><br />';
}
$n = $a+1;
echo '<a href="'.$n.'.html">Next</a><br />';
?>

 

Ray

 

Also, you need to set a floor for your article count.  Obviously there won't be a 0 article, but you don't want a link that shows "Article 0" because of the counter.  You can either set it to look to the max count of articles (which would be 50<->0<->1) or just say : if($a)== 0; display a greyed out link that isn't clickable, or nothing at all.

Exactly. You must have missed it.

<?php
if ($a>1) {
$p = $a-1;
echo '<a href="'.$p.'.html">Previous</a><br />';
}
$n = $a+1;
echo '<a href="'.$n.'.html">Next</a><br />';
?>

 

So it's  checking to make sure $a is greater than one and if it's not than it will not display a previous link.

And eventually I'll add this:

if ($a< some number) {
$n = $a+1;
echo '<a href="'.$n.'.html">Next</a><br />';
}

 

So I believe that's it. PHP isn't too hard :P

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.