Page 2 of 2

Posted: Thu Aug 30, 2007 7:16 am
by lafflin
Wow! I got it working. the best feeling in the world! Thanks to both of you guys. I was really having trouble explaining what I was trying to do. I would not have figured out how to do this without that tip about creating an array from a checkbox, thanks Moose. And thanks you too Everah.

I have been at this all day, the answer I was seeking was this:

Code: Select all

if (isset($_POST['submitted']))    /*-------------------------------------------------------------------------------------*/ {
if (isset($_POST['classes']))  {   $classes_selected = ' AND c.name = ' ;

$boxes = $_POST['classes'];



foreach ($boxes as $key=>$class)  
              { if ($classes_selected == ' AND c.name = ') {  $classes_selected .= '\''.$class.'\'' ;}
                                                             else {$classes_selected .= 'OR \''.$class.'\'' ;}                                
								   }
                                    } else $classes_selected = ' ' ;
I learned alot, and I am posting the answer to my question as well as my full code for anyone who might find it to be useful.

Code: Select all

<?php # - search.php -

require_once ('./includes/mysql_connect.php'); // Connect to the db.

$page_title = 'search students';
include ('./includes/header.inc.htm');

echo '<h1 id="mainhead" align="center">Search Students</h1>';
echo'
<form action="search_students.php" method="post">

<p>
<label> First Name </label><input name="fn" type="text"  maxlength="8" />
<label> Last Name </label><input name="ln" type="text"  maxlength="8" />
<label> Parent </label><input type="text" name="parent"  maxlength="8" />
</P>
<p>
<div align="center">
<input type="submit" name="Submit" value="Submit" accesskey="z" id="Submit" />
</div>
</p>

<input type="hidden" name="submitted" />
';  



$query_get_classes = "select name from classes";

$result0 = mysql_query ($query_get_classes) or die($query_get_classes); // Run the query.

if ($result0) { // If it ran OK, display the records.    
	echo '<table align="right"><tr><td>' ;
	// Fetch and print all the records.
	while ($row = mysql_fetch_array($result0, MYSQL_ASSOC)) {
		echo '<input type = "checkbox" name = "classes[]" value = "'.$row['name'].'" id="'.$row['name'].'"/><label for "'.$row['name'].'">'.$row['name'].'</label><br/>' ;
	}
	echo '</tr></td></table></form>' ;
	            }

// Check if the form has been submitted.
if (isset($_POST['submitted']))    /*-------------------------------------------------------------------------------------*/ {
if (isset($_POST['classes']))  {   $classes_selected = ' AND c.name = ' ;

$boxes = $_POST['classes'];



foreach ($boxes as $key=>$class)  
              { if ($classes_selected == ' AND c.name = ') {  $classes_selected .= '\''.$class.'\'' ;}
                                                             else {$classes_selected .= 'OR \''.$class.'\'' ;}                                
								   }
                                    } else {$classes_selected = ' ' ;}
									
	
	
$query = "SELECT    s.sid,
                    s.first_name,
					s.last_name,
					s.sex,
					s.dob,
					s.reg_date,
					s.active,
					p.parent_name,
					p.2nd_parent_name,
					p.email,
					p.home_phone,
					p.cell_phone,
					p.other_phone,
					p.emergency_contact_info,
					p.street_address,
					p.street_address_line_2,
					p.city,
					p.zip,
					c.name
			FROM 
				          student_info s,
						  student_parent sp, 
						  parent_info p,
						  classes c,
						  classes_students cs
						  
	WHERE
	       s.sid = sp.sid and s.sid = cs.sid and c.cid=cs.cid and s.first_name LIKE '%".trim($_POST['fn'])."%' AND s.last_name LIKE '%".trim($_POST['ln'])."%' and  p.parent_name LIKE '%".trim($_POST['parent'])."%'  $classes_selected    
		   " ;
		   
		   




$result = mysql_query ($query) or die($query); // Run the query.

if ($result) { // If it ran OK, display the records.    


	// Table header.
	echo '<table align="center" cellspacing="0" cellpadding="5">
	<tr ><td><td align="left"><h3>First Name</h3></td><td><h3>Last Name</h3></td>';
	
	// Fetch and print all the records.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  echo '<tr bgcolor="#993300"bgcolor="#993300">' . '<td>'.$row['sid'].'<td align="left">'.$row['first_name'] . '</td><td align="left">' . $row['last_name'] . '</td></tr>
		   <tr><td align="left"><P> Parent: ' . $row['parent_name'] . '</P></td><td align="left"><p>Class: ' . $row['name'] . '</p>
		   </td></tr>';
	}

	echo '</table>';
	
	mysql_free_result ($result); // Free up the resources.	

} else { // If it did not run OK.
	echo '<p class="error">The current users could not be retrieved. We apologize for any inconvenience.</p>'; // Public message.
	echo '<p>' . mysql_error() . '<br /><br />Query: ' . $query . '</p>'; // Debugging message.
}

mysql_close(); // Close the database connection.
                                                                                                              
/*--------------------------IF is submitted----------------------------------------------------------------------------------*/  }

echo '</body>
       </html>'

?>

Posted: Thu Aug 30, 2007 10:40 am
by RobertGonzalez
Glad you got it worked out bud.

solution for creating array from dynamic checkboxes [solved]

Posted: Thu Aug 30, 2007 9:38 pm
by lafflin
Thanks, it was hard to describe what I wanted to do because I really didn't know what I wanted to do.

Posted: Fri Aug 31, 2007 11:29 am
by RobertGonzalez
Hey, all of us had to start learning somewhere. I remember my early days of learning, and not being to convey what I wanted to do. It evolved into me being able to explain what I wanted to do into me being able to imagine the code doing what I wanted to do as I though of what I wanted.

You'll get there. Keep learning.