如何将表2的结果设置为与表1不同的样式

时间:2017-01-24 19:15:25

标签: php sql

以下代码有效但我希望通过设置赞助商'的结果来进一步开发它。表格不同于照片'表。我该如何定位呢?

<?php
$search = $_GET['s'];
$db = mysqli_connect("", "", "", "") or die ("could not connect to mysql");

if ($search) {
    $sql = "SELECT * FROM photos
            WHERE title LIKE '%".$search."%'
            ORDER BY id DESC";
} else {
    $sql = 'SELECT * FROM photos
            UNION SELECT * FROM sponsor
            ORDER BY id DESC';
}

$result = mysqli_query($db, $sql);
if(mysqli_num_rows($result) >=1) {
    while ($row = mysqli_fetch_array($result)) {
        echo"<a href=".$row['link'].">";
        echo"<div class='img_div'>";
        echo"<div class='white'><img src='images/".$row['image']."'></div>";
        echo"<h2>".$row['title']."</h2>";
        echo"<div class='date'>".$date = date('j F, Y', strtotime($row['date']))."</div>";
        echo"</div>";   
        echo"</a>"; 
}
//continue
}else{
echo "No Results";
}
?>

更新:尝试合并以下答案     

if ($search) {
$sql = "SELECT photos.*, 'photos' AS 'tableName1' FROM photos
        WHERE title LIKE '%".$search."%'
        ORDER BY id DESC";
} else {
    $sql = "SELECT photos.*, 'photos' AS 'tableName1' FROM photos
        UNION SELECT sponsor.*, 'sponsor' AS 'tableName2' FROM sponsor
        ORDER BY id DESC";
}

$result = mysqli_query($db, $sql);
if(mysqli_num_rows($result) >=1) {
    while ($row = mysqli_fetch_array($result)) {
        echo"<a class=\"{$row['tableName1']}\" href=".$row['link'].">";
        echo"<div class='img_div'>";
        echo"<div class='white'><img src='images/".$row['image']."'></div>";
        echo"<h2>".$row['title']."</h2>";
        echo"<div class='date'>".$date = date('j F, Y', strtotime($row['date']))."</div>";
        echo"</div>";   
        echo"</a>";


        echo"<a class=\"{$row['tableName2']}\" href=".$row['link'].">";
        echo"<div class='img_div2'>";
        echo"<div class='white2'><img src='images/".$row['image']."'></div>";
        echo"<h2 class='sponsor'>".$row['title']."</h2>";
        echo"</div>";   
        echo"</a>"; 
}
//continue
}else{
echo "No Results";
}
?>

1 个答案:

答案 0 :(得分:1)

假设您的表架构过于相同,无法轻易区分。您可以向查询添加自定义列以区分它们。类似的东西:

<?php
$search = $_GET['s'];
$db = mysqli_connect("", "", "", "") or die ("could not connect to mysql");

if ($search) {
    $sql = "SELECT photos.*, 'photos' AS 'tableName' FROM photos
            WHERE title LIKE '%".$search."%'
            ORDER BY id DESC";
} else {
    $sql = "SELECT photos.*, 'photos' AS 'tableName' FROM photos
            UNION SELECT sponsor.*, 'sponsor' AS 'tableName' FROM sponsor
            ORDER BY id DESC";
}

$result = mysqli_query($db, $sql);
if(mysqli_num_rows($result) >=1) {
    while ($row = mysqli_fetch_array($result)) {
        echo"<a class=\"{$row['tableName']}\" href=".$row['link'].">";
        echo"<div class='img_div'>";
        echo"<div class='white'><img src='images/".$row['image']."'></div>";
        echo"<h2>".$row['title']."</h2>";
        echo"<div class='date'>".$date = date('j F, Y', strtotime($row['date']))."</div>";
        echo"</div>";   
        echo"</a>"; 
}
//continue
}else{
echo "No Results";
}
?>

请注意,在查询中,我添加了'photos' AS 'tableName''sponsors' AS 'tableName',以便每个查询都会添加一个列,其中包含来自哪个表。这使您可以像引用任何其他列一样访问row['tableName']

此时,它就像在a标记中添加类一样简单。您可以将该类放在任何元素上,具体取决于您想要的标记方式。

请注意,编写的代码易受SQL Injection攻击。您应该使用prepared statements而不是直接在查询中插入GET个变量。