简体   繁体   中英

Open another page and send data in php?

I'm developing a simple online portfolio website for my final project. I have a page to show all design stored in database. This is the template of a item,

+---------------------+
+                     +
+                     +
+       image         +
+                     +
+                     +
+                     +
+---------------------+
+      Title          +
+                     +
+---------------------+

This is the code that i used to load all items from databse.

<?php
    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
        $title = $row['title'];
        $image = $row['image'];
        $designer = $row['designer'];
        $views = $row['views'];
        $likes = $row['likes'];
        $price = $row['price'];
?>
    <div class = "col-md-3 col-sm-6"> <div class = "wow fadeInUp animated portfolio-item" data-wow-duration = "500ms" data-wow-delay = "600ms">
        <div class = "img-wrapper">
            <img src = "<?php echo $image; ?>" class = "img-responsive" alt = "" ></div>
                <div class = "portfolio-item-text"><h4><a href = "preview.php?title">
                    <?php echo $title; ?>
                    </a>
                    </h4>
                    <p id = "portfolio-subtitle">
                    by <?php echo $designer; ?>
                    </p>
                    <div class="row" style="margin-top:10px;">
                        <div class="col-md-3" style="float:left;">
                            <h6><?php echo $price; ?></h6>
                        </div>
                        <div class="col-md-7" style="float:right;">
                            <div class="portfolio-icons">
                                <ul class="list-inline">
                                    <li><i class="fa fa-eye"></i> <?php echo $views; ?></li>
                                    <li><i class="fa fa-thumbs-o-up"></i> <?php echo $likes ?></li>
                                </ul> 
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    <?php
}
?>

I need to open another page to preview each design. So i used $_SESSION to bring data to my preview page inside the while loop.

$_SESSION['id'] = $row['design_id'];
$_SESSION['title'] = $row['title'];

but it only preview the last item. How can i solve this.

You need to change the link

<a href="preview.php?title"><?php echo $title; ?> </a>

to something like this:

<a href="preview.php?id=<?= $row['design_id'] ?>"><?php echo $title; ?></a>

Then you can get in the preview.php file the id with $_GET['id'] . But don't forget to check if it is a number before usage. (is_numeric() or int_val())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM