onedubios Posted December 18, 2008 Share Posted December 18, 2008 Hello and thanks for taking the time to read my question i am teaching myself Php and Mysql so if i seem like i don't know allot is because i don't. Now with that said i have created a form field with a check box it pulls up the information from the database. <td colspan="2" rowspan="7" align="left" valign="top" bgcolor="#EDF4FB" class="borderLeft text"><table border="0" cellspacing="0" cellpadding="0"> <?php @ $results = mysql_query('select * from Features order by Feature ASC'); @ $num = mysql_num_rows($results); for ($i=0;$i<$num;$i++) { @ $data = mysql_fetch_array($results); echo ' <tr> <td valign="middle"><label><input type="checkbox" name="features[]" value="'.$data['FeatureID'].'"'; @ $fnum = count($_POST['features']); for ($j=0;$j<$fnum;$j++) { if ($_POST['features'][$j] == $data['FeatureID']) { echo ' checked="true"'; } } echo '>'.$data['Feature'].'</label></td> </tr>'; } ?> </table></td> It seemed a little bulky so now i am trying clean up the code and here is what i come up with //Fetch Features $sql = "select * from categories order by title asc"; $result = $mysql->exSql($sql) or die($mysql->debugPrint()); while($row = mysql_fetch_assoc($result)){ if(isset($_GET['feature']) && $_GET['feature'] != ""){ if(is_array($_GET['feature'])){ if(in_array($row['FeatureID'],$_GET['feature'])){ $row['selected'] = "selected"; } }else{ $row['selected'] = ($_GET['feature'] == $row['FeatureID']?"selected":""); } } $xtpl->assign('row',$row); $xtpl->parse('main.advancedsearch.feature'); } but it doesn't work i cant figure out what am i doing wrong. also here is the code i use for the HTML side. <td class="column">Features</td> <td class="row"> <input type="checkbox" name="feature[]" id="feature'.$i.'" value="'.$fdata['FeatureID'].'" /> <!-- BEGIN: feature --> <option value="{row.id}" {row.selected}>{row.title}</option> <!-- END: feature --> </tr> Any help would be appreciated this is killing i almost felt like i had it. Link to comment https://forums.phpfreaks.com/topic/137501-newbie-question-understanding-code/ Share on other sites More sharing options...
solon Posted December 18, 2008 Share Posted December 18, 2008 if i am not mistaken in the clean-up code you are trying to use adodb. In that case you need to download the library an upload it to your server as well as include the necessary files in your php code! If you dont need to use that then your code should be as simple as: <table border="0" cellspacing="0" cellpadding="0"><td colspan="2" rowspan="7" align="left" valign="top" bgcolor="#EDF4FB" class="borderLeft text"><tr><td></td></tr> <?php //in order to use an sql query, firstly you need to connect to the database //use something like th following: /* $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } //Select database $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); }*/ $results = mysql_query('select * from Features order by Feature ASC'); $num = mysql_num_rows($results); for ($i=0;$i<$num;$i++) { $data = mysql_fetch_array($results); echo ' <tr> <td valign="middle"><label><input type="checkbox" name="features[]" value="'.$data['FeatureID'].'"'; $fnum = count($_POST['features']); //this should be posted from the html website through a form for ($j=0;$j<$fnum;$j++) { if ($_POST['features'][$j] == $data['FeatureID']) { echo ' checked="true"'; } } echo '>'.$data['Feature'].'</label></td> </tr>'; } ?> </table> Link to comment https://forums.phpfreaks.com/topic/137501-newbie-question-understanding-code/#findComment-718582 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.