No idea how else I could have worded that. Basically I am attempting to make this so that when I add another $directory all I will have to do is update it, $results and add another MySQL column with the same name as $directory['databaseName']. My difficulty with this is with my administration panel; where in order to check all of the boxes that describe the entry's (via the MySQL database) job types, I will have to use a complicated for loop with an if statement within it. Perhaps you can figure out what I am attempting to do and help me. This all would work if I were able to get the value of $directory[$i]['databaseName'] and insert it into the if statement to see if $results with that value as it's key equals 1.

$directory=
        array(
            array(
                'linkName' => 'Screen Room',
                'path' => 'sr',
                'databaseName' => 'ScreenRoom',
            ),
            array(
                'linkName' => 'Glass Window',
                'path' => 'gw',
                'databaseName' => 'GlassWindow',
            ),
            array(
                'linkName' => 'Roof-Under-Deck',
                'path' => 'rd',
                'databaseName' => 'RoofUnderDeck',
            ),
        );
$results = array(
                'ID' => '',
                'Invoice' => '',
                'FirstName' => '',
                'LastName' => '',
                'ImagesBefore' => '',
                'ImagesAfter' => '',
                'Description' => '',
                'Testimonial' => '',
                'Date' => '',
                'GlassWindow' => '0',
                'ScreenRoom' => '0');
for ($i=0; $i < count($directory); $i++) {
    if ($results["$directory[$i]['databaseName']"] === 1) {
        echo '<input type="checkbox" checked> ' . $directory[$i]['linkName'];
    }
    else
        echo '<input type="checkbox"> ' . $directory[$i]['linkName'];
    }
}
有帮助吗?

解决方案

Why not use it by assigning it to a variable & keep it simple. Also use isset along with to make sure it really exist,

$key = $directory[$i]['databaseName'];
if (isset($results[$key]) && $results[$key] == 1) {
    echo '<input type="checkbox" checked> ' . $directory[$i]['linkName'];
}
else{
    echo '<input type="checkbox"> ' . $directory[$i]['linkName'];
}

DEMO.

其他提示

It was actually quite a simple fix that I initally thought would not work. I guess this is why you NEVER second guess yourself without trying your first thought first. For those in the future who may run into this same issue, here is the solution I used. I simply removed the " surround $directory[$i]['databaseName'] and changed === to only ==.

for ($i=0; $i < count($directory); $i++) {
    if ($results[$directory[$i]['databaseName']] == 1) {
        echo '<input type="checkbox" checked>' . $directory[$i]['linkName'];
    }
    else {
        echo '<input type="checkbox">' . $directory[$i]['linkName'];
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top