【问题标题】:php mysql select from three tables without relation and create linksphp mysql 从三个没有关系的表中选择并创建链接
【发布时间】:2021-06-19 06:50:30
【问题描述】:

我有以下功能。 我正在尝试从 3 个没有任何关系的不同表中获取数据。 除了a href 属性外,一切正常。 我需要根据表格设置链接。 IE:如果insert_into_index = 1 in table performances我需要链接到performance.php,如果insert_into_index = 1 in table education,链接应该是education.php,如果insert_into_index = 1 in Artists_works链接应该是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>

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

【问题讨论】:

  • 我很好奇你为什么不想要关系。

标签: php mysql join union


【解决方案1】:

在您的 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代码中使用type了

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

【讨论】:

  • 非常感谢。你是救生员。 :-)
猜你喜欢
  • 1970-01-01
  • 2017-10-17
  • 2011-01-18
  • 2012-10-29
  • 1970-01-01
  • 1970-01-01
  • 2019-03-03
  • 1970-01-01
相关资源
最近更新 更多