<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: user input code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 08 Aug 2008 21:19:01 GMT</pubDate>
    <description>DZone Snippets: user input code</description>
    <item>
      <title>PHP Dynamic Checkbox Table Creator (data retrieved from MySQL DB)</title>
      <link>http://snippets.dzone.com/posts/show/5450</link>
      <description>Hi All.&lt;br /&gt;This is a function for Dynamically Create a Checbox Table retrieving information for from a MySQL Database.&lt;br /&gt;As it is quite commented, it's also good for learning how this things work :)&lt;br /&gt;Hope you find it useful.&lt;br /&gt;Feedback is welcome.&lt;br /&gt;Cheers&lt;br /&gt;Dan&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function dynamic_checkbox_table ($sql_str, $col_label, $col_name, $val_checked="S", $cant_cols_tbl=3){&lt;br /&gt;/*&lt;br /&gt;	by Daniel Neumann&lt;br /&gt;	this script creates dynamically permite a table containing checkboxes &lt;br /&gt;	getting the data for the checkboxes from a MySQL DB&lt;br /&gt;	$sql_str, SQL select string to retrieve data from DB (see example in last comment line)&lt;br /&gt;	$col_label, DB column that has values for the checkbox label &lt;br /&gt;	$col_name, DB column that has values for the checkbox name&lt;br /&gt;	$val_checked="S", value when checked (value="" attribute) it uses the same value for all of them. If you whish to use a dynamic value from a DB, you should comment the line (it&#180;s explained next to the code in the middle of the function) and de-comment the other line (check the code,. you'll understand what I mean). Also, you should use this parameter to specify the column name for the values&lt;br /&gt;	$cant_cols_tbl=3, quantity of columns for the table, it defaults to 3&lt;br /&gt;	usage example: dynamic_checkbox_table("SELECT * FROM keywords", "Keyword", "ID_Keywrd");&lt;br /&gt;*/&lt;br /&gt;	&lt;br /&gt;	//connect DB and run query&lt;br /&gt;	$db="MyDB";&lt;br /&gt;	$db_user="MyUser";&lt;br /&gt;	$pass="MyPass";&lt;br /&gt;	$host="localhost";&lt;br /&gt;	@mysql_connect($host,$db_user,$pass);&lt;br /&gt;	@mysql_select_db($db) or die ("cannot connect to DB");&lt;br /&gt;	$q_resultado = mysql_query($sql_str);&lt;br /&gt;	mysql_close();&lt;br /&gt;	if (mysql_num_rows($q_resultado)==0) exit("no rows returned");&lt;br /&gt;	&lt;br /&gt;	$next_row = mysql_fetch_array($q_resultado); //fetch first row&lt;br /&gt;	&lt;br /&gt;	$output = "&lt;table  border=\"1\"&gt;\n"; //open table tag&lt;br /&gt;	do {&lt;br /&gt;		$output .= "&lt;tr&gt;\n"; //open row tag&lt;br /&gt;		for ($i=1 ; $i &lt;= $cant_cols_tbl ; $i++ ){ //loops as many times as $cant_cols_tbl&lt;br /&gt;			$row=$next_row; //assign $row, next row will be checking next one, that avoids starting a new row when it's gonna be empty&lt;br /&gt;			$output .= "&lt;td&gt;"; //open TD tag&lt;br /&gt;			$output .= (!$row) ? "" : '&lt;input type="checkbox" name="'.$row[$col_name].'" value="'.$val_checked.'" /&gt;'.$row[$col_label]; //create checkbox and data from $row (**** you should comment this line if you whish to use dynamic $val_checked****)&lt;br /&gt;//			echo (!$row) ? "" : '&lt;input type="checkbox" name="'.$row[$col_name].'" value="'.$row[$val_checked].'" /&gt;'.$row[$col_label]; //create checkbox and data from $row (**** you should de-comment this line if you whish to use dynamic $val_checked****)&lt;br /&gt;			$next_row = mysql_fetch_array($q_resultado); //retrieve next row&lt;br /&gt;			$output .= "&lt;/td&gt;\n"; //close TD&lt;br /&gt;		} //close for loop&lt;br /&gt;		$output .= "&lt;/tr&gt;\n"; //close row&lt;br /&gt;	} while ($next_row); //close do-while (and checks if there's another row)&lt;br /&gt;	$output .= "&lt;/table&gt;\n"; //close table&lt;br /&gt;	return $output; &lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 02 May 2008 12:09:34 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5450</guid>
      <author>dneuma (Daniel Neumann)</author>
    </item>
  </channel>
</rss>
