TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-05-2009, 03:26 PM   #1 (permalink)
The Contributor
 
Join Date: Sep 2008
Posts: 39
Thanks: 9
code_junkie is on a distinguished road
Default Creating a function?

I am not sure how to go about creating a function. I have a search script that when returns the result there are 4 specific columns that will have either 11,12,13,14,15 but only one number per column. Order depends on how they enter them in (ex. col_1 = 13, col_2 = 11, col_3 = 15, col_4 = 12 OR col_1 = 11, col_2 = 15, col_3 = 13, col_4 = 12 and so on). Each of the numbers needs to be have a decimal added in the middle (ex. 11 = 1.1, 12 = 1.2 and so on). This is what I have happening right now.
PHP Code:
if($row['cargo_11'] == 11) {
    echo 
'1.1';
  } elseif (
$row['cargo_11'] == 12) {
    echo 
'1.2';
  } elseif (
$row['cargo_11'] == 13) {
    echo 
'1.3';
  } elseif (
$row['cargo_11'] == 14) {
    echo 
'1.4';
  } elseif (
$row['cargo_11'] == 15) {
    echo 
'1.5';
  } 
But I have one for each cargo_type. Is it possible to write a function that will do this in a shorter more efficient way?

I hope all that makes sense.
__________________
Trying to learn all I can about PHP. Teach me what you know...
code_junkie is offline  
Reply With Quote
Old 02-05-2009, 03:55 PM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

You could replace your code above with:
PHP Code:
echo sprintf('%0.1F'$row['cargo_11'] / 10); 
Salathe is offline  
Reply With Quote
Old 02-05-2009, 04:20 PM   #3 (permalink)
The Contributor
 
Join Date: Sep 2008
Posts: 39
Thanks: 9
code_junkie is on a distinguished road
Default

That works perfect, thank you. Would you mind explaining how that works?
__________________
Trying to learn all I can about PHP. Teach me what you know...
code_junkie is offline  
Reply With Quote
Old 02-05-2009, 06:58 PM   #4 (permalink)
The Contributor
 
Join Date: Jan 2009
Posts: 40
Thanks: 10
Scottymeuk is on a distinguished road
Default

Not 100% sure but i think the:

%0.1F

is the format of it and then it just divides it by 10.

Also, sprintf() is used to format strings i think.
Scottymeuk is offline  
Reply With Quote
Old 02-05-2009, 07:50 PM   #5 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

In the most simplest code this could be shown as the following.

All the sprintf function is doing is taking your integer (14) and diving by 10 to give you a float (1.4). The rounding of the numbers, as seen in the sprintf, is necessary merely to limit the number of digits that are displayed subsequent to the decimal point. In the sprintf's example, we are limited to a mere one digit after the decimal point. In my example I don't use any rounding, although if need be we could use round. I have left this out though to keep the example simple.

Although, I do suggest using the sprintf method because it looks better and also you have more control in fewer lines, I hope the code below helps you understand the functionality behind Salathe's solution.

php Code:
$iNumber = 14;
$iTotal = 14 / 10;
echo $iTotal;
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 02-05-2009, 08:06 PM   #6 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Edit: D'oh, you beat me too it

First we move the decimal point one place to the left (by dividing it by 10), then sprintf formats the result.

%0.1F (or %.nF) == format the value given as a floating point number (F) to one decimal place (0.1, the number after the . is the number of places, so %.4F would be to 4 decimal places and so on).

An alternative way (and a lot less elegant)
PHP Code:
echo round($row['cargo_11'] / 101); 
sprintf is a very powerful and useful function.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 02-11-2009, 09:10 PM   #7 (permalink)
The Contributor
 
Join Date: Sep 2008
Posts: 39
Thanks: 9
code_junkie is on a distinguished road
Default

Thanks for all your help.
__________________
Trying to learn all I can about PHP. Teach me what you know...
code_junkie is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create a gallery class Tanax Advanced PHP Programming 25 02-19-2013 04:25 AM
Creating a Simple Currency Converter with Automatic Symbols Wildhoney General 11 03-16-2010 05:22 PM
Part 2: Giving our Currency Conversion Script some Responsibility Wildhoney General 15 03-17-2009 01:53 PM
[Tutorial] How to organize your classes | Part 1 Tanax Advanced PHP Programming 10 03-01-2009 10:08 PM
Need help creating a function code_junkie Advanced PHP Programming 7 01-07-2009 11:09 PM


All times are GMT. The time now is 05:20 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design