[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

line break for option in select box

Posted on 2004-11-15
9
Medium Priority
?
3,084 Views
Last Modified: 2008-01-09
I need to  populate a select box with the results of a query.  The values of the query are long so what i need to do is insert a line break so that the select box doesnt cause a horizontal wrap on the webpage.  this has been done before in CF by doing the following;

selecting a predefined point that would relate to how many characters could fit into the select box and not cause a horizontal wrap

if the particular value is longer than the predefined point (aka number of values) then continue;
starting at that point and going backwards, looking for " " in the string value

when the space (" ") is found, enter a line break.

what is the code to do this in php?
0
Comment
Question by:tecumsehjoe
8 Comments
 
LVL 3

Expert Comment

by:steve918
ID: 12587628
$len = 8;  //Set your own length

//Cut the string into segments with $len length and put into an array
$temp = explode('\n',wordwrap($mystring, $len, "\n", 1));
//Get the first line and tack on "..."
$mystring = $temp[1] . "...";



check out wordwrap
http://us2.php.net/manual/en/function.wordwrap.php

and explode
http://us2.php.net/manual/en/function.explode.php
0
 

Author Comment

by:tecumsehjoe
ID: 12588491
ok I'm slightly confused here, this is where I am;

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<select name="MyNiceQuery">
<?php
require('../Main/PHPFile.php');
$query="SELECT distinct my_name
            FROM my_table
            ORDER BY my_name";
$results=mysql_query($query)
      or die(mysql_error());
while ($rows=mysql_fetch_assoc($results)) {
      foreach($rows as $val1) {
            $temp = explode('\n',wordwrap($val1, 20, "\n", 1));
            $val2 = $temp."...";
            echo "<option>\n";
            echo $val2;
            echo "</option>\n";
            }
}
?>
</select>
</body>
</html>

I tried temp as an array and no luck, so i changed it, but recieved the same results, whats wrong here?
0
 
LVL 3

Expert Comment

by:steve918
ID: 12588988
temp is an array of lines for example

if your string is "my really long string that I want to cut down a bit"
wordwrap will add \n so that the text wraps like so

my really long
string that I
want to cut
down a bit

now using explode we take each line and put it in an array so it would look like this
echo temp[1]; // my really long
echo temp[2]; //string that I
echo temp[3]; //want to cut
echo temp[4]; //down a bit

but you just want the first line so you need to just grab the first entry in the array:
$temp[1]


in your code you put
>> $val2 = $temp."...";
which assigned an array $temp to $val2, but you want to assign the value of the first member of the array $temp to $val to (aka temp[1])
so your code should look like this:
$val2 = $temp[1]."...";



0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:tecumsehjoe
ID: 12589305
thats how i 'tried temp as an array and no luck'

I get "Array" showing up in the select box several times
0
 
LVL 2

Expert Comment

by:_the_mars_
ID: 12592259
You cannot use a linebreak in an HTML <select>.  However it is possible to set the width of your <select> using CSS.
Try adding:
    style="width:50ex;"
in your <select> tag, e.g.:
    <select name="myname" id="myname_id" style="width:50ex;">

Your box will be the given width and the options may be longer.

"ex" means the height of the character "x" which is approx. the width of a character. You can use "px" (=pixels) or "%" too.
0
 
LVL 3

Expert Comment

by:Boris Aranovich
ID: 12593303
maybe modifying the content of the <option>s with a javascript, and breaking it with \n ?
0
 

Author Comment

by:tecumsehjoe
ID: 12596626
maybe splitting it into two different options, and when selecting either of the two ='s the same value?
0
 
LVL 1

Accepted Solution

by:
compustarsg earned 375 total points
ID: 12613015
i think in 'steve918' solution it not worked because  explode('\n',...   dont work. first argument should be string.

try this.

<?php
  echo "<select>\n";
  $val1 = "loooooo ooooooo ooooooooo oooooooo ooooooooo ooooooooo oooooooong";
         
  $temp = explode('$$',wordwrap($val1, 20, "$$", 1));
         
  foreach($temp as $key=>$val) {
    echo "<option value=1>\n";
    echo $val;
    echo "</option>\n";
  }
  echo "</select>";
?>
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month17 days, 23 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question