Page 1 of 1

Is there a neater way of doing this?

Posted: Tue Sep 14, 2010 4:35 am
by cneeds
Hi

What is the neater way of doing this?

$space = '                 ';

:mrgreen:

Chris

Re: Is there a neater way of doing this?

Posted: Tue Sep 14, 2010 4:51 am
by jazzercising011
hmm... It would help if I knew what you needed it for. If you need it for padding then I think

<span style="padding-right: 50px;"> blah </span>

might help

Re: Is there a neater way of doing this?

Posted: Tue Sep 14, 2010 5:04 am
by stuartr

Code: Select all

$space = str_pad(' ', 102, "&nbsp;")
should do it

The only thing to be aware of is that str_pad will count &nbsp; as 6 characters long when working out the pad so the second variable (102 in this example) needs to be set at 6 times the number of spaces you want (in this case 17).

You can also use this directly with a string field by replacing the first variable (" ") in my example with a variable or a text string

e.g.

Code: Select all

$padded_string = str_pad($original_string, 102, "&nbsp;")
PHP reference for str_pad:

Code: Select all

string str_pad ( string $input , int $pad_length [, string $pad_string = " " [, int $pad_type = STR_PAD_RIGHT ]] )
This functions returns the input string padded on the left, the right, or both sides to the specified padding length. If the optional argument pad_string is not supplied, the input is padded with spaces, otherwise it is padded with characters from pad_string up to the limit.

Re: Is there a neater way of doing this?

Posted: Tue Sep 14, 2010 7:55 am
by cneeds
@ jazzercising011

I need a variable indent depending on a value found in a menu record to show menu heirarchy.

Chris

Re: Is there a neater way of doing this?

Posted: Tue Sep 14, 2010 8:01 am
by cneeds
Thank you stuartr

This worked perfectly except that it left a space at the beginning of $space so I changed ' ' to ''.

$space = str_pad('', 102, "&nbsp;")

This is the logic where I use this

Code: Select all

      // indent with some spaces depending on menu_level
      // 3 = indent, 6 = number of characters in &nbsp;
      $nof_spaces = ($row['menu_level'] * 3 * 6);
      if (($nof_spaces) > 0) {
         $msg .= substr($space, 0, $nof_spaces);
      }
You know your php!!

Chris

Re: Is there a neater way of doing this?

Posted: Tue Sep 14, 2010 9:17 am
by stuartr
cneeds wrote:
You know your php!!
haha - I wish!! :wink:

Seriously I am fairly new to php, but I have a fair amount of experience in another programming language - Progress OpenEdge 4GL, so I tend to approach most problems with how would I solve them using Progress, and then try and find a PHP equivalent - fortunately on this occasion there was a close equivalent.

now all I have to do is to get my head fully around multidimensional arrays in php and I'll get on much faster :banghead:

Re: Is there a neater way of doing this?

Posted: Tue Sep 14, 2010 9:26 am
by cneeds
now all I have to do is to get my head fully around multidimensional arrays in php and I'll get on much faster :banghead:
do you mean like

Code: Select all

$array_var['x']['y']['z'] 
?

Chris

Re: Is there a neater way of doing this?

Posted: Tue Sep 14, 2010 11:17 am
by AbraCadaver
stuartr wrote:

Code: Select all

$space = str_pad(' ', 102, "&nbsp;")
should do it

The only thing to be aware of is that str_pad will count &nbsp; as 6 characters long when working out the pad so the second variable (102 in this example) needs to be set at 6 times the number of spaces you want (in this case 17).

Code: Select all

$space = str_repeat('&nbsp;', 17);

Re: Is there a neater way of doing this?

Posted: Tue Sep 14, 2010 12:51 pm
by cneeds
wow, neater yet!!

Chris