cyprus Posted September 22, 2006 Share Posted September 22, 2006 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 https://forums.phpfreaks.com/topic/21727-show-column-names-of-dropdown-box/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.