Jump to content

Show Column names of dropdown box


cyprus

Recommended Posts

This may be a tall order, but I am trying to display column names above a dropdown list. I have code that puts data in a list in columns that are correctly spaced apart. I know that the column appears in the variable $cname, 5 lines down from the code below. My problem is $cname values appear multiple times where its echoed as its within code running for every row. Can someone offer suggestion as to how I can get a variable/array containing 1 set of column names that can be displayed either above the list or as a first row within the list. Thanks 


// get data lengths
      $maxlen=array();
      foreach ($data_rows as $k=>$row)
      foreach($row as $cname=>$val) {
      echo $cname;           
      $val = trim($val);
           

switch ($cname) {
        case 'UnitPrice':      $val = '£' . number_format($val,2); break;
        case 'Qty':    $val = number_format($val, 0) . ' off'; break;
        case 'RunningTotal':      $val = '£' . number_format($val,2); break;
        case 'Orderdate':  $val = date("j-M-Y", strtotime($val)); break;
    }
        $data_rows[$k][$cname] = $val;
        $maxlen[$cname] = max($maxlen[$cname], strlen($val));
    }

        $contents = "<select style=\"font-family:monospace;\" name=\"selectbox\" multiple size=\"$numrows\">\r\n";

        foreach ($data_rows as $row) {
        $id = $row['id'];
        unset ($row['id']);
        $contents .= "<option value='$id'>";
        $line = '';
        foreach($row as $cname=>$val) {
        $line .= str_pad($val, $maxlen[$cname]+2, chr(160));    //pad with non breaking spaces
        }
        $contents .= $line;
        $contents .= "</option>\r\n";
    }
        $contents .= "</select>";
        echo "<div>$contents</div>";
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.