Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 117,282 PHP Programmers for FREE! Ask your question and get quick answers from experts. There are 2,141 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Im attempting at my comic engine.

 
Reply to this topicStart new topic

Im attempting at my comic engine., But the next button wont work.

mattisdada
post 15 Jul, 2008 - 03:40 AM
Post #1


New D.I.C Head

*
Joined: 30 May, 2008
Posts: 36


My Contributions


Now before you look at the code itself, just remember, i got the rest sorted out after i get this one hurdle completed tongue.gif.

Baiscly whats happening is, my $comic variable wont go above 2. And i dont know why. I can force it go go above 2, but not by via clicking the "next" buttons.

Now the reason i hypotosis this for happening is, it resets the page, and the inital val goes back to 1, then it adds 1 again, so it onyl goes to 2. I could use $_SESSION. But i find them messy and not very effective.

So baiscly. Im making a comic engine, without using DB's or SESSIONS. As ive made them(And hence why i just need to get this one hurdle over and i got the rest done).


I have attatched a version with some example images just to be quicker tongue.gif

CODE
<?php
$comic = $_GET['comic'];
if ($comic < 1) {$comic = 1;}
switch($_POST['Next_Prev']){
Case 'Next':
$comic++;
break;    
}
echo $comic;
?>
<p><img src="comic/<?=$comic . ".jpg"?>"></p>
<form name="Next_Prev" method="post" action="">
  <input name="Next_Prev" type="submit" id="First" value="First">
  <input type="submit" name="Next_Prev" id="Prev" value="Prev">
  <input type="submit" name="Next_Prev" id="Next" value="Next">
  <input type="submit" name="Next_Prev" id="Latest" value="Latest">
</form>


Attached File(s)
Attached File  comic.zip ( 33.7k ) Number of downloads: 5
User is offlineProfile CardPM

Go to the top of the page


MitkOK
post 15 Jul, 2008 - 04:09 AM
Post #2


D.I.C Regular

Group Icon
Joined: 9 Aug, 2007
Posts: 289



Thanked 9 times

Dream Kudos: 250
My Contributions


php
<form name="Next_Prev" method="post" action="<?php echo $_SERVER['PHP_SELF']."?comic=".$comic; ?>">


Your code is doing this :

1. Checks if $comic is basically set ( it's not )

2. $comic equals to 1

3. Checks if button is pressed ( it's not )

4. Shows picture 1

5. Press the button, $comic is deleted

6. Checks if $comic is set, but it's local variable for the previous page, so it's not.

7. $comic is set to 1

8. Checks if $_POST['Prev_Next'] is pressed. It is, so $comic equals 2.

9. Shows picture 2

10. Press button and goes to step 8.

Your mistake is that you don't pass $_GET['comic'] so we add it to the url, where $_GET takes it from.

This post has been edited by MitkOK: 15 Jul, 2008 - 04:25 AM
User is offlineProfile CardPM

Go to the top of the page

mattisdada
post 15 Jul, 2008 - 04:13 AM
Post #3


New D.I.C Head

*
Joined: 30 May, 2008
Posts: 36


My Contributions


QUOTE(MitkOK @ 15 Jul, 2008 - 04:09 AM) *

php
<form name="Next_Prev" method="post" action="<?php echo $_SERVER['PHP_SELF']."?comic=".$comic; ?>">


Wow thanks alot! Worked like a dream! By any chance could you explain WHY it works so i know for future reference? I get the basic idea... but not fully (Im new to PHP).

CODE
<?php
//Settings Start
$comic_dir = comic;
$ext = ".jpg";
//Settings End

$comic = $_GET['comic'];
$count = count(glob($comic_dir . "/*" . $ext));
if ($comic < 1) {$comic = 1;}
switch($_POST['Next_Prev']){
Case 'Next':
if ($comic >= $count) {} else {$comic++;}
break;    
Case 'Prev':
$comic--;if ($comic == 0) {$comic = 1;}
break;
Case 'First':
$comic = 1;
break;
Case 'Latest':
$comic = $count;
break;
}
echo $comic;
?>
<p><img src="comic/<?=$comic . $ext?>"></p>
<form name="Next_Prev" method="post" action="<?php echo $_SERVER['PHP_SELF']."?comic=".$comic; ?>">
  <input name="Next_Prev" type="submit" id="First" value="First">
  <input type="submit" name="Next_Prev" id="Prev" value="Prev">
  <input type="submit" name="Next_Prev" id="Next" value="Next">
  <input type="submit" name="Next_Prev" id="Latest" value="Latest">
</form>


Thats what i have thus far. But, there is a slight problem with your code.
Its got a delay on the address bar. For example, when you press Next, it will go up to 2, but the address bar says 1. Press Next again the comic goes to 3 but the address bar says 2. The only time its correct, is when you get the the last comic, and press next again. That will get it in sync(Or going down the first comic)

Now i thought i could fix it via going + 1 to the little code you posted for the form. But that wont work all the time. I want a proper solution, not a work around.

This post has been edited by mattisdada: 15 Jul, 2008 - 04:30 AM
User is offlineProfile CardPM

Go to the top of the page

MitkOK
post 15 Jul, 2008 - 04:45 AM
Post #4


D.I.C Regular

Group Icon
Joined: 9 Aug, 2007
Posts: 289



Thanked 9 times

Dream Kudos: 250
My Contributions


php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Comic Engine v2</title>
</head>

<body>
<?php

if (!isset($_GET['comic'])) {
$comic = 1;
} else { $comic = $_GET['comic']; }

echo $comic;
?>
<p><img src="comic/<?=$comic . ".jpg"?>"></p>
<form name="Next_Prev" method="post" action="<?php echo $_SERVER['PHP_SELF']."?comic=".++$comic; ?>">
<input name="Next_Prev" type="submit" id="First" value="First">
<input type="submit" name="Next_Prev" id="Prev" value="Prev">
<input type="submit" name="Next_Prev" id="Next" value="Next">
<input type="submit" name="Next_Prev" id="Latest" value="Latest">
</form>
</body>
</html>


This post has been edited by MitkOK: 15 Jul, 2008 - 04:46 AM
User is offlineProfile CardPM

Go to the top of the page

mattisdada
post 15 Jul, 2008 - 05:22 AM
Post #5


New D.I.C Head

*
Joined: 30 May, 2008
Posts: 36


My Contributions


CODE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Comic Engine v2</title>
</head>

<body>
<?php
//Settings Start
$comic_dir = comic;
$ext = ".jpg";
//Settings End
if (!isset($_GET['comic'])) {
     $comic = 1;
} else { $comic = $_GET['comic']; }

$count = count(glob($comic_dir . "/*" . $ext));
if ($comic < 1) {$comic = 1;}
switch($_POST['Next_Prev']){
Case 'Next':
if ($comic >= $count) {} else {$comic++;}
break;    
Case 'Prev':
$comic--;if ($comic == 0) {$comic = 1;}
break;
Case 'First':
$comic = 1;
break;
Case 'Latest':
$comic = $count;
break;
}
echo $comic;
?>
<p><img src="comic/<?=$comic . $ext?>"></p>
<form name="Next_Prev" method="post" action="<?php echo $_SERVER['PHP_SELF']."?comic=".++$comic; ?>">
  <input name="Next_Prev" type="submit" id="First" value="First">
  <input type="submit" name="Next_Prev" id="Prev" value="Prev">
  <input type="submit" name="Next_Prev" id="Next" value="Next">
  <input type="submit" name="Next_Prev" id="Latest" value="Latest">
</form>
</body>
</html>


Unless i inerpretued you wrongly, its now very broken :|. Prev doesnt work anymore, and the top bit only works properly sometimes.

EDIT: If anyone thinks that my style of programming is poor, and should be changend. Could you please tell me some pointers? Im fairly new to this, just know the basics. And sorta trying to do more then my ability with just the basics.....

This post has been edited by mattisdada: 15 Jul, 2008 - 05:54 AM
User is offlineProfile CardPM

Go to the top of the page

akozlik
post 15 Jul, 2008 - 07:35 AM
Post #6


D.I.C Addict

Group Icon
Joined: 25 Feb, 2008
Posts: 519



Thanked 19 times

Dream Kudos: 750
My Contributions


Just wanted to say this was one of the more intelligent calls for help that I've seen on the forum in a bit. Not only did you want a solution to your problem but when it was given to you, you asked why it worked. Thanks for wanting more than 'I can haz teh codez?'
User is offlineProfile CardPM

Go to the top of the page

MitkOK
post 15 Jul, 2008 - 07:40 AM
Post #7


D.I.C Regular

Group Icon
Joined: 9 Aug, 2007
Posts: 289



Thanked 9 times

Dream Kudos: 250
My Contributions


mattisdada, it is really simple to do this, but I leave it to you to put some effort.

PS: You cannot get my snippet and use it with the rest of your code. Get some books, read some tutorials and understand what you want to write.
User is offlineProfile CardPM

Go to the top of the page

akozlik
post 15 Jul, 2008 - 07:49 AM
Post #8


D.I.C Addict

Group Icon
Joined: 25 Feb, 2008
Posts: 519



Thanked 19 times

Dream Kudos: 750
My Contributions


I want to make some comments regarding your coding style. This is personal preference and everyone develops their own style over time, but I have a few notes that I think everyone would agree with.

First, I'm sure the formatting just got lost when you posted to the board, but make sure you use indentation in your source. I didn't download your zip so I'm sure you are using it properly. Always make the most use out of white space to help make your code readable.

Second, make sure you comment your code. This is something that everyone is guilty of, but it's best to get started early. I have written a lot of code for personal projects and didn't comment it, thinking I'd come back later and understand what I was trying to do. Ha, yeah right. Comment your code into sections so you and anyone who looks at your code will know what you're thinking.

Third, this is a personal preference but put your if/else statements on different lines. You have

php

if ($comic >= $count) {} else {$comic++;}


This should be split into

php


if ($comic >= $count) {
}

else {
$comic++;
}


However, my fourth point is to not use an if/else statement if you're only going to perform an action on one of the conditions. That is better handled by using just an if statement. What you are trying to do above could be accomplished by

php

if ($comic < $count) {
$comic++;
}


Do you see how much easier that is to read? You will really see the difference if you do that through all your source.

Again, break into multiple lines

php

$comic--;if ($comic == 0) {$comic = 1;}


Should be

php

$comic--;
if ($comic == 0) {
$comic = 1;
}


Also, make sure you know the difference between ++$comic and $comic++.

That's about it for now, just keep those notes in mind. I personally hate going through source code looking to fix it because my system isn't set up with the same files as yours, however I'll take a look and figure out what you need to do. I'm going to post a different method in a new message.
User is offlineProfile CardPM

Go to the top of the page

akozlik
post 15 Jul, 2008 - 07:57 AM
Post #9


D.I.C Addict

Group Icon
Joined: 25 Feb, 2008
Posts: 519



Thanked 19 times

Dream Kudos: 750
My Contributions


Ok you were really going about this in an odd way. Try this

php


<?php

// Notice the double quotes around comic and the trailing slash.
$comic_dir = "comic/";
$ext = ".jpg";

// This seems fine
if (!isset($_GET['comic'])) {
$comic = 1;
} else {
$comic = $_GET['comic'];
}

// That's interesting
$count = count(glob($comic_dir . "/*" . $ext));

// If count is less than one than there are no images to view, are you sure you want to set $comic to 1? It won't show anything
if ($count < 1) {
$comic = 1;
}

// Display the current comic

?>

// This will concatenate the src to <img src="comic/4.jpg">
<img src="<?= $comic dir . $comic . $ext ?>"

// This is where I was confused about what you were trying to do. You shouldn't need to use a form, just links that send the next and previous comic numbers as get variables

<a href="this_page.php?comic=<?= $comic-- ?>">Previous</a>
<a href="this_page.php?comic=<?= $comic++ ?>">Next</a>
<a href="this_page.php?comic=<?= $count ?>">Latest</a>



Try that. I didn't test it, so you may need to do some debugging but it shouldn't be difficult. If you receive bugs they should be very basic ones that you can handle. Let us know if you still don't get it.
User is offlineProfile CardPM

Go to the top of the page

mattisdada
post 15 Jul, 2008 - 06:07 PM
Post #10


New D.I.C Head

*
Joined: 30 May, 2008
Posts: 36


My Contributions


QUOTE(akozlik @ 15 Jul, 2008 - 07:57 AM) *

Ok you were really going about this in an odd way. Try this

php


<?php

// Notice the double quotes around comic and the trailing slash.
$comic_dir = "comic/";
$ext = ".jpg";

// This seems fine
if (!isset($_GET['comic'])) {
$comic = 1;
} else {
$comic = $_GET['comic'];
}

// That's interesting
$count = count(glob($comic_dir . "/*" . $ext));

// If count is less than one than there are no images to view, are you sure you want to set $comic to 1? It won't show anything
if ($count < 1) {
$comic = 1;
}

// Display the current comic

?>

// This will concatenate the src to <img src="comic/4.jpg">
<img src="<?= $comic dir . $comic . $ext ?>"

// This is where I was confused about what you were trying to do. You shouldn't need to use a form, just links that send the next and previous comic numbers as get variables

<a href="this_page.php?comic=<?= $comic-- ?>">Previous</a>
<a href="this_page.php?comic=<?= $comic++ ?>">Next</a>
<a href="this_page.php?comic=<?= $count ?>">Latest</a>



Try that. I didn't test it, so you may need to do some debugging but it shouldn't be difficult. If you receive bugs they should be very basic ones that you can handle. Let us know if you still don't get it.

Well i cant really test to see if it works or not(Im currently at school and dont have a php server here, and on that note, do you know of any portable php programs of sorts that wont need admin privilages?).

But while going over what you wrote, so it would be possible to do what you did with the links with buttons and putting them under the action=""? Would it work?

CODE
// If count is less than one than there are no images to view, are you sure you want to set $comic to 1?  It won't show anything  
if ($count < 1) {  
    $comic = 1;  
}  

This makes sense to me. If for whatever reason the number goes below 1 then it will set it to 1. So 1 will always be the lowest comic someone can go.

EDIT: The code completly doesnt work :| Ill have another go but use some of your ideas and tips smile.gif.

Idea's ive had but failed: Tried using header redirects, that failed. Baiscly whats going wrong is that the address bar get updated after the page has loaded the new comic. So its always one behind..... But it can also be one to low, so you cant just add one value to the address bar everytime, and not only does it not work when going down, but if you press next/prev again it will really screw it up tongue.gif.

This post has been edited by mattisdada: 16 Jul, 2008 - 04:58 AM


Attached File(s)
Attached File  comic.zip ( 34.22k ) Number of downloads: 5
User is offlineProfile CardPM

Go to the top of the page

mattisdada
post 17 Jul, 2008 - 12:05 AM
Post #11


New D.I.C Head

*
Joined: 30 May, 2008
Posts: 36


My Contributions


Any ideas?
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/6/08 10:46PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month