TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Creating a function? (http://www.talkphp.com/advanced-php-programming/3942-creating-function.html)

code_junkie 02-05-2009 03:26 PM

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.

Salathe 02-05-2009 03:55 PM

You could replace your code above with:
PHP Code:

echo sprintf('%0.1F'$row['cargo_11'] / 10); 


code_junkie 02-05-2009 04:20 PM

That works perfect, thank you. Would you mind explaining how that works?

Scottymeuk 02-05-2009 06:58 PM

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.

Wildhoney 02-05-2009 07:50 PM

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;

sketchMedia 02-05-2009 08:06 PM

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.

code_junkie 02-11-2009 09:10 PM

Thanks for all your help.^^


All times are GMT. The time now is 11:35 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0