Open another page and send data in php?

时间:2016-05-17 11:11:48

标签: php session while-loop

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.

1 个答案:

答案 0 :(得分:2)

您需要更改链接

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

这样的事情:

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

然后你可以在preview.php文件中输入$_GET['id']的id。但是在使用之前不要忘记检查它是否是一个数字。 (is_numeric()或int_val())