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

Dan++
Forum Newbie
Posts: 12
Joined: Sun Jan 30, 2005 5:02 pm

Webcomic Script Problem

Post by Dan++ »

I'm creating a script to display different strips of a webcomic. It's all going okay so far (as you can see), except for a few issues.

I want the default value when viewcomic.php is run to be the latest comic (i.e. the greatest value of comicID in the table), and so that when you are on the latest comic the 'next' link does not work, and on the first comic the 'previous' button does not work.

I tried an IF statement but I had trouble getting the URL within that to work. So, can anyone help me with any of these problems? Thanks in advance if you can. Here's the script at the moment, which is located at http://www.silverferret.co.uk/sc17/view ... ?comicID=1 :

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");
    $comicID = $_GET&#1111;'comicID'];
    $query="SELECT * FROM Comic WHERE comicID=$comicID";
    $result=mysql_query($query)
        or die ("Could not execute query");
    //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 print($comicTitle);?><br>
<a href="<?php print('http://www.silverferret.co.uk/sc17/viewcomic.php?comicID='.($comicID+1)); ?>">Next</a> | <a href="<?php print('http://www.silverferret.co.uk/sc17/viewcomic.php?comicID='.($comicID-1)); ?>">Previous</a>
<br>
<img src="<?php print($comicURL); ?>" alt="Spin City 17">
</body>
</html>
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Take a look at mysql_num_rows() then you'll be able to get the data in the last row so you can build your if statement. Also, the data in row 0 will give you the first row.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

that won't work. He is only selecting one row from the db at a time so mysql_num_rows will always be 1
hunterhp
Forum Commoner
Posts: 46
Joined: Sat Jan 22, 2005 5:20 pm
Contact:

Post by hunterhp »

Use this to get the default value to be the most recent id

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 (!emtpy($_GET&#1111;'comicID'])) &#123;
    $comicID = $_GET&#1111;'comicID'];
    $query="SELECT * FROM Comic WHERE comicID=$comicID";
    &#125;
    else
    &#123;
    $query = "SELECT * FROM Comics ORDERBY id DESC";
    &#125; 
    $result=mysql_query($query)
        or die ("Could not execute query");
    //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 print($comicTitle);?><br>
<a href="<?php print('http://www.silverferret.co.uk/sc17/viewcomic.php?comicID='.($comicID+1)); ?>">Next</a> | <a href="<?php print('http://www.silverferret.co.uk/sc17/viewcomic.php?comicID='.($comicID-1)); ?>">Previous</a>
<br>
<img src="<?php print($comicURL); ?>" alt="Spin City 17">
</body>
</html>
I haven't tested the code, but it should work. Alternatively, you can chang !empty with !isset
thegreatone2176
Forum Contributor
Posts: 102
Joined: Sun Jul 11, 2004 1:27 pm

Post by thegreatone2176 »

they seemed to answer your orginial question but i wanted to point out that this code is very unsecure

you simply assign the variable without modifying it at all. Search the forum for thing such as str_replace addslashes and check the security section
Dan++
Forum Newbie
Posts: 12
Joined: Sun Jan 30, 2005 5:02 pm

Post by Dan++ »

hunterhp wrote:Use this to get the default value to be the most recent id

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 (!emtpy($_GET&#1111;'comicID'])) &#123;
    $comicID = $_GET&#1111;'comicID'];
    $query="SELECT * FROM Comic WHERE comicID=$comicID";
    &#125;
    else
    &#123;
    $query = "SELECT * FROM Comics ORDERBY id DESC";
    &#125; 
    $result=mysql_query($query)
        or die ("Could not execute query");
    //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 print($comicTitle);?><br>
<a href="<?php print('http://www.silverferret.co.uk/sc17/viewcomic.php?comicID='.($comicID+1)); ?>">Next</a> | <a href="<?php print('http://www.silverferret.co.uk/sc17/viewcomic.php?comicID='.($comicID-1)); ?>">Previous</a>
<br>
<img src="<?php print($comicURL); ?>" alt="Spin City 17">
</body>
</html>
I haven't tested the code, but it should work. Alternatively, you can chang !empty with !isset
Tried both, but they don't seem to work. I get a "Could not execute query" error. It's odd, since it does look like it should work. I even tried $query='SELECT * FROM Comics ORDER BY DESC comicID'; - how very odd. Thanks anyway.
thegreatone2176 wrote:they seemed to answer your orginial question but i wanted to point out that this code is very unsecure

you simply assign the variable without modifying it at all. Search the forum for thing such as str_replace addslashes and check the security section
I realise it's not totally secure yet. For one I need to ensure that typing in strings won't mess it up, and typing in a number too high will go to the latest one.

What do you mean about modifying the variable though?

Also, I would like the 'next' link to be disabled on the latest strip and the 'previous' link to be disabled on the first, if anyone could help with that, too.

Thanks for the help so far, by the way!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

SELECT * FROM `Comics` ORDER BY `comicID` DESC
Dan++
Forum Newbie
Posts: 12
Joined: Sun Jan 30, 2005 5:02 pm

Post by Dan++ »

It's not working - however, I think I may be close now:

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 MAX(comicID) FROM Comic';
    &#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");
    //Get the results
    $row=mysql_fetch_array($result);
    extract($row);
?>
I've found using a MAX clause almost works - however, the results are not quite what I wanted. The 'Query could not be executed' message is not displayed. See for yourself. It's showing most parts, but it's supposed to go to comicID=2, showing the comic entitled "Sunrise 2". I've tried out a few things but it's not working. The part you want to be looking at for the error is in the first 'if' statement.

Thanks to whoever can helped or has helped so far.
Last edited by Dan++ on Mon Jan 31, 2005 3:11 pm, edited 1 time in total.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

actually, feyd's query DOES work

Code: Select all

//the most recent comic:
SELECT * FROM Comics ORDER BY comicID DESC LIMIT 1

//the requested comic:
SELECT * FROM Comics WHERE comcID=$comicid LIMIT 1

// the following comic:
SELECT * FROM Comics WHERE comicID > $comicid ORDER BY comicID ASC LIMIT 1

// the previous comic:
SELECT * FROM Comics WHERE comicID < $comicID ORDER BY comicID DESC LIMIT 1
Dan++
Forum Newbie
Posts: 12
Joined: Sun Jan 30, 2005 5:02 pm

Post by Dan++ »

timvw wrote:actually, feyd's query DOES work

Code: Select all

//the most recent comic:
SELECT * FROM Comics ORDER BY comicID DESC LIMIT 1

//the requested comic:
SELECT * FROM Comics WHERE comcID=$comicid LIMIT 1

// the following comic:
SELECT * FROM Comics WHERE comicID > $comicid ORDER BY comicID ASC LIMIT 1

// the previous comic:
SELECT * FROM Comics WHERE comicID < $comicID ORDER BY comicID DESC LIMIT 1
Well I'd agree, it should work, but this doesn't work:

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 Comics 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");
    //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 print($comicTitle);?><br>
<a href="<?php print('?comicID='.($comicID+1)); ?>">Next</a> | <a href="<?php print('?comicID='.($comicID-1)); ?>">Previous</a>

<br>
<img src="<?php print($comicURL); ?>" alt="Spin City 17">
</body>
</html>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

change

Code: Select all

or die ("Could not execute query");
to

Code: Select all

or die ("Could not execute query: ". mysql_error());
thegreatone2176
Forum Contributor
Posts: 102
Joined: Sun Jul 11, 2004 1:27 pm

Post by thegreatone2176 »

try putting

$result=mysql_query($query) or die (mysql_error());

instead of your custom error it will show you whats going wrong
Dan++
Forum Newbie
Posts: 12
Joined: Sun Jan 30, 2005 5:02 pm

Post by Dan++ »

Aha!
Could not execute query: Table 'silverf_website.Comics' doesn't exist
What a stupid mistake. The table is actually called 'Comic' not 'Comics'. Heh. Thanks!

Oh <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span>. On going to http://www.silverferret.co.uk/sc17/view ... ?comicID=1 it now says:

Could not execute query: Unknown column '$comicID' in 'where clause'

What the hell is going on? :?
thegreatone2176
Forum Contributor
Posts: 102
Joined: Sun Jul 11, 2004 1:27 pm

Post by thegreatone2176 »

strange phenom we posted the same thign at the same time

anyway

in your query you have

Code: Select all

$query='SELECT * FROM Comic WHERE comicID=$comicID'
and your error message says $comicID not an value so you need to make it

Code: Select all

$query="SELECT * FROM Comic WHERE comicID= '$comicID' ";
that way it will read the value of the variable
Dan++
Forum Newbie
Posts: 12
Joined: Sun Jan 30, 2005 5:02 pm

Post by Dan++ »

thegreatone2176 wrote:strange phenom we posted the same thign at the same time

anyway

in your query you have

Code: Select all

$query='SELECT * FROM Comic WHERE comicID=$comicID'
and your error message says $comicID not an value so you need to make it

Code: Select all

$query="SELECT * FROM Comic WHERE comicID= '$comicID' ";
that way it will read the value of the variable
Thanks, works great now.

So here's my next problem:

I don't want the 'next' link to display on the latest comic, and I don't want the 'previous' link to display on the first comic.

What I have so far (of the relevant code):
<?php
$nextlink('<a href="?comicID=<?php print('$comicID+1'); ?>">Next</a>');
$prevlink('<a href="?comicID=<?php print('$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);
}
?>
There is something wrong with it, as you can see.

I know it has something to do with the variables $prevlink and $nextlink, but I can't fix it. Is it the <?php and ?> tags within the variables? I think so...but how can I fix it?

Thanks for all your help so far, I promise that this is the last problem and then this script will be done! Thanks!
Post Reply