Page 2 of 3
Posted: Mon Jan 31, 2005 5:31 pm
by John Cartwright
as for assigning variables properly, its done like this
Code: Select all
<?php
$nextlink = '<a href="?comicID='.($comicID+1).'">Next</a>';
$prevlink = '<a href="?comicID'.($comicID-1).'">Previous</a>';
as for comparing two things to see if they are equal, use == instead of =
as for your logic,

Posted: Mon Jan 31, 2005 5:38 pm
by John Cartwright
Your going to end up doing doing like this
Code: Select all
$num_sql = "SELECT COUNT(*) FROM `comics`";
$num_query = mysql_query($num_sql) or die(mysql_error());
//num_query now holds the total amount of rows
if (!empty($_GETї'comicid']))
{
$sql_comic = "SELECT * FROM `comics` WHERE `comicid` = '".$_GETї'comicid']."' LIMIT 1"
$comic_query = mysql_query($comic_sql) or die(mysql_error());
$row = mysql_fetch_assoc($comic_query);
// run all your checks and determine what your links are going to have to look like...
}
else
{
// run your default stuff
}
Thats a start...
Posted: Tue Feb 01, 2005 1:27 am
by Dan++
As for my logic? I see this as a very efficient way of doing it. Then again, I'm not the expert - you are. This is the entire code, and it works exactly how I want it to. I guess there are security issues I need to address, right?
Code: Select all
<?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']))
{
$query='SELECT * FROM Comic ORDER BY comicID DESC LIMIT 1';
}
else
{
$comicID = $_GETї'comicID'];
$query="SELECT * FROM Comic WHERE comicID= '$comicID' ";
}
$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>
</head>
<body>
<?php
$nextlink = '<a href="?comicID='.($comicID+1).'">Next</a>';
$prevlink = '<a href="?comicID='.($comicID-1).'">Previous</a>';
print($comicTitle.'<br>');
if ($comicID==1)
{
print($nextlink.' | Previous');
}
elseif ($comicID='SELECT * FROM Comic ORDER BY comicID DESC LIMIT 1')
{
print('Next | '.$prevlink);
}
else
{
print($nextlink.' | '.$prevlink);
}
?>
<br>
<img src="<?php print($comicURL); ?>" alt="Spin City 17">
</body>
</html>
Posted: Tue Feb 01, 2005 1:31 am
by timvw
now we'll wait untill you remove an entry..
so comicids become fe: 1, 2, 4, 5 and your script doesn't work anymore if you go from 4 to 2.
Posted: Tue Feb 01, 2005 9:47 am
by magicrobotmonkey
Code: Select all
elseif ($comicID='SELECT * FROM Comic ORDER BY comicID DESC LIMIT 1')
This line is always going to return true.
Posted: Tue Feb 01, 2005 10:43 am
by Dan++
magicrobotmonkey wrote:Code: Select all
elseif ($comicID='SELECT * FROM Comic ORDER BY comicID DESC LIMIT 1')
This line is always going to return true.
Hmm...it seems you are correct. Anything you can suggest?
timvw wrote:now we'll wait untill you remove an entry..
so comicids become fe: 1, 2, 4, 5 and your script doesn't work anymore if you go from 4 to 2.
That's assuming I
will remove an entry....but since I'm also working on a script to add comics, perhaps there could be a way of deleting them from the script, too. Any suggestions, perhaps?
And thanks again. You've all been a great help.
Posted: Tue Feb 01, 2005 11:28 am
by magicrobotmonkey
you need some more queries to get the previous and next comics and find out if they exist.
1. You need on where it equals comicID to get the current info (you already have this)
2. You need one where it gets the nest comic id - not necesarily comicID+1 but rather > comicid. and if its empty, no next link
3. You need one to get the previous comic id - again not necesarily comic
id-1 but rather < comicid. And if it's empty, no previous link
Posted: Tue Feb 01, 2005 11:41 am
by Dan++
So you mean:
Code: Select all
elseif (empty(SOMETHING > $comicID))
??
What would the SOMETHING be?
Posted: Tue Feb 01, 2005 11:58 am
by magicrobotmonkey
no you need to use a query like the one you attempted to use in the elseif:
Code: Select all
$query='SELECT comicID FROM Comic WHERE comicID > '.$comicID.' ORDER BY comicID DESC LIMIT 1';
$result=mysql_query($query)
or die ("Could not execute query: ". mysql_error());
//now see if there was one
if(mysql_num_rows) != 1)
{
//no next comic so no link!
}
else
{
//there is a next comic, so display the link and the id of the next comic is...
list($nextComicID) = mysql_fetch_row($result);
//now use $nextComicID in your 'next' link
}
and something very similar for the previous link
note: untested
Posted: Tue Feb 01, 2005 12:05 pm
by feyd
untested query
Code: Select all
(SELECT comicID FROM Comic WHERE comicID = '$comicID' LIMIT 1)
UNION( SELECT comicID FROM Comic WHERE comicID < '$comicID' ORDER BY comicID DESC LIMIT 1 )
UNION( SELECT comicID FROM Comic WHERE comicID > '$comicID' ORDER BY comicID ASC LIMIT 1 )
if it works, it will provide the current comic, previous comic and next comic in each row respectively... provided UNION is supported by your version of mysql.
Posted: Tue Feb 01, 2005 12:10 pm
by magicrobotmonkey
Heh I always forget about union because we only have stupid mysql3.23 at work.
Posted: Tue Feb 01, 2005 12:56 pm
by Dan++
Is UNION part of the query, as in:
Code: Select all
$query2='UNION( SELECT comicID FROM Comic WHERE comicID > '$comicID' ORDER BY comicID ASC LIMIT 1 )';
$result2=mysql_query($query2);
And then the elseif being:
Code: Select all
elseif (empty(mysql_fetch_array($result2)))
{
print('Next | '.$prevlink);
}
Right?
Cheers again.
Posted: Tue Feb 01, 2005 1:03 pm
by magicrobotmonkey
no you only have one query - the one feyd wrote and it returns 3 rows, the first being the current comic, the second being the previous comic and the third being the next comic
Posted: Tue Feb 01, 2005 1:45 pm
by Dan++
But I only want to know if the next comic is empty or not...I'm getting confused now...

Posted: Tue Feb 01, 2005 1:52 pm
by magicrobotmonkey
I think it would be better for you to know what then next comic's ID is rather than simply relying on it being current+1. This way if your ids ever get out of sequence, you won't have to worry about it. Its better design than depending on your IDs always going up by one, which could be unreliable.