PDA

View Full Version : translating database info with scripts


Trusten
09-06-2002, 10:52 AM
alright, i need to know what to put to translate database info, or where to go to find out the commands.

i.e.

echo nl2br(

at any rate, i have date and time, i wanted to know how to translate that to actual months. ya know, instead of 2002:08:13 or whatever, it translates to August 13th, 2002

or something of that nature.

also, i wanted to make a shopping cart, problem is that with a shopping cart, it has to calculate money, etc. etc. etc. right now in my database, i'm actually using the $ sign, which I know must be a no-no but it never mattered till now.

what commands to I use to calculate the money, and shipping, etc. etc. etc. and or just the money even, and how do i write money.

if you don't feel like telling me, please point me in the right direction.

Much Obliged

mordred
09-06-2002, 12:19 PM
Now that's really more than one question in your post... ;)

I'll try to point you some hints that should help you along. First, it all depends on what you actually want to with the output from your database queries. If you have a lot of integer values taken from your db you could do some arithmetic on it, if you want to compare dates you'll use date and time functions, etc. pp. So basically it's up to you to first define very explicitly what you want to achieve, what you have sofar in your database, and what kind of transformation you have to use.

For dealing with strings the PHP string functions (http://www.php.net/manual/en/ref.strings.php) are quite powerful and plenty. If the text shall be used for websites, the functions htmlspecialchars (http://www.php.net/manual/en/function.htmlspecialchars.php), htmlentities (http://www.php.net/manual/en/function.htmlentities.php), strip_tags (http://www.php.net/manual/en/function.strip-tags.php) are very useful. You also know already about nl2br().

About the date example, I'm not sure if you get that value directly from a database field (like MySQLs DATE or DATETIME field), or if it's inside a string stored in a text field. Generally there are a lot of date and time functions in SQL, so you could probably let your db do the hard work. If you need to reformat the date with PHP, you can try


$timeStamp = strtotime(str_replace(':', '-', '2002:08:13'));
$formatedDate = strftime("%B %d, %Y", $timeStamp);
$formatedDate = str_replace(',', date('S,', $timeStamp), $formatedDate);

echo $formatedDate;


As you see, the code is already a little bit convoluted, see the Date/Time functions of PHP for explanation of the functions.

One thing about the money values: Never, never store monetary values as FLOAT/DOUBLE fields, e.g. 105.34 or sth. like that. That will lead to comparsion problems due and weird fractions due to how floating point values are stored in a binary system.
Rather, use either a column type like DECIMAL (for MySQL) or always count in the smallest currency unit, like 10534 cents and store that as an INTEGER.

just my 0.02 errh 2 Euro-Cents

Trusten
09-08-2002, 05:24 PM
wow, thank you.

darn, i didn't see your response to this in my mailbox, sorry for the delay.

with the 'timestamp' thing,

i'm a bit confused, is TimeStamp suppose to be the field in the db?

i'll fiddle with that.

I'll also check out those links you mentioned, and definitely do the decimal thing. problem is, i don't know the meaning of the float, etc. etc. etc. i tried to look for the commands on mysql.com and had no real luck.

if anyone would like to kick start me, i am a quick study. I've come a long way since I first came to this board. I can do a lot of things for myself now, which I didn't think would be possible. the only real trouble i have with the shopping cart is;

1. calculating the money
2. how to set up the database table
3. sessions and functions

if i could find a layout or a tutorial, i'd be good to go.

thanks mordred.