Help with checkboxes and array for inport and update

Hi all,

I am creating a contract database, and in the bit I am at now, the users are being created and they also need to be connected to the corporations that have already been created.

The form I have is i am using the same form to add and then if needed edit the users, and have managed to successfully create the checkboxes to display to be able to add the users with the code below:

(I know I need to update to mysqli, which I will do once i get this working, as need ot anyway as the servers are being changed too)


<label class="myCheckbox">Corporations</label>
<?php
 $q="select * from Corporations";
 $result = mysql_query($q) or die(mysql_error());
 while($row = mysql_fetch_assoc($result)) {
    echo <<<EOL
 <div class="myCheckbox">
 <input type="checkbox" name="name[]" value="{$row['ID']}" /> <span>{$row['Name']}</span><br />
 </div>
EOL;
} ?>


The checkboxes display fine on the page, but what I need to do now is allow the admin person to select the corporations they want to attach to the user they are adding in.

How do I get these checkbox values into the field name ‘Corporations’ in the Useradmin table as an array. Suppose start with that first.


 
if(isset($_POST['btnsubmit']))
{
$sr=$_POST['srno1'];
if ($flag==0)
{
$userFirstName=mysql_real_escape_string($_POST['firstName']);
$userSurName=mysql_real_escape_string($_POST['surName']);
$userUserName=mysql_real_escape_string($_POST['userName']);
$userPassWord=mysql_real_escape_string($_POST['pWord']);
$userActive=$_POST['Active'];  
$userSuperAdmin=mysql_real_escape_string($_POST['superAdmin']);
$userCorporationName=????????????;
$q24=mysql_query("update UserAdmin set First Name='$userFirstName', Last Name='$userSurName', Username='$userUserNam', Password='$userPassWord', Active='$userActive', Super Admin='$userSuperAdmin', Corporations='??????????', Date='$dt2' where ID=$sr") or die (mysql_error());
$flag=1;
$update="1";
}
else
{
$userFirstName=mysql_real_escape_string($_POST['firstName']);
$userSurName=mysql_real_escape_string($_POST['surName']);
$userUserName=mysql_real_escape_string($_POST['userName']);
$userPassWord=mysql_real_escape_string($_POST['pWord']);
$userActive=$_POST['Active'];  
$userSuperAdmin=mysql_real_escape_string($_POST['superAdmin']);
$userCorporationName=????????????;
        
$q=mysql_query("insert into UserAdmin (First Name, Last Name, Username, Password, Active, Super Admin, Corporations, Date) values('$userFirstName','$userSurName','$userUserName','userPassWord','userActive','$userCorporationName')") or die (mysql_error());
$update=1;
}
}



Ok I managed to get them in as used implode…


&lt;?php
 $q="select * from Corporations";
 $result = mysql_query($q) or die(mysql_error());
 while($row = mysql_fetch_assoc($result)) {
    echo &lt;&lt;&lt;EOL
 &lt;div class="myCheckbox"&gt;
 &lt;input type="checkbox" name="corpName[]" value="$row[ID]" /&gt; &lt;span&gt;{$row['Name']}&lt;/span&gt;&lt;br /&gt;
 &lt;/div&gt;
EOL;
} ?&gt;

$userFirstName=mysql_real_escape_string($_POST['firstName']);
$userSurName=mysql_real_escape_string($_POST['surName']);
$userUserName=mysql_real_escape_string($_POST['userName']);
$userPassWord=mysql_real_escape_string($_POST['pWord']);
$userActive=$_POST['Active'];  
$userSuperAdmin=$_POST['superAdmin'];
$userCorporationName=implode( ';' , $_POST['corpName']);
$dt2=date("Y-m-d");
        
mysql_query("INSERT INTO UserAdmin (`First Name`, `Last Name`, `Username`, `Password`, `Active`, `Super Admin`, `Corporations`, `Date`) values('$userFirstName','$userSurName','$userUserName','$userPassWord','$userActive','$userSuperAdmin','$userCorporationName','$dt2')") or die (mysql_error());

As its the same form to add users and to edit the users, can someone help me adapt the code below, so that if the checkbox value is in the array it checks it already.


&lt;?php
 $q="select * from Corporations";
 $result = mysql_query($q) or die(mysql_error());
 while($row = mysql_fetch_assoc($result)) {
    echo &lt;&lt;&lt;EOL
 &lt;div class="myCheckbox"&gt;
 &lt;input type="checkbox" name="corpName[]" value="$row[ID]" /&gt; &lt;span&gt;{$row['Name']}&lt;/span&gt;&lt;br /&gt;
 &lt;/div&gt;
EOL;
} ?&gt;

I recognise that I got to get the array values out so have done that when they click to edit the user.


if(isset($_GET['stock_Id']))
{
 $stock_Id=$_GET['stock_Id'];
 $q=mysql_query("select * from UserAdmin where ID=$stock_Id") or die (mysql_error()); 
 $rows=mysql_fetch_assoc($q);
 $ccorporationName=$rows["Corporations"];
 $flag=0;
}

And obviously somehow I need to match up the value in the checkbox to the value of the array fromt he database.


<?php
 echo $ccorporationName;
 
 $q="select * from Corporations";
 $result = mysql_query($q) or die(mysql_error());
 while($row = mysql_fetch_assoc($result)) {
    echo <<<EOL
 <div class="myCheckbox">
 <input type="checkbox" name="corpName[]" value="$row[ID]" /> <span>{$row['Name']}</span><br />
 </div>
EOL;
} ?>


I was going to put an if statement around the input checkbox code with if value matches check checkbox, and if not dont check the box, but cant seem to get the code right to do it, if that is the best way

Its wrong but somehting like this, i sort of had in my mind…


<?php
 echo $ccorporationName;
 
 $q="select * from Corporations";
 $result = mysql_query($q) or die(mysql_error());
 while($row = mysql_fetch_assoc($result)) {
    echo <<<EOL
 <div class="myCheckbox">
 if ($row[ID] like $ccorporationName) {
 <input type="checkbox" name="corpName[]" value="$row[ID]" checked /> <span>{$row['Name']}</span><br />
 } else {
 <input type="checkbox" name="corpName[]" value="$row[ID]" /> <span>{$row['Name']}</span><br /> 
 }
 </div>
EOL;
} ?>


This above actually outputs this below onto the page, so its obviously not right, but the idea i was looking to get too -

[QUOTE][
if (6 like 6;10) { Keenton International Limited
} else { Keenton International Limited
} if (7 like 6;10) { Unotron Limited (HK)
} else { Unotron Limited (HK)
}
if (8 like 6;10) { Unotron Ltd (UK)
} else { Unotron Ltd (UK)
}
if (9 like 6;10) { Unotron Inc (USA)
} else { Unotron Inc (USA)
}
if (10 like 6;10) { Padsafe Limited
} else { Padsafe Limited
}
if (11 like 6;10) { Kamwick International Limited
} else { Kamwick International Limited
}

/QUOTE]

The top ones are checked, and the bottoms ones arent, but obviously i want it so it checks the right checkboxes and leaves the ones that dont need checking.

So changed it to this, and better as no outputting everything, but the matching of the values is done the wrong way


<?php
 //echo $ccorporationName;
 
 $q="select * from Corporations";
 $result = mysql_query($q) or die(mysql_error());
 while($row = mysql_fetch_assoc($result)) { ?>
 <div class="myCheckbox">
 <?php if (($row['ID']) == $ccorporationName) { ?>
 <input type="checkbox" name="corpName[]" value="<?php echo $row['ID']?>" checked /> <span><?php echo $row['Name']?></span><br />
 <?php } else { ?>
 <input type="checkbox" name="corpName[]" value="<?php echo $row['ID']?>" /> <span><?php echo $row['Name'] ?></span><br /> 
 <?php } ?>
 </div>
<?php } ?>

Ive sort of dealt with the like issue by using strpos, but my problem is how do I break the array up inside $ccorporationName, to allow the compare to occur and so then to dictate which checkbox is checked or not.


 <?php if (strpos($row['ID'], $ccorporationName) !== FALSE) { ?>
 <input type="checkbox" name="corpName[]" value="<?php echo $row['ID']?>" checked /> <span><?php echo $row['Name']?></span><br />
 <?php } else { ?>
 <input type="checkbox" name="corpName[]" value="<?php echo $row['ID']?>" /> <span><?php echo $row['Name'] ?></span><br /> 
 <?php } ?>

I’m really struggling with this now, as it seems I can get the array values and at first create the array, but tying them both together is hard work.

This is where I am at the moment


$q="select * from Corporations";
 $result = mysql_query($q) or die(mysql_error());
 while($row = mysql_fetch_assoc($result))
 $query  = "select Corporations from UserAdmin where ID = $stock_Id";
 $result2 = mysql_query($query);
 $rows = array();  
 while ($row = mysql_fetch_array($result2, MYSQL_ASSOC)) 
 {
    $rows[] = $row;
 }
 print "<pre>".print_r($rows,true)."</pre>"; 
 { ?>
 <div class="myCheckbox">    
 <?php if (strstr($row['ID'], $words) !== FALSE) { ?>
 <input type="checkbox" name="corpName[]" value="<?php echo $row['ID']?>" checked /> <span><?php echo $row['Name']?></span><br />
 <?php } else { ?>
 <input type="checkbox" name="corpName[]" value="<?php echo $row['ID']?>" /> <span><?php echo $row['Name'] ?></span><br /> 
 <?php } ?>   
 </div>
<?php } ?>


I can print the array out ok, but its exploding those values and then printing all the checkboxes and marking the ones checked if they need to be.

This is what I can see at the moment, which is the array but no checkboxes.


Array
(
    [0] => Array
        (
            [Corporations] => 7;9;11
        )

)

[B]Notice[/B]: Undefined variable: words in [B]/home/linweb30/u/utgroup.co.uk/user/htdocs/admin/users.php[/B] on line [B]288[/B]
 



Kind of a mess you’ve got going here. Not sure how deep the $rows array will be… one record or more. Without rethinking this whole process you might try this.

 { ?>
 <div class="myCheckbox">
 <?php
 $corpIDs = explode(";",$rows[0]['Corporations']);
 $checked = (in_array($row['ID'],$corpIDs) ? " checked=\\"checked\\"" : '');
 ?>
 <input type="checkbox" name="corpName[]" value="<?php echo $row['ID']?>" <?php echo $checked; ?> /> <span><?php echo $row['Name']?></span><br />
 </div>
<?php } ?>

Ye sorry, it became a right mess in the end, it had me going crazy then I think I got all screwed up with it.

Thanks for the reply though/

I have taken your code and I think added it to mine, and wondered if you could take a look, as I probably gone wrong somewhere



$q="select * from Corporations";
 $result = mysql_query($q) or die(mysql_error());
 while($row = mysql_fetch_assoc($result))
 $query  = "select Corporations from UserAdmin where ID = $stock_Id";
 $result2 = mysql_query($query);
 $rows = array();  
 while ($row = mysql_fetch_array($result2, MYSQL_ASSOC)) 
 {
    $rows[] = $row;
 }
 print "<pre>".print_r($rows,true)."</pre>"; 
 { ?> 
 <div class="myCheckbox"> 
 <?php  
 $corpIDs = explode(";",$rows[0]['Corporations']); 
 $checked = (in_array($row['ID'],$corpIDs) ? " checked=\\"checked\\"" : ''); 
 ?> 
 <input type="checkbox" name="corpName[]" value="<?php echo $row['ID']?>" <?php echo $checked; ?> /> <span><?php echo $row['Name']?></span><br />     
 </div> 
<?php } ?>


Sorry if I have missed the point somwhere, as Im not getting the checkboxes showing.
I will have a look myself too,

You also have a conflict with $row going on here. You seem to be grabbing a single record from UserAdmin based on the variable $stock_Id, so I would grab just the Corporations field and explode it before going into your Corporations loop.

$query  = "select Corporations from UserAdmin where ID = $stock_Id";
$result2 = mysql_query($query);
$rowu = mysql_fetch_array($result2, MYSQL_ASSOC); 
$corpIDs = explode(";",$rowu['Corporations']);

Then as before with the loop.

$q="select * from Corporations";
$result = mysql_query($q) or die(mysql_error());
while($row = mysql_fetch_assoc($result))	
{ ?>
	<div class="myCheckbox">
		<?php
		$checked = (in_array($row['ID'],$corpIDs) ? " checked=\\"checked\\"" : '');
		?>
		<input type="checkbox" name="corpName[]" value="<?php echo $row['ID']?>" <?php echo $checked; ?> /> <span><?php echo $row['Name']?></span><br />    
	</div>
<?php } ?>

Thank you Drummin,
thats seems to have done the trick, so thank you very much.

It’s midnight here and been at this since 8am this morning, so can get some sleep without it playing on my mind.

Thanks again