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 python变量和列表之间的相互影响
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)