简体   繁体   English

动态php页面调用

[英]dynamic php page calls

So I have looked through many of the other questions posted but none seemed to properly answer my question. 因此,我已经浏览了许多其他发布的问题,但似乎没有一个能够正确回答我的问题。

I currently have a php script that reads rows from a database and runs a loop to display the info of each row in a html div. 我目前有一个php脚本,可从数据库中读取行并运行循环以在html div中显示每一行的信息。

The issue I am now having is that in each "card" of info previously loaded there has a picture. 我现在遇到的问题是,在先前加载的每个“信息卡”中都有一张图片。 And when the picture is clicked I need to load a different file names listing.html. 当单击图片时,我需要加载其他文件名listing.html。 The problem is that depending on which picture is clicked on I want to load different information based on which listing was clicked on. 问题是,根据要单击的图片,我想根据单击的列表加载不同的信息。 I looked into using sessions but since i dynamically load all of the pictures in one loop I do not know a way to differentiate between them. 我研究了使用会话,但是由于我会在一个循环中动态加载所有图片,因此我不知道如何区分它们。

here is the code I currently have that loads all the info from the database. 这是我当前从数据库加载所有信息的代码。

$query = "SELECT * FROM Listings ORDER BY OrderNumber";

                        if ($result = mysqli_query($con, $query))
                        {
                            while ($row = mysqli_fetch_row($result)) 
                            {
                            echo "<div class ='listing'>";
                            echo "<a href='listing.html'>";
                            echo "<img src=$row[2] alt='' width='60%' align='left' >";
                            echo "</a>";
                            echo "<span style = 'font-size: 25px;'>";
                            echo $row[4];
                            echo "</span>";
                            echo "<br>";
                            echo "<br>";
                            echo "MLS Number: #";
                            echo $row[1];
                            echo "<br>";
                            echo "<br>";
                            echo "<b>Open House Details: </b>";
                            echo $row[6];
                            echo "<br>";
                            echo "<br>";
                            echo $row[5];
                            echo "<br>";
                            echo "<br>";
                            echo "<a href='";
                            echo $row[9];
                            echo "' style='color: #8a0b0b; font-weight: bold; font-style: italic; font-size: 18px;' target='_blank'>Virtual Tour</a>";
                            echo "<div class='listingPrice'>";
                            echo "$ ";
                            echo $row[7];
                            echo "</div>";
                            echo "</div>";

                            }
                            mysqli_free_result($result);
                        }

I need some way that when the a tag is clicked to first be able to discern which picture was clicked then probably make a session variable with the corresponding row. 我需要某种方式,当单击一个标签时,首先能够辨别单击的图片,然后可能在相应的行中设置会话变量。

When you output the link, include a query string value from the row in the data. 输出链接时,请在数据行中包含查询字符串值。 Something like this: 像这样:

echo "<a href='listing.html?id=" . row[0] . "'>";

(I'm just guessing on the row[0] part, it would be any identifier which uniquely identifies that record.) That way all of the links would have the data you need on the listing.html page embedded directly in them. (我只是在row[0]部分上猜测,它将是可以唯一标识该记录的任何标识符。)这样,所有链接都将直接将listing.html页面上所需的数据嵌入其中。 (Shouldn't it be listing.php ?) (这不是listing.php吗?)

So on the page being referenced, the value would then be available in the query string. 因此,在被引用的页面上,该值将在查询字符串中可用。 Something like this: 像这样:

$_GET["id"]

There really isn't a need for session state in this case, if all you need to know is which record was clicked then that information can be placed directly in the link. 在这种情况下,实际上不需要会话状态,如果您只需要单击哪个记录,那么就可以将信息直接放置在链接中。 This reduces coupling in the code, keeps the links a little more RESTful, even lets people bookmark the link directly if they want to. 这样可以减少代码中的耦合,使链接保持更多的REST风格,甚至让人们根据需要直接为链接添加书签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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