PHP链接同一页面的链接并通过$ _POST

时间:2015-08-22 14:48:46

标签: php html

我有一个数据库表(NumSection(id)和NomSection)

在我的页面中,我希望显示来自' NomSection'的所有数据。像一个链接。当我点击链接时,我想用$_POST['nomSection']打开我的实际页面并显示此部分的数据。

从我的页面index.php:

<div>   
    <?php 
        $array = returnAllSection(); 

        foreach ($array as $section) {
            // link to same page but with a $_POST['NomSection'], For the                      //moment I just display it.. I don't know how do with php
            echo $section['NomSection'].'<br/>';

        }   
    ?>
</div>

<div>
    <?php
        // here I want have $array = returnAll('NomSection) or returnAll() //if empty (this function return ALL if empty or All of a section, can I just //put returnAll($_POST[nomSection])  ? 
        $array = returnAll();

        foreach ($array as $section) {

            echo 'Titre: ' .$section['TitreArticle'].'<br/>';
            echo 'Date: ' .$section['DateArticle'].'<br/>';
            echo 'Texte: ' .$section['TexteArticle'].'<br/>';
            echo '<br/>';
        }
    ?> 
</div>

我的职能:(效果很好)

function returnAll($arg = 'all') {

    global $connexion;

    if($arg == 'all'){

        $query = "select 
        NumArticle, 
        TitreArticle, 
        TexteArticle, 
        DateArticle, 
        RefSection, 
        NomSection 
        from Articles, Sections where 
        RefSection = NumSection or RefSection = null;";
        $prep = $connexion->prepare($query);
        $prep->execute();
        return $prep->fetchAll();
    }
    else {

        $query = "select NumArticle, 
        TitreArticle, 
        TexteArticle, 
        DateArticle, 
        RefSection, 
        NomSection 
        from Articles, Sections where 
        RefSection = NumSection and NomSection = :arg;";
        $prep = $connexion->prepare($query);
        $prep->bindValue(':arg', $arg, PDO::PARAM_STR);
        $prep->execute();
        return $prep->fetchAll();
    }
}



function returnAllSection() {

    global $connexion;
    $query = "select * from Sections;";
    $prep = $connexion->prepare($query);
    $prep->execute();
    return $prep->fetchAll();
}

1 个答案:

答案 0 :(得分:2)

据我所知,为了发布你需要使用表格或javascript ajax帖子。在这里,我展示了一种笨重的表单后期方法,可能适用于您要完成的任务。

<?php
function returnAllSection() {
    return array(
        array('NomSection' => 'foo'),
        array('NomSection' => 'bar'),
        array('NomSection' => 'baz'),
    );
}
?>

<?php
    $array = returnAllSection(); 

    foreach ($array as $section) { ?>
        <form action="" method="POST">
            <button type="submit">NomSection</button>
            <input type="hidden" name="NomSection" value="<?php echo htmlspecialchars($section['NomSection']); ?>">
        </form>
<?php } ?>

<?php
    if (isset($_POST['NomSection'])) {
        error_log(print_r($_POST,1).' '.__FILE__.' '.__LINE__,0);
        // do something with NomSection...
    }
?>