Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Condensing code?
a.live Offline
Junior Member
**
Posts: 8
Joined: Feb 2013
Post: #1
Condensing code?
Hi all,
I'm trying to condense down a code that I've written, and I'm having some trouble.  I've already chopped the thing in half from when I originally coded it (thanks in large majority to the nested OR conditionals as outlined in this thread), but it still isn't enough - when I put the whole thing into my template, it eats the rest of the page.  I guess that means there's too much information..??

Long and short of what I'm trying to do is that when fidAA, fidBB, and fidCC
are equal to a particular input (Novice, Apprentice, Adept, Venerated or Virtuoso), they output career type, as well as what level they are at, as judged by the three skills  inputs.

IE.,
Novice Painter, Apprentice Woodworker, Adept Sculptor would output Apprentice Artisan;
Virtuoso Painter, Novice Woodworker, Novice Sculptor would output Novice Artisan;
Adept Painter, Venerated Woodworker, Venerated Sculptor would output Venerated Artisan;
etc and so on

The Etc and so on I think is where the problem comes in - in trying to cover as many bases as possible, it scraps the rest of my page to accommodate for the contingencies.

PHP Code:
1
2
3
4
5
6
7
8
9
				<!--Begin SKILL-->
						<if $userfields['fidAA'] == 'None' && $userfields['fidBB'] == 'None' && $userfields['fidCC'] == 'None' || $userfields['fidAA'] != 'None' && $userfields['fidBB'] == 'None' && $userfields['fidCC'] == 'None' || $userfields['fidAA'] == 'None' && $userfields['fidBB'] != 'None' && $userfields['fidCC'] == 'None' || $userfields['fidAA'] == 'None' && $userfields['fidBB'] == 'None' && $userfields['fidCC'] != 'None' then>
							<elseif (($userfields['fidAA'] == 'Novice') || ($userfields['fidAA'] == 'Apprentice') || ($userfields['fidAA'] == 'Adept') || ($userfields['fidAA'] == 'Venerated') || ($userfields['fidAA'] == 'Virtuoso')) && $userfields['fidBB'] == 'Novice' && $userfields['fidCC'] == 'Novice' || $userfields['fidAA'] == 'Novice' && (($userfields['fidBB'] == 'Novice') || ($userfields['fidBB'] == 'Apprentice') || ($userfields['fidBB'] == 'Adept') || ($userfields['fidBB'] == 'Venerated') || ($userfields['fidBB'] == 'Virtuoso')) && $userfields['fidCC'] == 'Novice' || $userfields['fidAA'] == 'Novice' &&  $userfields['fidBB'] == 'Novice' && (($userfields['fidCC'] == 'Novice') || ($userfields['fidCC'] == 'Apprentice') || ($userfields['fidCC'] == 'Adept') || ($userfields['fidCC'] == 'Venerated') || ($userfields['fidCC'] == 'Virtuoso')) then>Novice SKILL<Br />
							<elseif (($userfields['fidAA'] == 'Apprentice') || ($userfields['fidAA'] == 'Adept') || ($userfields['fidAA'] == 'Venerated') || ($userfields['fidAA'] == 'Virtuoso')) && (($userfields['fidBB'] == 'Novice') || ($userfields['fidBB'] == 'Apprentice')) && $userfields['fidCC'] == 'Apprentice' || (($userfields['fidAA'] == 'Apprentice') || ($userfields['fidAA'] == 'Adept') || ($userfields['fidAA'] == 'Venerated') || ($userfields['fidAA'] == 'Virtuoso')) && $userfields['fidBB'] == 'Apprentice' && (($userfields['fidCC'] == 'Novice') || ($userfields['fidCC'] == 'Apprentice')) || (($userfields['fidAA'] == 'Novice') || ($userfields['fidAA'] == 'Apprentice')) && (($userfields['fidBB'] == 'Apprentice') || ($userfields['fidBB'] == 'Adept') || ($userfields['fidBB'] == 'Venerated') || ($userfields['fidBB'] == 'Virtuoso')) && $userfields['fidCC'] == 'Apprentice' || $userfields['fidAA'] == 'Apprentice' && (($userfields['fidBB'] == 'Apprentice') || ($userfields['fidBB'] == 'Adept') || ($userfields['fidBB'] == 'Venerated') || ($userfields['fidBB'] == 'Virtuoso')) && (($userfields['fidCC'] == 'Novice') || ($userfields['fidCC'] == 'Apprentice')) || (($userfields['fidAA'] == 'Novice') || ($userfields['fidAA'] == 'Apprentice')) &&  $userfields['fidBB'] == 'Apprentice' && (($userfields['fidCC'] == 'Apprentice') || ($userfields['fidCC'] == 'Adept') || ($userfields['fidCC'] == 'Venerated') || ($userfields['fidCC'] == 'Virtuoso')) || $userfields['fidAA'] == 'Apprentice' && (($userfields['fidBB'] == 'Novice') || ($userfields['fidBB'] == 'Apprentice')) &&  (($userfields['fidCC'] == 'Apprentice') || ($userfields['fidCC'] == 'Adept') || ($userfields['fidCC'] == 'Venerated') || ($userfields['fidCC'] == 'Virtuoso')) then>Apprentice SKILL<Br />
							<elseif (($userfields['fidAA'] == 'Adept') || ($userfields['fidAA'] == 'Venerated') || ($userfields['fidAA'] == 'Virtuoso')) && (($userfields['fidBB'] == 'Adept') || ($userfields['fidBB'] == 'Apprentice')) && $userfields['fidCC'] == 'Adept' || (($userfields['fidAA'] == 'Adept') || ($userfields['fidAA'] == 'Venerated') || ($userfields['fidAA'] == 'Virtuoso')) && $userfields['fidBB'] == 'Adept' && (($userfields['fidCC'] == 'Adept') || ($userfields['fidCC'] == 'Apprentice')) || (($userfields['fidAA'] == 'Adept') || ($userfields['fidAA'] == 'Apprentice')) && (($userfields['fidBB'] == 'Adept') || ($userfields['fidBB'] == 'Venerated') || ($userfields['fidBB'] == 'Virtuoso')) && $userfields['fidCC'] == 'Adept' || $userfields['fidAA'] == 'Adept' && (($userfields['fidBB'] == 'Adept') || ($userfields['fidBB'] == 'Venerated') || ($userfields['fidBB'] == 'Virtuoso')) && (($userfields['fidCC'] == 'Adept') || ($userfields['fidCC'] == 'Apprentice')) || (($userfields['fidAA'] == 'Adept') || ($userfields['fidAA'] == 'Apprentice')) &&  $userfields['fidBB'] == 'Adept' && (($userfields['fidCC'] == 'Adept') || ($userfields['fidCC'] == 'Venerated') || ($userfields['fidCC'] == 'Virtuoso')) || $userfields['fidAA'] == 'Adept' && (($userfields['fidBB'] == 'Adept') || ($userfields['fidBB'] == 'Apprentice')) &&  (($userfields['fidCC'] == 'Adept') || ($userfields['fidCC'] == 'Venerated') || ($userfields['fidCC'] == 'Virtuoso')) then>Adept SKILL<Br />
							<elseif (($userfields['fidAA'] == 'Venerated') || ($userfields['fidAA'] == 'Virtuoso')) && (($userfields['fidBB'] == 'Adept') || ($userfields['fidBB'] == 'Venerated') || ($userfields['fidBB'] == 'Virtuoso')) && (($userfields['fidCC'] == 'Venerated') || ($userfields['fidCC'] == 'Virtuoso')) || (($userfields['fidAA'] == 'Venerated') || ($userfields['fidAA'] == 'Virtuoso')) && (($userfields['fidBB'] == 'Venerated') || ($userfields['fidBB'] == 'Virtuoso')) && (($userfields['fidCC'] == 'Adept') || ($userfields['fidCC'] == 'Venerated') || ($userfields['fidCC'] == 'Virtuoso')) || (($userfields['fidAA'] == 'Adept') || ($userfields['fidAA'] == 'Venerated') || ($userfields['fidAA'] == 'Virtuoso')) && (($userfields['fidBB'] == 'Venerated') || ($userfields['fidBB'] == 'Virtuoso')) && (($userfields['fidCC'] == 'Venerated') || ($userfields['fidCC'] == 'Virtuoso')) || (($userfields['fidAA'] == 'Venerated') || ($userfields['fidAA'] == 'Virtuoso')) && (($userfields['fidBB'] == 'Adept') || ($userfields['fidBB'] == 'Venerated') || ($userfields['fidBB'] == 'Virtuoso')) && (($userfields['fidCC'] == 'Adept') || ($userfields['fidCC'] == 'Venerated') || ($userfields['fidCC'] == 'Virtuoso')) || (($userfields['fidAA'] == 'Adept') || ($userfields['fidAA'] == 'Venerated') || ($userfields['fidAA'] == 'Virtuoso')) &&  (($userfields['fidBB'] == 'Venerated') || ($userfields['fidBB'] == 'Virtuoso')) && (($userfields['fidCC'] == 'Venerated') || ($userfields['fidCC'] == 'Virtuoso')) || (($userfields['fidAA'] == 'Venerated') || ($userfields['fidAA'] == 'Virtuoso')) && (($userfields['fidBB'] == 'Adept') || ($userfields['fidBB'] == 'Venerated') || ($userfields['fidBB'] == 'Virtuoso')) &&  (($userfields['fidCC'] == 'Venerated') || ($userfields['fidCC'] == 'Virtuoso')) then>Venerated SKILL<Br />
							<else>Apprentice SKILL</else>
						</if>
					<!--End SKILL-->



I've got 7 different blocks of code that look like that - any help whatsoever would be really apprciated.

also, I tried changing the levels to numerical representations (0=None, 1 Novice, etc.), thinking I could use Greater or Equal to/Less than or Equal to input to eliminate some of the coding, but I couldn't get then Less than or Equal to conditional to work.

07-15-2013 02:44 PM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #2
RE: Condensing code?
(07-15-2013 02:44 PM)a.live Wrote:  but I couldn't get then Less than or Equal to conditional to work.
So what happens when you try to use it?

I'm not entirely sure what your conditionals are trying to achieve, there's a lot of logic in there after all.
Yes, I'd use numbers, and, looking at a few things, probably put them into a sorted array so that you don't have to write for every single combination of inputs.
This may be going a bit beyond simple template conditionals though.

Conceptual PHP:

PHP Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$skill2num = array('None' => 0, 'Novice' => 1, 'Apprentice' => 2, 'Adept' => 3, 'Venerated' => 4, 'Virtuoso' => 5);
$levels = array(
  (int)@$skill2num[$userfields['fidAA']],
  (int)@$skill2num[$userfields['fidBB']],
  (int)@$skill2num[$userfields['fidCC']],
);
sort($levels);

if($levels[0] == 0 && $levels[1] == 0) {
  // do nothing
} elseif($levels[0] == 1 && $levels[1] == 1 && $levels[2] >= 1) {
  echo 'Novice skill';
} elseif(($levels[0] == 1 || $levels[0] == 2) && $levels[1] == 2 && $levels[2] >= 2) { // unsure if this is correct, I'm too lazy to check through the entire conditional
  echo 'Apprentice skill';
}
// etc


My Blog
(This post was last modified: 07-16-2013 11:51 AM by ZiNgA BuRgA.)
07-16-2013 11:44 AM
Find all posts by this user Quote this message in a reply

« Next Oldest | Next Newest »

 Standard Tools
Forum Jump: