我正在尝试构建一个片段,该片段将显示我的投资组合的自定义页面标题。我遇到的问题是我的代码只返回'else',但是当我在MySql中运行查询时,我得到name
。
我做错了什么?
<?php
// Show All Errors
error_reporting(E_ALL);
ini_set('display_errors', '1');
$getID = $modx->quote($getID);
$ret = '';
$qry = "SELECT `name` FROM `modx_gallery_items` WHERE REPLACE(LOWER(`name`), ' ', '-') = $getID;";
$result = $modx->query($qry);
if ($result) {
$row = $result->fetch(PDO::FETCH_ASSOC);
if($row){
$ret = 'o7th Web Design » Portfolio » ' . $row['name'];
}else{ //It's showing this one on the page, yet the same query in MySQL returns `name`
$ret = 'o7th Web Design » Portfolio » Our Portfolio' . $qry;
}
unset($row);
}else{
$ret = 'o7th Web Design » Portfolio » Our Portfolio' . $qry;
}
// Return everything
echo $ret;
?>
答案 0 :(得分:2)
发现了这个问题。
$row = $result->fetch(PDO::FETCH_ASSOC);
需要改为:
$row = $result->fetchAll(PDO::FETCH_ASSOC);
然后我可以循环返回的数组,或者按索引返回项目:$row[0]['name']
答案 1 :(得分:0)
你绝对不知道如何在modx革命中进行查询。请阅读http://bobsguides.com/revolution-objects.html。这段代码的正确示例如下所示 -
// add class to work with gallery extra
$gallery = $modx->getService('gallery','Gallery',$modx->getOption('gallery.core_path',null,$modx->getOption('core_path').'components/gallery/').'model/gallery/',$scriptProperties);
if (!($gallery instanceof Gallery)) return '';
// get id of album
$getID = (int) $modx->getOption('getID',$scriptProperties,false);
if (empty($getID)) return 'no id';
// make query
$c = $modx->newQuery('galAlbum')
$c->where(array(
'id' => $getID,
));
$item = $modx->getObject('galAlbum',$c);
// get result
if (!empty($item)) {
$output = 'o7th Web Design » Portfolio » ' . $item->get('name');
}
else {
$output = 'o7th Web Design » Portfolio » Our Portfolio' . ' empty';
}
return $output;
答案 2 :(得分:-1)
这是问题吗?
$qry = "SELECT `name` FROM `modx_gallery_items` WHERE REPLACE(LOWER(`name`), ' ', '-') = $getID;";
应该是:
$qry = "SELECT `name` FROM `modx_gallery_items` WHERE REPLACE(LOWER(`name`), ' ', '-') = ".$getID.";";
看起来你的查询会搜索'$ getID'而不是它的实际值