Multi checkbox

Hi

I have form like this at attachment , the problem i want to choose two or more choise for each book
for example(book1–> economy and law)

type of books from database table ,and books at another table
i want to have these results

1---->law-Engineering
2---->law-Engineering-Economy

Create a really simple little page using straight html checkboxes.

Put them in a form.

Name each row differently.

Each row’s name can be an array, lets call one row ‘motoring’


<input type=checkbox name=motoring[law]>Law
<input type=checkbox name=motoring[economy]>Economy
<input type=checkbox name=motoring[engineerng]>Engineering

Make that form post back to itself and analyse how the checkbox values are passed back to PHP from your form by doing this:


<?php
var_dump($_POST);  // or $_GET if you are using that method
?>

... // your html form here

sorry this is myform and I want results to be
book number and it’s types
I want to save results in database in this way

num |type

1 |law,economy
2 |law,economy,Engineering


	
	<?
	$sql= "SELECT * ".
       "FROM books";

    $result=mysql_query($sql);?>
    <form name="myform" method="post" action="">
    	<table>
		<tr>
		<td >no</td>
		<td >books</td>
	<?$sql= "SELECT * ".
       "FROM booktypes "
       ;
    $result=mysql_query($sql);
    while($row=mysql_fetch_array($result))
    {?><td><? echo ' '.$row['typename'].' '; }?></td>	
		</tr>
		<?
    while($row=mysql_fetch_array($result))
    {


	?> <tr>
				<td><? echo $row[num]; ?></td>
		<td><? echo $row[name]; ?></td>

	<? $sqlt= "SELECT * ".
       "FROM booktype";

    $resultt=mysql_query($sqlt);
    while($rowt=mysql_fetch_array($resultt)){
    $tname=	$rowt[typename];
   ?>
					
	<td><input type="checkbox" name="branchname[]" value=""></td><?}?>
						
			</tr>
		<?} ?>
	<tr>
						
						</tr>
		</table><input type="submit" value="save" id="save" name="save"></form>

You should be able to assign book type and book id to each checkbox and compare posted values to array of books and their book type and if match is found display list of books. Not knowing your table structure I made something up so table fields may need to be adjusted. This sample might help.

<?php
//query books and build array
	$books_array = array();
    $sql= "SELECT id, bookname, booktype FROM books";
	$result=mysql_query($sql);
	while($row=mysql_fetch_array($result)){
		$books_array[$row[0]]['id'] = $row[0];
		$books_array[$row[0]]['bookname'] = $row[1];
		$books_array[$row[0]]['booktype'] = $row[2];
	}
//query book types and build array
	$books_types = array();
    $sql= "SELECT typename FROM booktypes";
	$result=mysql_query($sql);
	while($row=mysql_fetch_array($result)){
	$books_types[] = $row[0];
	}
	


	$content = '<form name="myform" method="post" action="">
        <table>';
		$booktype_cnt = count($books_types);
		$rowcnt = $booktype_cnt+2;
 	$content .= "<tr><th colspan=\\"$rowcnt\\">Select Books</th></tr>\\r";		
 	$content .= '<tr>';
	//use $books_types array to build form table headings
		foreach ($books_types as $book_type){
	$content .= "<td align=\\"center\\">$book_type</td>\\r";
		}
	$content .= "<td>books</td>
		<td>no</td>
	</tr>";
	
	//use foreach to run through array of books
	foreach ($books_array as $books){
		$content .= '<tr>';
	//use $books_types array to build form checbox set
		foreach ($books_types as $book_type){
			$content .= "<td><input type=\\"checkbox\\" name=\\"branchname[$book_type][]\\" value=\\"{$books['id']}\\" /></td>\\r";
		}
		$content .= "<td>{$books['bookname']}</td>
		<td align=\\"center\\">{$books['id']}</td>	
	</tr>";	
	}
	
	 	$content .= "<tr><td colspan=\\"$rowcnt\\"><input type=\\"submit\\" value=\\"save\\" id=\\"save\\" name=\\"save\\" /></td></tr>\\r";	
	$content .= '</table>
	</form>';
	
	if (isset($_POST['save'])){
		foreach ($_POST['branchname'] as $type => $selected_books){
			foreach ($selected_books as $k => $bookid){
			//if selected book id has a book type matching post type, list book
				$bt = explode(",",$books_array[$bookid]['booktype']);
				if (in_array($type,$bt)){
					$content .= "<a href=\\"Books.php?book_id=$bookid\\">{$books_array[$bookid]['bookname']}</a><br />\\r";
				}
			}		
		}	
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<?php		
echo "$content";
?>
</body>
</html>

every thing is good but there’s no results :slight_smile:
I don’t know what’s the problem at foreach??

  foreach ($_POST['branchname'] as $type => $selected_books){
            foreach ($selected_books as $k => $bookid){
            //if selected book id has a book type matching post type, list book
                $bt = explode(",",$books_array[$bookid]['booktype']);
                if (in_array($type,$bt)){
                    $content .= "<a href=\\"Books.php?book_id=$bookid\\">{$books_array[$bookid]['bookname']}</a><br />\\r";
                }
            }  

Hey sorry man, I didn’t read your post well. Thought we were searching for books of type. This copy should “update” book types based on form post. Be sure to look for table field differences as I don’t know what you have, for example I used ‘id’ as the primary key where you might have something else.

<?php
	//Process "save"
	if (isset($_POST['save'])){
	//build a little array with book id being primary key, types as values	
		$bookkeys = array();		
		foreach ($_POST['branchname'] as $type => $selected_books){
			foreach ($selected_books as $k => $bookid){
				$bookkeys[$bookid][] = $type;
			}	
		}
		if (!empty($bookkeys)){
			foreach($bookkeys as $id => $array){
				$types = implode(",",$array);
				$sql = "UPDATE books SET booktype = '$types' WHERE id = '$id'";				
				$result=mysql_query($sql);
			}
		}		
	}

//query books and build array
	$books_array = array();
    $sql= "SELECT id, bookname, booktype FROM books";
	$result=mysql_query($sql);
	while($row=mysql_fetch_array($result)){
		$books_array[$row[0]]['id'] = $row[0];
		$books_array[$row[0]]['bookname'] = $row[1];
		$books_array[$row[0]]['booktype'] = $row[2];
	}
//query book types and build array
	$books_types = array();
    $sql= "SELECT typename FROM booktypes";
	$result=mysql_query($sql);
	while($row=mysql_fetch_array($result)){
	$books_types[] = $row[0];
	}
	


	$content = '<form name="myform" method="post" action="">
        <table>';
		$booktype_cnt = count($books_types);
		$rowcnt = $booktype_cnt+2;
 	$content .= "<tr><th colspan=\\"$rowcnt\\">Select Book Types</th></tr>\\r";		
 	$content .= '<tr>';
	//use $books_types array to build form table headings
		foreach ($books_types as $book_type){
	$content .= "<td align=\\"center\\">$book_type</td>\\r";
		}
	$content .= "<td>books</td>
		<td>no</td>
	</tr>";
	
	//use foreach to run through array of books
	foreach ($books_array as $books){
		$content .= '<tr>';
	//use $books_types array to build form checbox set
		foreach ($books_types as $book_type){
			$types_array = explode(",",$books['booktype']);
			$checked = (in_array($book_type,$types_array) ? " checked=\\"checked\\"" : '');
			$content .= "<td><input type=\\"checkbox\\" name=\\"branchname[$book_type][]\\" value=\\"{$books['id']}\\"$checked /></td>\\r";
		}
		$content .= "<td>{$books['bookname']}</td>
		<td align=\\"center\\">{$books['id']}</td>	
	</tr>";	
	}
	
	 	$content .= "<tr><td colspan=\\"$rowcnt\\"><input type=\\"submit\\" value=\\"save\\" id=\\"save\\" name=\\"save\\" /></td></tr>\\r";	
	$content .= '</table>
	</form>';

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<?php		
echo "$content";
?>
</body>
</html>

thank,it’s work good

but This code will be implemented for all books in every time and I want the code modifies the books that are chosen only (If I added a new book or want to modify an old book)

Well, in order to compare old and new values you’ll need to have these old values available at POST, which could be gotten from an extra hidden field in your form to pass old values back or making a query within POST for old values. I opted for the later in this example. We can then use array_diff() to see if values are different however this comparison is one-way meaning if A is different than B so we need to also check if B is different than A, if you follow what I’m saying. I’ve set the results of these two checks as $value1 and $value2. Then a simple check if !empty() qualifies the particular record as needing to be updated. Keeping a new query after POST processing gives you updated results for display.

<?php	
	//Process "save"
	if (isset($_POST['save'])){
	
		//query books and build array
		$books_array = array();
	    $sql= "SELECT id, bookname, booktype FROM books";
		$result=mysql_query($sql);
		while($row=mysql_fetch_array($result)){
			$books_array[$row[0]]['id'] = $row[0];
			$books_array[$row[0]]['bookname'] = $row[1];
			$books_array[$row[0]]['booktype'] = $row[2];
		}
		
		//build a little array with book id being primary key, types as values	
		$bookkeys = array();		
		foreach ($_POST['branchname'] as $type => $selected_books){
			foreach ($selected_books as $k => $bookid){
				$bookkeys[$bookid][] = $type;
			}	
		}
		
		////////////////	
		if (!empty($bookkeys)){
			foreach($bookkeys as $id => $array){
				$old_types = explode(",",$books_array[$id]['booktype']);
				$values1 = array_diff($array,$old_types);
				$values2 = array_diff($old_types,$array);
				if (!empty($values1) || !empty($values2)){
					$types = implode(",",$array);
					$sql = "UPDATE books SET booktype = '$types' WHERE id = '$id'";				
					$result=mysql_query($sql);
				}
			}
		}		
	}

//query books and build array
	$books_array = array();
    $sql= "SELECT id, bookname, booktype FROM books";
	$result=mysql_query($sql);
	while($row=mysql_fetch_array($result)){
		$books_array[$row[0]]['id'] = $row[0];
		$books_array[$row[0]]['bookname'] = $row[1];
		$books_array[$row[0]]['booktype'] = $row[2];
	}
	
//query book types and build array
	$books_types = array();
    $sql= "SELECT typename FROM booktypes";
	$result=mysql_query($sql);
	while($row=mysql_fetch_array($result)){
	$books_types[] = $row[0];
	}
	


	$content = '<form name="myform" method="post" action="">
        <table>';
		$booktype_cnt = count($books_types);
		$rowcnt = $booktype_cnt+2;
 	$content .= "<tr><th colspan=\\"$rowcnt\\">Select Book Types</th></tr>\\r";		
 	$content .= '<tr>';
	//use $books_types array to build form table headings
		foreach ($books_types as $book_type){
	$content .= "<td align=\\"center\\">$book_type</td>\\r";
		}
	$content .= "<td>books</td>
		<td>no</td>
	</tr>";
	
	//use foreach to run through array of books
	foreach ($books_array as $books){
		$content .= '<tr>';
	//use $books_types array to build form checbox set
		foreach ($books_types as $book_type){
			$types_array = explode(",",$books['booktype']);
			$checked = (in_array($book_type,$types_array) ? " checked=\\"checked\\"" : '');
			$content .= "<td><input type=\\"checkbox\\" name=\\"branchname[$book_type][]\\" value=\\"{$books['id']}\\"$checked /></td>\\r";
		}
		$content .= "<td>{$books['bookname']}</td>
		<td align=\\"center\\">{$books['id']}</td>	
	</tr>";	
	}
	
	 	$content .= "<tr><td colspan=\\"$rowcnt\\"><input type=\\"submit\\" value=\\"save\\" id=\\"save\\" name=\\"save\\" /></td></tr>\\r";	
	$content .= '</table>
	</form>';

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<?php		
echo "$content";
?>
</body>
</html>

thanks Drummin
this script works well with me