Jump to content

[SOLVED] display zero in sql requete with php


prince198

Recommended Posts

hello

i have little probleme . i use zend but my probleme is in sql and not in zend

 

this is my script

 

 

Code:
sql      ::::                select sum (total) as sum group by week(date,1) order by (date)

<?php foreach($this->calcul as $calcul) : 


echo 'week: ' .$this->escape($calcul->week)

?>

so for example  i have resulu like this

 

week1: 222
week4 :222

my questio how i add in mysql or with boucle for in php to display 0 in week that i don't have result to be like this

week 1 :222
week 2 :0
wek   3 :0
week 4: 222

 

thanks for help

Link to comment
Share on other sites

thank you  for answering me

 

but for example i have table :

 

date              \    total

2008-01-01      \    2

2008-01-02    \    2

2008-01-15        \  5

 

so if i put this sql requete

select sum (total) as sum group by week(date,1) order by (date)

  so output will be

week1: 4
week 3  5

even i write this

if (!$calcul->week) {
    echo '0';
}

i can't display  have week 2 cause i don't have date in week 2 in database

but i have to put week 2 :  0

any idea??

Link to comment
Share on other sites

look this

<?php
$test = array(array('week' => 1, 'sum' => 3), array('week' => 3, 'sum' => 5));
$last_week = false;
foreach ($test as $x){
if ($last_week){
	for ($i = $last_week + 1; $i < $x['week']; $i++){
		echo "Week$i : 0<br />\n";
	}
}
echo "Week".$x['week']." : ".$x['sum']."<br />\n";
$last_week = $x['week'];
}
?>

Link to comment
Share on other sites

ho sasa again

i test again and i found some problem


$test = array(array('week' => 1, 'sum' => 3),array('week' => 6, 'sum' => 7), array('week' => 3, 'sum' => 5));
$last_week = false;
foreach ($test as $x){
if ($last_week){
	for ($i = $last_week + 1; $i < $x['week']; $i++){
		echo "Week$i : 0<br />\n";
	}
}
echo "Week".$x['week']." : ".$x['sum']."<br />\n";
$last_week = $x['week'];
}

so i have output not order :
week1:
week5
week3
[code]
of couse i know that i can use asort(array)
[code]
$test = array(array('week' => 1, 'sum' => 3),array('week' => 1, 'sum' => 3),array('week' => 6, 'sum' => 7), array('week' => 3, 'sum' => 5));
asort($test);

but i hope will work after requete sql

i wil try this and i will tell you[/code][/code]

Link to comment
Share on other sites

yes sasa u have reason 
just to execute in php i have to put this

<?php foreach($this->calcul as $calcul) : 
// take result from sql 
$test= array(array($this->escape($calcul->week)))
$last_week = false;
foreach ($test as $x){
if ($last_week){
	for ($i = $last_week + 1; $i < $x['week']; $i++){
		echo "Week$i : 0<br />\n";
	}
}
echo "Week".$x['week']." : ".$x['sum']."<br />\n";
$last_week = $x['week'];

end foreach
?>

just i'm afraid that take long time to execute script

juste to explain you why for first for each

i use zend and MVC MODEL

Link to comment
Share on other sites

yes sasa u have reason 
just to execute in php i have to put this

<?php foreach($this->calcul as $calcul) : 
// take result from sql 
$test= array(array($this->escape($calcul->week)))
$last_week = false;
foreach ($test as $x){
if ($last_week){
	for ($i = $last_week + 1; $i < $x['week']; $i++){
		echo "Week$i : 0<br />\n";
	}
}
echo "Week".$x['week']." : ".$x['sum']."<br />\n";
$last_week = $x['week'];

end foreach
?>

just i'm afraid that take long time to execute script

juste to explain you why for first for each

i use zend and MVC MODEL

 

That runs slowly?  Well, why are all those random arrays created anyway? o-O

 

P.S: Are you a French speaker?  Just curious.  Because I know that to tell someone that they're right, it's "Tu as raison", and you said "you have reason", which is a direct translation.

Link to comment
Share on other sites

hi again sasa and sorry if i take a long time to answer . because a work in other problem (calculate difference between 2 dates but group by week)cause different between 2 dates a know  . and until this moment i did'nt found solution

 

so to explain you why i use :

$this->escape($calcul->week))

i use Zend so in principal class that extend from zend_table ans so i create  in this class function sql

like in my example

select sum (total) as sum group by week(date,1) order by (date

of course i put just example not entire class or function

 

and in ZEND  we call this function to display with escape

so i to diplay column week i had to put this

 

$this->escape($calcul->week))

 

hope that u unterstand

Link to comment
Share on other sites

<?php foreach($this->calcul as $calcul) : 
// take result from sql 
//$test= array(array($this->escape($calcul->week)))
$last_week = false;
foreach ($test as $x){
if ($last_week){
	for ($i = $last_week + 1; $i < $calcul->week; $i++){
		echo "Week$i : 0<br />\n";
	}
}
echo "Week".$this->escape($calcul->week)." : ".$this->escape($calcul->total)."<br />\n";
$last_week = $calcul->week;

end foreach
?>

Link to comment
Share on other sites

hi sasa and thanks for help

i'm sorry to reply so late but i was busy

so i tried your last replay and i have error for variable $test

so i decide to use array_push and little  change in for each

so the script is like this

<?php foreach($this->calcul as $calcul) : 
$tab = array();
array_push($tab,array('week' =>$this->escape($calcul->week), 'sum'=>$this->escape($calcul->total)
endforeach;

and u use after this

<?php
$week = 0;
foreach ($tab  as $x)

  {
						    
for ($i = $week ; $i < $calcul->week;$i++)
{
echo week:$i : 0;
                       
}
echo "Week".$this->escape($calcul->week)." : ".$this->escape($calcul->total)."<br />\n";
$last_week = $calcul->week;
}
?>	   

so i change (for )from

<?php
$week=false;
for ($i = $week + 1; $i < $calcul->week; $i++){
?>

to

<?php
$week=0;
for ($i = $week ; $i < $calcul->week;$i++)
?>

 

because for example if my array start in week 2 i display week 1 as zero and not start output from first week in arrau

and i use array_push to use after array_sum to have total without written other sql requete

 

and That's  runs fast so i no probleme for time to excecute this script ;)

thank you sasa for help :D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.