Page 1 of 1

Dynamic Checkboxes... [solved]

Posted: Fri Feb 10, 2006 12:58 pm
by J_Iceman05
Ok, I am creating a page to edit info in the database. You can add schools, change the names of current ones, or delete current schools.
This is what have for that portion

Code: Select all

echo "<form name=\"updateSchools\" method=\"post\" action=\"".$admin_file.".php\">"
			."<input type=\"hidden\" name=\"op\" value=\"updateSchools\">";
		echo '<table border="1" cellspacing="0" cellspadding="9">';
		echo '<tr><td colspan="2" align="center"><span style="font-size:14;"><b>Update Schools</b></span></td></tr>';
		echo '<tr><td><span style="font-size:14;">Delete&nbsp;</span></td><td><span style="font-size:14;">&nbsp;Schools Name&nbsp;</span></td></tr>';
		$schoolsResult = $db->sql_query("SELECT * FROM Schools ORDER BY SchoolsID");
		while ($schoolsRow = $db->sql_fetchrow($schoolsResult)) {
			$id = $schoolsRow['SchoolsID'];
			$name = $schoolsRow['SchoolsName'];
			echo '<tr><td align="center"><input type="checkbox" name="delete[]" value="yes"></td>';
			echo '<td><input type="text" name="name[]" value="'.$name.'"></td>';
			echo '<input type="hidden" name="id[]" value="'.$id.'">';
			$id = '';
			$name = '';
			$school_num ++;
		}
		echo '<input type="hidden" name="num" value="'.$school_num.'">';
		echo '<tr><td colspan="3" align="right"><input type="submit" value="update"</td></tr>';
		echo '</table>';
		echo '<br><br><br>';
		echo '</td></tr>';
		echo '</form>';
The next file uses case statements. The case statement for updateSchools is as follows...

Code: Select all

case "updateSchools":
         updateSchools($delete, $name, $id, $num);
break;
And that updateSchools function is next

Code: Select all

function updateSchools($delete, $name, $id, $num) {
		global $language, $admin, $prefix, $db, $admin_file;
		$count = 0;
		while ($count < $num) {
			if ($delete[$count] == 'yes') {
				echo ("DELETE FROM Schools WHERE SchoolsID = ".$id[$count].""); // i have it echo so that i can fix it
				echo "<br>";
			} else {
				echo ("UPDATE Schools SET SchoolsName='".$name[$count]."' WHERE SchoolsID = ".$id[$count]."");
				echo "<br>";
			}
			$count++;
		}
		//Header("Location: ".$admin_file.".php?op=dropdownOptions"); // once it is working correctly, it will redirect to the designated page.
	}
If none of the check boxes are checked or if all of them are checked, then everything is fine.
But when a box is checked, then for the update section, it reads those checks from the first to last.

EXAMPLE::

4 results from the query
-delete-name
1 ) [ ] - Yale
2 ) [ ] - Havard
3 ) [X] - UNLV
4 ) [X] - BYU
(rough layout attempt... :? )

UNLV and BYU should be set to delete right?
Well when the queries print, it says that Yale and Harvard should delete

If just Havard is checked to delete, the query says Yale will be deleted...
And if Yale is checked to delete, then the query says Yale will be deleted...

Please, If anyone understands why that is, it will VERY much appreciated...
Thanks in advance

Posted: Fri Feb 10, 2006 1:06 pm
by feyd
hint: delete[$id]

Posted: Fri Feb 10, 2006 1:17 pm
by J_Iceman05
I changed the case statement as follows...

From:

Code: Select all

case "updateSchools":
      updateSchools($delete, $name, $id, $num);
break;
To:

Code: Select all

case "updateSchools":
	$count = 0;
	$delCount = 0;
	while ($count < $num){
		if ($delete[$delCount] == $id[$count]) {
			$del[$count] = 'yes';
			$delCount ++;
		} else {
			$del[$count] = 'no';
		}
		$count++;
	}
	updateSchools($del, $name, $id, $num);
break;
I also changed the delete input box's value to be the $id

And now seems to be working well.

I'm not sure exactly what you were talking about feyd, but it did help.
Thank you very much for you speedy answer.