top_left top_right
bottom_left
Next Event: Unknown | Forum Rules | QGL Website | Event Registration
openFolder AusForums.com
iconwatfolderLineopenFolder LANs
iconwatfolderLineopenFolder QGL
iconwatfolderLineopenFolder QGL Forum
Author
Topic: PHP question.
Bj
Posts: 1316
Location: Brisbane, Queensland

Im building a small online shopping cart and could do with some quick help.

When its calculating the maths whole numbers show up fine obviously but with any cents it will just show say $50.3 instead of $50.30

Is there any basic function for numeracy or do I have to write something to check if there is a "." 2 places from the right and add a 0 onto it if so? if thats the way to do it can some pro show me how as ive never needed to play around with strings and basically couldnt give much of a s*** to learn.

Also with storing items in the shopping cart im assuming you just use a array, or is there another more "correct" way to do it?
system
--
Astroboy
Posts: 3338
Location: Germany
Why dont you pay someone to do it with your mountains of cash?

last edited by Astroboy at 17:51:54 10/Apr/06
Persay
Posts: 4033
Location: Brisbane, Queensland
number_format($number,2);

array sounds cool

can i have a fence in return for this info plz
Bj
Posts: 1317
Location: Brisbane, Queensland
Thanks persay ill try it out later. Yeah anytime you need a fence i wont charge you if you wanna help build it with me ;)

Lol quick astro on the bandwagon, you might finally find a place where you fit in and accepted. Also I got mountains of cash from saving it, not spending it.
Jim
Posts: 4197
Location: Brisbane, Queensland
check the manual for sprintf()

specifically, sprintf("%01.2f", $value)
Bj
Posts: 1318
Location: Brisbane, Queensland
now you've lost me. im just a skilless trade contractor not a code guru.

Thanks for the tip anyway jim if persay's way doesnt work ill give that a shot.
nF
Posts: 12225
Location: Wynnum, Queensland
why not use an already available online shopping thing? some are even free.

you'd want it to be fairly secure and bugfree wouldn't you?
scuzzy
Posts: 12114
Location: Brisbane, Queensland
Coding your own atleast means no one else has the code (eg opensource oscomerce), but it still means you could hamfist up your own code :)
King Of Shibby!
Posts: 2367
Location: Canberra, Australian Capital Territory
hamfisting is my specialality

f***ING CSS
whoop
Posts: 9917
Location: Brisbane, Queensland
remind me never to shop at whatever online shopping thing this guy codes for.
hast
Posts: 739
Location: Brisbane, Queensland
you should also use arbitrary precision arithmetic when dealing with money, not floating point

http://au3.php.net/bc

this way you won't have funny things happen because numbers like 0.1 can't properly be represented in binary
fpot
Posts: 12952
Location: Gold Coast, Queensland
you should also use arbitrary precision arithmetic when dealing with money, not floating point
Yeh what an idiot for not thinking of that.
Bj
Posts: 1319
Location: Brisbane, Queensland
you should also use arbitrary precision arithmetic when dealing with money, not floating point


what did this crazy nigga just say?

why not use an already available online shopping thing? some are even free.

you'd want it to be fairly secure and bugfree wouldn't you?


I could, and usually i'd like to save time where I can and not re-invent the wheel, but this is something a little different. This site im coding from the ground up myself, for my own purposes. So theres no time limit or stress, so I dont mind taking a while playing around and learning abit as I go.

As for secure, not really for the first stage. It wont handle online transactions or anything for awhile, more just generate a list of what they want, so the seller can generate a genuine invoice to send back.

As for the bugs, well.. every step of the way. But they get sorted out with time.

Whoop dont lie you bitch, you f***ing love me.
scuzzy
Posts: 12115
Location: Brisbane, Queensland
what did this crazy nigga just say?
Means you've got some researching to be done on data and database integrity
whoop
Posts: 9920
Location: Brisbane, Queensland
I may love you, however buggy internet code gets no love from the hairy one.
nF
Posts: 12227
Location: Wynnum, Queensland
do you have anything in the way of programming experience burt?
Jim
Posts: 4200
Location: Brisbane, Queensland
im just a skilless trade contractor not a code guru.
join the club
Bj
Posts: 1320
Location: Brisbane, Queensland
Means you've got some researching to be done on data and database integrity


As long as the customer knows what they're ordering, and how much its gonna cost, thats all thats needed for my purposes, this aint a million dollar contract for eBay or something.

I may love you, however buggy internet code gets no love from the hairy one.


Give me a break homeboy, you aint even seen the s*** yet. Its been in development for maybe 2 months, all code starts out buggy. Once everything just works, then ill go over it all properly.

So far its got full user admin (create, login, edit etc) news system complete with comments from users, hell you can even upload a avatar and add a custom title for your account, bit like here. The shop is well underway and going ok, im getting there, time is the main thing slowing its development down as its one thing I have little of. Its taking extra time too as im sorta doing it all object orientated stylez so my partner can use the same engine easily when when I do her site.

do you have anything in the way of programming experience burt?


Give you one guess what I was going to do before I decided to stick with contract work? programming was always an interest since high school and got myself a diploma in software eng just to wet my feet to see if it was a path I would like to try but changed my mind, I couldnt stand working in an office writing code day in day out. Sure some people are happy to go down that path, but it just aint me.

I came close to leaving my current trade and applied for a web-dev job, I dont know about now, but when I applied the market was flooded with people who would accept peanuts just to get a job so they were only offering about $30k, so now looking back im glad I decided not to go ahead with that path.

It may have taken 5+ years and alot of hard work, but im quite happy where I am now working for myself, with no stress and now got quite a passion for landscaping.

However I will never master happily getting out of bed < 5:30am.
blahnana
Posts: 203
Location: Brisbane, Queensland
Give me a break homeboy, you aint even seen the s*** yet. Its been in development for maybe 2 months, all code starts out buggy. Once everything just works, then ill go over it all properly.


Not your fault, and you're not alone, but this is _the_ most dangerous attitude towards programming, and leads to a most of the exploits in programs (web and otherwise) around today.

I definitely support you in your learning, you have to start somewhere... but you need to realise that the code you write shouldn't make it to production. You should rewrite and redesign it after you get it going and get the bugs out.

This is because you won't grasp the significance of the things that you're about to learn until you've actually done it. Of course, you have to learn these things, and to do that, you have to actually do the programming and spend time learning and get hands on in there... but the better way to do it is to work on small internal projects that are just for yourself or a couple of other people, and are insulated from the internet at large.

However, I understand that your desire is to make something useful and that's the driving force behind this learning. Please understand though, that the things that you think are benign are sometimes not as benign as they appear. You need to make a big effort to understand concepts before just putting them into practice because they work.

There are dangers. One is that you'll open your server up to abuse. I realise that you just want to draw up invoices and that there's no money involved in that for now... but how would you feel if a customer got access to the details of another customer? How would they feel about it? Information sometimes isn't as innocuous as you think.

Two is that you won't get time or the desire to go back over the code and fix all the bugs. This is the most likely of the dangers. From what you've said, it's likely that you'll go back over the code and fix things that don't work. Those aren't the only bugs that will be in your program. For one, there'll be a lot of bugs that won't be evident until people complain because they did things you didn't think of. For another, there'll be bugs that you won't even understand... but other people might use your code for purposes you never intended.

Three is that you'll extend your code to add in functionality that may be a whole lot more serious down the line. Like handling internet banking. Sure, you don't think you want it now... but down the line it's almost certain that you'll try and put it in if your site starts working for you the way you hope. When you extend your code like that without seriously thinking about its initial design and knowing about several security concerns within the framework you're creating your code, you leave yourself open to all sorts of future problems.


You're right to an extent... all code starts out buggy... but it's much better code that is buggy because of accidents rather than a lack of knowledge.

Of course you have to start somewhere, and I'm all for learning new skills... just be careful how you approach it when you're thinking that you can just whip something up that'll be live and in production as your first project... and do make an effort to find out about some of the very common security concerns surrounding your weapon of choice... like SQL injection and code injection and cleaning user input and checking return values.

Also promise yourself that you'll redesign your site and recode it a couple of times before you actually put it live (you'll get faster and better at it the longer you do it), and don't copy and paste chunks of code out of other programs without knowing exactly what they do, what their limitations are. A lot of the code floating around out there is poorly written, and even the stuff that's well written may burn you when it's used in your program in a way that it wasn't intended for.

I know that you'll probably break/ignore a lot of this, but hopefully it'll help you make time to educate yourself along the way.

Good luck!
Obes
Posts: 4293
Location: Brisbane, Queensland
Shopping carts and e-Commerce are not places for people to begin to learn to code.
infi
Posts: 3407
Location: Brisbane, Queensland
tldr
Bj
Posts: 1321
Location: Brisbane, Queensland
Woah, unit post. Thanks for all that info blahnana there is a few things I havnt thought about in there.

work on small internal projects that are just for yourself or a couple of other people, and are insulated from the internet at large


I guess I didnt really make it clear, but thats exactly what im doing. Im making it for my own purposes then after awhile i'll port it over so my partner can use the same system once its been trialed and tested abit.

I wouldnt be taking on a contract for a company doing this sort of s*** while im still learning alot, Infact I aint ever planning on doing that sort of work anyway -ever.

I do plan to get a few people I know to test it for a month or so before I send it live of course.

However seeing you took the time to write such an informative post, I did take the time to read over it a few times and try take it all in. Thanks again
TicMan
Posts: 716
Location: Brisbane, Queensland
Also (and I'm not being a prick) http://www.php.net/manual/en/ .. it's the most comprehensive reference guide for any language I've ever seen.
scuzzy
Posts: 12116
Location: Brisbane, Queensland
Agreed, everything you need to know about PHP is all there.
TicMan
Posts: 717
Location: Brisbane, Queensland
It's where Jim sent me many years ago :)
Insom
Posts: 863
Location: Brisbane, Queensland

you should also use arbitrary precision arithmetic when dealing with money, not floating point

http://au3.php.net/bc


or you could just store currency as integer cents
Fizzer
Posts: 424
Location: Brisbane, Queensland
When learning about good secure programming practises you might want to checkout phpsec as well.

http://phpsec.org/projects/
Jim
Posts: 4204
Location: Brisbane, Queensland
you should also use arbitrary precision arithmetic when dealing with money, not floating point

http://au3.php.net/bc



or you could just store currency as integer cents


both of those comments are for performing maths on, or storage of money values - he's asking how to format the output post maths/storage, which is what I answered

Also, I dunno if we all just missed it or if bj edited his original post afterwards to include the cart item storage question - you'd probably best store the shopping cart items in a database unless there was some reason not to. A php array will only last as long as the current page request, which means you'd be passing the array elements around from page to page if you wanted the cart to be present over all the page requests.
scuzzy
Posts: 12117
Location: Brisbane, Queensland
I assume they ment storing the array in a session.
nF
Posts: 12229
Location: Wynnum, Queensland
maybe he meant hes going to have all his database stored in his php files

$item['computer']['name'] = "computer";
$item['computer']['cost'] = 15000;

which would be lol
scuzzy
Posts: 12118
Location: Brisbane, Queensland
serialize arrays and store them as text files, duh
Bj
Posts: 1322
Location: Brisbane, Queensland
I never edited the post, the q has always been there.

Storing the cart items in a db would be one way, but seems to me like alot of work unless you wanted to keep the data there after the session finished, I was just thinking a session variable maybe like scuzzy said, but I cant really think of the best way to do it hence the question. I dont care if the cart items are lost once they close the browser.

Also im just using mysql, its easy enough for me to understand.
scuzzy
Posts: 12119
Location: Brisbane, Queensland
you can just make an array of item ID's that match products in a database and store them in a session...

$_SESSION['cart'][] = 1112;
$_SESSION['cart'][] = 1113;
$_SESSION['cart'][] = 1114;
scuzzy
Posts: 12120
Location: Brisbane, Queensland
or maybe:

$_SESSION['cart'][] = array('id'=>1112,'quantity'=>40);
Jim
Posts: 4206
Location: Brisbane, Queensland
it's not much work storing the data in a database, and I reckon it's a more reliable, secure and logical means of storage for that kind of information than session variables

here's some info: http://www.acros.si/papers/session_fixation.pdf

if you do go the session route anyway, then you can store practically any type of php variable in a session other than objects with circular references and resource variables so yeah, you can use an array if that seems useful to you
flagger
Posts: 197
Location: Brisbane, Queensland
i would use a db for storing data

use ADODB for database abstraction, so you can start off with a small database engine (eg. MSDE or mysql 4) then if your volume increases a few simple line changes its all go
Bj
Posts: 1323
Location: Brisbane, Queensland
If a supergeek wants to go through that sort of effort to haxor my small-time website, lol... good for them.

I see where your coming from though. If it was a huge project and this was something I was working on during my days I would take the time to make it harder for the geek lords. But just for my needs I basically just need s*** to work, keep s*** remotely safe and mostly user fool proof for people using the site.

I gotta draw the line somewhere on security, and I think the idea of people haxorin their own sessions is further then im worried about. Ive seen a few "commercial/professional" sites that make my site look pro :P

Scuzzy; thats just the way I was thinking of doing it, was just curious as to what ideas people with alot more experience have.

Jim why do I not stay logged in after I login to this site with firefox? works everywhere else. Have to use IE to post and its pissin me off.
Fuknukle
Posts: 4524
Location: Brisbane, Queensland
your firefox cookies.
flush them out
whoop
Posts: 9921
Location: Brisbane, Queensland
why wouldn't you want your customers items to be stored in a DB? You could have it so they can log in, each user has their own little section of the DB and whatever they order goes into that DB. You then open up the DB and process their orders, send them an invoice and delete their stuff from the DB/flag it as "ready to collect".

You could limit it to say, 5 previous items in everyone's list (or previous orders which would be groups of items I assume) and after that they just auto delete to save room.



Hell, I got bored enough to mysql-ize my "favourites". I was sick of backing up my bookmarks folders from n'th different browsers in a format, and having a site bookmarked on one computer & not on another and having to shift computers to find URL's so I decided to kill 2 birds with one stone (other bird was my boredom). I already had mysql and apache on my computer from a previous endeavour so now I have a list of links that can be accessed from any computer in my house that runs off php/mysql. Databases are piss easy man, if I can figure them out you can :)
Jim
Posts: 4207
Location: Brisbane, Queensland
Jim why do I not stay logged in after I login to this site with firefox? works everywhere else. Have to use IE to post and its pissin me off.
yeah as fuknukle said it'll be something dodgy going on with your cookies for the ausforums domain - if you can get rid of them and start over you should hopefully be ok
stinky
Posts: 1528
Location: Brisbane, Queensland
Just remember guys, never, I repeat never use Jim in a production environment.
Jim
Posts: 4208
Location: Brisbane, Queensland
you say that now, but when you get ornery you'll use me anywhere
Bj
Posts: 1324
Location: Brisbane, Queensland
yeah as fuknukle said it'll be something dodgy going on with your cookies for the ausforums domain - if you can get rid of them and start over you should hopefully be ok


Yeah that was just the prob, fixed now.

Ive got mysql and databases sorted out thats no worries, just seemed like alot of work for all the queries updating their list if I could just store it all in a few session variables.

But if u gonna keep on my back about it then ill store them in the db :P
Jim
Posts: 4211
Location: Brisbane, Queensland
one thing you might do, is store them in the db
stinky
Posts: 1530
Location: Brisbane, Queensland
hardware is cheap, mysql isn't overly resource intensive as long as you set appropriate indexes and keys. You could probably cut the cart into a single data field split with |'s like 1024|2|39.95+2340|1|22.45 but really you're better off being as specific in the cart database as you can. this will allow you to have greater flexibility when it comes to sales, promos etc.
Bj
Posts: 1325
Location: Brisbane, Queensland
one thing you might do, is store them in the db


http://beatninja.ausgamers.com/lol/images/smiles/emot-psyduck.gif
Jim
Posts: 4214
Location: Brisbane, Queensland
while we're talking about db's, it might be an idea to store your carts there instead of in sessions
stinky
Posts: 1531
Location: Brisbane, Queensland
I'm a little slow jim, but i think you're suggesting he should probably store the cart in a db rather than in a session?
Jim
Posts: 4216
Location: Brisbane, Queensland
give yourself some credit bloke, you got it in one
Bj
Posts: 1326
Location: Brisbane, Queensland
so you reckon store it in a text file?
Fizzer
Posts: 425
Location: Brisbane, Queensland
I heard that if you mated a bunch of monkeys together you can make your own mini super army of cart item remembererereres.
Persay
Posts: 4042
Location: Brisbane, Queensland
php is gay anyway, what you really wanna do is hardcode every possible shopping cart combination into its own .html file duh
stinky
Posts: 1534
Location: Brisbane, Queensland
Oh and remember to run apache as root to make sure it doesn't have any permissions problems accessing stuff.
Hogfather
Posts: 928
Location: Cairns, Queensland

http://www.php.net/manual/en/

it's the most comprehensive reference guide for any language I've ever seen.


For any Open Source / free language, sure - though the Python API doc is pretty good, and the Java one while not as newbie friendly is pretty comprehensive too.

That said, the MSDN library makes that look like a pamphlet tho.

last edited by Hogfather at 17:31:31 12/Apr/06
Jim
Posts: 4217
Location: Brisbane, Queensland
for any language he's seen
Hogfather
Posts: 929
Location: Cairns, Queensland
Yes.
Jim
Posts: 4218
Location: Brisbane, Queensland
yeah
whoop
Posts: 9922
Location: Brisbane, Queensland
no you've got it all wrong, it's http://mbm.livewiredev.com
TicMan
Posts: 718
Location: Brisbane, Queensland
MSDN is a piss weak piece of crap compared to some of the other offerings out there.. the lengths you have to goto to find what properties of an ADSI WINNT object you can query is ridiculous.
Jim
Posts: 4219
Location: Brisbane, Queensland
are you sure it's http://mbm.livewiredev.com/? I'm pretty sure it's http://mbm.livewiredev.com/
Hogfather
Posts: 930
Location: Cairns, Queensland
MSDN is a piss weak piece of crap compared to some of the other offerings out there.. the lengths you have to goto to find what properties of an ADSI WINNT object you can query is ridiculous.


lol. lern2MSDN.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/adsi/adsi/adsi_objects_of_winnt.asp
Jim
Posts: 4220
Location: Brisbane, Queensland
lol. lern2comprehend.

http://www.qgl.org/cluepon
Hogfather
Posts: 931
Location: Cairns, Queensland
If you say so Jim.

Bored this morning?
Jim
Posts: 4222
Location: Brisbane, Queensland
not so much as horny
stinky
Posts: 1535
Location: Brisbane, Queensland
didn't you hire blah to take care of that sort of problem?
Jim
Posts: 4224
Location: Brisbane, Queensland
he doesn't come in until 9
Opec
Posts: 4052
Location: Brisbane, Queensland

so you reckon store it in a text file?


If you're using MySQL then yeah it's really not that much different.
Jim
Posts: 4225
Location: Brisbane, Queensland
haha when did you go sour on mysql
TicMan
Posts: 719
Location: Brisbane, Queensland
lol. lern2MSDN.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/adsi/adsi/adsi_objects_of_winnt.asp


I never said I didn't find it, I said it was difficult to find it.

lol. lern2comprehend.
stinky
Posts: 1537
Location: Brisbane, Queensland
php rocks. I just got it talking to our PBX, so I can click on a phone number in our intranet and it will dial the caller ( set by a cookie atm, but will be an AD field when I'm done), then the callee and connect them together.

php=win++
Hogfather
Posts: 932
Location: Cairns, Queensland
I never said I didn't find it, I said it was difficult to find it.


I found that link in about 15 seconds, and I've never made use of that part of the API.

The size of the library makes it a bit of a c*** from time to time to narrow in on something that you need, and the MSDN search is kinf of arse. You'll prolly need to make use of a combination of Code Guru, KBAlerts, Google and the MSDN search engine itself to find what you want if its being difficult.

But it is bloody extensive.

If you wanna be nit picky about "comprehension" the original quote that made me bring up MSDN was:

it's the most comprehensive reference guide for any language I've ever seen.


MSDN is comprehensive. OK?

last edited by Hogfather at 23:40:56 13/Apr/06
Jim
Posts: 4226
Location: Brisbane, Queensland
no, it's not ok
Hogfather
Posts: 933
Location: Cairns, Queensland
I really wish it was :(
system
--
Not a new post since your last visit.
New Post Since your last visit
Back To Forum
Advertise with Us | Privacy Policy | Contact Us
© Copyright 2001-2026 AusGamers Pty Ltd. ACN 093 772 242.
Hosted by Mammoth Networks - Australian VPS Hosting
Web development by Mammoth Media.