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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘