views:

390

answers:

1

--Edited for clarity.

Database:

tblModule, contains a list of modules that can be enabled or disabled. tblData, contains a list of trusts and the modules they have enabled. This links to tblModule on tblData.M01 = tblModule.mod_key

The PHP page is accessed from an index page and passes a variable lstModTrust to this page, to limit the returned records from tblData for a single trust. tblData.trust_key

A query runs, qryModuleList which returns a list of all modules. This is used to generate a table of all available modules. Each row shows module name tblModules.mod_name, module code tblModules.mod_code and a checkbox.

qryModData will return a list of the modules that are enabled for a single trust, and the corresponding checkboxes need to be ticked in the table.

This page will then be used to enable and disable modules for a trust. If a module is unticked the entry will be deleted from tblData, if it is ticked an entry will be inserted, and if no change then no change in the DB.

At the moment I'm having trouble getting the checkboxes ticked correctly based on qryModData

Any thoughts anyone?

--Edited to include code--

<table width="50%" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td>Module</td>
      <td>Module Code</td>
      <td>&nbsp;</td>
    </tr>
    <?php do { ?>

    <tr>
        <td><?php echo $row_qryModuleList['mod_name']; ?></td>
        <td><?php echo $row_qryModuleList['mod_code']; ?></td>
        <td><input <?php if (!(strcmp($row_qryModData['M01'],$row_qryModuleList['mod_code']))) {echo "checked=\"checked\"";} ?>name="chkMod" type="checkbox" id="chkMod" value="<?php echo $row_qryModData['M01']; ?>" /></td>

</tr><?php } while ($row_qryModuleList = mysql_fetch_assoc($qryModuleList)); ?>
  </table>

Then there's two SQL queries, one that generates the list to build the table, and the second which I'm trying to use to set the boxes to ticked.

qryModuleList
SELECT *
FROM tblmodules
ORDER BY mod_name ASC

qryModData
SELECT *
FROM tbldata
WHERE trust_key = varTrust

varTrust is pulled from a URL variable.

Apologies for not including the code in the first place.

--Edit for new code.

<?php while ($row_qryModuleList = mysql_fetch_assoc($qryModuleList)) { ?>

    <tr>
        <td><?php echo $row_qryModuleList['mod_name']; ?></td>
        <td><?php echo $row_qryModuleList['mod_code']; ?></td>
        <td><input <?php if (strcmp($row_qryModData['M01'],$row_qryModuleList['mod_key']) != 0) {echo "checked=\"checked\"";} ?>name="chkMod" type="checkbox" id="chkMod" value="<?php echo $row_qryModData['M01']; ?>" /></td>

</tr><?php } ; ?>

--Edited for new Code.

    <tr class="tblHead">
      <td>Module</td>
      <td>Module Code</td>
      <td>Enabled\Disabled</td>
    </tr>

<?php $mod_data = array();

while ($row_qryModData = mysql_fetch_assoc($qryModData)) array_push($mod_data, $row_qryModData['M01']);
?>

    <?php $currentRow = 0;?>
    <?php while ($row_qryModuleList = mysql_fetch_assoc($qryModuleList)) { ?>

    <tr bgcolor="<?php echo($currentRow++ % 2)?"#CCFFFF":"#FFCCFF";?>">
        <td><?php echo $row_qryModuleList['mod_name']; ?></td>
        <td><?php echo $row_qryModuleList['mod_code']; ?></td>
        <td><input <?php if (false !== (array_search($mod_data, $row_qryModuleList['mod_key']))) echo "checked=\"checked\""; ?> name="chkMod" type="checkbox" id="chkMod" value="<?php echo $row_qryModData['M01']; ?>" /></td>
    </tr><?php } ; ?>
A: 

I think I'm understanding this now. What you need to do is place all of the allowed modules into an array and use the array_search() function to find it.

Example:

$mod_data = array();

while ($row_qryModData = mysql_fetch_assoc($qryModData)) array_push($mod_data, $row_qryModData['M01']);

That will get all the available modules into one array.

Next, while you cycle through the 'ModuleList' query, use the array_search() method to try and find the 'ModKey' variable in it. If you do, check the box. If not, do nothing.

Example:

<td><input <? if (false !== (array_search($mod_data, $row_qryModuleList['mod_code'])) echo "checked=\"checked\" "; ?>name="chkMod" type="checkbox" id="chkMod" value="<?php echo $row_qryModData['M01']; ?>" /></td>

(The reason I use a "!==" is because the function can return false or something that might equal false and might not. Read the page I've linked above for more)

Stephen
I think it's usually better to add that attribute as such... checked="checked"
KyleFarris
I was thinking the same thing (being compliant and all) but it was just quicker to type. Thanks, though.
Stephen
Edited the code.
Stephen
Have added code, sorry for not placing earlier.
4D
Hi Stephen. Have made changes you have suggested. What happens now is all but one of the checkboxes come out checked. Have I made some major error somewhere?Am I actually going about this the correct way or is there a better solution for what I am trying to do?Have also made a slight change as I noticed I had it comparing the wrong fields. 'mod_key' not 'mod_code', my mistake.
4D
@4D: I edited my code because I think I may have misunderstood you before. Instead of a not-equal ("!="), try using an equals-exactly ("==") for your comparison.Also, just wondering, do you call the second query (the one that you base your checkboxes being checked off) once at the beginning of the script or should it be called on each iteration of the while loop?
Stephen
Stephen. Sometimes I'm not good at explaining things. As each checkbox is created, I'd like it to look in the results of the second query 'qryModData' to see if that module exists in column M01. If it does then the checkbox should be checked.Having '==' now means nothing is checked. I shall re-edit question to try and explain better.Thanks for your help with this too Stephen. I do appreciate it.
4D
@4D: Try that and let me know how it works out.
Stephen
@Stephen. Been trying what you have suggested this weekend, but haven't got very far. Not sure if I've implemented it correctly. Updated code above to show what I currently have. Non of the check boxes are being ticked as they should.
4D