VaultNetwork.netVault Network Boards
Author Topic: Ok board for PHP/SQL help? Looking to make first/previous/next/last buttons [Locked]
Phases_OgMaxim  1 star
Posts: 52
Registered: 2002-7-16 16:32:23
EDIT: Resolved! See last post.


Hey all. Long time member - no longer active poster but..


Many, many, many a moons ago I used this forum for some help with some web work I did and it seemed good then so I bookmarked it.


Fast forward a few years, and here we are. I'm hoping it's still okay for this. If not, mods feel free to move or delete.


Ok, So I'm brining my little stick comic back to life and would like to figure out how to do a first/previous/next/last buttons.


I'd like to be able to just save the images as filename x.jpg, x+1.jpg, x+2.jpg, etc. and have it automajically use the highest number it finds, on the main page, if possible. Obviously 'last' could just point to the main page url. Unfortunately the other three aren't so easy.


I've got html (obviously), php, and sql to work with. I've created a database in sql and have my dbconnect file set up already.


It's been several years since I've done anything web related so, bear with me. Here's the site in question:


http://www.superstickphase.com


Thanks for any help.

 

-----signature-----
Every forum should have a Phases.
Bringing SSP back to life at http://www.superstickphase.com !
Retired AC/AC2 - Phases Og'Maxim - Leafcull
Raiztlin  2 stars
Title: Dick Tracy
Posts: 397
Registered: 2002-1-23 08:10:37
<?php

if (isset($_GET['id'])) {

$comic_number = $_GET['id'];

$next = $comic_number+1;

$prev = $comic_number-1;

}

?>

<a href=\"mysite.com/comic.php?id={$next}\">Next Comic</a>

<a href=\"mysite.com/comic.php?id={$prev}\">Prev Comic</a>


Something like that. not very elegant, but it should work.


Keep in mind that I just typed this up from the top of my head, and it probably won't work without some tweaking

 

-----signature-----
I has a flavor!
CC always welcome.
Phases_OgMaxim  1 star
Posts: 52
Registered: 2002-7-16 16:32:23
Hey thanks for the reply. I wish I'd seen this before I went for a nap!


I was layin there thinking about it had logic like this churning . But, would that display the newest one on main page?


Alright well Imma try it real quick.


Thanks again for the reply!

 

-----signature-----
Every forum should have a Phases.
Bringing SSP back to life at http://www.superstickphase.com !
Retired AC/AC2 - Phases Og'Maxim - Leafcull
Phases_OgMaxim  1 star
Posts: 52
Registered: 2002-7-16 16:32:23
EDITED Ok. Sweet! Partially working. From what I can tell:


I have it displaying the right image, and the next previous buttons working, when you put in the id=x in the URL manually. Now I need to figure out how to make it automatially pull the highest number it can find out of the images folder. Or is that possible? Edit: ok, I've set a variable "newest" that I'llj ust change anytime I upload a picture, no big deal. That makes the "Last" button work. So, What I still need to get working:


-when people come to www.url.com/index.php it will automatically put the ?id=$newest in there.

-make the previous button stop at 1.jpg, not allow going to 0, -1, -2. I guess just an if then?

-make next button stop at current, not allow going to current+1 if current is the highest. if current=newest then...


I hope that's doable. thanks for your help so far, got me started!


Here's my source thus far:


<?php

$newest = 4;

if (isset($_GET['id'])) {

$current = $_GET['id'];

$next = $current+1;

$prev = $current-1;

}

?>


<!-- CONTENTS TABLE -->

<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0" align="center">

<tr>

<td width="14%" align="center"><div class="buttonwrapper"><a class="squarebutton" href="index.php?id=<?php echo ("{$prev}" ?>"><span>Previous</span></a></div>

<br><br><div class="buttonwrapper"><a class="squarebutton" href="/index.php?id=1"><span>First</span></a></div></td>

<td width="72%" align="center"><?php echo ("<img src=images/{$current}.jpg>"; ?></img></td>

<td width="14%" align="center"><div class="buttonwrapper"><a class="squarebutton" href="index.php?id=<?php echo ("{$next}" ?>"><span>Next</span></a></div>

<br><br><div class="buttonwrapper"><a class="squarebutton" href="index.php?id=<?php echo ("{$newest}" ?>"><span>Last</span></a></div></td>

</tr>

</table>

<!-- CONTENTS TABLE -->


I will edit more into this post as I come up with stuff.


This is fun!


Thanks again!

 

-----signature-----
Every forum should have a Phases.
Bringing SSP back to life at http://www.superstickphase.com !
Retired AC/AC2 - Phases Og'Maxim - Leafcull
Phases_OgMaxim  1 star
Posts: 52
Registered: 2002-7-16 16:32:23
EDITED:


... almost done! This is all that's left(!):


I need to figure out:


if (id (someone manual puts in URL to be slick) > $newest OR < 1) goto (a custom 404 type page)


I take it I need to edit this:


<?php

$newest = 4;

$current = $newest;

$next = $current+1;

$prev = $current-1;

if (isset($_GET['id'])) {

$current = $_GET['id'];

$next = $current+1;

$prev = $current-1;

}

?>


to include an AND in the IF to say {if over $newest or under 1 goto 'whatever'} then after the if an else { goto index.php?id=$newest }


Man this is fun. Why did I ever quit making little sites?

 

-----signature-----
Every forum should have a Phases.
Bringing SSP back to life at http://www.superstickphase.com !
Retired AC/AC2 - Phases Og'Maxim - Leafcull
Phases_OgMaxim  1 star
Posts: 52
Registered: 2002-7-16 16:32:23
Got it!


Thanks a ton for getting me started on the right track! A little common sense later (and no googling, gasp!) I got it all working. Index.php pulls up newest, previous and next work only in the array of comics uploaded, first and last take to first and latest, and someone being slick changing the ID manually will go to a 404 comic.


Sweet! Thanks again. here's my final code if you're interested.


<?php

$newest = 4;

$current = $newest;

$next = $current+1;

$prev = $current-1;

if (isset($_GET['id'])) {

$current = $_GET['id'];

$next = $current+1;

$prev = $current-1;

}

if ($current > $newest) {

$current = -404;

$next = $newest;

$prev = 1;

}

if ($current < 1) {

$current = -404;

$next = $newest;

$prev = 1;

}

?>


<!-- CONTENTS TABLE -->

<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0" align="center">

<tr>

<td width="14%" align="center"><div class="buttonwrapper"><a class="squarebutton" href="index.php?id=<?php if ($current < 2) { echo ("1";} else { echo ("{$prev}"; } ?>"><span>Previous</span></a></div>

<br><br><div class="buttonwrapper"><a class="squarebutton" href="/index.php?id=1"><span>First</span></a></div></td>

<td width="72%" align="center"><?php echo ("<img src=images/{$current}.jpg>"; ?></img></td>

<td width="14%" align="center"><div class="buttonwrapper"><a class="squarebutton" href="index.php?id=<?php if ($current < $newest) { echo ("{$next}";} else { echo ("{$newest}"; } ?>"><span>Next</span></a></div>

<br><br><div class="buttonwrapper"><a class="squarebutton" href="index.php?id=<?php echo ("{$newest}" ?>"><span>Last</span></a></div></td>

</tr>

</table>

<!-- CONTENTS TABLE -->

 

-----signature-----
Every forum should have a Phases.
Bringing SSP back to life at http://www.superstickphase.com !
Retired AC/AC2 - Phases Og'Maxim - Leafcull
Raiztlin  2 stars
Title: Dick Tracy
Posts: 397
Registered: 2002-1-23 08:10:37
Ah, nice. Sorry I didn't check this earlyer :]


As for automating everything, check this out:


<?php

//Make a function to find the newest file, and output only the filename.

function find_newest () {

$dir = 'images'; //Set the dir where the images reside.

$files = scandir($dir); // Scan the dir, and load all elements into an array.

$file = end($files); // Find last element in array, should be last image in dir.

$newest = explode(".",$file); // Stril the .jpg part of the filename.

$newest = $newest[0]; // set $newest to be the filename.


return $newest;

}


$newest = find_newest(); // run the function.

print ($newest); // echo number of last file.

 

-----signature-----
I has a flavor!
CC always welcome.
Phases_OgMaxim  1 star
Posts: 52
Registered: 2002-7-16 16:32:23
Duuuuude, that is awesome. I didn't think I could automate it witout SQL.


Works great! Thanks!


(it returned the newest and outputted the number on the page, so i just removed the print line)

 

-----signature-----
Every forum should have a Phases.
Bringing SSP back to life at http://www.superstickphase.com !
Retired AC/AC2 - Phases Og'Maxim - Leafcull
Raiztlin  2 stars
Title: Dick Tracy
Posts: 397
Registered: 2002-1-23 08:10:37
Check this out.


http://pastecode.org/6267


Should work pretty well with using just $newest, $current, $prev and $next for output.


Edit: Doesn't work any better then your code, but it looks a bit cleaner, so should be a bit easyer to maintin / document / whatever :>


Edit2: you might have troubles once you start running into double digits, 10 would be listed before 2 and so on. so it might be clever to call your files 001.jpg instead of just 1.jpg

 

-----signature-----
I has a flavor!
CC always welcome.
Phases_OgMaxim  1 star
Posts: 52
Registered: 2002-7-16 16:32:23
Good call, but when I change them 001 etc, the $next and $prev appear to omit the zeros..


i might have to just do 1....9..11..19..20..29.. ?


edit, that didn't work either..


My original method works, where it's just $current+/-1 so it's not a show stopper, thankfully, but I'd like to get the automated working.


000's or not it seesm to nly go 1-9 then stop, counts 9 as newest.


I guess i could name them


1-9

91-99

991-999

but that wouldn't work either, cuz on 9 it would try to go to 10. hmm..

 

-----signature-----
Every forum should have a Phases.
Bringing SSP back to life at http://www.superstickphase.com !
Retired AC/AC2 - Phases Og'Maxim - Leafcull

VaultNetwork.net is an independently operated community forum and is not affiliated with, endorsed by, or technically based on IGN, GameSpy, FilePlanet, GameStats, or the former IGN/GameSpy Vault Network.
References to VaultNetwork.net mean this site/domain. VNBoards-style presentation is a visual homage only. By using this site, you agree to the forum rules.