简体   繁体   English

PHP / MySQL-ID匹配时打印行

[英]PHP/MySQL - Print rows when id matches

I have two tables in MySQL Database, namely: 我在MySQL数据库中有两个表,分别是:

maintenance : name(primary key), url, and description

feeds : feed_id(primary key), feed_name(foreign key reference name), feed_url, feed_notes

When i select the name from list <ul><li> it suppose to print the corresponding information related to the selected name. 当我从列表<ul><li>选择名称时,它假定打印与所选名称相关的相应信息。

Any idea how to do it? 知道怎么做吗?

Expected Outcome 预期结果

预期产量

This is what I've done so far 这是我到目前为止所做的

LIST: LIST:

<div id="cssmenu">
            <?php
            $servername = "localhost";
            $username = "root";
            $password = "";
            $database = "rssfeed";

            $connect = mysqli_connect($servername, $username, $password, $database);
            if (!$connect) {
                die("Cannot connect: " . mysqli_connect_error());
            }

            mysqli_select_db($connect, $database);

            $sql = "SELECT * from maintenance";
            $data = mysqli_query($connect, $sql);


            if (mysqli_num_rows($data) > 0) {
                while ($row = mysqli_fetch_assoc($data)) {
                    ?>
                    <ul id="record<?php echo $row['name']; ?>">
                        <li class="name"></li>
                        <li><span><a href="rssoutput" name="title"><?php echo $row['name']; ?></a></span></li>
                    </ul>
                    <?php
                }
            }
            ?>

        </div>

OUTPUT OUTPUT

<div id ="rssoutput">
            <?php
                $name = $_POST['name'];

                $sql = "SELECT * FROM feeds, maintenance WHERE feed_name=$name";
                $result = mysqli_query($connect, $sql);

                $link = $title = $notes = array();

                if (mysqli_num_rows($result)) {
                    while ($row = mysqli_fetch_array($result)) {
                        $link = $row['feed_url'];
                        $title = $row['feed_title'];
                        $notes = $row['feed_notes'];

                        echo "<a id=title href='" . $link . "'>" . $title . "</a>";
                        echo "<p>" . $notes . "</p>";
                        echo "<hr>";
                    }
                }
            ?>
        </div>

AJAX AJAX

getrss.php getrss.php

    <?php

$servername = "localhost";
$username = "root";
$password = "";
$database = "rssfeed";

$connect = mysqli_connect($servername, $username, $password, $database);
if (!$connect) {
    die("Cannot connect: " . mysqli_connect_error());
}

mysqli_select_db($connect, $database);

$name = $_GET['name'];

$sql = "SELECT * FROM feeds, maintenance WHERE feed_name='$name' AND name='$name'";
$result = mysqli_query($connect, $sql);

$link = $title = $notes = array();

while ($row = mysqli_fetch_array($result)) {

    $link = $row['feed_url'];
    $title = $row['feed_title'];
    $notes = $row['feed_notes'];

    echo "<a id=title href='" . $link . "'>" . $title . "</a>";
    echo "<p>" . $notes . "</p>";
    echo "<hr>";
}
?>

action.js action.js

    function showRSS(id) {
    var str = $("#record" + id + ".name").html();
    if (str.length == 0) {
        document.getElementById("rssoutput").innerHTML = "";
        return;
    }
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else {  // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.open("GET", "getrss.php?id=" + id, true);
    xmlhttp.send();
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("rssoutput").innerHTML = xmlhttp.responseText;
        }
    }
}

myfeeds.php myfeeds.php

            <div id="cssmenu">
            <?php
            $servername = "localhost";
            $username = "root";
            $password = "";
            $database = "rssfeed";

            $connect = mysqli_connect($servername, $username, $password, $database);
            if (!$connect) {
                die("Cannot connect: " . mysqli_connect_error());
            }

            mysqli_select_db($connect, $database);

            $sql = "SELECT * from maintenance";
            $data = mysqli_query($connect, $sql);


            if (mysqli_num_rows($data) > 0) {
                while ($row = mysqli_fetch_assoc($data)) {
                    ?>
                    <ul id="record<?php echo $row['name']; ?>">
                        <li class="name"></li>
                        <li><span><a  href="#rssoutput" name="title" onclick="showRSS(<?php echo $row['name'] ?>)"><?php echo $row['name']; ?></a></span></li>
                    </ul>
                    <?php
                }
            }
            ?>

        </div>

I know my codes doesn't make sense. 我知道我的代码没有道理。 For me at least, cause I don't have any idea how to start working on it So if you guys can help me out, It's really really really appreciated 至少对我来说,因为我不知道如何开始研究它,所以如果你们能帮助我,真的非常感谢

It think there's no problem with your code. 它认为您的代码没有问题。

But I check that in getrss.php you use $name = $_GET['name']; 但是我在getrss.php中检查是否使用$ name = $ _GET ['name']; while in your action.js you make request with getrss.php?id= 而在action.js中,您使用getrss.php?id =发出请求

I solved the problem. 我解决了问题。

Only one problem syntax in my js 我的js中只有一种问题语法

    function showRSS(name) {
    var str = $("#record" + id + ".name").html();
    if (str.length == 0) {
        document.getElementById("rssoutput").innerHTML = "";
        return;
    }
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else {  // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.open("GET", "getrss.php?name=" + id, true);
    xmlhttp.send();
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("rssoutput").innerHTML = xmlhttp.responseText;
        }
    }
}

instead of id, i change it to name :) 而不是ID,我将其更改为名称:)

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

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