PHP Echo - popout div中的echo结果只是回显数据库中的第一个结果

时间:2012-09-22 15:43:37

标签: php mysql database echo

我有一个搜索引擎,它显示数据库的结果,并使用了一个显示模式,当用户点击“Click Me For More Contact Info”时会打开一个弹出框,但是当前无论在哪个链接上点击任何链接结果,相同的联系信息被回显,这是来自第一个结果的信息。

有人可以指出我正确的方向向我展示如何制作它,以便当用户点击“Click Me For More Contact Info”时,该结果的弹出框中会显示正确的联系信息吗?

感谢您的帮助,非常感谢!!

echo "<div id='myModal' class='reveal-modal'>";
echo "<h1>Modal Title</h1>";
echo "<p>Contact:" . $row['contact'] . "</p>";
echo "<a class='close-reveal-modal'>&#215;</a>";
echo "</div>";

echo "<a href='#' data-reveal-id='myModal'>Click Me For More Contact Info</a>";

编辑:搜索结果显示正常,只是当结果被回显到pop up div时,这是我的其余代码:

include('config.inc');



    // Check and set username
$username = (isset($_SESSION['username']) ? $_SESSION['username'] : 'guest');

// Check and set category
$category = (!empty($_GET['category']) ? $_GET['category'] : null);

// Check and set search
if(!empty($_GET['search'])){
$search = $_GET['search'];
}else{
$search = null;
}

// Check that $_GET['price'] is ASC if not set to DESC
// as static values its ok to directly put in the query 
if(isset($_GET['price']) && $_GET['price'] == 'ASC'){
$price = 'ASC';
}else{
$price = 'DESC';
}

if ($search !== null){

$sql = "SELECT * FROM people WHERE MATCH (lname,fname) AGAINST (:search IN BOOLEAN MODE)";

$q   = $conn->prepare($sql) or die("failed!");
// Bind the params to the placeholders
$q->bindParam(':search', $search, PDO::PARAM_STR);
$q->execute();
}

if ($search !== null && $category !== null){

$sql = "SELECT * FROM people WHERE MATCH (lname,fname) AGAINST (:search IN BOOLEAN MODE) AND category = :category";

$q   = $conn->prepare($sql) or die("failed!");
// Bind the params to the placeholders
$q->bindParam(':search', $search, PDO::PARAM_STR);
$q->bindParam(':category', $category, PDO::PARAM_STR);
$q->execute();
}

if ($category !== null && $search !== null && isset($price)){

$sql = "SELECT   *
        FROM     people
        WHERE    MATCH (lname,fname) AGAINST (:search IN BOOLEAN MODE)
        AND      category = :category
        ORDER BY price ".$price;

$q = $conn->prepare($sql);
// Bind the params to the placeholders
$q->bindParam(':search', $search, PDO::PARAM_STR);
$q->bindParam(':category', $category, PDO::PARAM_STR);
$q->execute();
}

if ($category == null && $search !== null && isset($price)){

$sql = "SELECT   *
        FROM     people
        WHERE    MATCH (lname,fname) AGAINST (:search IN BOOLEAN MODE)

        ORDER BY price ".$price;

$q = $conn->prepare($sql);
// Bind the params to the placeholders
$q->bindParam(':search', $search, PDO::PARAM_STR);
$q->execute();
}

if ($q){

//declaring counter
    $count=0;
    while($r = $q->fetch(PDO::FETCH_ASSOC)){
    $row = $r;

    $fname = $row['fname'];
    $lname = $row['lname'];
    $firstname = $row['firstname'];
    $surname = $row['surname'];

//counter equals
$count++;

//insert an image every 5 rows
    if($count==5){
    $count=0;
   echo "<table width='50%' style='border-bottom:1px solid #000000;'";
echo "<tr>";
echo "<td>";
echo "<div id='page-wrap'>";
echo "<div class='discounted-item freeshipping'>";

echo "<a href='images/box1.png' rel='lightbox'><img src='images/box1.png' width='20%' height='98%' /></a>";


echo "<div class='reasonbar'><div class='prod-title' style='width: 70%;'>AN AD CAN GO HERE</div><div class='reason' style='width: 29%;'><b>Ad Company</b></div></div>";

echo "<div class='reasonbar'><div class='prod-title1' style='width: 70%;'>Description about the advert from a company</div><div class='reason1' style='width: 29%;'>Category: Advert</div></div>";

echo "<div class='reasonbar'><div class='prod-title2' style='width: 70%;'>HELLO, User</div><div class='reason2' style='width: 29%;'></div></div>";


echo "</td>";
echo "</tr>";
echo "</td>";
echo "</tr>";
echo "</table>";
}




echo "<table width='50%' style='border-bottom:1px solid #000000;'";
echo "<tr>";
echo "<td>";
echo "<div id='page-wrap'>";
echo "<div class='discounted-item freeshipping'>";

echo "<a href='./img/users/" . $row['category'] . "/" . $row['username'] . "/" . $row['filename'] . "' rel='lightbox'><img src=\"./img/users/" . $row['category'] . "/" . $row['username'] . "/" . $row['filename'] . "\" alt=\"\" width='15%' height='80%' /></a>";


echo "<div class='reasonbar'><div class='prod-title' style='width: 70%;'>" .$row['fname'] . "</div><div class='reason' style='width: 29%;'><b>". $row['firstname'] . " " . $row['surname'] ."</b></div></div>";

echo "<div class='reasonbar'><div class='prod-title1' style='width: 70%;'>" .   $row['lname'] . "</div><div class='reason1' style='width: 29%;'>Category:<br /> ".   $row['category'] . "</div></div>";

echo "<div class='reasonbar'><div class='prod-title2' style='width: 70%;'>Contact:" . $row['contact'] . "</div><div class='reason2' style='width: 29%;'>Price: &pound;".    $row['price'] . "</div></div>";



echo "</td>";
echo "</tr>";
echo "</td>";
echo "</tr>";
echo "</table>";

echo "<div id='myModal' class='reveal-modal'>";
echo "<h1>Modal Title</h1>";
echo "<p>Contact:" . $row['contact'] . "</p>";
echo "<a class='close-reveal-modal'>&#215;</a>";
echo "</div>";

echo "<a href='#' data-reveal-id='myModal'>Click Me For A Modal</a>";


}


}

else
echo "No results found for \"<b>$search</b>\"";

2 个答案:

答案 0 :(得分:2)

从您提供的链接:

Just give your modal div the class "reveal-modal"and a unique ID (we'll use the ID to launch this modal)By putting the "data-reveal-id" attribute on the anchor, when clicked the plugin matches the value of the "data-reveal-id" attribute (in this case "myModal") with an HTML element with that ID

目前你似乎给每个模态赋予相同的id,所以所有的模态链接都将以第一个为目标。

echo "<div id='myModal".$count."' class='reveal-modal'>";
echo "<h1>Modal Title</h1>";
echo "<p>Contact:" . $row['contact'] . "</p>";
echo "<a class='close-reveal-modal'>&#215;</a>";
echo "</div>";

echo "<a href='#' data-reveal-id='myModal".$count."'>Click Me For A Modal</a>";

答案 1 :(得分:0)

从数据库中检索数据时,请确保使用while($row=$req->fetch())检索所有数据。然后你必须选择符合你需要的正确行。

注意,$row是一个包含所有行的数组。因此:$row[0]['contact']将包含第一行的联系人,$row[1]['contact']来自第二行,等等...