0

I'm not getting the correct result from my attempt on creating a table with all the different months displayed and then how many the avg weight was for that month. THIS IS WHAT I'VE GOT:

Right now i use the `WHERE CLAUSE with which month it is manually, but i want it to more dynamic if its possible?

<table align="left" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr align="left" valign="top">
    <td>MONTH</td>
    <td>Kilo</td>
<?php
global $wpdb;
$rows = $wpdb->get_results( "SELECT dato, avg(fiske_vaegt) as fiske_vaegt 
    FROM `wp_registreringer`
    WHERE MONTH(dato) = 11 AND YEAR(dato) = 2017;");

foreach ( $rows as $row ) {

    ?>
<tr>
  <td><?php echo $row->dato;?></td>
  <td><?php echo $row->fiske_vaegt;?></td>          

OUTPUTS: There is of course only one month displayed, but then the output of the month needs to be formattet to only display the month like January and so on So to sum up is it possible to create a select statement to dynamically take all records of all the month and display the correct month with the correct avg. amount of weight from my Database?

  • This doesn't seem WP specific to me if your table is custom? $wpdb essentially just executes your SQL in this scenario. – Rarst Nov 2 '17 at 14:07
  • I see your point, i'm sorry this was probably better to ask on stackoverflow – Mg10 Nov 2 '17 at 14:13
1

You could use the MONTHNAME(dato) on your select Like

$rows = $wpdb->get_results( "SELECT MONTHNAME(dato) as month_name, avg(fiske_vaegt) as fiske_vaegt 
    FROM `wp_registreringer`
    WHERE MONTH(dato) = 11 AND YEAR(dato) = 2017;");

and access it like this echo $row->month_name

  • its not displaying anything but the weight number is showing – Mg10 Nov 2 '17 at 12:33
  • This should work. Are you using Mysql? – Drupalizeme Nov 2 '17 at 12:35
  • Yes im using Mysql. Maybe i'm not echoing it the write way? cuz its just blank. – Mg10 Nov 2 '17 at 12:39
  • You probably didn't update the Name so you can access it. The Select will return the Name of the month and the Average value. So I add an alias (month_name) to be able to access this value in the foreach loop – Drupalizeme Nov 2 '17 at 13:18
  • Sry but its not posting anything in that row – Mg10 Nov 2 '17 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.