dsl2014 2013-09-03 08:12
浏览 71
已采纳

将一个表中的两列合并为一个输出

I a have a table like this:

Table

and I want to combine colums 'uitvoeringid' and 'uitvoeringoms' and output as one with space between them.

This is my class:

public function getBanden($id = NULL, $merk = NULL, $seizoen = NULL)
{
    $sql = "SELECT * FROM Uitvoering";
    if(!empty($id)) 
    {
        $sql .= " WHERE uitvoeringid=:id"; 
        if(!empty($merk)) { $sql .= " AND merkcode=:merk"; }
        if(!empty($seizoen)) { $sql .= " AND uitvoeringseizoen=:seizoen"; }
    }
    else if(!empty($merk)) 
    { 
        $sql .= " WHERE merkcode=:merk"; 
        if(!empty($seizoen)) { $sql .= " AND uitvoeringseizoen=:seizoen"; }
        $sql .= " ORDER BY uitvoeringvoertuigtype ASC, uitvoeringoms ASC";
    }

    try
    {
        $stmt = $this->db->prepare($sql);
        if(!empty($id)) { $stmt->bindParam(":id", $id, PDO::PARAM_INT); }
        if(!empty($merk)) { $stmt->bindParam(":merk", $merk, PDO::PARAM_STR); }
        if(!empty($seizoen)) { $stmt->bindParam(":seizoen", $seizoen, PDO::PARAM_STR); }
        $stmt->execute();
        $this->bandenlijst = $stmt->fetchAll(PDO::FETCH_OBJ);
        $stmt->closeCursor();

        return $this->bandenlijst;
    }
    catch (Exception $e)
    {
        die ( $e->getMessage() );
    }
}

This is a part of my file where I output the data:

if(isset($_POST['band_submit']) && $_POST['band_submit'] == "Zoek" || isset($_GET['merk']) && isset($_GET['type']) && isset($_GET['profiel']))
{
    $merk = NULL;
    $seizoentype = NULL;
    if(isset($_POST['band_submit']) && $_POST['band_submit'] == "Zoek")
    {
        if($_POST['band_seizoen'] != "0") { $seizoentype = $_POST['band_seizoen']; }
        $merk = $_POST['band_merk'];
    }
    else if(isset($_GET['merk']) && isset($_GET['type']))
    {
        if($_GET['type'] != "0") { $seizoentype = $_GET['type']; }
        $merk = $_GET['merk'];
    }
    else { $seizoentype = NULL; $merk = NULL; }
    $strSeizoen = NULL;
    if ($seizoentype == "ZO") { $strSeizoen = "Onze zomerbanden"; }
    elseif ($seizoentype == "WI") { $strSeizoen = "Onze winterbanden"; }
    elseif ($seizoentype == "AS") { $strSeizoen = "Onze All-seasonbanden"; }
    elseif ($seizoentype == "OV") { $strSeizoen = "Onze Overige banden"; }
    else { $strSeizoen = "Alle A-merken en topklasse huismerken"; }
    echo "\t\t\t\t\t<h2>" . $strSeizoen . "</h2>
\t\t\t\t\t<br />
";

    $merken = $merkclass->getMerken($merk);
    $banden = $bandclass->getBanden(NULL, $merk, $seizoentype);
    $nCount = 0;
    $selband = NULL;
?>
                    <img src="http://www.website.net/logos/<?php echo str_replace(".png", "_150.png", $merken[0]->merk_logo); ?>" width="150" class="logo" alt="<?php echo $merken[0]->merk_naam; ?>"/>
                    <div id="merken">
                        <ul>
<?php
    foreach($banden as $band)
    {
?>
<li><a href="http://example-website.com/<?php   
    echo $band->merkcode;?>/<?php if(isset($seizoentype) && $seizoentype == "ZO") {echo "zomerbanden";}
    else if ($seizoentype == "WI") {echo "winterbanden";}
    else if ($seizoentype == "AS") {echo "all-season-banden";}
    else if ($seizoentype == "OV") {echo "overig";}
    else{ echo "alle-types";}?>/<?php echo $band->uitvoeringid;?>">
    <?php echo str_replace(array(' ', ',', '/', '!'), '-',strtolower($band->uitvoeringoms));?>
    </a>
    </li>
<?php
        if(isset($_GET['profiel']) && $band->uitvoeringid == $_GET['profiel']) { $selband = $band; }
        $nCount++;
    }
    if(empty($selband) && count($banden) > 0)
    {
        $selband = $banden[0];
    }
    else if(count($banden) > 0)
    {
    }
    else
    {
        echo "\t\t\t\t\t\t\t<li>Nothing Found</li>
";
    }
?>
                        </ul>
                        <div class="clearboth"></div>
                    </div>

How can I manage to keep the working of this the same but combine 'uitvoeringid' and 'uitvoeringoms' to one output.

So in this part:

<a href="http://example-website.com/<?php   
    echo $band->merkcode;?>/<?php if(isset($seizoentype) && $seizoentype == "ZO") {echo "zomerbanden";}
    else if ($seizoentype == "WI") {echo "winterbanden";}
    else if ($seizoentype == "AS") {echo "all-season-banden";}
    else if ($seizoentype == "OV") {echo "overig";}
    else{ echo "alle-types";}?>/<?php echo $band->uitvoeringid;?>">
    <?php echo str_replace(array(' ', ',', '/', '!'), '-',strtolower($band->uitvoeringoms));?>
    </a>

I want this line <?php echo $band->uitvoeringid;?> to be 'uitvoeringoms' and 'uitvoeringid' combined to something like "test-2341"

I tried something like:

$sql = "SELECT concat(uitvoeringid, uitvoeringoms) AS single FROM Uitvoering";

But I still want to SELECT everything and not only (uitvoeringid, uitvoeringoms)

I got a bit lost trying to get this working in a good way. Can somebody help me please? :)

It was very hard to explain this in a good way for me so I hope you guys understand it.

Thanks

  • 写回答

3条回答 默认 最新

  • douhuang4166 2013-09-03 08:13
    关注

    Isn't this what you are looking for? A space in the middle?

    $sql = "SELECT *,concat(uitvoeringid, ' ', uitvoeringoms) AS single FROM Uitvoering";
    

    Or simply:

    echo $uitvoeringsid.' '.$uitvoeringoms;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab