xoops forums

Arowana

Friend of XOOPS
Posted on: 2011/1/22 0:39
Arowana
Arowana (Show more)
Friend of XOOPS
Posts: 323
Since: 2004/8/6 2
#1

Formatting Height

I am trying to enter Height do you know how I can get the input to except 5' 4" and what the database table Type would be?
Arowana

Peekay

XOOPS is my life!
Posted on: 2011/1/22 1:00
Peekay
Peekay (Show more)
XOOPS is my life!
Posts: 2335
Since: 2004/11/20
#2

Re: Formatting Height

I think you are going to need to write a PHP script that takes the value of the the 'feet' field, multiplies it by 12 and adds it to the 'inches' field to create a single value, e.g. 64 inches. Then reverse the function for output.

If you use TINYINT (unsigned) in MySQL you can store a positive value of 255, which gives you a range up to 21 feet. SMALLINT (unsigned) would allow over 5,000 feet.
A thread is for life. Not just for Christmas.

Arowana

Friend of XOOPS
Posted on: 2011/1/22 1:06
Arowana
Arowana (Show more)
Friend of XOOPS
Posts: 323
Since: 2004/8/6 2
#3

Re: Formatting Height

ouch I was hopeing to just have a way to format it in the database as 5' 2"

Arowana

Mamba

Moderator
Posted on: 2011/1/22 1:11
Mamba
Mamba (Show more)
Moderator
Posts: 10828
Since: 2004/4/23
#4

Re: Formatting Height

There is one example here of what Peekay suggests:

Here's a simple function to convert someone's height in inches to a feet/inch ratio using explode():

function 
get_height($inches)
{
  
//divide by 12 to get inches
  
$feet $inches/12;

  
//round decimal so we can divide by 100 and use as percent
  
$feet round($feet2);

  
// separate whole from decimal
  
$parts explode("."$feet);
  
$whole_feet $parts[0];

  
//turn remaining into percent of 12
  
$remaining_inches round(($parts[1]/100)*12);

  
$height $whole_feet."' ".$remaining_inches.""";

  return 
$height;
}


Or you can use just text field
Support XOOPS => DONATE
Use 2.5.10 | Docs | Modules | Bugs

Peekay

XOOPS is my life!
Posted on: 2011/1/22 1:22
Peekay
Peekay (Show more)
XOOPS is my life!
Posts: 2335
Since: 2004/11/20
#5

Re: Formatting Height

That function takes an inches value and converts it into feet and inches... which is cool, but I suspect the input will be from two fields, feet and inches?

Get the feet field value from the form (make sure it's a number) then multiply it by 12. Then get the inches field (make sure it's a number between 1 and 12). Add the two together.

Or... just create two database fields per record, height_feet and height_inches (make sure they are numbers)

Or... as Mamba suggests, use a text field.
A thread is for life. Not just for Christmas.

Mamba

Moderator
Posted on: 2011/1/22 1:31
Mamba
Mamba (Show more)
Moderator
Posts: 10828
Since: 2004/4/23
#6

Re: Formatting Height

I think, a text field will be the easiest.

Unless he wants to have two numeric fields, one for feet and the second for inches, so he can manipulate them, if needed
Support XOOPS => DONATE
Use 2.5.10 | Docs | Modules | Bugs

Peekay

XOOPS is my life!
Posted on: 2011/1/22 1:35
Peekay
Peekay (Show more)
XOOPS is my life!
Posts: 2335
Since: 2004/11/20
#7

Re: Formatting Height

Sorry Mamba, we crossed posts there I think! I agree 2 numeric fields is probably best.

It sounds like a simple enough task, but it depends whether or not you want to stop someone entering 9 foot 14 inches.
A thread is for life. Not just for Christmas.

Arowana

Friend of XOOPS
Posted on: 2011/1/22 1:49
Arowana
Arowana (Show more)
Friend of XOOPS
Posts: 323
Since: 2004/8/6 2
#8

Re: Formatting Height

So if I used a select field
like
<select name="height">
        <
option selected="selected" value=""></option>
        <
option value="53">4'5"</option>
        <option value="54">4'
6"</option>
</select>


and stored the VALUE in the database
Then on the template converted the VALUE to feet / inches

how do I write it

Sorry not a programmer but if you say the file name of a module and what woudl go in the file I should be able to figure it out.

Thanks this is the last piece to my puzzle

my database table is "height"
Arowana

Arowana

Friend of XOOPS
Posted on: 2011/1/22 1:58
Arowana
Arowana (Show more)
Friend of XOOPS
Posts: 323
Since: 2004/8/6 2
#9

Re: Formatting Height

When I did just the test field and put 5' 4" the database table filled with 0

Arowana

Peekay

XOOPS is my life!
Posted on: 2011/1/22 2:04
Peekay
Peekay (Show more)
XOOPS is my life!
Posts: 2335
Since: 2004/11/20
#10

Re: Formatting Height

Adding the single numeric value in inches to the DB via the select menu (as you have shown) and then getting the value back from the DB and doing the conversion to feet and inches in PHP (perhaps with the script posted by Mamba) is a better idea. It depends on the height range you want to use, otherwise you could have a very long select menu.
A thread is for life. Not just for Christmas.