...

View Full Version : Resolved if, elseif help please



LJackson
05-05-2009, 11:52 PM
Hi all,

i have what looks to be complicated if statments which im sure can be condensed much smaller

here is the first if statment which works

if($itemno <= 9){
$itemtype = "DVD";
}
elseif ($itemno >=10 AND $itemno <=18){
$itemtype = "CD";
}
elseif ($itemno >=19 AND $itemno <=27){
$itemtype = "Video Games";
}
elseif ($itemno >=28 AND $itemno <=36){
$itemtype = "Blu-Ray";
}
elseif ($itemno >=37 AND $itemno <=45){
$itemtype = "Books";
}

can the above be reduced to get the same result ?

and the second if statment which does not work for some reason


if($itemno >=1 AND $itemno <=3) or ($itemno >=10 AND $itemno <=12) or ($itemno >=19 AND $itemno <=21) or ($itemno >=28 AND $itemno <=30) or ($itemno >=37 AND $itemno <=39){
$subcat = "Bestsellers";
}
elseif($itemno >=4 AND $itemno <=6) or ($itemno >=13 AND $itemno <=15) or ($itemno >=22 AND $itemno <=24) or ($itemno >=31 AND $itemno <=33) or ($itemno >=40 AND $itemno <=42){
$subcat = "New Releases";
}
elseif($itemno >=7 AND $itemno <=9) or ($itemno >=16 AND $itemno <=18) or ($itemno >=25 AND $itemno <=27) or ($itemno >=34 AND $itemno <=36) or ($itemno >=43 AND $itemno <=45){
$subcat = "Gifted";
}

again if this can be condensed and corrected i'd really appreciate it :)

many thanks
Luke

nobackseat88
05-05-2009, 11:55 PM
and the second if statment which does not work for some reason

Do you have an error you could post?

LJackson
05-05-2009, 11:56 PM
yep sorry mate forgot to include it ooops...

Parse error: syntax error, unexpected T_LOGICAL_OR in /customers/kernow-connect.com/kernow-connect.com/httpd.www/entertainmentPCtest.php on line 68

nobackseat88
05-05-2009, 11:57 PM
Also, shouldn't
or be replaced with ||
and AND be replaced with &&

Correct me if I"m wrong...

LJackson
05-05-2009, 11:59 PM
dunno mate it seems to work ok for the first one? i'll try it though :)

LJackson
05-06-2009, 12:01 AM
hi i tried this


if($itemno >=1 && $itemno <=3) || ($itemno >=10 && $itemno <=12) || ($itemno >=19 && $itemno <=21) || ($itemno >=28 && $itemno <=30) || ($itemno >=37 && $itemno <=39){
$subcat = "Bestsellers";
}

but am getting

Parse error: syntax error, unexpected T_BOOLEAN_OR in /customers/kernow-connect.com/kernow-connect.com/httpd.www/entertainmentPCtest.php on line 68

cheer

mlseim
05-06-2009, 12:04 AM
My shot at this.
Not necessarily shorter, but easier ...


<?php

$itemno=30;

if($itemno<=45){
$itemtype = "Books";
}
if($itemno<=36){
$itemtype = "Blu-Ray";
}
if($itemno<=27){
$itemtype = "Video Games";
}
if($itemno<=18){
$itemtype = "CD";
}
if($itemno<=9){
$itemtype = "DVD";
}

echo $itemtype;
?>

LJackson
05-06-2009, 12:10 AM
thanks mlseim that looks tidy and easier to read :)

but does it work :D lol cant test it until the other one is resolved...
im sure it will.

cheers mate
Luke

Fou-Lu
05-06-2009, 12:12 AM
As for the second if, its because you're brackets are mismatched:


if($itemno >=1 AND $itemno <=3) or ($itemno >=10 AND $itemno <=12) or ($itemno >=19 AND $itemno <=21) or ($itemno >=28 AND $itemno <=30) or ($itemno >=37 AND $itemno <=39){

It evaluates the if ($itemno >=1 AND $itemno <= 3) and then stops the conditional and fails on the 'or'. If you use a complex statement like this, you can exploit the operator precedence - || has a higher weight than AND, so you can get away without any brackets and use the || and AND operators.

As for you're first question, yes, by exploiting bounds.



if($itemno <= 9)
{
$itemtype = "DVD";
}
elseif ($itemno <=18)
{
$itemtype = "CD";
}
elseif ($itemno <=27)
{
$itemtype = "Video Games";
}
elseif ($itemno <=36)
{
$itemtype = "Blu-Ray";
}
elseif ($itemno <=45)
{
$itemtype = "Books";
}



Holy crap, this had tons of posts, there were none when I started writing >.<
AND and OR are valid operators, but have lower precedence weights than && and ||.

LJackson
05-06-2009, 12:21 AM
lol... thank you Fou-Lu that works a treat.

cheers everyone for your help :)
Luke

venegal
05-06-2009, 12:44 AM
Oh, I guess I'm too late to join in on the fun. And boy are those solutions still complicated.

Here's my version:



$itemtypes = array("DVD", "CD", "Video Games", "Blu-Ray", "Books");
$subcats = array("Bestsellers", "New Releases", "Gifted");

$itemtype = $itemtypes[ceil($itemno / 9) - 1];
$subcat = $subcats[ceil((($itemno - 1) % 9 + 1) / 3) - 1];


Or, make those magic numbers dependent on array length, then you can easily add new categories by adding elements to the arrays.

LJackson
05-06-2009, 01:08 AM
Your never to late to join in the fun :D

thank you for your input venegal it is definatly more condensed thank the previous examples but when looking back at this in a month or two im not going to know what the code does or how to change it :(
so im going to stick with what ive got as i am able to understand it :P

its nice to see the different ways in which the same thing can be accomplished :)

just out of curiosity what does ceil mean and the numbers after it??? might help me understand whats going on :)

cheers mate
Luke

mlseim
05-06-2009, 01:13 AM
Venegal ... smarta** :)

but it's fun to see the various ways ... good for PHP novices.

LJackson
05-06-2009, 01:16 AM
venegal ... Smarta** :)

but it's fun to see the various ways ... Good for php novices.

:D agreed!!!

venegal
05-06-2009, 01:21 AM
Alright, first line, let's start from the inside out:
itemno 1-9 are in the first category, 10-18 in the second cat, and so on.
If you divide those numbers by 9, the numbers 1-9 become some float larger than 0 and smaller or equal 1; 10-18 similarly become floats between 1 and 2, and so on.
ceil() returns the next highest integer, so what was originally the numbers 1-9, then something between 0 and 1, now all become 1; 10-18 all become 2, and so on.
Now just substract one, because the array keys start from 0, not from 1.

Second line:
Maybe you can figure it out on your own. If you have questions, just ask.

LJackson
05-06-2009, 02:25 PM
thanks for that mate i kind of understand what your saying :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum