MySQL如何在foreach循环中离开联接并回显变量

时间:2019-03-05 06:58:09

标签: php mysql pdo left-join

朋友,我尝试在php端使用mysql左联接查询,我需要一点帮助。  我无法作为php对象进行查询,如果我查询对象,则查询为空:$ data = $ categories-> fetchAll(PDO :: FETCH_OBJ);

如何查询作为php对象?

<?php
$categories = $vt->prepare("SELECT t1.baslik AS lev1, t2.baslik as lev2, t3.baslik as lev3, t4.baslik as lev4
FROM menuler AS t1
LEFT JOIN menuler AS t2 ON t2.katID = t1.id
LEFT JOIN menuler AS t3 ON t3.katID = t2.id
LEFT JOIN menuler AS t4 ON t4.katID = t3.id WHERE t1.katID=?");

foreach ($listeSonuc as $listele) {
    # code...
 }

$categories->execute(array($listele->katID));
$data=$categories->fetchAll(PDO::FETCH_ASSOC);
$categoriesResult = array_map(function($item){
    return (object)array_combine(['lev1', 'lev2', 'lev3', 'lev4'], $item);
},$data);

// code begins here
$newData = [];
foreach ($categoriesResult as $row) {
    $tree = get_object_vars($row);
    do {
        $title = array_pop($tree);
    } while($title === null);
    $catTree = implode(' > ', $tree);
    $newData[] = (object)[
        'title' => $title,
        'tree'  => $catTree
    ];
}?>
<div class="table">
        <div>
            <div class="th">Başlık</div>
            <div class="th kucukteldegizle">Bölüm</div>
            <div class="th kucukteldegizle">Özellik</div>
            <div class="th kucukteldegizle">Yayınla?</div>
            <div class="th text-right">İşlem</div>              
        </div>

<?php foreach ($newData as $row) {?>
        <div class="cizgili">
            <div class='bosluk'><?php echo $row->title?></div>
            <div class="kucukteldegizle"><?php echo $row->tree?></div>
            <div class="kucukteldegizle">???</div>
            <div class="kucukteldegizle">???</div>
            <div>???</div>
        </div>
<?php }?>
</div>

5 个答案:

答案 0 :(得分:2)

std::string标志将每一行作为php对象返回。所以您的代码应该像这样。

FETCH_OBJ

这应该显示您的结果。

答案 1 :(得分:1)

$categoriesResult=$categories->fetchAll(PDO::FETCH_ASSOC);

foreach ($categoriesResult as $title => $value) {
    echo "$title = $value";
}

答案 2 :(得分:1)

String topic = "my-topic";
int partition = 42;
int offset = 13;
boolean running = true;

while(running) {
    TopicPartition topicPartition = new TopicPartition(topic, partition);
    consumer.assign(List.of(topicPartition));
    consumer.seek(topicPartition, offset);

    ConsumerRecords records = consumer.poll(0);
    // process records
    // .....

    // release consumer
    consumer.unsubscribe();
    // Change topic, partition, offset as needed
}

这就是您应该能够访问您的值的方式。当然,您可以将HTML放入echo语句中以格式化结果。

答案 3 :(得分:1)

$newData = [];
foreach ($categoriesResult as $row) {
    $tree = get_object_vars($row);
    do {
        $title = array_pop($tree);
    } while($title === null);
    $catTree = implode(' > ', $tree);
    $newData[] = (object)[
        'title' => $title,
        'tree'  => $catTree
    ];
}

$newData现在看起来像这样:

array (
  0 => 
  stdClass::__set_state(array(
     'title' => 'Sony 72 Ekran Led Television',
     'tree' => 'All Televisions > Led Televisions > Sony Led Televisions',
  )),
  1 => 
  stdClass::__set_state(array(
     'title' => 'Samsung 142" Lcd Television',
     'tree' => 'All Televisions > Led Televisions > Samsung Lcd Televisions',
  )),
  2 => 
  stdClass::__set_state(array(
     'title' => 'Uhd Televisions',
     'tree' => 'All Televisions',
  )),
)

演示:https://rextester.com/IOCYY93067

这是stdClass个对象的数组,这些对象具有属性title(第一列)和tree(第二列)。

现在,您只需要将其呈现为HTML表即可。

这是一个如何呈现输出的示例:

echo '<table>';
echo '<tr>
    <th>TITLE</th>
    <th>CATEGORIE</th>
    <th>PROPERTIES</th>
    <th>POSTED?</th>
    <th><!-- edit/delete --></th>
</tr>';
foreach ($newData as $row) {
    echo '<tr>';
    echo "<td>{$row->title}</td>";
    echo "<td>{$row->tree}</td>";
    echo "<td>???</td>";
    echo "<td>???</td>";
    echo "<td>???</td>";
    echo '</tr>';
}
echo '</table>';

演示:https://rextester.com/IRF19069

答案 4 :(得分:1)

最后我用不同的技术修复了它:

在SQL部分中,我创建了一个函数:     

    BEGIN
        DECLARE res TEXT;
        CALL catpath(cat_id, res);
        RETURN res;
    END
   

然后我创建了一个过程:     

    BEGIN
        DECLARE catname VARCHAR(300);
        DECLARE temppath TEXT;
        DECLARE tempparent INT;
        SET max_sp_recursion_depth = 255;
        SELECT baslik, katID FROM categories WHERE id=cat_id INTO catname,        tempparent;
        IF tempparent IS NULL
        THEN
            SET path = catname;
        ELSE
            CALL catpath(tempparent, temppath);
            SET path = CONCAT(temppath, '/', catname);
        END IF;
    END
    

然后我叫我的php代码:

   <?php
     $sql = $vt->prepare ("SELECT id, baslik, catpath(id) AS path FROM menuler WHERE id = '$kayitListesi->id' ");
     $sql ->execute(array());
     $sqlsonuc=$sql->fetchAll(PDO::FETCH_OBJ);
        //print_r($sqlsonuc);
         foreach($sqlsonuc as $kategoriagaci){
             echo $kategoriagaci->path;
         }
     ?>

然后我得到了类别树:

Electronics/Computers/Desktop Computers/Asus Desktop I7

希望,对任何人都有帮助