查询Join表按menuindex排序

问题描述 投票:0回答:1

这是我的询问:

$results = $modx->query("SELECT contentid FROM modx_site_tmplvar_contentvalues WHERE tmplvarid=107");

我需要按菜单索引对其进行排序,所以我尝试了这个:

$results = $modx->query("SELECT contentid FROM modx_site_tmplvar_contentvalues JOIN modResource WHERE tmplvarid=107 ORDER BY menuindex DESC");

我对此一点也不熟悉。当然这是行不通的。有人告诉我使用 XPDO 但我对此一无所知。我错过了什么吗?我应该做什么才能让它发挥作用?

这是完整的代码:

<?php
$results = $modx->query("SELECT contentid FROM modx_site_tmplvar_contentvalues WHERE tmplvarid=107");

// description tv: 108
// vignette tv: 121

if (!is_object($results)) {
  return;
}
$breves = array();


while($r = $results->fetch(PDO::FETCH_COLUMN, 0)){
  $breves[] = $r;
}

$queryIn = implode(',' , $breves);

$results_2 = $modx->query("SELECT contentid, tmplvarid, value FROM modx_site_tmplvar_contentvalues WHERE contentid IN ({$queryIn}) AND tmplvarid IN (108, 121)");

if (!is_object($results_2)) {
  return;
}
$tvValues = array();

while($v = $results_2->fetch(PDO::FETCH_ASSOC)){
  if($v['tmplvarid'] == 108){
    $tvValues[$v['contentid']]['desc'] = $v['value'];
  } else {
    $tvValues[$v['contentid']]['vignette'] = $v['value'];
  }
}

$output = "";

foreach ($breves as $res_id) {
  $page = $modx->getObject('modResource', $res_id);
  $alias = $page->get('alias');
  $id = $page->get('id');

  $page_title = $page->get('pagetitle');
  $description = $tvValues[$res_id]['desc'];
  if(!$description || $description == ""){
    $description = $page->get('content');
  }
  $description = strip_tags($description);
  $description = mb_strimwidth($description, 0, 150, "...");
  $vignette = $tvValues[$res_id]['vignette'];
  if($vignette){
    $vignette = "/assets/upload/pln/" . $vignette;
  } else {
    $vignette = "https://www.commune-ploudaniel.fr/assets/templates/pln/default-b7824fcd998f51baf0f0af359a72e760.png";
  }

$output .= <<<HTML
  <div class="xpro-slider-item">
    <div class="xp-news-classic-block">
        <div class="xp-hover-image">
          <a href="{$alias}.html" ><div class="xp-overlay"></div></a>
            <a class="xp-view-lightbox" href="{$alias}.html" ><i class="fa fa-plus"></i></a>
            <img src="{$vignette}" alt="{$alias}" style="width:375px; height:270px; object-fit: cover;"/>
    </div>
        <div class="xp-news-detail">
          <h4><a href="{$alias}.html" data-display="lightbox">{$id}{$page_title}</a></h4>
          <p>{$description}</p>
          <div class='xp-news-footer'></div>
        </div>
    </div>
  </div>
HTML;

$output .= PHP_EOL;
}
return $output;
php mysql sql pdo modx
1个回答
0
投票

尝试一下 MODX:

$q = $modx->newQuery('modTemplateVarResource');
$q->leftJoin('modResource', 'mr', 'mr.id = modTemplateVarResource.contentid');
$q->where(array(
    'modTemplateVarResource.tmplvarid' => 107
));
$q->select(array(
   'modTemplateVarResource.contentid'
));
$q->sortby('modResource.menuindex', 'DESC');
$q->prepare();
$q->stmt->execute();
$result = $q->stmt->fetch(PDO::FETCH_ASSOC);
print_r($result);
© www.soinside.com 2019 - 2024. All rights reserved.