Dear php gods, could you look at this code and help me?
Posted: Wed Aug 29, 2007 1:59 am
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:
Please feel free to ask me to explain anything.
Thanks
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>'
?>Thanks