Jump to content

Why does this show 2?


pavankat

Recommended Posts

I can't understand for the life of me - why the value for the variable:  $total_vit_k is 2 in the red box: http://content.screencast.com/users/rockstarvisible/folders/Jing/media/beafff1f-2168-47b8-91bb-950ace0722c2/2012-03-21_1437.png.

 

The reason why I can't understand this is because $total_vit_k is the weekly total for everything from jan 29 to feb 4 but there's no values for those dates logged in the mysql table it's pulling from.

 

Please help me. It should be displaying 0 Mcg in the red box but it isn't. I know it's something wrong in my code.

 

When I echo out all the values in the while loop like below, the browser echos out nothing for the queries from jan 29 2012 to feb 4 2012.

 

code below is from file 2:

while ($row = mysql_fetch_assoc($query_run)){
                
echo "<br /><br /><b><i>" . '$sql_food ' . $sql_food."</b></i><br /><br/ >";
echo "<br /><br /><b><i>" . '$sql_total_servings ' . $sql_total_servings."</b></i><br /><br/ >";
echo "<br /><br /><b><i>" . '$sql_day ' . $sql_day."</b></i><br /><br/ >";
echo "<br /><br /><b><i>" . '$sql_month ' . $sql_month."</b></i><br /><br/ >";
echo "<br /><br /><b><i>" . '$sql_year ' . $sql_year."</b></i><br /><br/ >";
echo "<br /><br /><b><i>" . '$sql_vit_k ' . $sql_vit_k."</b></i><br /><br/ >";

echo "<br /><br /><b><i>" . ' $sql_servings ' .  $sql_servings."</b></i><br /><br/ >";
echo "<br /><br /><b><i>" . ' $total_vit_k ' .  $total_vit_k."</b></i><br /><br/ >";
  include 'inside_while.acc-over.php';
        }

 

full code for file2:

 

<?php

if(!function_exists('getWeeklyVitK') ){

function getWeeklyVitK($value, $bday, $bmonth, $byear, $eday, $emonth, $eyear){

//initialize the variables so no errors occur
$query_month =$bmonth;
$query_year =$byear;
$query_day = $bday;


//initialize variables
$total_vit_k = 0;
//$total_cal = 0;
//$total_protein = 0;
//$total_fiber = 0;
//$total_carbs = 0;
//$total_sugar = 0;
//$total_sodium = 0;
//$total_chol = 0;

if ($value==1){
    echo "Something went wrong - tell pavankat[at]gmail.";
  break;  
}

if ($value==2){
// if $value = 2
// end day >= start day
// end month = start month
// end year = start year


$day_temp = $eday;
			    
     while($bday<=$day_temp){
     $query_day = $bday;

    //redeclare the mysql query
    include 'acc-over-sql-queries.php';
    if ($query_run = mysql_query($query_value2)){
    
        while ($row = mysql_fetch_assoc($query_run)){
        include 'inside_while.acc-over.php';
        
        }   
    }
        $bday++;
     }      
return round($total_vit_k, 2);
}

if ($value==3){
// if $value = 3
// end day and beg day don't matter
// end month > beg month
// end year = beg year

for($bmonth; $bmonth<=$emonth; $bmonth++){

$query_month = $bmonth;

$days_in_month = date('t',mktime(0,0,0,$query_month,1,$byear));
			$day_temp = $days_in_month;
			    
                                    
                                echo "<br />day_temp variable:<u> ".$day_temp. "</u><br />";
                                    
			while($bday<=$day_temp){
			$query_day = $bday;

    //redeclare the mysql query
    include 'acc-over-sql-queries.php';
    echo "<br />".'$query_value3:' . $query_value3 . "<br />";
    
    if ($query_run = mysql_query($query_value3)){

        while ($row = mysql_fetch_assoc($query_run)){
                
echo "<br /><br /><b><i>" . '$sql_food ' . $sql_food."</b></i><br /><br/ >";
echo "<br /><br /><b><i>" . '$sql_total_servings ' . $sql_total_servings."</b></i><br /><br/ >";
echo "<br /><br /><b><i>" . '$sql_day ' . $sql_day."</b></i><br /><br/ >";
echo "<br /><br /><b><i>" . '$sql_month ' . $sql_month."</b></i><br /><br/ >";
echo "<br /><br /><b><i>" . '$sql_year ' . $sql_year."</b></i><br /><br/ >";
echo "<br /><br /><b><i>" . '$sql_vit_k ' . $sql_vit_k."</b></i><br /><br/ >";

echo "<br /><br /><b><i>" . ' $sql_servings ' .  $sql_servings."</b></i><br /><br/ >";
echo "<br /><br /><b><i>" . ' $total_vit_k ' .  $total_vit_k."</b></i><br /><br/ >";
  include 'inside_while.acc-over.php';
        }
    
    }
    $bday++;
                                }
                                $bday = 1;
}
//return round($total_vit_k, 2);
return $total_vit_k;
}

if ($value==4){
// if $value = 4
// end day and beg day dont matter
// end month and beg month dont matter
// end year > beg year 
  
for($byear; $byear<=$eyear; $byear++){

$query_year = $byear;

$month_temp = 12;
    
        while($bmonth<=$month_temp){//while loop starts
        
        $query_month = $bmonth;
        
                                //cycle through the days
			$days_in_month = date('t',mktime(0,0,0,$query_month,1,$byear));
			$day_temp = $days_in_month;
	    
			while($bday<=$day_temp){
			$query_day = $bday;
             
            //redeclare the mysql query
            include 'acc-over-sql-queries.php';
            if ($query_run = mysql_query($query_value4)){
            
                while ($row = mysql_fetch_assoc($query_run)){
                include 'inside_while.acc-over.php';
                
                }     
            }
        
        			//-----------------------------------$bday++ unless stuff
			if ($byear < $eyear){
			$bday++;
			}else if ($byear == $eyear){
				    if ($bmonth < $emonth){
					    $bday++;
				    }else if($bmonth == $emonth){
						    
						    if ($bday < $eday){
							$bday++;
						    }else if($bday == $eday){
							$bday = $day_temp+1;
						    }
						    
				    }
			      
			     }
			//-----------------------------------$bday++ unless stuff


			}//while day loop end
			$bday = 1;
        
            
                    //-----------------------------------$bmonth++ unless stuff		
	    if ($byear < $eyear){
	    $bmonth++;
	    }else if ($byear == $eyear){
			if ($bmonth < $emonth){
				$bmonth++;
			}else if($bmonth == $emonth){
					$bmonth = 13;
			}
		  
	         }
	    //-----------------------------------$bmonth++ unless stuff
        }//while loop ends
            if ($byear <= $eyear){
                $bmonth = 1;     
            }
}

return round($total_vit_k, 2);
}


		}
}
                        ?>

 

 

full code for the contents of the while loop:

<?php

                $sql_food = $row["name"];
                
               
                $sql_year = $row["calendar_year"];
                
                $sql_month = $row["calendar_month"];
                $sql_day = $row["calendar_day"];
                $sql_total_servings = $row["total_servings"];
                $sql_vit_k = $row["vit_k"];
                $sql_vit_k = $sql_vit_k/$sql_total_servings;
                $sql_servings = $row["servings"];              
                if ($sql_servings == 0){
                    $total_vit_k += $sql_vit_k;
                 
                }else{
                    $total_vit_k += $sql_vit_k * $sql_servings;

                }

?>

 

full code for the sql query getting included in the previous file:

 

$query_value3 = "SELECT 
foods.id, users_calendar.id, `name`, `source`,`vit_k`, `cal`,`protein`, `fiber`, `carbs`, `sugar`, `sodium`, `chol`, `total_servings`,
`calendar_month`, `calendar_day`, `calendar_year`, `servings`
FROM `foods`
LEFT JOIN users_calendar
ON foods.id=users_calendar.food_id
WHERE users_calendar.user_id =". $_SESSION['user_id']." && " . $query_day . "=calendar_day && ".$query_month."=calendar_month && ".$byear."=calendar_year
ORDER BY users_calendar.id";

Link to comment
Share on other sites

Thanks for taking the time to look at this.

 

It's too long to take a screen shot with all that's being echo'd so I made a video:

 

http://screencast.com/t/w9gditjXz

 

At 1 minute and 8 seconds you'll see the calendar and you'll see in the Weekly Vit K Total first  box has v k: 2 Mcg where it should be v k: 0 Mcg.

 

Please help me. Would mean the world.

Link to comment
Share on other sites

$query_day . "=calendar_day && ".$query_month."=calendar_month && ".$byear."=calendar_year

these are the wrong way round, you have the value on the left of the = and the field on the right, should be field on left and value on right.

  $query_value3 = "SELECT 
foods.id, users_calendar.id, `name`, `source`,`vit_k`, `cal`,`protein`, `fiber`, `carbs`, `sugar`, `sodium`, `chol`, `total_servings`,
`calendar_month`, `calendar_day`, `calendar_year`, `servings`
FROM `foods`
LEFT JOIN users_calendar
ON foods.id=users_calendar.food_id
WHERE ((users_calendar.user_id ={$_SESSION['user_id']}) && (calendar_day=$query_day) && (calendar_month=$query_month) && (calendar_year=$byear))
ORDER BY users_calendar.id";

Link to comment
Share on other sites

Thanks Muddy but that wasn't it. That surprisingly didn't change anything - I guess it's improper syntax what I did in the query but that doesn't change the query's purpose.

 

I got it to work with the help of a friend pointing out where the code was broken.

 

I changed: $day_temp = $days_in_month;

 

to:

 

if ($bmonth < $emonth){

                                $day_temp = $days_in_month;

    }else{

                                        $day_temp = $eday;

                                    }

 

 

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.