Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

control length of select box

Status
Not open for further replies.

jjpetrucelli

Programmer
Dec 5, 2003
99
0
0
US
I want to query the db and output the results into the options of a select box. Now the values are going to be quite long and will cause horizontal scrolling, which is not desireable.

What is the best way to account for this? I am thinking that while outputting the results i should look for a space in the value (i.e. " ") and add a line break at that point. I figure that the way to look for the break would be to start at a specified point in the results and work backwards.

Is this the best way to handle this, if so how do i write this script?

I am told that css maybe the best way to go but i have to account for browsers only accepting embeded css. can / should i use css instead and will 'require' help with not embeding css in everypage?

Please let me know if this is not clear?
 
I think you're needing to check out the HTML or CSS forums for help on this one. I'm not sure you can add line breaks to select elements.... but if you can then we can help you with that.
 
well css isnt looking like the right way to go so i am concentrating on the php solution.

for simplicity lets consider the following;

1 $results=mysql_query($query)
2 or die(mysql_error());
3 while ($rows=mysql_fetch_assoc($results)) {
4 foreach($rows as $val1) {
5 echo "<option>\n";
6 echo $val1;
6 echo "</option>\n";

can i do the following;

define a variable that relates to how many characters can fill the <option> without causing a horizontal wrap on the page

then start a loop that starts at the $variable and works backward - passing this value to substr() and substr() looks for the " "

now i am told there is a way to do a line break by adding \n but there is some issue with escaping out...

if i find how to do this, how could i enter said value using php

= can you assist in solving everything im asking besides how to get the line to break?



 
instead of just $val1, use substr($val1,0,x) where x is how many characters you want to display, but do this for the actual value: <option value="<? echo $val1; ?>">

syntax: substr(string,start,length)
length is optional. for example, if you do substr("Hello World",6) it'll print out: World

 
I believe what you want is something like this...

Insert some set of characters (in my example I put in the literal \n, you may need something else) every so many characters or less, depending on when you encounter a space.

I think there needs to be an easier way to do this ... but I'm slow today.

Code:
<?php

$in = "This will be my option string but it is too long way way way too long do you not understand what I am saying about how way too damn long it is?";

$max_line = 20;

$length = strlen($in);
$iteration = 1;
$last_pos = 0;
while ($length > $max_line) 
{
  $offset = $max_line * $iteration;
  $swap_space_position = strrpos(substr($in, 0, $offset), ' ');
  
  $in_start = substr($in, 0, $swap_space_position);
  $in_break = '\n';
  $in_end = substr($in, $swap_space_position);

  $in = $in_start.$in_break.$in_end;

  ++$iteration;
  $length = $length - ($swap_space_position - $last_pos);

  $last_pos = $swap_space_position;
  echo $length."\n";
}

echo $in;
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top