简体   繁体   中英

PHP Repeating Region and Checkboxes

--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']);
$currentRow = 0;
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 } ; ?>

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM