1
votes

I am attempting to build a snippet which will display a custom page title for my portfolio. The issue that I am having is my code only returns the 'else', yet when I run the query in MySql I am getting the name.

What am I doing wrong?

<?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;
?>
3
Have you var_dump($result) ? If its returning 0 or FALSE, then your getting the last else, which is a duplicate of the else you commented. - phpisuber01
This seems like a lot of code for a page title, Can you describe more specifically the result you are looking for? - mmcglynn
@phpisuber01 - var_dump($row) is returning (bool)False. I have not done $result as of yet, but I assume that it will be something since it gets past that point. I'll let you know in a few - Kevin
@mmcglynn this is not a typical page title. I have built a custom 'gallery' for my portfolio, so when you are on my list page and click a link to an item, it goes to a bogus "page", which pulls the items details. On this "page" I want to display the title as formatted in the code above - Kevin
@phpisuber01 - var_dump($result) shows object(PDOStatement)#26 (1) { ["queryString"]=> string(109) "SELECT name` FROM modx_gallery_items WHERE REPLACE(LOWER(name), ' ', '-') = 'accu-time-systems';" }` - Kevin

3 Answers

2
votes

Found the issue.

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

Needs to change to:

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

And then I can either loop the returned array, or return the item by index: $row[0]['name']

0
votes

You absolutely do not know how to make query in modx revolution. Please read http://bobsguides.com/revolution-objects.html . The correct example of this code looks like this -

// 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;
-1
votes

is this the issue?

$qry = "SELECT `name` FROM `modx_gallery_items` WHERE REPLACE(LOWER(`name`), ' ', '-') = $getID;";

should be:

$qry = "SELECT `name` FROM `modx_gallery_items` WHERE REPLACE(LOWER(`name`), ' ', '-') = ".$getID.";";

looks like your query would be searching for '$getID' rather than it's actual value