2

i have over one hundred html files that all roughly have the same structure. the html files represent each page of a comic.

naturally, i've realized that it would be more efficient to apply the structure of these pages to one file, and allow that file to dynamically create all of the comic pages.

my goals are so that, one; when a user accesses a comic page, the page number that the link contained can be passed through as a number value to determine the image file and the url.

two; when a user clicks "next" or "previous", the number value that defines the page is either added to or subtracted by one to load in the new url & image file.

and three; for the "next" and "previous" buttons to define themselves/decide whether to appear or not based on a filecheck i've already written.

<?php
if ($_GET["img"]) {
    $imgnum = $_GET["img"];
} else { $imgnum = 1; }

?>

<html>
    <div class="block">
        <?php echo '<img class="page" src="' . $imgnum . '.png">'; ?>
    </div> 
</html>

this code does the trick for changing the image file and url based on the value of $imgnum but when it comes to passing values into the "next" and "prev" buttons, i have no idea what the hell im doing. i tried doing THIS:

<?php $prevValue = $imgnum - 1?>
<a href="?img=<?php$prevValue?>"><h2 class="arrow">Previous</h2> </a>

clearly, passing a variable into that key does not work. is there any way to preserve the dynamism of the buttons with this php method? these buttons need variables to work on their own. do i have to switch to jquery? i honestly above all else need advice on where to start. i barely understand what's going on in this php script itself.

0

2 Answers 2

3
<?php
if ($_GET["img"]) {
    $imgnum = $_GET["img"];
} else { $imgnum = 1; }

?>

What the above does.....

If (the URL string contains the [img] value ) {

     Get the value of [?img=] from the URL string... 
     i.e. index.php?img=4 .. (it is getting the 4) 
     and set the [$imgnum] variable to that value

} else {

        But if the URL does NOT contain the [img=] value, 
        then set the variable value to 1 [$imgnum = 1;]

}

It's difficult to tell exactly what you want, but the below will add the value properly to the button.

<?php $preValue = $imgnum - 1; ?>
<a href="?img=<?php echo $preValue; ?>"><h2 class="arrow">Previous</h2> </a>

PHP lines end with a semicolon... and you have to echo variables for them to be output to the HTML.

I have no clue what jQuery and javascript have to do with your question. But yes.. this could all be handled by an ajax request and you wouldn't need to reload the page every time a button is clicked.

However, if you don't understand what the PHP is already doing, explaining how to do this via AJAX will be exceptionally confusing to you. And much more of your code structure would need to be seen.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you for explaining that piece of code! so helpful. and yeah, that syntax correction does cover it. heres the page ive been using to test (is it bad to give this away on the internet?): spicyyeti.com/test/dynamic.php
Sanitizing the variable as @Lala suggest is never a bad thing. However, if you aren't pulling data from a database based on the URL values, chances of injection are minimal. PHP used to just walk through variables (like the code above) doesn't offer any real "meat" to anyone that may be looking to maliciously hack a site. Injection means.. things like spicyyeti.com/test/dynamic.php?img=hacked where the url is used to get the PHP to see something other than what was intended.
3

You're creating the right code but you just forgot to echo it.

You need to change your line to;

<a href="?img=<?php echo $prevValue;?>"><h2 class="arrow">Previous</h2> </a>

or you can use the shorter version if your php.ini configuration allows to;

<a href="?img=<?=$prevValue?>"><h2 class="arrow">Previous</h2> </a>

Note:

You should be aware of SQL injection and secure your get variables by;

$imgnum = (int)$_GET["img"];

As you are taking the $_GET value as a variable, this time, only integer values will work so hackers cannot inject a code.

1 Comment

ah, a syntax error. typical me. i will do extra research on "sql injection", though. what do you mean when you say "...so hackers cannot inject a code"? what would this be doing to my site?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.