line break for option in select box

Posted on 2004-11-15
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?
Question by:tecumsehjoe
    LVL 3

    Expert Comment

    $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

    and explode

    Author Comment

    ok I'm slightly confused here, this is where I am;

    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <select name="MyNiceQuery">
    $query="SELECT distinct my_name
                FROM my_table
                ORDER BY my_name";
          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";

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

    Expert Comment

    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:

    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]."...";


    Author Comment

    thats how i 'tried temp as an array and no luck'

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

    Expert Comment

    You cannot use a linebreak in an HTML <select>.  However it is possible to set the width of your <select> using CSS.
    Try adding:
    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.
    LVL 3

    Expert Comment

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

    Author Comment

    maybe splitting it into two different options, and when selecting either of the two ='s the same value?
    LVL 1

    Accepted Solution

    i think in 'steve918' solution it not worked because  explode('\n',...   dont work. first argument should be string.

    try this.

      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>";

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Join & Write a Comment

    As this topic comes over and over again in different forms, I've finally decided to write a short (yea, right...) article / tutorial about pagination with PHP with MySQL database. There are dozens of these kind of tutorials, I know - I wanted to mak…
    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to count occurrences of each item in an array.

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now