Jump to content

Newbie Question understanding code


onedubios

Recommended Posts

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

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>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.