0

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";
}
}
4
  • 1
    <article>... <?php bankTotal(get_the_title());?> </article> Also read a bit about $wpdb->prepare() as you should use it to filter data. Commented May 5, 2014 at 12:18
  • You can submit the form or use ajax. Those are your only choices. Commented May 5, 2014 at 12:19
  • Please supply the answer to what enabled this to start working for you. Will give insight to future readers Commented May 5, 2014 at 12:57
  • Im changing my comment to answer, so others can fast find it. Commented May 5, 2014 at 13:29

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.