Jump to content

Recommended Posts

Hi folks, relative newbie so my code wont be pretty, however...

 

This one has me stumped.

 

I have written some PHP to change the background colour of a cell in a table based on a variable.

 

The code is as follows:

 

// Convert Summer 8 Grade to a number value
if ($row['Sum8']=='1a') {
$sum8_value=3;
} elseif ($row['Sum8']=='1b') {
$sum8_value=2;
} elseif ($row['Sum8']=='1c') {
$sum8_value=1;
} elseif ($row['Sum8']=='2a') {
$sum8_value=6;
} elseif ($row['Sum8']=='2b') {
$sum8_value=5;
} elseif ($row['Sum8']=='2c') {
$sum8_value=4;
} elseif ($row['Sum8']=='3a') {
$sum8_value=9;
} elseif ($row['Sum8']=='3b') {
$sum8_value=8;
} elseif ($row['Sum8']=='3c') {
$sum8_value=7;
} elseif ($row['Sum8']=='4a') {
$sum8_value=12;
} elseif ($row['Sum8']=='4b') {
$sum8_value=11;
} elseif ($row['Sum8']=='4c') {
$sum8_value=10;
} elseif ($row['Sum8']=='5a') {
$sum8_value=15;
} elseif ($row['Sum8']=='5b') {
$sum8_value=14;
} elseif ($row['Sum8']=='5c') {
$sum8_value=13;
} elseif ($row['Sum8']=='6a') {
$sum8_value=18;
} elseif ($row['Sum8']=='6b') {
$sum8_value=17;
} elseif ($row['Sum8']=='6c') {
$sum8_value=16;
} elseif ($row['Sum8']=='7a') {
$sum8_value=21;
} elseif ($row['Sum8']=='7b') {
$sum8_value=20;
} elseif ($row['Sum8']=='7c') {
$sum8_value=19;
} elseif ($row['Sum8']=='N') {
$sum8_value=0;
} elseif ($row['Sum8']=='X') {
$sum8_value=25;
} elseif ($row['Sum8']=='B') {
$sum8_value=5;
} elseif ($row['Sum8']=='-') {
$sum8_value=150;
}


// Calculate progress for Summer 8

$sum8_progress = number_format(($ks3_target_value-$ks2_value)*0.75)+$ks2_value;

$sum8_progress_final = $sum8_value-$sum8_progress;



if ($sum8_progress_final< -1) {
$progress_colour_sum8="#ac3845";} elseif ($sum8_progress_final==-1) {
$progress_colour_sum8="#c5a751";} elseif ($sum8_progress_final==0) {
$progress_colour_sum8="#93a336";} elseif ($sum8_progress_final>= 1 && $sum8_progress_final<= 34) {
$progress_colour_sum8="#477dae";} elseif ($sum8_progress_final>= 35) {
$progress_colour_sum8=$row_color;
}

 

If you can follow my code, the problem is that currently $sum8 = '-' for every entry in the table and therefore working everything through the cell should end up being $row_color which is basically grey. However it is showing up as #477dae

 

This code works on every other column in my table (the only difference being sum8 is spr8, aut8, sum7, spr7 etc etc (for autumn, spring, summer year 7, 8, 9 etc etc. It is only the sum8 based column that does not work.

 

I have echo'd the results to check and find the following:

$sum8_progress_final is in the 100's for every result

$progress_colour_sum8 however is #477dae for every result?

 

Any suggestions or thoughts?

Edited by djs1971
Link to comment
https://forums.phpfreaks.com/topic/270329-if-elseif-problem/
Share on other sites

That whole if and elseif seems kinda stupid.

In most cases the best solution would have been to do something like this:

<?php
$row['Sum8'] = '3c';
if($row['Sum8'][0]<22){
$sum8_value = ($row['Sum8'][0]*3) - (ord($row['Sum8'][1])-97);
}
echo $sum8_value;
?>

 

But since your system doesn't seem to follow this all the way, I could recommend using an array like this:

$values[8][$row['Sum8']];

^ Yes and just used that all the time. ;) Of course only if there is something special about row 8, if all rows are checked against the same values, simply do this instead:

$values[$row['Sum8']];

$values array would contain all the various values and the key to the value would be the same as you check for in the if and else part of your code.

 

Why is it called $row? Do you get it from a database? If that is the case, then you should store this in the database too, and instead of having to write all these if else stuff you could have had a fast db do it all for you dynamically. So easy to update and change.

 

Usually you also don't need to make 8 variable names to store 8 different values, but keep them in an array and access them by their named key value. o.O If that makes any sense.

 

Bottom line, use arrays, (maybe database) and write a proper functions.

Edited by MMDE
Link to comment
https://forums.phpfreaks.com/topic/270329-if-elseif-problem/#findComment-1390402
Share on other sites

Hi MMDE,

 

Thanks for the reply - as I said at the top of my post, I'm a real newbie to PHP & MYSQL so aware I'm likely to be using weird methods and odd code!

 

As for the $row - yep I'm using a database.

 

For a bit of context - the system is storing a grade (1a, 2a, etc etc) for a set of 63 students - 3 times per year, for 5 years (Year 7 to Year 11)

 

I'm using some maths to work out if their grade is below, on or above target based on a start point and target grade and set a background colour to the table cell to indicate this.

 

This screenshot hopefully shows what I mean and what the issue is in terms of the random column.

 

Screen_Shot.png

 

I'll read through your suggestions and then do some google research to see if I can apply it. Doesn't make total sense at the moment but sure it will after a bit of reading up.

 

Thanks again for taking the time to reply.

Link to comment
https://forums.phpfreaks.com/topic/270329-if-elseif-problem/#findComment-1390417
Share on other sites

Hi again MMDE,

 

Quick update: I've got my head around arrays a little more thanks to your reply. I have set up the following:

 

$grade_number=array(3=>"1a",2=>"1b",1=>"1c",6=>"2a",5=>"2b",4=>"2c",9=>"3a",8=>"3b",7=>"3c",12=>"4a",11=>"4b",10=>"4c",15=>"5a",14=>"5b",13=>"5c",18=>"6a",17=>"6b",16=>"6c",21=>"7a",20=>"7b",19=>"7c",25=>"X",0=>"N",2000=>"-",2010=>"");

 

and then used:

 

$sum8_value1=array_search($row['Sum8'],$grade_number);

 

To grab the correct value.

 

It has massively trimmed down my code as I can use the single array for each of the 3 columns per year whereas before I had a massive if, elseif list block of code for every column!

 

In the process it seems to have solved the problem I had with the one column (Sum8) having the incorrect background colour to the cells.

 

I'm now going to work trimming down further code based on this new bit of knowledge.

 

Thanks again :-)

Edited by djs1971
Link to comment
https://forums.phpfreaks.com/topic/270329-if-elseif-problem/#findComment-1390443
Share on other sites

If you posted all of your code we could help you more with suggestions.. I do not see in your code anywhere where you are defining colors or setting the background.  Are you using CSS classes or what?..

 

Looking at your very first snippet of code I would immediately suggest the use of a switch, but then you mention that arrays have helped you out more now... I am sure there is an easier solution to what you are doing becaue you are doing nothing more than checking a value....after some apparent arithmetic that you have decided not to show (which is probably best seeing as you are only asking help with the structure and trimming the code down) but I am guessing you already have that part down pat.

Link to comment
https://forums.phpfreaks.com/topic/270329-if-elseif-problem/#findComment-1390452
Share on other sites

Hi again MMDE,

 

Quick update: I've got my head around arrays a little more thanks to your reply. I have set up the following:

 

$grade_number=array(3=>"1a",2=>"1b",1=>"1c",6=>"2a",5=>"2b",4=>"2c",9=>"3a",8=>"3b",7=>"3c",12=>"4a",11=>"4b",10=>"4c",15=>"5a",14=>"5b",13=>"5c",18=>"6a",17=>"6b",16=>"6c",21=>"7a",20=>"7b",19=>"7c",25=>"X",0=>"N",2000=>"-",2010=>"");

 

and then used:

 

$sum8_value1=array_search($row['Sum8'],$grade_number);

 

To grab the correct value.

 

It has massively trimmed down my code as I can use the single array for each of the 3 columns per year whereas before I had a massive if, elseif list block of code for every column!

 

In the process it seems to have solved the problem I had with the one column (Sum8) having the incorrect background colour to the cells.

 

I'm now going to work trimming down further code based on this new bit of knowledge.

 

Thanks again :-)

You should do the other way around, no need to search! ;D

$row['Sum8'] = "1b";
$grade_number=array("1a"=>3,"1b"=>2,"1c"=>1); // etc
echo $grade_number[$row['Sum8']]; // should output 2

 

see that works just fine, eh? No need to search.

 

You can do this too:

<?php
$row['Sum8'] = 'X';
if(is_numeric($row['Sum8'])){
$sum8_value = ($row['Sum8'][0]*3) - (ord($row['Sum8'][1])-97);
}else{
$special_grades = array('X'=>25,'N'=>0,'-'=>2000,''=>2010);
$sum8_value = $special_grades[$row['Sum8']];
}
echo $sum8_value;
?>

 

Or you can do what I really recommend, create a table for it in your MySQL database.

How is it currently looking? We can probably help you make that smoother, and join these grade numbers, which is preferable, because PHP treats these types of "arrays" as hash tables.

Edited by MMDE
Link to comment
https://forums.phpfreaks.com/topic/270329-if-elseif-problem/#findComment-1390523
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.