PDA

View Full Version : problem with advanced php for flash book


Billy T
04-12-2003, 08:51 AM
If anyone has any suggestions regarding this please let me know - http://www.phpforflash.com/board/go.php?threadID=2703

Thanks

CyanBlue
04-12-2003, 08:56 AM
I don't think I can be much of help, but...

What is in that line 36??? :D

Billy T
04-12-2003, 09:02 AM
line 36 is actually blank

just under that is my login details for mySQL

Thanks

CyanBlue
04-12-2003, 09:09 AM
Um... I wish I have the book and the code to help you, but I don't... :(

One last thing I need to ask... Did you give proper username and the password for the MySQL database??? Um... I don't think it does make much sense after looking it again what I just wrote down... :D

Mind to post the code so that I can take a look???

Billy T
04-12-2003, 09:47 AM
removed

Billy T
04-12-2003, 09:48 AM
sorry I should have done that in the first place

here's the entire file - pretty long



<?
include("classinfo.php");
session_start();

if (isset($HTTP_POST_VARS["boolAdd"])) {
if ($HTTP_POST_VARS["boolAdd"] == "true") {
$boolSame = false;
$tempID = -1;

for ($x=0; $x < sizeof($_SESSION["myCart"]->arrBooks); $x++) {
if ($HTTP_SESSION_VARS["myCart"]->arrBooks[$x]->bookID == $HTTP_POST_VARS["bookid"]) {
$boolSame = true;
$tempID = $x;
}
}

if ($boolSame) {
$nextQuantity = 1 + $HTTP_SESSION_VARS["myCart"]->arrBooks[$tempID]->quantity;
$HTTP_SESSION_VARS["myCart"]->arrBooks[$tempID]->UpdateQuantity($nextQuantity);
} else {
$HTTP_SESSION_VARS["myCart"]->addBook($HTTP_POST_VARS["bookid"], $HTTP_POST_VARS["price"], $HTTP_POST_VARS["name"]);
}

} else if ($HTTP_POST_VARS["boolSubtract"] == "true") {

$nextQuantity = $HTTP_SESSION_VARS["myCart"]->arrBooks[$HTTP_POST_VARS["arrayid"]]->quantity - 1;
$HTTP_SESSION_VARS["myCart"]->arrBooks[$HTTP_POST_VARS["arrayid"]]->UpdateQuantity($nextQuantity);

} else if ($HTTP_POST_VARS["boolDelete"] == "true") {
array_splice ($HTTP_SESSION_VARS["myCart"]->arrBooks, $HTTP_POST_VARS["arrayid"], 1);
}
}

//////////
//Please enter data for your mysql setup below

$ServerLoc = "localhost";
$User = "myuser";
$Password = "mypass";

//////////

function getBooks() {
$conn = @mysql_connect($ServerLoc, $User, $Password);
$connRS = @mysql_select_db('bookdb', $conn);
$result = mysql_query("select * from books");
$numBooks = 0;

while($row = mysql_fetch_array($result))
{
$numBooks += 1;

if (($numBooks % 2) == 1) {
echo "<TR>";
}

?>
<TD WIDTH="50%">
<IMG SRC="<? echo $row["bookImageLoc"]; ?>"><BR>
<b><? echo $row["bookName"]; ?></b><BR><BR>
<FONT SIZE="-1">ISBN: <? echo $row["bookISBN"]; ?></font><BR>
<FONT SIZE="-1">Authors: <? echo $row["bookAuthors"]; ?></font><BR>
<FONT SIZE="-1">Price: <? echo $row["bookPrice"]; ?></font><BR>
<FONT SIZE="-1">Page Count: <? echo $row["bookPageCount"]; ?></font><BR>
<A HREF="javascript:addToCart('<? echo $row["bookID"]; ?>','<? echo $row["bookPrice"]; ?>', '<? echo $row["bookName"]; ?>')"><IMG SRC="images/AddToCart.gif" WIDTH=100 HEIGHT=30 BORDER=0></A>
</TD>
<?

if (($numBooks % 2) == 0) {
echo "</TR><TR><TD><BR><BR></TD><TD><BR><bR></TD></TR>";
}
}
}

?>

<HTML>
<HEAD>
<TITLE>Flash MX Shopping Cart</TITLE>

<SCRIPT>

function addToCart(bookid, price, name) {
document.form1.boolAdd.value = "true";
document.form1.bookid.value = bookid;
document.form1.name.value = name;
document.form1.price.value = price;
document.form1.submit()
}


</SCRIPT>
</HEAD>
<BODY>

<FONT SIZE="+3">Flash MX Shopping Cart Example</FONT><BR>
<TABLE WIDTH="100%" CELLSPACING=0 CELLPADDING=0 BORDER=0>
<TR><TD BGCOLOR="#CCCCCC">&nbsp;</TD></TR>
</TABLE>

<BR>
<TABLE WIDTH="100%">
<TR VALIGN="TOP">
<TD>
<TABLE WIDTH="100%" BORDER=0>
<? getBooks() ?>
</TABLE>
<BR><BR>
</TD>
<TD WIDTH=250>
<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"
WIDTH="250" HEIGHT="400" id="ShoppingCart" ALIGN="">
<PARAM NAME=movie VALUE="ShoppingCart.swf">
<PARAM NAME=quality VALUE=high>
<PARAM NAME=bgcolor VALUE=#FFFFFF>
<PARAM NAME=FlashVars VALUE="<?
for ($x=0; $x < sizeof($_SESSION["myCart"]->arrBooks); $x++) {
echo "&book";
echo $x;
echo "=";
echo $_SESSION["myCart"]->arrBooks[$x]->bookID;
echo "&bookPrice";
echo $x;
echo "=";
echo $_SESSION["myCart"]->arrBooks[$x]->bookPrice;
echo "&bookQuantity";
echo $x;
echo "=";
echo $_SESSION["myCart"]->arrBooks[$x]->quantity;
echo "&bookName";
echo $x;
echo "=";
echo $_SESSION["myCart"]->arrBooks[$x]->bookName;
}

echo "&numBooks=";
echo sizeof($_SESSION["myCart"]->arrBooks);

?>">
<EMBED src="ShoppingCart.swf" FlashVars="<?
for ($x=0; $x < sizeof($_SESSION["myCart"]->arrBooks); $x++) {
echo "&book";
echo $x;
echo "=";
echo $_SESSION["myCart"]->arrBooks[$x]->bookID;
echo "&bookPrice";
echo $x;
echo "=";
echo $_SESSION["myCart"]->arrBooks[$x]->bookPrice;
echo "&bookQuantity";
echo $x;
echo "=";
echo $_SESSION["myCart"]->arrBooks[$x]->quantity;
echo "&bookName";
echo $x;
echo "=";
echo $_SESSION["myCart"]->arrBooks[$x]->bookName;
}

echo "&numBooks=";
echo sizeof($_SESSION["myCart"]->arrBooks);

?>" quality=high bgcolor=#FFFFFF WIDTH="250" HEIGHT="400" NAME="ShoppingCart" ALIGN="" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED>
</OBJECT>
</TD>
</TR>
</TABLE>
<FORM NAME="form1" ACTION="shoppingcart.php" METHOD="POST">
<INPUT TYPE="HIDDEN" NAME="boolAdd" VALUE="false">
<INPUT TYPE="HIDDEN" NAME="bookid" VALUE="">
<INPUT TYPE="HIDDEN" NAME="name" VALUE="">
<INPUT TYPE="HIDDEN" NAME="price" VALUE="">
</FORM>
</BODY>
</HTML>


Thanks

Billy T
04-12-2003, 10:20 AM
just tested it on my host as well and got the same problem :(

CyanBlue
04-12-2003, 10:42 AM
I took a glance of the file, but I didn't see anything wrong...

I guess you will have to wait for freddycodes to get up... :(

PS>> You've got the duplicate for the last two replies... ;)

Billy T
04-12-2003, 11:00 AM
oops

Thanks

freddycodes
04-13-2003, 03:52 AM
I guess you will have to wait for freddycodes to get up...

LOL!!!!!!1

Its just a scope issue. Unlike actionscript variables declared outside a function that are not passed as parameters or are not declared as global are not available inside a function. You should pass them in as arguments to the function as having too many global variables around can cause unseen problems sometimes.
Change

<? getBooks() ?>


to


<?php getBooks($serverLoc, $User, $Password); ?>


And the function needs to accept the args.


function getBooks($serverLoc, $User, $Password) {
$conn = @mysql_connect($ServerLoc, $User, $Password);
$connRS = @mysql_select_db('bookdb', $conn);
$result = mysql_query("select * from books");
$numBooks = 0;

//etc..............

CyanBlue
04-13-2003, 04:35 AM
Here comes our superhero!!! :D

Scope problem, eh??? Hm... I am not so sure, but Billy T said that the code is from the book and it is supposed to be working as it is if it were to be published, right??? Is it because of the PHP version then???

Billy T
04-13-2003, 04:39 AM
thanks again for your help freddy

that was indeed the problem. for some reason the code you posted didn't work so I just made those variables global before I tried to use them in the connect query

function getBooks() {
global $ServerLoc;
global $User;
global $Password;
$conn = @mysql_connect($ServerLoc, $User, $Password);
//etc

is this a security risk?

You can see this masterpiece in action here - http://billyt.homeip.net/shoppingcart/shoppingcart.php

there are still problems however. when I click on the add to cart button I get

Fatal error: Call to a member function on a non-object in /Library/WebServer/Documents/shoppingcart/shoppingcart.php on line 18

which, again, has me stumped :(

Billy T
04-13-2003, 04:44 AM
Originally posted by CyanBlue
it is supposed to be working as it is if it were to be published, right???

supposed to be yes!!

the file is riddled with errors. For example, as you can see in the code I posted above, every mention of 'bookAdd' and 'bookSame' has been typed as 'boolAdd' and 'boolSame'

very very sloppy and obviously not tested which is just ridiculous

there seems to be very little support happening for the book over at the forum set up for it :(

freddycodes
04-13-2003, 05:23 AM
Well if the variables are only used in that one function, just declare them inside it.


function getBooks() {
$ServerLoc = "localhost";
$User = "myuser";
$Password = "mypass";
$conn = @mysql_connect($ServerLoc, $User, $Password);
$connRS = @mysql_select_db('bookdb', $conn);
$result = mysql_query("select * from books");
$numBooks = 0;


BTW it should have worked, are you sure you had


function getBooks($ServerLoc, $User, $Password) {
$conn = @mysql_connect($ServerLoc, $User, $Password);


and


<?php getBooks($ServerLoc, $User, $Password); ?>



I don't use global, because I like to know exactly how and when variables are passed to a function. But for this I guess it doesn't really matter.

Another thing that is interesting is the use of both $HTTP_SESSION_VARS and $_SESSION

It should be just $_SESSION.

I would switch all of them over to $_SESSION

freddycodes
04-13-2003, 05:28 AM
BTW what I believe it to be saying here is that one of your session objects is not correct and its trying to run a method of the non-existent object. I can't really tell whats going on cause I can't see the rest of the code, ie the include file.

But this is really poor PHP, IMHO. $HTTP_POST_VARS should be $_POST and the session bit from my last post.

freddycodes
04-13-2003, 05:49 AM
Here comes our superhero!!!

Scope problem, eh??? Hm... I am not so sure, but Billy T said that the code is from the book and it is supposed to be working as it is if it were to be published, right??? Is it because of the PHP version then???


I hope you don't really believe that Jason! I just downloaded the source code and it is littered with errors, I can't even test it because they omitted the database dump file. Waste of money!!:rolleyes: :rolleyes: :rolleyes: :rolleyes: :rolleyes:

CyanBlue
04-13-2003, 05:55 AM
Um...Here comes our superhero!!! This, I DO believe in that... ;)

Book... Hm... I haven't really seen the book, but just generally speaking if the author is good at what he/she writes and has pride in the book, he/she should make sure things are right... That's what I have meant...

Speaking of book... Hey, freddycodes... Any chance you are going to publish a book soon??? I am sure that it will be great one as usual... ;)

Billy T
04-13-2003, 05:56 AM
Originally posted by freddycodes

Another thing that is interesting is the use of both $HTTP_SESSION_VARS and $_SESSION


its all over the place...and in the book they go through the code and they have used different code to what was provided in the source files LOL

sloppy as hell

this always seems to happen with books that have multiple authors...

freddycodes
04-13-2003, 05:57 AM
I'll tell you what, you can interview for as long as you need, and I will supply sample code, you write the book and we'll split the profits. What do you say? I can't write to save my life.

freddycodes
04-13-2003, 05:59 AM
I agree, if the author cared about his/her reputation, they would at least test the sample code and proofread it. Maybe they were under the gun to get it done, due to the demise of friendsofed. Don't really know but its horrible.

Billy T
04-13-2003, 06:08 AM
yeah not sure

what I do know is that the author of the first book (which I know you found too simple but was great for me) Steve Webster, was taken off the project halfway through due to a death in his family (he was unable to meet the tight deadlines for a few weeks or something...). It was then handed over to a bunch of authors and I'm guessing that's when it turned to ****.

The first book was very well written with very few errors (well the edition I bought anyway...may have been fixed up along the way as I bought it very late)

Anyway, I'm only on chapter 3 of the second book so I havent given up yet but if chapter 4 is just as sloppy then I'll stop wasting my (and freddy's) time

cheers

freddycodes
04-13-2003, 06:20 AM
As I suspected $_SESSION['myCart'] was never instantiated as an instance of the shoppingCart class. I was able to fix that one by adding the follow line to the top of shoppingcart.php

<?php
include("classinfo.php");
session_start();

//Add this line
if(!is_object($_SESSION["myCart"])) $_SESSION["myCart"] = new shoppingCart();



It seems to function now, I have not looked at the other pages though. ie, checkout.php Here is my revised copy of shoppingcart.php


<?php
include("classinfo.php");
session_start();


if(!is_object($_SESSION["myCart"])) $_SESSION["myCart"] = new shoppingCart();

if (isset($_POST["boolAdd"])) {
if ($_POST["boolAdd"] == "true") {
$boolSame = false;
$tempID = -1;

for ($x=0; $x < sizeof($_SESSION["myCart"]->arrBooks); $x++) {
if ($_SESSION["myCart"]->arrBooks[$x]->bookID == $_POST["bookid"]) {
$boolSame = true;
$tempID = $x;
}
}

if ($boolSame) {
$nextQuantity = 1 + $_SESSION["myCart"]->arrBooks[$tempID]->quantity;
$_SESSION["myCart"]->arrBooks[$tempID]->UpdateQuantity($nextQuantity);
} else {
$_SESSION["myCart"]->addBook($_POST["bookid"], $_POST["price"], $_POST["name"]);
}

} else if ($_POST["boolSubtract"] == "true") {

$nextQuantity = $_SESSION["myCart"]->arrBooks[$_POST["arrayid"]]->quantity - 1;
$_SESSION["myCart"]->arrBooks[$_POST["arrayid"]]->UpdateQuantity($nextQuantity);

} else if ($_POST["boolDelete"] == "true") {
array_splice ($_SESSION["myCart"]->arrBooks, $_POST["arrayid"], 1);
}
}

//////////
//Please enter data for your mysql setup below

$ServerLoc = "localhost";
$User = "admin";
$Password = "adminpass";

//////////

function getBooks($ServerLoc, $User, $Password) {
$conn = @mysql_connect($ServerLoc, $User, $Password);
$connRS = @mysql_select_db('bookdb', $conn);
$result = mysql_query("select * from books");
$numBooks = 0;

while($row = mysql_fetch_array($result))
{
$numBooks += 1;

if (($numBooks % 2) == 1) {
echo "<TR>";
}

?>
<TD WIDTH="50%">
<IMG SRC="<?php echo $row["bookImageLoc"]; ?>"><BR>
<b><?php echo $row["bookName"]; ?></b><BR><BR>
<FONT SIZE="-1">ISBN: <?php echo $row["bookISBN"]; ?></font><BR>
<FONT SIZE="-1">Authors: <?php echo $row["bookAuthors"]; ?></font><BR>
<FONT SIZE="-1">Price: <?php echo $row["bookPrice"]; ?></font><BR>
<FONT SIZE="-1">Page Count: <?php echo $row["bookPageCount"]; ?></font><BR>
<A HREF="javascript:addToCart('<?php echo $row["bookID"]; ?>','<?php echo $row["bookPrice"]; ?>', '<?php echo $row["bookName"]; ?>')"><IMG SRC="images/AddToCart.gif" WIDTH=100 HEIGHT=30 BORDER=0></A>
</TD>
<?php

if (($numBooks % 2) == 0) {
echo "</TR><TR><TD><BR><BR></TD><TD><BR><bR></TD></TR>";
}
}
}

?>

<HTML>
<HEAD>
<TITLE>Flash MX Shopping Cart</TITLE>

<SCRIPT>

function addToCart(bookid, price, name) {
document.form1.boolAdd.value = "true";
document.form1.bookid.value = bookid;
document.form1.name.value = name;
document.form1.price.value = price;
document.form1.submit()
}


</SCRIPT>
</HEAD>
<BODY>

<FONT SIZE="+3">Flash MX Shopping Cart Example</FONT><BR>
<TABLE WIDTH="100%" CELLSPACING=0 CELLPADDING=0 BORDER=0>
<TR><TD BGCOLOR="#CCCCCC">&nbsp;</TD></TR>
</TABLE>

<BR>
<TABLE WIDTH="100%">
<TR VALIGN="TOP">
<TD>
<TABLE WIDTH="100%" BORDER=0>
<?php getBooks($ServerLoc, $User, $Password); ?>
</TABLE>
<BR><BR>
</TD>
<TD WIDTH=250>
<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"
WIDTH="250" HEIGHT="400" id="ShoppingCart" ALIGN="">
<PARAM NAME=movie VALUE="ShoppingCart.swf">
<PARAM NAME=quality VALUE=high>
<PARAM NAME=bgcolor VALUE=#FFFFFF>
<PARAM NAME=FlashVars VALUE="<?php
for ($x=0; $x < sizeof($_SESSION["myCart"]->arrBooks); $x++) {
echo "&book";
echo $x;
echo "=";
echo $_SESSION["myCart"]->arrBooks[$x]->bookID;
echo "&bookPrice";
echo $x;
echo "=";
echo $_SESSION["myCart"]->arrBooks[$x]->bookPrice;
echo "&bookQuantity";
echo $x;
echo "=";
echo $_SESSION["myCart"]->arrBooks[$x]->quantity;
echo "&bookName";
echo $x;
echo "=";
echo $_SESSION["myCart"]->arrBooks[$x]->bookName;
}

echo "&numBooks=";
echo sizeof($_SESSION["myCart"]->arrBooks);

?>">
<EMBED src="ShoppingCart.swf" FlashVars="<?php
for ($x=0; $x < sizeof($_SESSION["myCart"]->arrBooks); $x++) {
echo "&book";
echo $x;
echo "=";
echo $_SESSION["myCart"]->arrBooks[$x]->bookID;
echo "&bookPrice";
echo $x;
echo "=";
echo $_SESSION["myCart"]->arrBooks[$x]->bookPrice;
echo "&bookQuantity";
echo $x;
echo "=";
echo $_SESSION["myCart"]->arrBooks[$x]->quantity;
echo "&bookName";
echo $x;
echo "=";
echo $_SESSION["myCart"]->arrBooks[$x]->bookName;
}

echo "&numBooks=";
echo sizeof($_SESSION["myCart"]->arrBooks);

?>" quality=high bgcolor=#FFFFFF WIDTH="250" HEIGHT="400" NAME="ShoppingCart" ALIGN="" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED>
</OBJECT>
</TD>
</TR>
</TABLE>
<FORM NAME="form1" ACTION="shoppingcart.php" METHOD="POST">
<INPUT TYPE="HIDDEN" NAME="boolAdd" VALUE="false">
<INPUT TYPE="HIDDEN" NAME="bookid" VALUE="">
<INPUT TYPE="HIDDEN" NAME="name" VALUE="">
<INPUT TYPE="HIDDEN" NAME="price" VALUE="">
</FORM>
</BODY>
</HTML>

freddycodes
04-13-2003, 06:25 AM
In all fairness to the author, the line I added in was only to overcome the instance of shoppingCart not ebing there, however if you would have loaded startshopping.php in the browser first and click the link to the store, the class would have been instantiated properly and my line would not have been loaded. But they don't tell you that anywhere, unless its in the book which of course I do not have.

CyanBlue
04-13-2003, 06:27 AM
Originally posted by freddycodes
I'll tell you what, you can interview for as long as you need, and I will supply sample code, you write the book and we'll split the profits. What do you say? I can't write to save my life. Muhahaha... Me??? Write something??? I wish I could, but I am BAD at writing something... I have spent more than twice of usual time it taks when I was writing my thesis... Nah... Thanks for the offer, but I'll just skip it... Forget about the writing skill... I do not know much about PHP or even Flash to write a book... It doesn't really matter who helps me while I am doing that... If a crappy hand write something, all the crappy things come out... It's GIGO... Garbage Inside, garbage outcome... :p

Billy T
04-13-2003, 03:25 PM
working great now thanks freddy!!

well the + and - buttons in the flash movie dont work but I should be able to sort that one.

Now I just gotta study your code and make note of the changes you've made.

Did you get a chance to have a play with any of the other source files that come with the book? If so, were they just as crap? ;)

Thanks again mate you're a lifesaver

cheers

CyanBlue
05-27-2003, 03:53 AM
Hey, Billy T...

Just out of curiosity...

Is this the book that you and Mortimer Jazz were having problem with???
http://www.amazon.com/exec/obidos/ASIN/1904344038/damurah-20/102-5328153-2180944

Billy T
05-27-2003, 03:59 AM
yep that's it

a couple of good chapters but mostly crap

cheers

Mortimer Jazz
05-27-2003, 06:34 PM
Heheh, no I'm still on the first book!

I don't know if I'm alone but I find that I can read a book, understand everything in it, but when I try to write an application from scratch I just get lost and can't recall anything!

So, with php I'm trying to get to saturation point with each chapter. I want to be able to write each application with my eyes closed. This is obviously taking me ages (I'm back on chapter 10 for the third time!) but interestingly it's bring things into question that I didn't consider the first time round.

I hope this works for me. I learned Flash in a weird way, and so sometimes I surprise myself by being able to answer really complicated stuff, and the next moment I'm asking dumb question about tweens or something.

You lot are all smart arses! I'm just the village idiot
:p

Billy T
05-28-2003, 12:19 AM
yeah I know exactly what you mean but I prefer to read a book once and then go back to it as a reference when there is something in particular I need to know

luckily, I do a lot of work from home so I don't have to carry a **** load of books around...