-2

我在使用以下代码时遇到问题。它只是阻止站点的其余部分加载,并且没有执行它的预期功能。

主PHP前面有一个JS,如下所示:

<script type="text/javascript">
    //http://jsbin.com/ujuse/1/edit
$(function() {
    $("input[type='checkbox']").on('change', function() {
        var boxes = [];
        // You could save a little time and space by doing this:
        var name = this.name;
        // critical change on next line
        $("input[type='checkbox'][name='"+this.name+"']:checked").each(function() {
            boxes.push(this.value);
        });
        if (boxes.length) {
            $(".loadingItems").fadeIn(300);
            // Change the name here as well
            $(".indexMain").load('store/indexMain.php?'+this.name+'=' + boxes.join("+"),
            function() {
                $(".indexMain").fadeIn('slow');
                $(".loadingItems").fadeOut(300);
            });

        } else {
            $(".loadingItems").fadeIn(300);
            $(".indexMain").load('store/indexMain.php', function() {
                $(".indexMain").fadeIn('slow');
                $(".loadingItems").fadeOut(300);
            });
        }
    });
});
</script>

<?php
function echoCheckboxSet($con, $header, $divClass, $columnName, $setName) {
$checkboxes = $con -> prepare("SELECT DISTINCT $columnName FROM item_descr ORDER BY $columnName ASC");
$checkboxes->execute();
?>
<div class="bgFilterTitles">
    <h1 class="filterTitles"><?php echo $header;?></h1>
</div>
<div class="<?php echo $divClass; ?>">
<?php
    while ($box = $checkboxes->fetch(PDO::FETCH_ASSOC)):
    $boxColumnName = str_replace('_',' ',$box[$columnName]);
?>
        <input type='checkbox' class='regularCheckbox' name='<?php echo $setName; ?>' value='<?php echo $box[$columnName]; ?>' />
        <font class='similarItemsText'><?php echo $boxColumnName; ?></font>
        <br />
<?php
endwhile;
?>
</div>
<?php
} // end of echoCheckboxSet

// Call our method twice, once for colors and once for prices
echoCheckBoxSet("COLOR", "colors", "color_base1", "color");
echoCheckBoxSet("PRICE", "prices", "price", "price");
?>

    Warning: Missing argument 5 for echoCheckboxSet(), called in 
\store\indexLeftBar.php on line 58 and defined in \store\indexLeftBar.php on line 35

    Fatal error: Call to a member function prepare() on a non-object in \store\indexLeftBar.php on line 36

你看有什么问题吗?

谢谢!

4

1 回答 1

3

$con被使用,但不在范围内:

function echoCheckboxSet($header, $divClass, $columnName, $setName) {
    $checkboxes = $con->....
}

要么将其作为参数(如下所示)传递给函数,要么将其声明为global(不推荐后者)。

function echoCheckboxSet($con, $header, $divClass, $columnName, $setName) {
    $checkboxes = $con->....
}

$con = some_method_that_connects_to_your_db(); // <!-- Still need this
echoCheckBoxSet( $con, "COLOR", "colors", "color_base1", "color");
于 2013-01-09T01:05:29.960 回答