0

It always drops the first checkbox, but not the actual part of the record the checkbox represents. If I keep clicking 'save' it drops off the next checkbox, and the first record. For instance, if the record is 'DMG, SCR, KLS, AST' when I click 'save' and the page refreshed, the DMG checkbox is not checked but the record still says 'DMG, SCR, KLS, AST' If I click it again, the record becomes 'SCR, KLS, AST' and the SCR checkbox is unchecked. The checkbox part of the form is near the bottom.

Thanks!

// delete
if(isset($_GET['id']) && $_GET['x'] == "d") {
    $eventType = EventType::find_by_id($_GET['id']);
    if($eventType && $eventType->delete()) {
        $message = "The Event Type '{$eventType->name}' was deleted.";
    } else {
        $message = "The Event Type could not be deleted.";
    }
}

// save
if(isset($_POST['submit'])) {

    $eventType = new EventType();
    if(isset($_POST['id'])) {
        $eventType->id = mysql_prep($_POST['id']);
    }
    $eventType->name = mysql_prep($_POST['name']);

    $scoreTypes = ScoreType::find_all();
    $score_set = array();
    foreach($scoreTypes as $scoreType) {
        if(isset($_POST[$scoreType->name]) && $_POST[$scoreType->name] == $scoreType->name) {
            array_push($score_set, mysql_prep($_POST[$scoreType->name]));
        }
    }

    if(!empty($score_set)) {
        $scores = implode(", ", $score_set);
    } else {
        $scores = "empty";
    }
    if(isset($scores)) { $eventType->score_types = $scores; } else { $eventType->score_types = ""; }
    if(isset($_POST['is_disp'])) { $eventType->is_disp = mysql_prep($_POST['is_disp']); }
    $eventType->save();
}

// edit
if(isset($_GET['id']) && ($_GET['x'] == "e")) {
    $eventType = EventType::find_by_id($_GET['id']);
}

//view
$eventTypes = EventType::find_all($ord="name");
$scoreTypes = ScoreType::find_all();
?>
<div id="admin_table">
<div id="admin_thead">
    <table>
        <tr>
            <td width="35%">Name</td>
            <td width="40%">Score Types</td>
            <td width="5%">Display</td>
            <td width="7%">Actions</td>
            <td width="12"></td>
        </tr>
    </table>
</div>
<div id="admin_tdata">
    <table>
        <colgroup></colgroup>
        <colgroup class="odd"></colgroup>
        <colgroup></colgroup>
        <colgroup class="actions"></colgroup>
    <?php foreach($eventTypes as $eventType_v): ?>
        <tr>
            <td width="35%"><?php echo $eventType_v->name; ?></td>
            <td width="40%" title="<?php echo $eventType_v->score_types; ?>"><?php echo $eventType_v->score_types; ?></td>
            <td width="5%" class="bool"><img src="../images/<?php echo ($eventType_v->is_disp == 1) ? "x_yes.png" : "x_no.png"; ?>"></td>
            <td width="7%" class="but_admin"><a class="but_admin_edit" title="edit" href="index.php?con=event_types&id=<?php echo $eventType_v->id; ?>&x=e">edit</a><a class="but_admin_delete" title="delete" href="index.php?con=event_types&id=<?php echo $eventType_v->id; ?>&x=d" onclick="return confirm('Are you sure you want to delete the Event Type <?php echo "\'{$eventType_v->name}\'"; ?>?')">delete</a></td>
        </tr>
    <?php endforeach; ?>
    </table>
</div>
</div>
<div id="admin_ops_cont">
<div id="admin_lt_panel">
    <div id="admin_msg">
        <h2>Messages</h2>
        <p><?php if(!$message) { echo "no message"; } else { echo $message; } ?></p>
    </div>
</div>
<div id="admin_form">
<?php if(isset($_GET['id'])) { if(isset($_GET['x'])) { if($_GET['x'] == "e") { $edit = 1; } else { $edit = 0; }}} else { $edit = 0; } ?>
    <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>?con=event_types<?php if(isset($_GET['id'])) { echo "&x=e&id={$eventType->id}"; } ?>" method="post">
        <fieldset>
            <legend><?php if($edit == 1) { echo "Edit: <span>{$eventType->name}</span>"; } else { echo "New EventType"; } ?></legend>
            <?php if($edit == 1): ?>
                <input type="hidden" name="id" id="id" value="<?php echo $eventType->id; ?>">
            <?php endif; ?>
            <p>
                <label for="name">Name</label>
                <input name="name" id="name" type="text"<?php if($edit == 1): ?> value="<?php echo $eventType->name; ?>"<?php endif; ?>>
            </p>
            <p>
            <?php foreach($scoreTypes as $scoreType): ?>
                <label for="<?php echo $scoreType->name; ?>"><?php echo $scoreType->description; ?></label>
                <input type="checkbox" id="<?php echo $scoreType->name; ?>" name="<?php echo $scoreType->name; ?>" value="<?php echo $scoreType->name; ?>"
                    <?php if($edit == 1): if(strpos("{$eventType->score_types}", "{$scoreType->name}")): ?> checked<?php endif; endif; ?>>
            <?php endforeach; ?>
            </p>
            <p>
                <label for="is_disp">Display?</label>
                <input type="checkbox" id="is_disp" name="is_disp" value="1"<?php if($edit == 1) { if($eventType->is_disp == 1) { echo " checked"; }} ?>>
            </p>
            <p>
                <button type="submit" name="submit">Save</button><?php if($edit == 1): ?><a class="btn_cancel" href="index.php?con=event_types">done</a><?php endif; ?>
            </p>
        </fieldset>
    </form>
</div><!-- end .admin_form -->
</div><!-- end #admin_ops_con -->
4

1 回答 1

0

I think the problem is in the line

 <?php if($edit == 1): if(strpos("{$eventType->score_types}", "{$scoreType->name}")): ?> checked<?php endif; endif; ?>>

if the entry is in the first position(index in the String is 0) strpos returns 0. PHP assumes that 0 equals false, so the first entry with the match with the index 0 will not be "checked".

this code should solve the problem (checking for false !==)

    <?php if($edit == 1): if(strpos("{$eventType->score_types}", "{$scoreType->name}")!==false): ?> checked<?php endif; endif; ?>>

// Not tested since i dont have PHP installed on this machine. but will try to confirm this asap

Update:

Info strpos(...) return the index in the string. Counting starts by 0 so you would have to check explicitly for false (===false) oder (!==false) to be sure, that PHP doesnt treat the index 0 as false. here is a link to the manual (Link)

于 2013-06-25T21:12:35.600 回答