<?php function graphImage($numachieved, $weektarg)
{

    //working out how many days are in the month
 	$day = (date('t')/4.33333);	
	//week1
	$day1 = $day;
	//week2
	$day2 = ($day*2);
	//week3
	$day3 = ($day*3);
	//week4
	$day4 = ($day*4);
	//tagert for the week
	$perWeek = $numachived;
	//height of graph

	
	$numtarg = ($weektarg*1);
	$numtarg2 = ($weektarg*2);
	$numtarg3 = ($weektarg*3);
	$numtarg4 = ($weektarg*4);
	
	
switch (true) {
    case ((date('j') < $day1) && ($perWeek > $numtarg)): ;
        echo "<img src='images/grid_stats.jpg' alt='grid' height=' ".($numachieved * 5)." ' width='20' />";
        break;
	
	case ((date('j') > $day1) && (date('j') < $day2) && ($perWeek > $numtarg)): ;
        echo "<img src='images/grid_stats.jpg' alt='grid' height=' ".($numachieved * 5)." ' width='20' />";
        break;
    case ((date('j') > $day2) && (date('j') < $day3) && ($perWeek > $numtarg2)):
        echo "<img src='images/grid_stats.jpg' alt='grid' height=' ".($numachieved * 5)." ' width='20' />";
        break;
    case ((date('j') > $day3) && (date('j') < $day4) && ($perWeek > $numtarg3)):
        echo "<img src='images/grid_stats.jpg' alt='grid' height=' ".($numachieved * 5)." ' width='20' />";
        break;
		case ((date('j') > $day4) && ($perWeek > $numtarg4)):
		echo "<img src='images/grid_stats.jpg' alt='grid' height=' ".($numachieved * 5)." ' width='20' />";
		break;
    default:
       echo "<img src='images/grid_stats2.jpg' alt='grid' height=' ". ($numachieved * 5)." ' width='20' />";
}
}
?>

Hey Guys,
Im new to PHP,
Im creating a little performance graph for some consultants,
The graph consists of 14 tasks, it`s a bar graph.
The Problem i`m having is that i need to display 2 images.
A blue bar if they are on the right target for the set week in the month and a red bar if they are of course. The graph never seems to have both. if i put switch = true then it finds all the cases to be false, if i put switch = false then it finds all the cases to be "true" in the aspect that it is all false,

Can anyone please help me correct my code.

Thanks in advance!

Hello,
One thing that I have never understood in programming languages is the switch clause. What is the need of that and especially how do they make the code cleaner when use break (or anything like that), let me compare that only to the old GOTO command or the reckless use of WHILE. If you transform your code whit if … else … and let us know your input and the expected output I could help you find out why the output isn’t what you are expecting.

Hey jkon Thank you for your reply,
To be honest with you, this would have been the first time i would have used the switch case but now i see it seems to be more of a problem.

the if/else would have been::

<?php function graphImage($numachieved, $weektarg)
{

    //working out how many days are in the month
 	$day = (date('t')/4.33333);	
	//week1
	$day1 = $day;
	//week2
	$day2 = ($day*2);
	//week3
	$day3 = ($day*3);
	//week4
	$day4 = ($day*4);
	//tagert for the week
	$perWeek = $numachived;
	//height of graph

	
	$numtarg = ($weektarg*1);
	$numtarg2 = ($weektarg*2);
	$numtarg3 = ($weektarg*3);
	$numtarg4 = ($weektarg*4);


if (date('j') < $day1 && $perWeek > ($numtarg*2)){
echo '<img src="images/grid_stats.jpg" alt="grid" height="'.$heightgraph.'" />';}else
		{echo '<img src="images/grid_stats2.jpg" alt="grid" height="'.$heightgraph.'" />';}
elseif (date('j') > $day2 && date('j') < $day3 && $perWeek > ($numtarg*2))
		{echo '<img src="images/grid_stats.jpg" alt="grid" height="'.$heightgraph.'" />';}else
		{echo '<img src="images/grid_stats2.jpg" alt="grid" height="'.$heightgraph.'" />';};
		}
	elseif (date('j') > $day3 && date('j') < $day4 && $perWeek > ($numtarg*3))
		{echo '<img src="images/grid_stats.jpg" alt="grid" height="'.$heightgraph.'" />';}else
		{echo '<img src="images/grid_stats2.jpg" alt="grid" height="'.$heightgraph.'" />';};
	elseif (date('j') > $day4 && $perWeek > ($numtarg*4)){
		echo '<img src="images/grid_stats.jpg" alt="grid" height="'.$heightgraph.'" />';}else
		{echo '<img src="images/grid_stats2.jpg" alt="grid" height="'.$heightgraph.'" />';};

see attached gif for the desired result of the graph (sorry for the poor illustration);
your help is greatly appreciated!

Hello, the code you provided has syntax error (unexpected elseif)
If date('j') < $day1 && $perWeek > ($numtarg*2)) it will do something else it will do something deferent, but you continue after that with an elseif clause that it is not logic (I would recommend Eclipse PDT for easy editing , such things made them red and will explain you the problems). The first code that you provided (with switch) is equivalent with the following code using if else.

<?php 
function graphImage($numachieved, $weektarg)
{

    //working out how many days are in the month
 	$day = (date('t')/4.33333);	
	//week1
	$day1 = $day;
	//week2
	$day2 = ($day*2);
	//week3
	$day3 = ($day*3);
	//week4
	$day4 = ($day*4);
	//tagert for the week
	$perWeek = $numachived;
	//height of graph

	
	$numtarg = ($weektarg*1);
	$numtarg2 = ($weektarg*2);
	$numtarg3 = ($weektarg*3);
	$numtarg4 = ($weektarg*4);
	
	if((date('j') < $day1) && ($perWeek > $numtarg))
	{
		echo "<img src='images/grid_stats.jpg' alt='grid' height=' ".($numachieved * 5)." ' width='20' />";
	}
	else if((date('j') > $day1) && (date('j') < $day2) && ($perWeek > $numtarg)) 
	{
		echo "<img src='images/grid_stats.jpg' alt='grid' height=' ".($numachieved * 5)." ' width='20' />";
	}
	else if ((date('j') > $day2) && (date('j') < $day3) && ($perWeek > $numtarg2))
	{
		echo "<img src='images/grid_stats.jpg' alt='grid' height=' ".($numachieved * 5)." ' width='20' />";
	}
	else if((date('j') > $day3) && (date('j') < $day4) && ($perWeek > $numtarg3))
	{
		echo "<img src='images/grid_stats.jpg' alt='grid' height=' ".($numachieved * 5)." ' width='20' />";
	}
	else if ((date('j') > $day4) && ($perWeek > $numtarg4))
	{
		echo "<img src='images/grid_stats.jpg' alt='grid' height=' ".($numachieved * 5)." ' width='20' />";
	}
	else 
	{
		echo "<img src='images/grid_stats2.jpg' alt='grid' height=' ". ($numachieved * 5)." ' width='20' />";
	}
}
?>

What you can do now is to inspect this and understand if this is what wants your code to do (in business logic) or if you should modify the logical operators to fit your needs.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.