1

How can I write a value into correct month which is located in an html table? Sorry for my bad English.

$mesi = array("January", "February", "March", "April", "May", "June",
               "July", "August", "September", "October", "November", "December");

$array = $class->startEndDate('12'); // chiave arrays
echo '<table class="table table-bordered">';
echo '<thead>';
echo '<th class="text-center">Country</th>';
foreach ($mesi as $mese) {
    echo '<th class="text-center">' . $mese . '</th>';
}
echo '</thead>';
echo '<tbody>';
echo '<tr>';
$query = "SELECT * FROM countries";
$result = mysql_query($query);

while ($row = mysql_fetch_array($result)) {
    echo '<td class = "text-center">' . $row['country_name'] . '</td>';

    $sql = "select sum(quantity), country_id, 
                   concat(year(date), '_', month(date)) as meseAnno 
            from subscription 
            where country_id = " . $row['country_id'] . 
            " group by meseAnno";

    $res = mysql_query($sql);
    while ($rows = mysql_fetch_assoc($res)) {
        //the code here!
    }
    echo '</tr>';
}
echo '</tbody>';
echo '</table>';
?>

With the second query I retrieve the subscription for country with id country_id.

Result of second query:

enter image description here

The column meseAnno of the first row says 2015_4, so April, but the value 7 is inserted in January. Why?

Thanks in advanced.

enter image description here

  • 3
    Please note that ´mysql´ functions are deprecated: http://php.net/manual/de/migration55.deprecated.php – Tanuel Mategi Mar 14 '16 at 15:17
  • 1
    What your expected output? – Mawia HL Mar 14 '16 at 15:21
  • 3
    I've been starting at this question for 5 mins. and I still don't know what it's about. *scusi, ma non capito la vostra questione*. – Funk Forty Niner Mar 14 '16 at 15:23
  • the output is the value of the sum(quantity) column – Giuseppe Giubaldo Mar 14 '16 at 15:26
  • 1
    Hey Giuseppe, sorry. We're a little confused - what does the value 7 represent and where do you have January? – dmgig Mar 14 '16 at 15:26
  • 1
    What do you mean by _“The value, 7, is inserted in Jan.”?_ The column `sum(qty)` contains the _sum_ of all quantities for a given year/month combination. Are you aware of that? If your image shows the complete output of the 2nd query, then there's data only for the 3 months shown. – PerlDuck Mar 14 '16 at 15:27
  • I'm guessing he wants the output from `meseAnno` (monthYear?) to be in the English language name (or perhaps the correct localised name for the given country) for the given month as listed in the array `$mesi` ... it would be much better if `meseAnno` was a `Datetime` field of course. – CD001 Mar 14 '16 at 15:28
  • the value 7 represent the number of subscription in april – Giuseppe Giubaldo Mar 14 '16 at 15:29
  • @CD001 He coded `concat(year(date), '_', month(date)) as meseAnno` so I guess it already _is_ a date/datetime/timestamp field. I guess he's wondering where his 7 he inserted for Jan went. – PerlDuck Mar 14 '16 at 15:30
  • yes, meseAnno is monthYear and is a dateTime field – Giuseppe Giubaldo Mar 14 '16 at 15:32
  • @PerlDog - ah yeah, I see it now... – CD001 Mar 14 '16 at 15:32
  • @GiuseppeGiubaldo, you could clear things up if you would show the output of `select quantity, concat(year(date), '_', month(date)) from subscription where country_id=1 order by 2` (without the `sum` and `group by`). Is that possible? – PerlDuck Mar 14 '16 at 15:33
  • Well I would start by **not** concatenating _Year_month_ and outputting them as 2 seperate fields. If you want them shown as concatenated, do that in PHP once you decide where to put the data in your table. But then you have a `Year` and a `Month` column in your result you can easily test with an IF – RiggsFolly Mar 14 '16 at 15:33
  • @RiggsFolly True, but the `concat` is not very error prone and I assume there's simply no data for Janury. _That_ is what I'd like to see. – PerlDuck Mar 14 '16 at 15:36
  • In my opinion __//the code here!__ indicates no attempt to solve the problem. That means this is a __code it for me__ question and therefore off topic – RiggsFolly Mar 14 '16 at 15:36
  • 2
    @PerlDog Yea, but in the **//the code here** he will have to test the _Month_ in order to decide which cell to put the output he has generated. So having a `month` column would be a great advantage over un_concateneating it before testing it – RiggsFolly Mar 14 '16 at 15:37
  • no no, I try to resolve many times but i post without code – Giuseppe Giubaldo Mar 14 '16 at 15:39
  • @RiggsFolly I don't believe he wants code for free. He could execute the SQL in phpadmin or whatever. – PerlDuck Mar 14 '16 at 15:40
  • for example I had controlled the month with a if statement like this: if($array[$a] == $i) {echo '$row[sum(quantinty)]'} – Giuseppe Giubaldo Mar 14 '16 at 15:41
  • after increment $i and scroll array completly – Giuseppe Giubaldo Mar 14 '16 at 15:44
  • @GiuseppeGiubaldo, If you're sure that query results is true, maybe you have some mistakes at the part of code that visualize data... As I can see you have 7 subs in March, 7 subs in April and 9 subs in August, but your first line in the table filled from the first cell of the row and so on.. actually it is not corresponding with query results. – Wizard Mar 14 '16 at 15:46
  • yes, this is the problem – Giuseppe Giubaldo Mar 14 '16 at 15:47
  • @GiuseppeGiubaldo , above in the comments you wrote `if($array[$a] == $i) {echo '$row[sum(quantinty)]'}`, but here must be `else`-branch too, that adds empty table-cell. Is that code presents? – Wizard Mar 14 '16 at 15:49
  • if i retrieve the value of meseAnno column, i control this. In particular i control the month with date('n') – Giuseppe Giubaldo Mar 14 '16 at 15:50
  • that code was presents in a previous attempt but it's incorrect – Giuseppe Giubaldo Mar 14 '16 at 15:51
  • i try also with the explode function. so i can retrieve the month and make if control – Giuseppe Giubaldo Mar 14 '16 at 15:51
  • It's seems we're playing in `solve the puzzle telepathically` :) .. show your code. – Wizard Mar 14 '16 at 15:52
  • $i = 1; $query = "SELECT * FROM countries"; $result = mysql_query($query); – Giuseppe Giubaldo Mar 14 '16 at 15:54
  • while ($row = mysql_fetch_array($result)) { echo '' . $row['country_name'] . ''; $sql = "select sum(quantity), country_id, concat(year(date), '_', month(date)) as meseAnno from subscription where country_id = " . $row['country_id'] . " group by meseAnno"; $res = mysql_query($sql); – Giuseppe Giubaldo Mar 14 '16 at 15:55
  • while ($rows = mysql_fetch_assoc($res)) { $a = explode("_", $rows['meseAnno']); if ($a[1] == $i) { echo '' . $rows['sum(quantity)'] . ''; } else { echo '0'; } $i++; } echo ''; } – Giuseppe Giubaldo Mar 14 '16 at 15:55
  • @GiuseppeGiubaldo , fusion3k answer resolves your problem ) .. your results from database query doesn't have all the 12 month statistics.. for example, IT has 3 month with subs activity, result will have 3 rows, you have to iterate only 3 month .. but you attempt to fill all of 12 table-cells only in 3 loops, ofc you can do that, but you need to sort by `meseAnno` and analyze the current processing date to add properly count of empty table-cells – Wizard Mar 14 '16 at 16:07

2 Answers2

2

The problem is that months are 12, but your query result can be have less than 12 rows. You can change the SQL query to obtain a coherent result, or — without modify the query — try with something like this:

(...)
$res = mysql_query( $sql );

$tableLine = array_fill( 1, 12, '<td>&nbsp;</td>' );
while( $row = mysql_fetch_assoc( $res ) )
{
    $month = preg_replace( '/^\d+_(\d+)$/', '\1', $row['meseAnno'] );
    $tableLine[ $month ] = "<td>{ YOUR MONTH VALUE HERE }</td>";
}
echo implode( '', $tableLine );

echo '</tr>';
(...)

First, you fill an array with 12 empty <td>, then — inside your while() loop — replace only matched months. At the end, you echo imploded array.

fusion3k
  • 11,568
  • 4
  • 25
  • 47
  • And if you take my suggestion of SELECTING the Year and Month you dont even need the complexity of a `preg_replace` as you get given the index to your table row array from the query – RiggsFolly Mar 14 '16 at 15:55
  • @RiggsFolly Totally agree. (I upvoted yr comment before). This is a short dirty solution. Best way is to rewrite mySQL query... also for some alias... – fusion3k Mar 14 '16 at 15:58
  • _Dirty_?? I think its quite elegant actually, it got my upvote – RiggsFolly Mar 14 '16 at 15:58
  • 1
    I *think* you might still want to select the month and year separately, order by year and add a new row when it changes (or something); there seems to be data in the list for both 2015 and 2016 ... so, in this case, wouldn't the data for March 2016 overwrite the data for March 2015? – CD001 Mar 14 '16 at 16:05
  • awesome, it's work. thanks fusion3k and thanks guys. – Giuseppe Giubaldo Mar 14 '16 at 16:11
0

Using Fusion3k's answer and applying the idea that if you SELECT the Year and Month from the database as individual columns you will actualy have the index to his tableLine array given to you in an easily usable way, you could do this.

$sql = "select sum(quantity), country_id, 
               YEAR(`date`) as yr, MONTH(`date`) as mth,
               concat(year(date), '_', month(date)) as meseAnno 
        from subscription 
        where country_id = {$row['country_id']} 
        group by meseAnno 
        ORDER BY mth";

$res = mysql_query( $sql );

$tableLine = array_fill( 1, 12, '<td>&nbsp;</td>' );

while( $row = mysql_fetch_assoc( $res ) ) {
    $t = sprintf('%s_%s', $row['yr'], $row['mth']);
    $tableLine[ $row['mth'] ] = "<td>$t</td>";
}

echo implode( '', $tableLine );

echo '</tr>';

This would work assuming you added a yr = 2016 to the search criteria. If you want this report across multiple years there is a little more to do.

And of course I have to add :

Please dont use the mysql_ database extension, it is deprecated (gone for ever in PHP7) Especially if you are just learning PHP, spend your energies learning the PDO or mysqli_ database extensions, and here is some help to decide which to use

Community
  • 1
  • 1
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149