PHP : I need to fill “0” on months where there is no data available from a sql query
This seemed simple at first, but now it has become a headache.
I have a number of robberies for each month in a year, lets say 2016. My query
SELECT MONTH(denuncia.fecha_registro_denuncia) as mes, count(denuncia.codigo_incidente)
FROM denuncia
INNER JOIN incidentes ON incidentes.codigo_incidente=denuncia.codigo_incidente
WHERE YEAR(denuncia.fecha_registro_denuncia)='".$a."' AND denuncia.codigo_incidente=0
GROUP BY mes
denuncia is the same as "report to the police"
returns this:
+-------+--------+
| month | robber.|
+-------+--------+
| 1 | 2 |
| 2 | 2 |
| 3 | 3 |
| 4 | 2 |
| 5 | 1 |
| 8 | 2 |
| 9 | 3 |
| 10 | 1 |
| 11 | 5 |
| 12 | 2 |
+-------+--------+
The query is fetched as an PHP multidimensional array, using
while($rows=mysqli_fetch_array($result,MYSQLI_NUM))
$b=$rows;
Data in array format:
Array
(
[0] => Array
(
[0] => 1
[1] => 2
)
[1] => Array
(
[0] => 2
[1] => 2
)
[2] => Array
(
[0] => 3
[1] => 3
)
[3] => Array
(
[0] => 4
[1] => 2
)
[4] => Array
(
[0] => 5
[1] => 1
)
[5] => Array
(
[0] => 8
[1] => 2
)
[6] => Array
(
[0] => 9
[1] => 3
)
[7] => Array
(
[0] => 10
[1] => 1
)
[8] => Array
(
[0] => 11
[1] => 5
)
[9] => Array
(
[0] => 12
[1] => 2
)
)
As you can see, I am missing months 6 and 7 from my query result. Those months there were no incidents. As I searched through StackOverflow, filling the query with zeroes is somewhat a complex SQL query.
So, I have PHP to solve this. I need to output this data to a chart display library so I can view it nice and clean. The format for the input data would be:
[2,2,3,2,1,0,0,2,3,1,5,2]
So I need to put 0's on positions 6 and 7 to fill empty months with no incidents.
Thing is, my coding skills are poor. I tried some solutions but keep getting into a wall, been hours already.
Basically the algorythm is:
-Go through the array
-Check if month exists
-If it doesn´t exist, echo '0,";
-Go next position
-Check again if month exists
-...
Been coding for many hours and my mind can´t grasp the solution. Thanks for any help.
php mysql sql
|
show 1 more comment
This seemed simple at first, but now it has become a headache.
I have a number of robberies for each month in a year, lets say 2016. My query
SELECT MONTH(denuncia.fecha_registro_denuncia) as mes, count(denuncia.codigo_incidente)
FROM denuncia
INNER JOIN incidentes ON incidentes.codigo_incidente=denuncia.codigo_incidente
WHERE YEAR(denuncia.fecha_registro_denuncia)='".$a."' AND denuncia.codigo_incidente=0
GROUP BY mes
denuncia is the same as "report to the police"
returns this:
+-------+--------+
| month | robber.|
+-------+--------+
| 1 | 2 |
| 2 | 2 |
| 3 | 3 |
| 4 | 2 |
| 5 | 1 |
| 8 | 2 |
| 9 | 3 |
| 10 | 1 |
| 11 | 5 |
| 12 | 2 |
+-------+--------+
The query is fetched as an PHP multidimensional array, using
while($rows=mysqli_fetch_array($result,MYSQLI_NUM))
$b=$rows;
Data in array format:
Array
(
[0] => Array
(
[0] => 1
[1] => 2
)
[1] => Array
(
[0] => 2
[1] => 2
)
[2] => Array
(
[0] => 3
[1] => 3
)
[3] => Array
(
[0] => 4
[1] => 2
)
[4] => Array
(
[0] => 5
[1] => 1
)
[5] => Array
(
[0] => 8
[1] => 2
)
[6] => Array
(
[0] => 9
[1] => 3
)
[7] => Array
(
[0] => 10
[1] => 1
)
[8] => Array
(
[0] => 11
[1] => 5
)
[9] => Array
(
[0] => 12
[1] => 2
)
)
As you can see, I am missing months 6 and 7 from my query result. Those months there were no incidents. As I searched through StackOverflow, filling the query with zeroes is somewhat a complex SQL query.
So, I have PHP to solve this. I need to output this data to a chart display library so I can view it nice and clean. The format for the input data would be:
[2,2,3,2,1,0,0,2,3,1,5,2]
So I need to put 0's on positions 6 and 7 to fill empty months with no incidents.
Thing is, my coding skills are poor. I tried some solutions but keep getting into a wall, been hours already.
Basically the algorythm is:
-Go through the array
-Check if month exists
-If it doesn´t exist, echo '0,";
-Go next position
-Check again if month exists
-...
Been coding for many hours and my mind can´t grasp the solution. Thanks for any help.
php mysql sql
What are your tables structure and the query that outputs the result you have showed?
– Shidersz
Nov 15 '18 at 4:40
duplicate
– danblack
Nov 15 '18 at 4:44
You might have luck with the MySQL COALESCE function. Feed it a list and it returns the first non-null value. So feed it a list of therobber
column and 0. If robber is null, it returns zero. You do this within the MySQL query, before it returns anything to PHP
– Stephen R
Nov 15 '18 at 4:54
@danblack I checked that question but didn´t help.
– Martincho
Nov 15 '18 at 5:29
quite right, it said use PHP like you implied in the question but was light on implementation. Answer here looks sufficient.
– danblack
Nov 15 '18 at 6:20
|
show 1 more comment
This seemed simple at first, but now it has become a headache.
I have a number of robberies for each month in a year, lets say 2016. My query
SELECT MONTH(denuncia.fecha_registro_denuncia) as mes, count(denuncia.codigo_incidente)
FROM denuncia
INNER JOIN incidentes ON incidentes.codigo_incidente=denuncia.codigo_incidente
WHERE YEAR(denuncia.fecha_registro_denuncia)='".$a."' AND denuncia.codigo_incidente=0
GROUP BY mes
denuncia is the same as "report to the police"
returns this:
+-------+--------+
| month | robber.|
+-------+--------+
| 1 | 2 |
| 2 | 2 |
| 3 | 3 |
| 4 | 2 |
| 5 | 1 |
| 8 | 2 |
| 9 | 3 |
| 10 | 1 |
| 11 | 5 |
| 12 | 2 |
+-------+--------+
The query is fetched as an PHP multidimensional array, using
while($rows=mysqli_fetch_array($result,MYSQLI_NUM))
$b=$rows;
Data in array format:
Array
(
[0] => Array
(
[0] => 1
[1] => 2
)
[1] => Array
(
[0] => 2
[1] => 2
)
[2] => Array
(
[0] => 3
[1] => 3
)
[3] => Array
(
[0] => 4
[1] => 2
)
[4] => Array
(
[0] => 5
[1] => 1
)
[5] => Array
(
[0] => 8
[1] => 2
)
[6] => Array
(
[0] => 9
[1] => 3
)
[7] => Array
(
[0] => 10
[1] => 1
)
[8] => Array
(
[0] => 11
[1] => 5
)
[9] => Array
(
[0] => 12
[1] => 2
)
)
As you can see, I am missing months 6 and 7 from my query result. Those months there were no incidents. As I searched through StackOverflow, filling the query with zeroes is somewhat a complex SQL query.
So, I have PHP to solve this. I need to output this data to a chart display library so I can view it nice and clean. The format for the input data would be:
[2,2,3,2,1,0,0,2,3,1,5,2]
So I need to put 0's on positions 6 and 7 to fill empty months with no incidents.
Thing is, my coding skills are poor. I tried some solutions but keep getting into a wall, been hours already.
Basically the algorythm is:
-Go through the array
-Check if month exists
-If it doesn´t exist, echo '0,";
-Go next position
-Check again if month exists
-...
Been coding for many hours and my mind can´t grasp the solution. Thanks for any help.
php mysql sql
This seemed simple at first, but now it has become a headache.
I have a number of robberies for each month in a year, lets say 2016. My query
SELECT MONTH(denuncia.fecha_registro_denuncia) as mes, count(denuncia.codigo_incidente)
FROM denuncia
INNER JOIN incidentes ON incidentes.codigo_incidente=denuncia.codigo_incidente
WHERE YEAR(denuncia.fecha_registro_denuncia)='".$a."' AND denuncia.codigo_incidente=0
GROUP BY mes
denuncia is the same as "report to the police"
returns this:
+-------+--------+
| month | robber.|
+-------+--------+
| 1 | 2 |
| 2 | 2 |
| 3 | 3 |
| 4 | 2 |
| 5 | 1 |
| 8 | 2 |
| 9 | 3 |
| 10 | 1 |
| 11 | 5 |
| 12 | 2 |
+-------+--------+
The query is fetched as an PHP multidimensional array, using
while($rows=mysqli_fetch_array($result,MYSQLI_NUM))
$b=$rows;
Data in array format:
Array
(
[0] => Array
(
[0] => 1
[1] => 2
)
[1] => Array
(
[0] => 2
[1] => 2
)
[2] => Array
(
[0] => 3
[1] => 3
)
[3] => Array
(
[0] => 4
[1] => 2
)
[4] => Array
(
[0] => 5
[1] => 1
)
[5] => Array
(
[0] => 8
[1] => 2
)
[6] => Array
(
[0] => 9
[1] => 3
)
[7] => Array
(
[0] => 10
[1] => 1
)
[8] => Array
(
[0] => 11
[1] => 5
)
[9] => Array
(
[0] => 12
[1] => 2
)
)
As you can see, I am missing months 6 and 7 from my query result. Those months there were no incidents. As I searched through StackOverflow, filling the query with zeroes is somewhat a complex SQL query.
So, I have PHP to solve this. I need to output this data to a chart display library so I can view it nice and clean. The format for the input data would be:
[2,2,3,2,1,0,0,2,3,1,5,2]
So I need to put 0's on positions 6 and 7 to fill empty months with no incidents.
Thing is, my coding skills are poor. I tried some solutions but keep getting into a wall, been hours already.
Basically the algorythm is:
-Go through the array
-Check if month exists
-If it doesn´t exist, echo '0,";
-Go next position
-Check again if month exists
-...
Been coding for many hours and my mind can´t grasp the solution. Thanks for any help.
php mysql sql
php mysql sql
edited Nov 16 '18 at 20:04
Martincho
asked Nov 15 '18 at 4:35
MartinchoMartincho
188
188
What are your tables structure and the query that outputs the result you have showed?
– Shidersz
Nov 15 '18 at 4:40
duplicate
– danblack
Nov 15 '18 at 4:44
You might have luck with the MySQL COALESCE function. Feed it a list and it returns the first non-null value. So feed it a list of therobber
column and 0. If robber is null, it returns zero. You do this within the MySQL query, before it returns anything to PHP
– Stephen R
Nov 15 '18 at 4:54
@danblack I checked that question but didn´t help.
– Martincho
Nov 15 '18 at 5:29
quite right, it said use PHP like you implied in the question but was light on implementation. Answer here looks sufficient.
– danblack
Nov 15 '18 at 6:20
|
show 1 more comment
What are your tables structure and the query that outputs the result you have showed?
– Shidersz
Nov 15 '18 at 4:40
duplicate
– danblack
Nov 15 '18 at 4:44
You might have luck with the MySQL COALESCE function. Feed it a list and it returns the first non-null value. So feed it a list of therobber
column and 0. If robber is null, it returns zero. You do this within the MySQL query, before it returns anything to PHP
– Stephen R
Nov 15 '18 at 4:54
@danblack I checked that question but didn´t help.
– Martincho
Nov 15 '18 at 5:29
quite right, it said use PHP like you implied in the question but was light on implementation. Answer here looks sufficient.
– danblack
Nov 15 '18 at 6:20
What are your tables structure and the query that outputs the result you have showed?
– Shidersz
Nov 15 '18 at 4:40
What are your tables structure and the query that outputs the result you have showed?
– Shidersz
Nov 15 '18 at 4:40
duplicate
– danblack
Nov 15 '18 at 4:44
duplicate
– danblack
Nov 15 '18 at 4:44
You might have luck with the MySQL COALESCE function. Feed it a list and it returns the first non-null value. So feed it a list of the
robber
column and 0. If robber is null, it returns zero. You do this within the MySQL query, before it returns anything to PHP– Stephen R
Nov 15 '18 at 4:54
You might have luck with the MySQL COALESCE function. Feed it a list and it returns the first non-null value. So feed it a list of the
robber
column and 0. If robber is null, it returns zero. You do this within the MySQL query, before it returns anything to PHP– Stephen R
Nov 15 '18 at 4:54
@danblack I checked that question but didn´t help.
– Martincho
Nov 15 '18 at 5:29
@danblack I checked that question but didn´t help.
– Martincho
Nov 15 '18 at 5:29
quite right, it said use PHP like you implied in the question but was light on implementation. Answer here looks sufficient.
– danblack
Nov 15 '18 at 6:20
quite right, it said use PHP like you implied in the question but was light on implementation. Answer here looks sufficient.
– danblack
Nov 15 '18 at 6:20
|
show 1 more comment
2 Answers
2
active
oldest
votes
one way you can do is you can pree fill month array with zero and set value
$month = array_fill_keys(range(1, 12),0); //fill month array key with month(1-12)
while($rows=mysqli_fetch_array($result,MYSQLI_NUM))
$month [$rows[0]]= $rows[1]; //check if exists set value
Nice! Now, should you use $month in the while loop?
– Martincho
Nov 15 '18 at 4:57
add a comment |
Found solution. I had to use a helper variable ($indexhelp)
$indexhelp=0;
for($month_counter=1;$month_counter<=12;$month_counter++)
if($month_counter==$query5[$indexhelp][0])
echo $query5[$indexhelp][1].',';
$indexhelp++;
else echo '0,';
The result is added to a variable like
//this is javascript
chart_data=[<?php CODE HERE ?>];
Also, a friend told me that most logic is convenient to use in client-side, because a complex query can overload the database server.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312505%2fphp-i-need-to-fill-0-on-months-where-there-is-no-data-available-from-a-sql-q%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
one way you can do is you can pree fill month array with zero and set value
$month = array_fill_keys(range(1, 12),0); //fill month array key with month(1-12)
while($rows=mysqli_fetch_array($result,MYSQLI_NUM))
$month [$rows[0]]= $rows[1]; //check if exists set value
Nice! Now, should you use $month in the while loop?
– Martincho
Nov 15 '18 at 4:57
add a comment |
one way you can do is you can pree fill month array with zero and set value
$month = array_fill_keys(range(1, 12),0); //fill month array key with month(1-12)
while($rows=mysqli_fetch_array($result,MYSQLI_NUM))
$month [$rows[0]]= $rows[1]; //check if exists set value
Nice! Now, should you use $month in the while loop?
– Martincho
Nov 15 '18 at 4:57
add a comment |
one way you can do is you can pree fill month array with zero and set value
$month = array_fill_keys(range(1, 12),0); //fill month array key with month(1-12)
while($rows=mysqli_fetch_array($result,MYSQLI_NUM))
$month [$rows[0]]= $rows[1]; //check if exists set value
one way you can do is you can pree fill month array with zero and set value
$month = array_fill_keys(range(1, 12),0); //fill month array key with month(1-12)
while($rows=mysqli_fetch_array($result,MYSQLI_NUM))
$month [$rows[0]]= $rows[1]; //check if exists set value
edited Nov 15 '18 at 4:58
answered Nov 15 '18 at 4:50
suresh bambhaniyasuresh bambhaniya
945415
945415
Nice! Now, should you use $month in the while loop?
– Martincho
Nov 15 '18 at 4:57
add a comment |
Nice! Now, should you use $month in the while loop?
– Martincho
Nov 15 '18 at 4:57
Nice! Now, should you use $month in the while loop?
– Martincho
Nov 15 '18 at 4:57
Nice! Now, should you use $month in the while loop?
– Martincho
Nov 15 '18 at 4:57
add a comment |
Found solution. I had to use a helper variable ($indexhelp)
$indexhelp=0;
for($month_counter=1;$month_counter<=12;$month_counter++)
if($month_counter==$query5[$indexhelp][0])
echo $query5[$indexhelp][1].',';
$indexhelp++;
else echo '0,';
The result is added to a variable like
//this is javascript
chart_data=[<?php CODE HERE ?>];
Also, a friend told me that most logic is convenient to use in client-side, because a complex query can overload the database server.
add a comment |
Found solution. I had to use a helper variable ($indexhelp)
$indexhelp=0;
for($month_counter=1;$month_counter<=12;$month_counter++)
if($month_counter==$query5[$indexhelp][0])
echo $query5[$indexhelp][1].',';
$indexhelp++;
else echo '0,';
The result is added to a variable like
//this is javascript
chart_data=[<?php CODE HERE ?>];
Also, a friend told me that most logic is convenient to use in client-side, because a complex query can overload the database server.
add a comment |
Found solution. I had to use a helper variable ($indexhelp)
$indexhelp=0;
for($month_counter=1;$month_counter<=12;$month_counter++)
if($month_counter==$query5[$indexhelp][0])
echo $query5[$indexhelp][1].',';
$indexhelp++;
else echo '0,';
The result is added to a variable like
//this is javascript
chart_data=[<?php CODE HERE ?>];
Also, a friend told me that most logic is convenient to use in client-side, because a complex query can overload the database server.
Found solution. I had to use a helper variable ($indexhelp)
$indexhelp=0;
for($month_counter=1;$month_counter<=12;$month_counter++)
if($month_counter==$query5[$indexhelp][0])
echo $query5[$indexhelp][1].',';
$indexhelp++;
else echo '0,';
The result is added to a variable like
//this is javascript
chart_data=[<?php CODE HERE ?>];
Also, a friend told me that most logic is convenient to use in client-side, because a complex query can overload the database server.
answered Nov 16 '18 at 20:09
MartinchoMartincho
188
188
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312505%2fphp-i-need-to-fill-0-on-months-where-there-is-no-data-available-from-a-sql-q%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What are your tables structure and the query that outputs the result you have showed?
– Shidersz
Nov 15 '18 at 4:40
duplicate
– danblack
Nov 15 '18 at 4:44
You might have luck with the MySQL COALESCE function. Feed it a list and it returns the first non-null value. So feed it a list of the
robber
column and 0. If robber is null, it returns zero. You do this within the MySQL query, before it returns anything to PHP– Stephen R
Nov 15 '18 at 4:54
@danblack I checked that question but didn´t help.
– Martincho
Nov 15 '18 at 5:29
quite right, it said use PHP like you implied in the question but was light on implementation. Answer here looks sufficient.
– danblack
Nov 15 '18 at 6:20