prince198 Posted August 12, 2008 Share Posted August 12, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/119276-solved-display-zero-in-sql-requete-with-php/ Share on other sites More sharing options...
ignace Posted August 12, 2008 Share Posted August 12, 2008 if (!$calcul->week) { echo '0'; } Quote Link to comment https://forums.phpfreaks.com/topic/119276-solved-display-zero-in-sql-requete-with-php/#findComment-614585 Share on other sites More sharing options...
prince198 Posted August 12, 2008 Author Share Posted August 12, 2008 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?? Quote Link to comment https://forums.phpfreaks.com/topic/119276-solved-display-zero-in-sql-requete-with-php/#findComment-614765 Share on other sites More sharing options...
sasa Posted August 12, 2008 Share Posted August 12, 2008 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']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/119276-solved-display-zero-in-sql-requete-with-php/#findComment-614854 Share on other sites More sharing options...
prince198 Posted August 12, 2008 Author Share Posted August 12, 2008 hi sasa thank you for helping i tested and it's work fine and very good i will updated with my sql script and i will tell you if work or not thanks lot :) Quote Link to comment https://forums.phpfreaks.com/topic/119276-solved-display-zero-in-sql-requete-with-php/#findComment-614919 Share on other sites More sharing options...
prince198 Posted August 12, 2008 Author Share Posted August 12, 2008 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] Quote Link to comment https://forums.phpfreaks.com/topic/119276-solved-display-zero-in-sql-requete-with-php/#findComment-614927 Share on other sites More sharing options...
sasa Posted August 12, 2008 Share Posted August 12, 2008 i see on your code order by date Quote Link to comment https://forums.phpfreaks.com/topic/119276-solved-display-zero-in-sql-requete-with-php/#findComment-614942 Share on other sites More sharing options...
prince198 Posted August 12, 2008 Author Share Posted August 12, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/119276-solved-display-zero-in-sql-requete-with-php/#findComment-614966 Share on other sites More sharing options...
DarkWater Posted August 12, 2008 Share Posted August 12, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/119276-solved-display-zero-in-sql-requete-with-php/#findComment-614972 Share on other sites More sharing options...
prince198 Posted August 12, 2008 Author Share Posted August 12, 2008 yes i'm french and sorry if i made some mistake in english but i didn't unterstand your question about array Quote Link to comment https://forums.phpfreaks.com/topic/119276-solved-display-zero-in-sql-requete-with-php/#findComment-614976 Share on other sites More sharing options...
DarkWater Posted August 12, 2008 Share Posted August 12, 2008 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)) >_> Quote Link to comment https://forums.phpfreaks.com/topic/119276-solved-display-zero-in-sql-requete-with-php/#findComment-614980 Share on other sites More sharing options...
prince198 Posted August 12, 2008 Author Share Posted August 12, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/119276-solved-display-zero-in-sql-requete-with-php/#findComment-614993 Share on other sites More sharing options...
sasa Posted August 12, 2008 Share Posted August 12, 2008 for output you need two values one is number of week and 2nd is sum what is value of variable $calcul->week Quote Link to comment https://forums.phpfreaks.com/topic/119276-solved-display-zero-in-sql-requete-with-php/#findComment-615009 Share on other sites More sharing options...
prince198 Posted August 12, 2008 Author Share Posted August 12, 2008 $this->escape($calcul->week) it's for number week for example : week1 $this->escape($calcul->total ) t's for total for exaple 55555 Quote Link to comment https://forums.phpfreaks.com/topic/119276-solved-display-zero-in-sql-requete-with-php/#findComment-615016 Share on other sites More sharing options...
sasa Posted August 12, 2008 Share Posted August 12, 2008 <?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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/119276-solved-display-zero-in-sql-requete-with-php/#findComment-615047 Share on other sites More sharing options...
prince198 Posted August 13, 2008 Author Share Posted August 13, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/119276-solved-display-zero-in-sql-requete-with-php/#findComment-615902 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.