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

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??

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'];
}
?>

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]

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

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.

yes i'm french and sorry if i made some mistake in english

but i didn't unterstand your question about  array

 

:)

 

No, it's okay, I just wanted to know.

 

Anyway, what's the point of:

$test= array(array($this->escape($calcul->week))

 

>_>

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

<?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
?>

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.