dqan70724 2014-12-12 16:12
浏览 41

MODx从数据库中检索数据并排序到表中

I'm trying to set up a table with scores from the MODX database, this data has been entered in via tv.variables in the MODX admin panel.

My SQL code retrieves my desired data with:

SELECT sc.pagetitle, cv.value, t.name
FROM pphc_site_tmplvar_contentvalues cv, pphc_site_content sc, pphc_site_tmplvars t
WHERE sc.id = cv.contentid
AND cv.tmplvarid = t.id
ORDER BY cv.value * 1 DESC

My SQL retrieves the team name, range of team scores (plays, wins, losses, draws, total points) and if they won/lost/drew.

Below is a screenshot of what I'm after (with the SQL I can get at the moment)

enter image description here

My code at the moment for dealing with the SQL/loop is:

//$output = $x; //get draws, losses, played, points and wins
$id = $x; //id of current page

function sort_by_value($a, $b) {
    return $b["value"] - $a["value"];
}

$sql = "SELECT sc.pagetitle, cv.value, t.name
FROM pphc_site_tmplvar_contentvalues cv, pphc_site_content sc, pphc_site_tmplvars t
WHERE sc.id = cv.contentid
AND cv.tmplvarid = t.id
AND sc.id = $id
ORDER BY cv.value * 1 DESC";

$result = $modx->query($sql)->fetchAll();

usort($result, "sort_by_value");
//print_r($result);die();
$html = '';

foreach ($result as $row) {
    $html .= ''. $row['value'] .'<br>';
}
return $html;

At the moment all this code does is retrieve the results of each team - Great!

I can't seem to figure out how to lay out the data in table form AND include the team names

  • 写回答

1条回答 默认 最新

  • doushadu0901 2014-12-12 17:46
    关注

    Go back to your SQL & rewrite the query so that you get your results like this:

    pagetitle | wins | losses | played | draws | points 
    Bath HC   |  5   |   4    |   2    |   5   |  234  
    

    then you will be able to:

    foreach ($result as $row) {
    
        $html.= $modx->getChunk($rowTpl,$row);
    }
    
    return $html;
    

    where $rowTpl is a chunk you have defined in modx ~ something like:

    <tr>
         <td>[[+pagetitle]]</td>
         <td>[[+wins]]</td>
         <td>[[+losses]]</td>
         <td>[[+played]]</td>
         <td>[[+draws]]</td>
         <td>[[+points]]</td>
    </tr>
    

    It's just a matter of getting the query returning the data formatted in a more useful way.

    评论

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据