如果与数据库值相同,则跳过foreach

时间:2017-07-17 13:26:24

标签: php xml mysqli foreach

首先抱歉我的英语不好。我想问你如果值在数据库中,如何从xml跳过foreach循环。

这是来自xml的foreach代码:

$xml=simplexml_load_file("http://localhost:80/ijarah/webservice/show_data_mobil.xml") or die("Error: Cannot create object");
foreach($xml->children() as $mobil){
    echo $mobil->nama;
    echo "<br>";
}

结果:

Toyota Avanza 2012 B 4230 XCM
Toyota Innova 2004 B 12349 DAC
Honda Carrens 2002 B 1408 N0H
Honda Supra X 2007 B 6754 NAC
Yamaha Vixion 2013 B 1564 TBR
Honda Supra 2002 B 8764 ZMN
Honda Supra Fit 2007 B 5533 KAC

这是我的数据库的过滤器代码

$getnas = $con->query("select * from transaksi_pembiayaan where STATUS='Belum Lunas'");
    while ($row = $getnas->fetch_assoc()) {
        $getBar = $con->query("select id_barang from pengajuan where id_nasabah='$row[id_nasabah]'");
        while ($rows = $getBar->fetch_assoc()) {
            $getBarang = $con->query("select nama_barang from barang where id_barang='$rows[id_barang]'");
            while ($rok = $getBarang->fetch_assoc()) {
                    echo $rok['nama_barang'];
                    echo '<br>';
                }
            }
        }

结果:

Honda Carrens 2002 B 1408 N0H
Honda Supra X 2007 B 6754 NAC

那么,如何从数据库过滤器中跳过数据foreach循环xml?换句话说,这是我想要的基于xml和数据库过滤器的结果。

Toyota Avanza 2012 B 4230 XCM
Toyota Innova 2004 B 12349 DAC
Honda Carrens 2002 B 1408 N0H <- Needs to be skipped
Honda Supra X 2007 B 6754 NAC <- This one too
Yamaha Vixion 2013 B 1564 TBR
Honda Supra 2002 B 8764 ZMN
Honda Supra Fit 2007 B 5533 KAC

我该怎么做?谢谢。

1 个答案:

答案 0 :(得分:0)

将数据库过滤器输出存储在数组中:

$filterOutput=array();
$getnas = $con->query("select * from transaksi_pembiayaan where STATUS='Belum Lunas'");
while ($row = $getnas->fetch_assoc())
{
    $getBar = $con->query("select id_barang from pengajuan where id_nasabah='$row[id_nasabah]'");
    while ($rows = $getBar->fetch_assoc())
    {
        $getBarang = $con->query("select nama_barang from barang where id_barang='$rows[id_barang]'");
        while ($rok = $getBarang->fetch_assoc())
        {
//          echo $rok['nama_barang'];
//          echo '<br>';
            $filterOutput[]=$rok['nama_barang'];
        }
    }
}

然后使用过滤器输出:

$xml=simplexml_load_file("http://localhost:80/ijarah/webservice/show_data_mobil.xml") or die("Error: Cannot create object");
foreach($xml->children() as $mobil)
{
    if (in_array($mobil->nama,$filterOutput))
    {
        continue;
    }
    echo $mobil->nama;
    echo "<br>";
}