/    Sign up×
Community /Pin to ProfileBookmark

How can I make this if/else statement mroe effective/shorter?

Hello

I have a very long/uneffective if/else statement. I have ~200 items so this would mean that i need ~200 elseif which is way to much. I have been trying to come up with a better soloution but cant seem to get there. Maybe you can help me on the way?

“`php
if($item== “number5478fs77s465”){
if($c != “done”){
if($this->count_number5478fs77s465 > 10){
$class = “hide”;
}else{
$class = “”;
}
$this->count_number5478fs77s465 = $this->count_number5478fs77s465 +1;
}else{
$class = “”;
}
}else if($item== “number5745yu45125”){
if($c != “done”){
if($this->count_number5745yu45125> 10){
$class = “hide”;
}else{
$class = “”;
}
$this->count_number5745yu45125= $this->count_number5745yu45125 +1;
}else{
$class = “”;
}
}else if$item== “number5774408s0tty8979”){
if($c != “done”){
if($this->count_number5774408s0tty8979> 10){
$class = “hide”;
}else{
$class = “”;
}
$this->count_number5774408s0tty8979= $this->count_number5774408s0tty8979 +1;
}else{
$class = “”;
}
}
“`

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmApr 10.2018 — 1 - please use the forum's proper code tags when posting your code here to make easier to read

2 - perhaps you should identify what you want done under each specific condition and use your selection logic to assign a condition to your search results. That would lessen the amount of code you are writing for each and every if/else.

3 - Now - think about how this data should be examined. Is it static? Can you setup an array for the data or a db table? If so that would be the thing to do. Then you simply write a query to see if you have the desired value already stored and have that query return to you the 'condition' mentioned above. Or if you have to use an array, do a query lookup (in_array() perhaps) that returns a condition as a value of that array lookup.

I would seriously not use an array.
Copy linkTweet thisAlerts:
@NogDogApr 11.2018 — It would be cleaner if you used arrays, e.g. $this->count['number1234k2341'], but anyway, you could use variable variables and an array of items you want to do the check on to DRY things up a bit:
``php<i>
</i>$numbersToCheck = array(
"number5478fs77s465",
"number5745yu45125",
"number5774408s0tty8979"
);
if(in_array($item, $numbersToCheck)) {
$class = "";
if($c != "done") {
if($this-&gt;${'count_'.$item} &gt; 10) {
$class = "hide";
}
${'count_'.$item}++;
}
}<i>
</i>
``
Copy linkTweet thisAlerts:
@jag1authorApr 11.2018 — @NogDog#1591065 thank you!

@ginerjm#1591062 Why wouldnt you use an array? Is it becasue of bad performance? I have noticed that the array solution takes some loading time. But i am at the same time afraid that I cant make this work with the db becasue the "done" class is given to elements based on the current time
Copy linkTweet thisAlerts:
@ginerjmApr 11.2018 — IMHO, If one is talking about "storing" a "bunch" of things, an array is not the device to use. That's all. That said, I do hope that if you continue to use the array concept that you define the array in a separate module that you then include in the scripts that need to reference it.
Copy linkTweet thisAlerts:
@rootApr 15.2018 — I would have thought a database, query for what you want, render the output and "Bob's ya uncle" (and likely your father too...) as the saying goes...
×

Success!

Help @jag1 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...