[英]php mysql select from three tables without relation and create links
我有以下功能。 我正在嘗試從 3 個不同的表中獲取沒有任何關系的數據。 除了a href
屬性外,一切正常。 我需要根據表格設置鏈接。 IE: If insert_into_index = 1 in table performances I need link to performance.php, if the insert_into_index = 1 in table education, link should be education.php and if insert_into_index = 1 in artists_works the link should be 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'].'">';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.