PHP - 使用$ _GET过滤?

时间:2018-02-19 10:12:49

标签: php html css arrays get

enter image description here

我希望在顶部给定的过滤器上过滤这些小div(AB Main Hall,AB Club,Huis 23)。

我猜这必须用$ _GET完成?这是我现在拥有的代码。

<?php

$concert = [
    [
        "datum" => "20 FEB",
        "naam" => "Kula Shakar",
        "zaal" => "AB Club",
        "image" => "https://img.discogs.com/Ub_IwvshvOIC1n-JL2x5mHHFfRg=/fit-in/300x300/filters:strip_icc():format(jpeg):mode_rgb():quality(40)/discogs-images/A-256590-1106988648.jpg.jpg"
    ],
    [
        "datum" => "25 FEB",
        "naam" => "Black Box",
        "zaal" => "AB Main Hall",
        "image" => "http://highly-suspect.concertticketsq.com/images/performers/event/13-concert-tickets.jpg"
    ],
    [
        "datum" => "28 FEB",
        "naam" => "Screening Pulp",
        "zaal" => "Huis 23",
        "image" => "https://dhzjvxyl79yzn.cloudfront.net/6/83626_0_layton-concert-hall.jpg"
    ],
    [
        "datum" => "4 JUN",
        "naam" => "White Box",
        "zaal" => "AB Club",
        "image" => "data:image/jpeg;base64,/9IiIAIiIAIiIAIiIAIiIAAIiIAIiIAIiIAIiIAIiIAIiIAIiIAIiIA/9k="
    ]

];

?>

<!doctype html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>Concerten</title>
</head>

<body>

<a href="#">AB Main Hall</a>
<a href="#">AB Club</a>
<a href="#">Huis 23</a>
    <?php foreach($concert as $concerten): ?>


    <section class="container">

        <p><?php echo $concerten['datum']; ?></p>
        <p><?php echo $concerten['naam']; ?></p>
        <h2><?php echo $concerten['zaal']; ?></h2>
        <img src="<?php echo $concerten['image']; ?>"/>

    </section>

<?php endforeach; ?>  

<style>

    .container {
        border: 1px solid black;
        margin: 50px 0px;
        width: 300px;
    }
    p {
        text-align: center;
    }



</style>
</body>

</html>

这些项目是从数组中提取出来的,例如这里是第一个。

<?php
$concert = [
    [
        "datum" => "20 FEB",
        "naam" => "Kula Shakar",
        "zaal" => "AB Club",
        "image" => "https://img.discogs.com/Ub_IwvshvOIC1n.jpg"
    ],
?>

2 个答案:

答案 0 :(得分:1)

如果您使用GET变量过滤显示,则可以实现此目标。

<?php
    $concert = array(
        array(
            "datum" => "20 FEB",
            "naam" => "Kula Shakar",
            "zaal" => "AB Club",
            "image" => "https://img.discogs.com/Ub_IwvshvOIC1n-JL2x5mHHFfRg=/fit-in/300x300/filters:strip_icc():format(jpeg):mode_rgb():quality(40)/discogs-images/A-256590-1106988648.jpg.jpg"
        ),
        array(
            "datum" => "25 FEB",
            "naam" => "Black Box",
            "zaal" => "AB Main Hall",
            "image" => "http://highly-suspect.concertticketsq.com/images/performers/event/13-concert-tickets.jpg"
        ),
        array(
            "datum" => "28 FEB",
            "naam" => "Screening Pulp",
            "zaal" => "Huis 23",
            "image" => "https://dhzjvxyl79yzn.cloudfront.net/6/83626_0_layton-concert-hall.jpg"
        ),
        array(
            "datum" => "4 JUN",
            "naam" => "White Box",
            "zaal" => "AB Club",
            "image" => "data:image/jpeg;base64,/9IiIAIiIAIiIAIiIAIiIAAIiIAIiIAIiIAIiIAIiIAIiIAIiIAIiIA/9k="
        )
    );
    /* create array of venues from above to use in generating menu */
    $venues=array();
    foreach( $concert as $arr )$venues[]=$arr['zaal'];

    /* remove duplicates from array */
    $venues=array_unique( $venues );
    /* Add a new item to display all records */
    $venues[]='All';

    $club = isset( $_GET['zaal'] ) && in_array( $_GET['zaal'], $venues ) ?  $_GET['zaal'] : false;

?>
<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Concerten</title>
        <style>
            .container {
                border: 1px solid black;
                margin: 50px 0px;
                width: 300px;
            }
            p {
                text-align: center;
            }
        </style>
    </head>
    <body>
    <?php
        /* display chosen club as title */
        if( $club ) printf('<h1>%s</h1>',$club);

        /* create the list of hyperlinks */
        echo '<ul>';
        array_walk( $venues, function( $club ){
            printf( '<li><a href="?zaal=%s">%s</a></li>', $club, $club );
        });
        echo '</ul>';


        foreach( $concert as $arr ){
            /* display either selected club or all clubs */
            if( $club && $club == $arr['zaal'] or $club=='All' ){
                echo "
                <section class='container'>
                    <p>{$arr['datum']}</p>
                    <p>{$arr['naam']}</p>
                    <h2>{$arr['zaal']}</h2>
                    <img src='{$arr['image']}' />
                </section>";
            }
        }
    ?>
    </body>
</html>

答案 1 :(得分:0)

首先,您在网址中添加一些获取数据:

// Care, it may don't work I add it like this so you understand the idea
<a href="?zaal=AB_Main_Hall">AB Main Hall</a>
<a href="?zaal=AB_Club">AB Club</a>
<a href="?zaal=Huis_23">Huis 23</a>

<?php 
foreach($concert as $concerten): 
    // Check if you have some $_GET data in your url
    if (!empty($_GET['zaal']) {
        // I replace the "_" I get from the URL by " " so it will look the same as your array data
        $zaal = str_replace($_GET['zaal'], "_", " ");

        // METHOD 1 : If the row contain the good data, you display it, else do nothing
       if ($concerten['zaal'] == $zaal) {
          <section class="container">
             <p><?php echo $concerten['datum']; ?></p>
             <p><?php echo $concerten['naam']; ?></p>
             <h2><?php echo $concerten['zaal']; ?></h2>
             <img src="<?php echo $concerten['image']; ?>"/>
          </section>
       }

    // If you have no $_GET data, just display all your row
    } else {
?>

<section class="container">

    <p><?php echo $concerten['datum']; ?></p>
    <p><?php echo $concerten['naam']; ?></p>
    <h2><?php echo $concerten['zaal']; ?></h2>
    <img src="<?php echo $concerten['image']; ?>"/>

</section>

<?php 

} // end of else 

endforeach; 

?>  

其他方面: 正如评论中所述: 而不是测试数组的每一行然后显示它(参见方法1)您可以使用您想要的过滤器在阵列上使用array_filter()(doc here),然后显示过滤后的数组。

此方法的问题在于,每次要过滤时都必须重新加载页面。关心我如何在URL中添加$ _GET数据,这可能是错误的,但我希望你明白这一点。

这是你在找什么?