Dynamic Checkboxes... [solved]

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

Post Reply
User avatar
J_Iceman05
Forum Commoner
Posts: 72
Joined: Wed Aug 03, 2005 10:52 am
Location: Las Vegas, NV

Dynamic Checkboxes... [solved]

Post 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
Last edited by J_Iceman05 on Fri Feb 10, 2006 4:26 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

hint: delete[$id]
User avatar
J_Iceman05
Forum Commoner
Posts: 72
Joined: Wed Aug 03, 2005 10:52 am
Location: Las Vegas, NV

Post 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.
Post Reply