sadiuk Posted April 16, 2010 Share Posted April 16, 2010 Searched a while, but since I'm not entirely sure what I am searching for, I didn't really find anything useful. Basically, what I have is a PHP page with a dynamic data table, that pulls data for the table, as well as the column and row headings depending on user choice from a MySQL DB. The code for this is below. What I need to do is format the outputted data. Since the data output (i.e. the cell contents) change depending on user selection, I can't actually place my formatting on the table cells. I have also listed below the way each of the DBCrits should be formatted. Any help will be more than appreciated. -------------------------------------------------------------------------------------------------- <?php function __autoload($class_name) { require_once($_SERVER['DOCUMENT_ROOT'].'/classes/'.$class_name.'.php'); } $database_manager = new database_manager(); $cons = $database_manager->get_query('SELECT id, name FROM elect2010 ORDER BY name ASC'); array_unshift($cons, array(id=>'',$value=>'')); $crits = array( array('','Please Select...',''), array('open','Population & Voters'), array(12,'Total population'), array(18,'All people over 16'), array(13,'Muslim population (as % of constituency population)'), array(14,'Muslim voter estimate'), array('close'), array('open','Qualifications'), array(1,'No qualifications'), array(2,'GCSEs - A-levels or equivalent'), array(3,'First degree, higher degree, qualified professional status'), array('close'), array('open','Social Grade'), array(4,'AB (Higher and Intermediate) & C1 (Supervisory, clerical, junior managerial)'), array(5,'C2 (Skilled manual workers) & D (Semi-skilled and unskilled)'), array(6,'E (On state benefits, unemployed, lowest grade worker)'), array('close'), array('open','Economic Activity'), array(8,'Unemployed'), array(9,'Economically inactive'), array('close'), array('open','Tenure-Households'), array(10,'All households'), array(11,'Social housing'), array('close'), array('open','Candidates'), array(15,'Incumbent MP'), array(16,'Party'), array(17,'Margin of victory'), array('close') ); $dbCrits = array( 1=>'Q_no', 2=>'Q_123', 3=>'Q_45', 4=>'SG_abc1', 5=>'SG_c2d', 6=>'SG_e', 8=>'EA_unemplyd', 9=>'EA_inactv', 10=>'TH_all', 11=>'TH_sclHsng', 12=>'PV_all', 13=>'PV_m', 14=>'PV_mVtrsNUM', 15=>'C_mp', 16=>'C_prty', 17=>'C_mrgnNUM', 18=>'Q_o16' ); function valNum($var,$max){ if($var!=''&&is_numeric($var)&&$var>=1&&$var<=$max){ return true; } return false; } for($con=1;$con<=5;$con++){ if(valNum($_POST['con'.$con],50)==true){ define('CON'.$con,$_POST['con'.$con]); }else{ define('CON'.$con,''); } } for($crit=1;$crit<=5;$crit++){ if(valNum($_POST['crit'.$crit],21)==true){ define('CRIT'.$crit,$_POST['crit'.$crit]); }else{ define('CRIT'.$crit,''); } } for($c=1;$c<=5;$c++){ if(constant('CON'.$c)!=''){ $details[$c] = $database_manager->get_query('SELECT * FROM elect2010 WHERE id="'.constant('CON'.$c).'"'); } } function echoCon($conNo){ global $cons; global $database_manager; foreach($cons as $value){ if(constant('CON'.$conNo)==$value['id']){ echo '<option value="'.$value['id'].'" selected="selected">'.$database_manager->prep($value['name']).'</option>'; }else{ echo '<option value="'.$value['id'].'">'.$database_manager->prep($value['name']).'</option>'; } } } function echoCrit($critNo){ global $crits; global $database_manager; foreach($crits as $value){ if(constant('CRIT'.$critNo)==$value[0]){ echo '<option value="'.$value[0].'" selected="selected">'.$database_manager->prep($value[1]).'</option>'; }elseif($value[0] == 'open'){ echo '<optgroup label="'.$database_manager->prep($value[1]).'">'; }elseif($value[0] == 'close'){ echo '</optgroup>'; }else{ echo '<option value="'.$value[0].'">'.$database_manager->prep($value[1]).'</option>'; } } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <title>Elections 2010 - Compare Constituencies</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/> <meta http-equiv="Content-Style-Type" content="text/css"/> <style type="text/css"> <!-- .content { width: 800px; background: #fffff; } .ConsHead { text-align: center; font-size: 110%; border-bottom: 1px solid #CECECE; padding-bottom: 10px; } .DataHead { text-align: center; font-size: 110%; -webkit-transform: rotate(-90deg); -moz-transform: rotate(-90deg); filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=3); } .ConCol{ width:150px; height:22px; font-size:10px; background: #ffffff; color: #3d3d3d; border: 0px; border-bottom: 3px solid #CECECE; } .CriRowS{ width:130px; height:25px; font-size:10px; margin: -5px 0px -5px -5px; background: #ffffff; color: #3d3d3d; border: 2px solid #ffffff; border-left: 1px solid #CECECE; } tr.CriRow td{ border-bottom: 1px solid #CECECE; background: #b8e1f7; padding: 4px 0px 5px 5px; color: #404040; font-size:12px; text-align:center; } h1 { font-family:Arial, Helvetica, sans-serif; font-size:1.2em; margin:0px; padding:0px; color:#083a81; } --> </style> </head> <body> <h1>Compare Constituencies</h1><br /> Use the table below to compare details from any five constituencies we have listed. Simply select the constituencies that you want to compare in the first row and the data that you wish to view in the left hand column, then click ‘Compare’.<hr /><br /> <div style="width: 100%" class="content"><form action="" method="post"><table border="0" align="center" cellpadding="0" cellspacing="0"> <th rowspan="9" class="DataHead">Data</th> </tr> <tr> <td> </td> <td colspan="5" class="ConsHead">Constituencies</td> </tr> <tr> <tr> <td> </td> <td><select name="con1" id="con1" class="ConCol"> <?php echoCon(1); ?> </select> </td> <td><select name="con2" id="con2" class="ConCol"> <?php echoCon(2); ?> </select></td> <td><select name="con3" id="con3" class="ConCol"> <?php echoCon(3); ?> </select></td> <td><select name="con4" id="con4" class="ConCol"> <?php echoCon(4); ?> </select></td> <td><select name="con5" id="con5" class="ConCol"> <?php echoCon(5); ?> </select></td> </tr> <tr class="CriRow"> <td><select name="crit1" id="crit1" class="CriRowS"> <?php echoCrit(1); ?> </select></td> <td><?php echo nl2br($database_manager->prep($details[1][0][$dbCrits[CRIT1]])); ?></td> <td><?php echo nl2br($database_manager->prep($details[2][0][$dbCrits[CRIT1]])); ?></td> <td><?php echo nl2br($database_manager->prep($details[3][0][$dbCrits[CRIT1]])); ?></td> <td><?php echo nl2br($database_manager->prep($details[4][0][$dbCrits[CRIT1]])); ?></td> <td><?php echo nl2br($database_manager->prep($details[5][0][$dbCrits[CRIT1]])); ?></td> </tr> <tr class="CriRow"> <td><select name="crit2" id="crit2" class="CriRowS"> <?php echoCrit(2); ?> </select></td> <td><?php echo nl2br($database_manager->prep($details[1][0][$dbCrits[CRIT2]])); ?></td> <td><?php echo nl2br($database_manager->prep($details[2][0][$dbCrits[CRIT2]])); ?></td> <td><?php echo nl2br($database_manager->prep($details[3][0][$dbCrits[CRIT2]])); ?></td> <td><?php echo nl2br($database_manager->prep($details[4][0][$dbCrits[CRIT2]])); ?></td> <td><?php echo nl2br($database_manager->prep($details[5][0][$dbCrits[CRIT2]])); ?></td> </tr> <tr class="CriRow"> <td><select name="crit3" id="crit3" class="CriRowS"> <?php echoCrit(3); ?> </select></td> <td><?php echo nl2br($database_manager->prep($details[1][0][$dbCrits[CRIT3]])); ?></td> <td><?php echo nl2br($database_manager->prep($details[2][0][$dbCrits[CRIT3]])); ?></td> <td><?php echo nl2br($database_manager->prep($details[3][0][$dbCrits[CRIT3]])); ?></td> <td><?php echo nl2br($database_manager->prep($details[4][0][$dbCrits[CRIT3]])); ?></td> <td><?php echo nl2br($database_manager->prep($details[5][0][$dbCrits[CRIT3]])); ?></td> </tr> <tr class="CriRow"> <td><select name="crit4" id="crit4" class="CriRowS"> <?php echoCrit(4); ?> </select></td> <td><?php echo nl2br($database_manager->prep($details[1][0][$dbCrits[CRIT4]])); ?></td> <td><?php echo nl2br($database_manager->prep($details[2][0][$dbCrits[CRIT4]])); ?></td> <td><?php echo nl2br($database_manager->prep($details[3][0][$dbCrits[CRIT4]])); ?></td> <td><?php echo nl2br($database_manager->prep($details[4][0][$dbCrits[CRIT4]])); ?></td> <td><?php echo nl2br($database_manager->prep($details[5][0][$dbCrits[CRIT4]])); ?></td> </tr> <tr class="CriRow"> <td><select name="crit5" id="crit5" class="CriRowS"> <?php echoCrit(5); ?> </select></td> <td><?php echo nl2br($database_manager->prep($details[1][0][$dbCrits[CRIT5]])); ?></td> <td><?php echo nl2br($database_manager->prep($details[2][0][$dbCrits[CRIT5]])); ?></td> <td><?php echo nl2br($database_manager->prep($details[3][0][$dbCrits[CRIT5]])); ?></td> <td><?php echo nl2br($database_manager->prep($details[4][0][$dbCrits[CRIT5]])); ?></td> <td><?php echo nl2br($database_manager->prep($details[5][0][$dbCrits[CRIT5]])); ?></td> </tr> </table> <br /><input type="submit" name="button" id="button" value="Compare"/> </form> </div> <!------------------CONTENT-------END--------------------------------> </body> </html> ------------------------------------------------------------------------------------------------ FORMATTING 1=>'Q_no', = number_format() 2=>'Q_123', = prefix % 3=>'Q_45', = prefix % 4=>'SG_abc1', = prefix % 5=>'SG_c2d', = prefix % 6=>'SG_e', = prefix % 8=>'EA_unemplyd', = prefix % 9=>'EA_inactv', = prefix % 10=>'TH_all', = number_format() 11=>'TH_sclHsng', = = prefix % 12=>'PV_all', = number_format() 13=>'PV_m', = prefix % 14=>'PV_mVtrsNUM', = number_format() 15=>'C_mp', = text 16=>'C_prty', = text 17=>'C_mrgnNUM', = number_format() 18=>'Q_o16' = number_format() Link to comment https://forums.phpfreaks.com/topic/198712-dynamic-data-table-output-formatting/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.