Dynamic Checkboxes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
brassfid
Forum Newbie
Posts: 18
Joined: Thu Jan 13, 2011 11:09 pm

Dynamic Checkboxes

Post by brassfid »

Alright, I built this script off of something that I found online. I spent quite some time working past all the errors, but now I have absolutely nothing populating. It is just blank. Can anyone point out the issue? Thanks!

Code: Select all

<?php

function dynamic_checkbox_table ($sql_str, $col_label, $col_name, $val_checked, $cant_cols_tbl){

	$sql_str = dynamic_checkbox_table("SELECT * FROM Categories");
	$col_label = "catlevel3";
	$col_name = "catlevel3";
	$val_checked="S";
	$cant_cols_tbl=3;
	
	//connect DB and run query
	$db="TB";
	$db_user="";
	$pass="";
	$host="localhost";
	@mysql_connect($host,$db_user,$pass);
	@mysql_select_db($db) or die ("cannot connect to DB");
	$q_resultado = mysql_query($sql_str);
	mysql_close();
	if (mysql_num_rows($q_resultado)==0) exit("no rows returned");
	
	$next_row = mysql_fetch_array($q_resultado); //fetch first row
	
	$output = "<table  border=\"1\">\n"; //open table tag
	do {
		$output .= "<tr>\n"; //open row tag
		for ($i=1 ; $i <= $cant_cols_tbl ; $i++ ){ //loops as many times as $cant_cols_tbl
			$row=$next_row; 
			$output .= "<td>"; //open TD tag
			$output .= (!$row) ? "" : '<input type="checkbox" name="'.$row[$col_name].'" value="'.$val_checked.'" />'.$row[$col_label]; 
			echo (!$row) ? "" : '<input type="checkbox" name="'.$row[$col_name].'" value="'.$row[$val_checked].'" />'.$row[$col_label]; 
			$next_row = mysql_fetch_array($q_resultado); //retrieve next row
			$output .= "</td>\n"; //close TD
		} //close for loop
		$output .= "</tr>\n"; //close row
	} while ($next_row); //close do-while (and checks if there's another row)
	$output .= "</table>\n"; //close table
	return $output; 
}

?>
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: Dynamic Checkboxes

Post by danwguy »

You are closing your sql and then trying to define variables. get rid of that mysql_close() for one.
brassfid
Forum Newbie
Posts: 18
Joined: Thu Jan 13, 2011 11:09 pm

Re: Dynamic Checkboxes

Post by brassfid »

Isn't that just closing mysql if there are no rows returned?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Dynamic Checkboxes

Post by John Cartwright »

As memory serves, mysql_close() closes the connection and frees resources (which you need to work with).
brassfid
Forum Newbie
Posts: 18
Joined: Thu Jan 13, 2011 11:09 pm

Re: Dynamic Checkboxes

Post by brassfid »

Alright, ultimately I decided to go with what I could piece together with my limited knowledge. Below is the code that I made to create a dynamic radio button list. Not exactly what I wanted, but it still looks and works great. I am posting primarily for anyone that is looking for a similar thing.

Next on the list, how do I pull data from here when a radio button is pushed besides none?

Thanks everyone for your help, it is much appreciated from this novice.

Code: Select all

<?php

$con = mysql_connect("localhost","","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("db", $con);
$result = mysql_query("SELECT * FROM Categories WHERE cattype = 'Aroma' ORDER BY catdesc");
while ($array = mysql_fetch_array($result)) {
 
 echo '<table border="1">
<tr>
    <td width = "36%">
	<b>'.$array['catdesc'].'</ b>
    </td>
    <td width = "16%" align=center>
	<INPUT TYPE="radio" NAME="'.$array['catdesc'].'" VALUE="0" checked>
	<font size="2">None</font>
    </td>
    <td width = "16%" align=center>
	<INPUT TYPE="radio" NAME="'.$array['catdesc'].'" VALUE="1">
	<font size="2">Mild</font>
    </td>
    <td width = "16%" align=center>
	<INPUT TYPE="radio" NAME="'.$array['catdesc'].'" VALUE="2">
	<font size="2">Med.</font>
    </td>
    <td width = "16%" align=center>
	<INPUT TYPE="radio" NAME="'.$array['catdesc'].'" VALUE="3">
	<font size="2">Strong</font>
    </td>
    </tr>
</table>';
 
 }
 mysql_close($con)
 
?>
Post Reply