php mysql select 来自三个没有关系的表并创建链接

php mysql select from three tables without relation and create links

我有以下功能。 我试图从 3 个不同的 table 中获取数据,但没有任何关系。 除了 a href 属性外,一切正常。 我需要根据 table 设置 link。 I.E.:如果 insert_into_index = 1 in table 表演我需要 link 到 performance.php,如果 insert_into_index = 1 in table 教育,link 应该是 education.php 如果 insert_into_index = 1 in artists_works 那么 link 应该是 art.php

    function intro_news($lang) {
    global $connection;
    $lang = $_GET['lang'];  
    
    $sql = "
    SELECT id, title, title_en, insert_into_index, ordering FROM performances
    WHERE insert_into_index=1
    UNION
    SELECT id, title, title_en, insert_into_index, ordering FROM education
    WHERE insert_into_index=1
    UNION
    SELECT id, title, title_en, insert_into_index, ordering FROM artists_works
    WHERE insert_into_index=1
    ORDER BY ordering ASC
    ";  
    
    $query = mysqli_query($connection,$sql) or die(mysqli_error($connection));
    $count = mysqli_num_rows($query);
    if ($count > 0) {
        echo '<div class="grid">';
        echo '<div class="row justify-content-center">';
        while ($row = mysqli_fetch_array($query)) {
            echo '<div class="col-sm-12 col-md-6 col-lg-3 margin-wrapp rotate my-auto">';

            // HERE IS THE ISSUE
            if ($row['insert_into_index'] == '1') {
                echo '<a href="';
                echo 'performance';
                echo '.php?lang='.$lang.'&name='.$row['id'].'">';
            }
            if ($row['insert_into_index'] == '1') {
                echo '<a href="';
                echo 'education';
                echo '.php?lang='.$lang.'&name='.$row['id'].'">';
            }
            if ($row['insert_into_index'] == '1') {
                echo '<a href="';
                echo 'art';
                echo '.php?lang='.$lang.'&name='.$row['id'].'">';
            }
            // ISSUE


            echo '<span class="name">';
            if ($lang === "sk") {
                echo $row['title'];
            }
            if ($lang === "en") {
                echo $row['title_en'];
            }
            echo '</span>';
            echo '</a>';
            echo '</div>';
        } // while loop
        echo '</div>';
        echo '</div>';
    }
}

现在的结果是:

<a href=art.php?lang=sk&name=1>bla bla</a>
<a href=art.php?lang=sk&name=1>bla bla</a>
<a href=art.php?lang=sk&name=4>bla bla</a>

预期结果应该是:

<a href=performance.php?lang=sk&name=1>bla bla</a>
<a href=education.php?lang=sk&name=1>bla bla</a>
<a href=art.php?lang=sk&name=4>bla bla</a>

感谢您的帮助。 非常感谢。

将类型添加到您的 SQL 查询中:

SELECT id, title, title_en, insert_into_index, ordering, 'performance' as type FROM performances
    WHERE insert_into_index=1
    UNION
    SELECT id, title, title_en, insert_into_index, ordering, 'education' as type FROM education
    WHERE insert_into_index=1
    UNION
    SELECT id, title, title_en, insert_into_index, ordering, 'art' as type FROM artists_works
    WHERE insert_into_index=1
    ORDER BY ordering ASC

然后您就可以使用输入 php 代码

echo '<a href="';
echo $row['type'];
echo '.php?lang='.$lang.'&name='.$row['id'].'">';