如何从php中的url段获取json数据

问题描述 投票:0回答:1

我想询问有关获取段的网址。我有一个讲师页面和一个详细讲师。我这样制作的网址

http://cru68.test/instructor-detail.php?id=1

并且我想将其更改为此。

http://cru68.test/instructor-detail.php/schedule#/instructor/site/1/id/1

我从json文件中获取了数据。我如何根据需要获取带有URL的ID的数据?

instructors.php代码

<section id="instructors">
            <div class="container">
                <div class="row">
                    <div class="col col-3">
                        <a href="instructor-detail.php?id=1" class="card-leader">
                            <div class="image">
                                <img src="<?= $base_url ?>assets/img/packleaders/shiqeen.jpg" class="img-fluid">
                            </div>
                            <div class="content">
                                <div class="name">Shiqeen</div>
                                <div class="initial">S</div>
                            </div>
                        </a>
                    </div>
                    <div class="col col-3">
                        <a href="instructor-detail.php?id=2" class="card-leader">
                            <div class="image">
                                <img src="<?= $base_url ?>assets/img/packleaders/mandy.jpg" class="img-fluid">
                            </div>
                            <div class="content">
                                <div class="name">Mandy</div>
                                <div class="initial">M</div>
                            </div>
                        </a>
                    </div>
                    <div class="col col-3">
                        <a href="instructor-detail.php?id=3" class="card-leader">
                            <div class="image">
                                <img src="<?= $base_url ?>assets/img/packleaders/hannah.jpg" class="img-fluid">
                            </div>
                            <div class="content">
                                <div class="name">Hannah</div>
                                <div class="initial">H</div>
                            </div>
                        </a>
                    </div>
                    <div class="col col-3">
                        <a href="packleader.php" class="card-leader">
                            <div class="image">
                                <img src="<?= $base_url ?>assets/img/packleaders/bebe.jpg" class="img-fluid">
                            </div>
                            <div class="content">
                                <div class="name">Bebe</div>
                                <div class="initial">B</div>
                            </div>
                        </a>
                    </div>
                    <div class="col col-3">
                        <a href="instructor-detail.php?id=4" class="card-leader">
                            <div class="image">
                                <img src="<?= $base_url ?>assets/img/packleaders/charisse.jpg" class="img-fluid">
                            </div>
                            <div class="content">
                                <div class="name">Charisse</div>
                                <div class="initial">C</div>
                            </div>
                        </a>
                    </div>
                </div>
            </div>
        </section>

instructor-detail.php代码

<?php
        $id = $_GET['id'];

        $data = file_get_contents("http://cru68.test/assets/js/data-instructor.json");
        $data = json_decode($data, TRUE);
        $data = array_filter($data, function ($var) use ($id) {
            return ($var['id'] == $id);
        });

        // var_dump($data);
        // die;
        ?>

        <section id="packleader">
            <div class="container-fluid custom-container">
                <div class="row">
                    <?php foreach ($data as $d) { ?>
                        <div class="col-12 col-sm-12 col-md-12 col-lg-6  video">
                            <div class="main-video d-flex-justify-content-center">
                                <div class="content-video">
                                    <img src="<?= $d['image']; ?>" alt="" class="img-fluid">
                                </div>
                            </div>
                        </div>
                        <div class="col-12 col-sm-12 col-md-12 col-lg-6 ">
                            <div class="biography">
                                <legend class="title"><?= $d['name']; ?></legend>
                                <div class="text">
                                    <?= $d['description']; ?>
                                </div>
                            </div>
                        </div>
                    <?php } ?>
                </div>
            </div>
        </section>

data-instructor.json代码示例:

 [{
         "id": "1",
         "name": "Shiqeen",
         "description": "<p>Shiqeen wasn’t always a sporty spice. She picked up sports at the age of 17, playing touch rugby for 6 years. After graduating with a diploma in Sports and Exercise Science, she went on to to become a flight attendant. While flying, Shiqeen would visit gyms and studios, trying out fitness programs all around the world which inspired her to take a personal training course.</p> <p>Shiqeen loves high intensity workouts as they challenge the mind and body. Naturally, she got hooked on spinning and boxing, leading her to become a pack leader at CRU. When in class, expect her to be your personal hypegirl, cheering and pushing you past your limits. Dance (twerk) it out on the bag with ‘Shakween’ to hip hop and R&B tunes. We promise you a great sweaty time! </p>",
         "image": "assets/img/packleaders/shiqeen.jpg"
     },
     {
         "id": "2",
         "name": "Mandy",
         "description": "<p>Music and Endorphins — that’s what got Mandy hooked on CRU (cycle and box) and motivated her to become a pack leader at both studios - expect block rockin’ beats in her classes that will push you further than you thought you could go.</p> <p>Outside the studio - Mandy is an avid hiker and has explored peaks in Europe, Asia, and North America. The air is better up there and there is a sense of peace you get from being able to disconnect from everything.</p> <p>And if that doesn't do it for you, go for her class so you can hang out with resident CruCycle pupper, Udon.</p>",
         "image": "assets/img/packleaders/mandy.jpg"
     },
     {
         "id": "3",
         "name": "Hannah",
         "description": "<p>Hannah's Indoor Cycling journey – from struggling to catch the beat on bike 35, to rallying her riders from the instructor bike – happened right here at CruCycle. Known for her love of old to new school hip hop, Hannah's class is full of speed and swag. Get ready to add that bounce, and throw some groove into your ride.</p> <p> When she isn't teaching, hannah spends her downtime moseying around museums, eating Acai at Juice Junkie, or chillin' at home with her cats.</p> ",
         "image": "assets/img/packleaders/hannah.jpg"
     },
     {
         "id": "4",
         "name": "Charisse",
         "description": "<p>Charisse found her way to CruCycle when she was trying to get back in shape after her first child. She loved it so much, the mom of two never left.</p> <p>Hailing from Hong Kong, she has found indoor cycling to be one of the constants in her life, no matter where she is. She truly believes in pursuing what she loves, and wastes no time waiting.</p> <p>When she's not in the studio, she's usually on the plane, searching for a new adventure with her kiddos. If you're looking for some infectious laughs and catchy tunes, come ride with our resident mama.</p>",
         "image": "assets/img/packleaders/charisse.jpg"
     },
     {
         "id": "5",
         "name": "Sherie",
         "description": "<p>Sherie lives by the motto: sweat once a day. From yoga to spinning, the sweat life has been part of her journey, and she’s always game to try new workouts.</p> <p> To Sherie, Indoor spinning is more than just a workout – this sweat drippin’, heart racin’, endorphin pumpin’ activity is the most responsible dance party alive. Sherie believes that being on the bike is her way of being 100% present in the moment as she keeps her focus on the beats, the reps, and the energy in the room.</p> <p>Expect slick moves, fast tracks, and a sweat you won’t regret when you show up for Sherie’s class!</p>",
         "image": "assets/img/packleaders/charisse.jpg"
     }
 ]
php arrays json web url
1个回答
0
投票

您可以像下面那样获得URI段-

$uriSegments = explode("/", parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));

假设您的URL是-http://cru68.test/instructor-detail.php/schedule#/instructor/site/1/id/1

然后它将给您-

echo $uriSegments[3]; //returns instructor
echo $uriSegments[7]; //returns 1 (the id param)
© www.soinside.com 2019 - 2024. All rights reserved.