朋友我尝试mysql左侧加入查询在PHP方面,我需要一点帮助。我不能查询为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>
FETCH_OBJ
标志将每行作为php对象返回。所以你的代码应该是这样的。
echo '<table>';
echo '<tr><th>lev1</th><th>lev2</th><th>lev3</th><th>lev4</th></tr>';
foreach ($categoriesResult as $titles) {
echo "<tr><td>{$titles->lev1}</td><td>{$titles->lev2}</td><td>{$titles->lev3}</td><td>{$titles->lev4}</td></tr>";
}
echo '</table>';
这应该显示你的结果。
$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',
)),
)
但是:Kua zxsw指出
这是一个https://rextester.com/IOCYY93067对象的数组,其属性为stdClass
(第一列)和title
(第二列)。
现在您只需要将其呈现为HTML表格。
以下是如何呈现输出的示例:
tree
但是:Kua zxsw指出
最后我用不同的技术修复它:
在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代码:
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>';
然后我得到我的类别树:
Electronics/Computers/Desktop Computers/Asus Desktop I7
希望,对任何人都有帮助
https://rextester.com/IRF19069
这是您应该能够访问您的值的方式。您当然可以将HTML放入echo语句中以格式化结果。
<?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;
}
?>