I'm creating a simple(ish) webcomic script. So far it's not so bad, I've had alot of help from other places but I think they may have given up.
I've got three scripts: viewcomic.php, addcomicform.php and addcomic.php.
Viewcomic is run with ?comicID= after it. And at the moment, if viewcomic.php is not followed by a ?comicID= it just goes to the latest comic (the greatest comicID in the table). It shows the comic title and a link to the next and previous comics (however, if it's at the first there will be no previous and if it's at the last there will be no next).
That's all fine then, but near the bottom, it starts a new <?php part. First of all, I've been told that I shouldn't assume the next comicID will be one higher than the current. I know there's something to do with a query (similar to the UNION one below it) but I'm not sure exactly how to do this. Anyone got any idea? Then it goes to the IF statements. The IF and ELSE are okay, but the ELSEIF I'm having trouble with. I want it to display the previouslink and the text "Next" without a link, if[/if] you are viewing the last comic (the greatest value of comicID). I was told it has something to do with the UNION query, but I really can't be sure how to use this.
Keep in mind I'm a bit of a newbie with PHP & MySQL and I only got so far with this script before getting confused, so I would be eternally grateful if you could tell me what to do and how to do it (as in, the code). Thank you so, so much, if you can help me.
<?php
$dbhost = 'localhost';
$dbname = 'silverf_website';
$dbuser = 'silverf_dan';
$dbpasswd = '*******';
$connection=mysql_connect($dbhost,$dbuser,$dbpasswd)
or die("Could not connect to server");
$db=mysql_select_db($dbname,$connection)
or die("Could not select database");
if (empty($_GET['comicID'])) //Checking if there is no ?comicID= after viewcomic.php
{
$query='SELECT * FROM Comic ORDER BY comicID DESC LIMIT 1'; //Here, it's finding the greatest 'comicID' in the 'Comic' table.
}
else
{
$comicID = $_GET['comicID'];
$query="SELECT * FROM Comic WHERE comicID= '$comicID' "; // if there is a ?comicID= after viewcomic.php, it will go to the number it is
}
$result=mysql_query($query)
or die ("Could not execute query: ". mysql_error());
//Get the results
$row=mysql_fetch_array($result);
extract($row);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Spin City 17 - <?php print($comicTitle); ?></title> <!--Gets the title from the table 'comic'-->
</head>
<body>
<?php
$nextlink = '<a href="?comicID='.($comicID+1).'">Next</a>'; //this is the link to use to go to the next comic
$prevlink = '<a href="?comicID='.($comicID-1).'">Previous</a>'; //this is the link to use to go to the previous comic
$query2='UNION( SELECT comicID FROM Comic WHERE comicID > '$comicID' ORDER BY comicID DESC LIMIT 1 )';
$result2=mysql_query($query2);
print($comicTitle.'<br>');
if ($comicID==1) //If the comicID being viewed is one
{
print($nextlink.' | Previous'); //display the next link but not the previous link
}
elseif (empty(mysql_fetch_array($result2)))
{
print('Next | '.$prevlink);
}
else
{
print($nextlink.' | '.$prevlink);
}
?>
<br>
<img src="<?php print($comicURL); ?>" alt="Spin City 17">
</body>
</html>
By the way, I am having a little trouble with addcomic.php but I think with the solution to this I could work out a solution to that.