Page 1 of 1

Looping trough an array of checkboxes ?!?

Posted: Wed Oct 22, 2003 7:56 am
by ThaRebel
Its been a strugeling in ASP, and now i have to write it in PHP. I have read many articles about this issue but it seems i cant solve it. :cry:

This is the following problem:


ASP-CODE

'Run through till all checked messages are approved

For each iaryMesageID in Request.Form("approve")


'Initalise the uploadSQL variable with the SQL string
uploadSQL = "SELECT * FROM disapprovedfiles"
uploadSQL = uploadSQL & " WHERE UploadID=" & iaryMesageID
'Query the database
adoRec.Open uploadSQL, strCon

'Approve the record is one is returned by the recordset
If NOT adoRec.EOF Then
adoRec.fields("Approved") = true
' boxname = "downloads" & adoRec.Fields.Item(0)
downloadstemp = Request.Form("downloads" & adoRec.Fields.Item(0))

adoRec.fields("Downloads") = downloadstemp
end if

'Close the recordset
adoRec.update
adoRec.Close

Next


What I got in PHP code now

while (list($key,$val) = each ($_POST)) {

echo "Key: $key; Value: $val<br>\n";

}

This would have been the same as the foreach.

I have set the value of a ID out of the database! The question is how to loop trough all checkboxes with name "approve", and get the value out of it?

Posted: Wed Oct 22, 2003 9:56 am
by murph
name all the checkboxes the same name with a [] after them. so it would be like name[]. then i php just do

Code: Select all

for ( $i = 0; $i < count ( $_POST['name']; $i++ )
{
    echo $_POST['name'][$i];
}

Posted: Wed Oct 22, 2003 10:22 am
by ThaRebel
yup thnx :P

this is my code, same as yours.

Code: Select all

<?php
//reset ($HTTP_POST_VARS);
if (is_array($approve)) {

	// Loop through the array of results 
	foreach ($approve as $value) {
	
      // Formulate SQL statement to approve files
      $ActivateUsersSQL = "UPDATE listfiles SET Approved=1 WHERE UploadID='$value'";

      // Run the query
      $ActivateUsersResult = mysql_query($ActivateUsersSQL) or die ("Couldn't get results");


	}

}
?>

Posted: Wed Oct 22, 2003 12:22 pm
by ThaRebel
And what about textboxes? I got textboxes generated as follow:

Code: Select all

<?php
<td width="70" align="center" bgcolor="#FEE0B2"><INPUT style="WIDTH: 54px; HEIGHT: 22px" size=6 value=0 name="downloads[]" id="<?echo $row["UploadID"]?>"></td>
?>
I want to loop through the textboxes same as in the last example for the checkboxes, it might not be possible to do it in the same loop.

The Code

Code: Select all

<?php
//downloadcountadd loop
if (is_array($downloads)) {


	// Loop through the array of results 
	foreach ($downloads as $id) {
	
      // Formulate SQL statement to approve files

		echo $id . "<br>";
		echo $value . "<br>";
		
      //$downloadsSQL = "UPDATE listfiles SET Downloads='$value' WHERE UploadID='$ID'";		
      // Run the query
      //$downloadsResult = mysql_query($downloadsSQL) or die ("Couldn't get results");


	}

}
?>
The outcome of this is 2 records wich i checked with the checkboxes with the same ID:

0 <<<< is the value in the textbox
952 <<<< id of the textbox
0 <<<< is the value in the textbox
952 <<<< id of the textbox(not they array number)

The quesion issue is as follow: How can i loop through the textboxes and get the value out and update the record according to the ID :?: