【问题标题】:Modx Revolution Custom Page TitleModx 革命自定义页面标题
【发布时间】:2013-01-02 14:18:37
【问题描述】:

我正在尝试构建一个 sn-p,它将显示我的投资组合的自定义页面标题。我遇到的问题是我的代码只返回“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 &raquo; Portfolio &raquo; ' . $row['name'];
    }else{ //It's showing this one on the page, yet the same query in MySQL returns `name`
        $ret = 'o7th Web Design &raquo; Portfolio &raquo; Our Portfolio' . $qry;
    }
    unset($row);        
}else{
    $ret = 'o7th Web Design &raquo; Portfolio &raquo; Our Portfolio' . $qry;
}

// Return everything
echo $ret;
?>

【问题讨论】:

  • 你有var_dump($result) 吗?如果它返回 0 或 FALSE,那么你得到了最后一个 else,它是你评论的 else 的副本。
  • 这似乎是一个页面标题的很多代码,你能更具体地描述你正在寻找的结果吗?
  • @phpisuber01 - var_dump($row) 返回 (bool)False。我还没有完成 $result ,但我认为它会是一些东西,因为它已经过了那个点。过一会儿我会告诉你的
  • @mmcglynn 这不是典型的页面标题。我为我的投资组合建立了一个自定义“画廊”,所以当你在我的列表页面上并单击一个项目的链接时,它会转到一个虚假的“页面”,它会提取项目的详细信息。在这个“页面”上,我想按照上面代码中的格式显示标题
  • @phpisuber01 - var_dump($result) 显示 object(PDOStatement)#26 (1) { ["queryString"]=&gt; string(109) "SELECT name` FROM modx_gallery_items WHERE REPLACE(LOWER(name), ' ', '-') = 'accu-time-systems ';" }`

标签: php modx modx-revolution


【解决方案1】:

发现问题。

$row = $result-&gt;fetch(PDO::FETCH_ASSOC);

需要改成:

$row = $result-&gt;fetchAll(PDO::FETCH_ASSOC);

然后我可以循环返回的数组,或者按索引返回项目:$row[0]['name']

【讨论】:

    【解决方案2】:

    你绝对不知道如何在 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 &raquo; Portfolio &raquo; ' . $item->get('name');
    }
    else {
        $output = 'o7th Web Design &raquo; Portfolio &raquo; Our Portfolio' . ' empty';
    }
    
    return $output;
    

    【讨论】:

    • "你绝对不知道如何在 modx 革命中进行查询。"其实我很感谢你,我发布的代码比你发布的开销要少得多
    • 最重要的是,我的画廊不是 modx 对象,它只是 modx 数据库中的一个表供我使用
    • the code I posted is much less overhead than what you have posted - 您编写的代码并没有利用提供缓存 xpdo 的巨大机会,因此您的代码将花费更多资源
    • 如果您实际使用的是 Gallery extra,那么我会选择 Vasis 发布的内容,因为“包”已经存在。但这并不是说使用普通的 PDO 方法是错误的,因为 xPDO 基本上是它的扩展。根据可用的时间和预算,有时需要在“推荐”方式或更快的方式之间进行选择,但它们都达到了相同的结果。保持友好的家伙...
    • 另外,虽然 Vasis 的方法确实受益于内置缓存,但如果您调用缓存的 sn-p,那么在第一次加载之后,性能将完全没有差异。
    【解决方案3】:

    这是问题吗?

    $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”而不是它的实际值

    【讨论】:

    • php 是一种模板化的脚本语言。使用“”使其字面化(我认为这就是它的名称),这意味着查询是正确的
    • 不,伙计 - 你错了,你的 php 变量没有被解释。再看一遍并检查你的语法。
    • 不,伙计,实际上,我没有错,它正在被解释。如果您阅读该问题下的第 5 条评论,您会发现它完全按照我的意愿行事,并在正确的时间向我返回正确的查询。
    • 感谢您证明我的观点...我引用The most important feature of double-quoted strings is the fact that variable names will be expanded. See string parsing for details.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多