Webcomic Script Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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 =

Code: Select all

if ($comicID== 1)
as for your logic, 8O
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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&#1111;'comicid']))
&#123;
$sql_comic = "SELECT * FROM `comics` WHERE `comicid` = '".$_GET&#1111;'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... 

&#125;
else
&#123;

// run your default stuff

&#125;
Thats a start...
Dan++
Forum Newbie
Posts: 12
Joined: Sun Jan 30, 2005 5:02 pm

Post 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&#1111;'comicID']))
	&#123;
		$query='SELECT * FROM Comic ORDER BY comicID DESC LIMIT 1';
    &#125;
    else
    &#123;
    	$comicID = $_GET&#1111;'comicID'];
		$query="SELECT * FROM Comic WHERE comicID= '$comicID' ";
    &#125;
    $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)
	&#123;
		print($nextlink.' | Previous');
	&#125;
	elseif ($comicID='SELECT * FROM Comic ORDER BY comicID DESC LIMIT 1')
	&#123;
		print('Next | '.$prevlink);
	&#125;
	else
	&#123;
		print($nextlink.' | '.$prevlink);
	&#125;
?>
<br>
<img src="<?php print($comicURL); ?>" alt="Spin City 17">
</body>
</html>
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

Code: Select all

elseif ($comicID='SELECT * FROM Comic ORDER BY comicID DESC LIMIT 1')
This line is always going to return true.
Dan++
Forum Newbie
Posts: 12
Joined: Sun Jan 30, 2005 5:02 pm

Post 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.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post 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
Dan++
Forum Newbie
Posts: 12
Joined: Sun Jan 30, 2005 5:02 pm

Post by Dan++ »

So you mean:

Code: Select all

elseif (empty(SOMETHING > $comicID))
??

What would the SOMETHING be?
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post 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)
&#123;
    //no next comic so no link!
&#125;
else
&#123;
    //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
&#125;
and something very similar for the previous link


note: untested
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

Heh I always forget about union because we only have stupid mysql3.23 at work.
Dan++
Forum Newbie
Posts: 12
Joined: Sun Jan 30, 2005 5:02 pm

Post 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)))
	&#123;
		print('Next | '.$prevlink);
	&#125;
Right?

Cheers again.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post 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
Dan++
Forum Newbie
Posts: 12
Joined: Sun Jan 30, 2005 5:02 pm

Post by Dan++ »

But I only want to know if the next comic is empty or not...I'm getting confused now... :?
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post 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.
Post Reply