Dear php gods, could you look at this code and help me?

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

lafflin
Forum Contributor
Posts: 123
Joined: Thu Jul 26, 2007 6:26 pm

Dear php gods, could you look at this code and help me?

Post by lafflin »

Hi, first off let me thank anyone who takes the time to read all this code. If i didn't write it myself I would never understand it.
Second, please understand that this is my first project, I have only a few weeks of php and mysql under my belt.

Before I post my code I will try to explain what I'm trying to do.

I have a long query in the middle, on the end is a variable ($classes_selected), this variable corresponds to some check boxes which are dynamic in that this script will query the DB to find out how many and what the names are. The user will have created the table in another form. The table will be of class names, and each season when the user (a teacher) plans the schedule it will be different.

Because these check boxes are dynamic I had to create a series of conditional statements to format the $classes_selected variable for appending to the end. So far I was able to do that correctly with two static checkbox values (mine, and children) , but that was just a practice excercise for me, those boxes will not be in my final product.

I have a query/array combo now that will create the dynamic boxes in my form, but I have no idea how to implement the $classes_selected variable conditional statements to prepare the dynamic checkbox values for appending to my query like I did with the two static ones.

I'm not sure that anyone is going to be able to decipher all this, but anyone who can understand whats going on and actually be able to come up with a solution would be a PHP god as far as I'm concerned and shall ever be known as such.
on a more serious note, I appreciate any consideration given to this. I'm sorry that my code is sloppy, I am just learning this stuff.

here is my code:

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">

<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" />
<input type="checkbox" name="mine" id="Mine" /><label for="mine">Mine</label>
                 
<input type="checkbox" name="childrens" id="childrens" /><label for="childrens">childrens</label>

<input type="submit" name="Submit" value="Submit" accesskey="z" id="Submit" />
<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.    
	
	// Fetch and print all the records.
	while ($row = mysql_fetch_array($result0, MYSQL_ASSOC)) {
		echo '<input type = "checkbox"  name = "'.$row['name'].'" id="'.$row['name'].'"/><label for "'.$row['name'].'">'.$row['name'].'</label><br/>' ;
	}
	echo '</form>' ;
	            }

// Check if the form has been submitted.
if (isset($_POST['submitted']))    /*-------------------------------------------------------------------------------------*/ {

if ((isset($_POST['mine'])) or (isset($_POST['childrens'])))       {
//----------if checkboxes are selected than we need to prepare them to be tacked onto the end of our query----------------------------//

                   $classes_selected = 'AND c.name =' ;
				   
if (isset($_POST['mine'])) {$classes_selected .="'mine'" ;}
 
   if (isset($_POST['childrens'])) {    if ($classes_selected == 'AND c.name =') 
                                            {$classes_selected .="'childrens'" ; }
											  else {  $classes_selected .="' OR 'childrens'" ;
											          }
   
   
                                     			}	 
												                     } else { $classes_selected = ' '; }  


	


$query = "SELECT 
                    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 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 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>'

?>
Please feel free to ask me to explain anything.
Thanks
User avatar
xpgeek
Forum Contributor
Posts: 146
Joined: Mon May 22, 2006 1:45 am
Location: Kyiv, Ukraine
Contact:

Post by xpgeek »

Please post small piece of you code which you think not working and good explain of error or errors.
It's fastest way to get help.
We are all busy people.
lafflin
Forum Contributor
Posts: 123
Joined: Thu Jul 26, 2007 6:26 pm

shorter version

Post by lafflin »

Code: Select all

while ($row = mysql_fetch_array($result0, MYSQL_ASSOC)) { 
                echo '<input type = "checkbox"  name = "'.$row['name'].'" id="'.$row['name'].'"/><label for "'.$row['name'].'">'.$row['name'].'</label><br/>' ;  }
That is how i am creating a series of checkboxes.

I need to figure out how to proccess the value of each selected checkbox for insertion into a query like this:

Code: Select all

if ((isset($_POST['mine'])) or (isset($_POST['childrens'])))                         {

//----------if checkboxes are selected than we need to prepare them to be tacked onto the end of our query------

                   $classes_selected = 'AND c.name =' ;
				   
if (isset($_POST['mine'])) {$classes_selected .="'mine'" ;}
 
   if (isset($_POST['childrens'])) {    if ($classes_selected == 'AND c.name =') 
                                            {$classes_selected .="'childrens'" ; }
		                            else {  $classes_selected .="' OR 'childrens'" ;
											          }
   
   
                                     			}	 
												                     } else 
                                              { $classes_selected = ' ';                          }

Issue for me is that I have used the above method only for proccessing a checkbox to which I know the name of. I want to be able to do that same thing when I don't know the number of checkboxes that will be returned by the above query or the names.

just for clarification here is the clause part of my query:

Code: Select all

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    
		   " ;
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Can I ask what is not working and what you want to work and how it is supposed to work? That would help straighten things up a bit.
lafflin
Forum Contributor
Posts: 123
Joined: Thu Jul 26, 2007 6:26 pm

Post by lafflin »

Sorry Ev, I am trying to explain this, but it's tough. let me try to take another shot.

I have this script which is far from being complete, it's purpose is to both create and handle a form which will be at the heart of my application. The purpose of the form is to search the DB by as many or as few fields as the user would like.
The complication comes from the checkboxes on the form. The values for the checkboxes are rows of a table that will be created by the user in another form. I have no way of knowing how many rows will be created or what there names will be, so I have to query the DB in this script to pull the values and create these checkboxes. Which you will see I have done.
But then in order to allow ther user to select which of these checkbox values (classes) they would like to pull information from I have to prepare the value in such a way that I can append that value as a string into my query similar to this:

Code: Select all

if ((isset($_POST['mine'])) or (isset($_POST['childrens'])))       {
//----------if checkboxes are selected than we need to prepare them to be tacked onto the end of our query----------------------------//

                   $classes_selected = 'AND c.name =' ;
				   
if (isset($_POST['mine'])) {$classes_selected .="'mine'" ;}
 
   if (isset($_POST['childrens'])) {    if ($classes_selected == 'AND c.name =') 
                                            {$classes_selected .="'childrens'" ; }
											  else {  $classes_selected .="' OR 'childrens'" ;
											          }
   
   
                                     			}	 
												                     } else { $classes_selected = ' '; }
However the values 'mine' and 'children' were just examples. I would actually need to use the values in the array that is created from querying the DB to do the same thing.

Code: Select all

$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.    
	
	// Fetch and print all the records.
	while ($row = mysql_fetch_array($result0, MYSQL_ASSOC)) {
		echo '<input type = "checkbox"  name = "'.$row['name'].'" id="'.$row['name'].'"/><label for "'.$row['name'].'">'.$row['name'].'</label><br/>' ;
	}
I'm sure that this can probably be done, but I just don't have the know how to do this. So what I'm asking for is any ideas on how I can accomplish this.
lafflin
Forum Contributor
Posts: 123
Joined: Thu Jul 26, 2007 6:26 pm

Post by lafflin »

It seems I can't come up with a short answer and I'm complicating things a bit.
Let me try to explain this more efficiently:

I have a form created in this script, there are several text inputs for a user to fill out to query my DB, there is also some checkboxes to select which classes to search in. These classes change periodically, in name and number of classes. I am using the $classes_selected variable to tie the classes in which to search to my query. because I have to append "AND or "OR" into my query depending on the number of classes selected I have an IF ELSE conditional to format the $classes_selected variable into the correct string for the query. Right now I have it working with static checkbox data that I created just for testing.
The question is this: How can I implement this same technique for data that will be created by my first query and stored as the $results0 array?

Sorry If I haven't explained this clearly, I am trying.
User avatar
TheMoose
Forum Contributor
Posts: 351
Joined: Tue May 23, 2006 10:42 am

Post by TheMoose »

Makes a little more sense now, and I think I know what you're asking.

What you can do is change the NAME attribute in your checkboxes to "classes[]", and set the VALUE attribute of the checkbox to the name of the class, that way inside your PHP code when you do a $_POST['classes'], it will return an array of checked boxes and their associated names.

IE:

Code: Select all

echo $_POST['classes'][0]; // "Foo"
echo $_POST['classes'][1]; // "Bar"
lafflin
Forum Contributor
Posts: 123
Joined: Thu Jul 26, 2007 6:26 pm

Post by lafflin »

ooh, that might just be the bit of info I needed to come up with a solution, I'm going to work on it a bit and see what I come up with.
lafflin
Forum Contributor
Posts: 123
Joined: Thu Jul 26, 2007 6:26 pm

Post by lafflin »

I'm trying some things with the advice you gace me, but after namming my boxes " classs[]" and then calling them as such

Code: Select all

echo $_POST['classes'];
I am getting printed just the word "array"

when I try to call on an index of the array as such

Code: Select all

echo $_POST['classes'][1];
I am getting nothing at all printed out to the browser. I have tried several other numbers, but with the same result.
lafflin
Forum Contributor
Posts: 123
Joined: Thu Jul 26, 2007 6:26 pm

Post by lafflin »

Actually, I'm not really sure how that will help me anyway. Ofcourse I'm not saying that it won't help me find a solution, but just that I haven't figured out how.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Don't use echo, use either print_r() or var_dump() to get information about the variable.
User avatar
TheMoose
Forum Contributor
Posts: 351
Joined: Tue May 23, 2006 10:42 am

Post by TheMoose »

Add the following to help debug:

Code: Select all

var_dump($_POST['classes']);
It will print out everything in that variable. If you don't check any checkbox, it will print out NULL.
lafflin
Forum Contributor
Posts: 123
Joined: Thu Jul 26, 2007 6:26 pm

Post by lafflin »

alright, I got that working and my array does have the values as it should, now I just have to figure out how to loop through them and modify my query appropriately. It might be a while before I post again, because I have alot to try to figure out, but thatnks alot for what you've shown me.
lafflin
Forum Contributor
Posts: 123
Joined: Thu Jul 26, 2007 6:26 pm

Post by lafflin »

OK, now I'm approaching abusing forum support, but I just don't know how to use the loop through the classes[] array each time running the above mentioned variable formatting technique :

Code: Select all

$classes_selected = 'AND c.name =' ;
				   
if (isset($_POST['mine'])) {$classes_selected .="'mine'" ;}
 
   if (isset($_POST['childrens'])) {    if ($classes_selected == 'AND c.name =') 
                                            {$classes_selected .="'childrens'" ; }
											  else {  $classes_selected .="' OR 'childrens'" ;
											          }
   
   
                                     			}	 
												                     } else { $classes_selected = ' '; }
I've done alot of research and I just can't figure it out on my own, any advice on how to do this would really be appreciated.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Post Reply