dongyong6428 2014-05-05 12:15
浏览 41
已采纳

MySQL选择PHP数据而不将其发布到Wordpress

I'm using Wordpress(last)

I try to Echo SUM(feed) from SQL table in the archive loop where all posts of specific category shown

<section category="category1">
...
<?php get_template_part( 'entry_category' ); ?>
<?php endwhile; endif; ?>
</section>

In my entry_category i've got basic HTML tags to output an article and my function to echo SUM(feed)

<article>...
<?php bankTotal(the_title);?>
</article>

And here is the function in my function.php

function bankTotal($bank_name){
global $wpdb;
$result = $wpdb->get_results($wpdb->prepare("SELECT SUM(feed) FROM feeds_table WHERE bank_name='$bank_name'"));
print_r($result);

I understand that I Need to $_POST $bank_name to a function. But is there a way to do so WITHOUT $_POST ? Something like bankTotal($bank_name)

I'l need to show SUM(feed) for each <Article>...</article> of a <section>

Is there a way to pass the $bank_name to a function without using ajax($_POST['bank_name'];)?

SQL table looks like this:

bank_name | segment_name | feed |
---------------------------------
bank-A    | segmentB     | 12   |
bank-A    | segmentC     |  2   |
.................................
bank-X    | segmentA     | 32   |

***SOLVED(thanks to Seti) UPD: If you are trying to ECHO out SUM(row) do it like this

function bankTotal($bank_name){
global $wpdb;
$result = $wpdb->get_results($wpdb->prepare("SELECT SUM(feed) AS someTotal FROM feeds_table WHERE bank_name='$bank_name'", ARRAY_N));
foreach($result as $row){
echo "$row->someTotal";
}
}
  • 写回答

1条回答 默认 最新

  • douwen9343 2014-05-05 13:31
    关注

    Change your code part to

    <article>
        ...
        <?php bankTotal(get_the_title());?>
    </article>
    

    get_the_title() returns the title, while you used the_title() and that was printing title therefore not giving anything to the function;

    Also read a bit about $wpdb->prepare() as you should use it to filter data

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数据库数据成问号了,前台查询正常,数据库查询是?号
  • ¥15 算法使用了tf-idf,用手肘图确定k值确定不了,第四轮廓系数又太小才有0.006088746097507285,如何解决?(相关搜索:数据处理)
  • ¥15 彩灯控制电路,会的加我QQ1482956179
  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)
  • ¥15 如何解决MIPS计算是否溢出
  • ¥15 vue中我代理了iframe,iframe却走的是路由,没有显示该显示的网站,这个该如何处理
  • ¥15 操作系统相关算法中while();的含义
  • ¥15 CNVcaller安装后无法找到文件
  • ¥15 visual studio2022中文乱码无法解决