Problem with my function, not select the good picture with the year old

Explication:

The picture will be choose automatic with the old of the person.
$this->date4 = Year of the date of birth like “XXXX”
So for example, we will choose: 90 = 1990 but we take the last two number.
We will take the sexe: $this->sexe (“M” or “F”)
And then the function.

On my FTP: https://prnt.sc/i9rz9b
Some pictures for the old.

But when i generate the final, the picture is not changing, its only the same picture, when i change the date of birth, the picture no changing.

$photos = array(); $age = explode($this->date4[2].$this->date4[3]); $photos_path = "static/photos/" . $this->sexe . "/" . $this->getAgeRange($age) . "/";

private function getAgeRange($age) { if($age < 20) return "-20"; else if($age > 19 and $age < 30) return "20-29"; else if($age > 29 and $age < 40) return "30-39"; else if($age > 39 and $age < 50) return "40-49"; else if($age > 49 and $age < 60) return "50-59"; else if($age > 60) return "60+"; }

I don’t think there should be a space between the words “else” and “if”.
It should be written as elseif with no space.

Same result.

You’re right, that doesn’t make a difference. :slight_smile:

I think it’s a problem with your explode function, it should take at least two parameters: delimiter, then string, with an optional limit.

How i do that?
Can you write the code?
Im novice in PHP ^^

DONE!

$date4 = $this->date4; 		
$age = date('Y') - $date4;
1 Like

Yes, you need to calculate the age from the current year and year of birth. :+1:

For something like this I would most likely use a select drop down or radio buttons with predefined values instead.

But anyway, explanations such as “problem” or “doesn’t work” aren’t the most helpful for me. I have better luck understanding error messages. If you have seen any, please post them. If you haven’t, please enable error reporting and display while you’re developing the code. i.e. this at the beginning of the file:

<?php 
declare(strict_types=1); // if PHP ver 7+ 
ini_set('display_errors', 'true');
error_reporting(-1); 

There are more sophisticated ways of dealing with errors, but for now simple echo, print_r and var_dump lines should be easy enough for you do to and help you see what’s happening with the code.

I think a good start would be to temporarily change your code so it’s like this eg.

$age = explode($this->date4[2].$this->date4[3]); 
print_r($age); // remove this line after debugging 
exit; // remove this line after debugging 
$photos_path = "static/photos/" . .....

Does what you see look like what you expect to see?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.