Log in

View Full Version : Help simplify repeated code



gwmbox
07-25-2013, 02:48 AM
Hi all, I have some php code I have been working on that sets a specific class per a rows column which works fine for one row, but if I have 12 rows how would I use the same code for each row without having to repeat the code for each row number

Here is my code


/* Does each item exist */
$row1c1 = (If exists) ? 1 : 0);
$row1c2 = (If exists) ? 2 : 0);
$row1c3 = (If exists) ? 3 : 0);
$row1c4 = (If exists) ? 4 : 0);

/* Total of items that exist */
$row1 = $row1c1 + $row1c2 + $row1c3 + $row1c4;

/* Set col for each row-c */
$row1c1_class = ($row1 = 10) ? 'col3' : ($row1 = 6) ? 'col4' : ($row1 = 4) ? 'col6' : ($row1 = 1) ? 'col12' : ($row1 = 9) ? 'col6' : ($row1 = 8) ? 'col3' : ($row1 = 7) ? 'col3' : ($row1 = 3) ? 'col3' : ($row1 = 5) ? 'col9' : '';
$row1c2_class = ($row1 = 10) ? 'col3' : ($row1 = 6) ? 'col4' : ($row1 = 4) ? '' : ($row1 = 1) ? '' : ($row1 = 9) ? '' : ($row1 = 8) ? '' : ($row1 = 7) ? 'col3' : ($row1 = 3) ? 'col9' : ($row1 = 5) ? '' : '';
$row1c3_class = ($row1 = 10) ? 'col3' : ($row1 = 6) ? 'col4' : ($row1 = 4) ? 'col6' : ($row1 = 1) ? '' : ($row1 = 9) ? 'col3' : ($row1 = 8) ? 'col6' : ($row1 = 7) ? '' : ($row1 = 3) ? '' : ($row1 = 5) ? '' : '';
$row1c4_class = ($row1 = 10) ? 'col3' : ($row1 = 6) ? '' : ($row1 = 4) ? '' : ($row1 = 1) ? '' : ($row1 = 9) ? 'col3' : ($row1 = 8) ? 'col3' : ($row1 = 7) ? 'col6' : ($row1 = 3) ? '' : ($row1 = 5) ? 'col3' : '';

To get the next row to be calculated the same way I could repeat the code like


/* Does each item exist */
$row2c1 = (If exists) ? 1 : 0);
$row2c2 = (If exists) ? 2 : 0);
$row2c3 = (If exists) ? 3 : 0);
$row2c4 = (If exists) ? 4 : 0);

/* Total of items that exist */
$row2 = $row2c1 + $row2c2 + $row2c3 + $row2c4;

/* Set col for each row-c */
$row2c1_class = ($row2 = 10) ? 'col3' : ($row2 = 6) ? 'col4' : ($row2 = 4) ? 'col6' : ($row2 = 1) ? 'col12' : ($row2 = 9) ? 'col6' : ($row2 = 8) ? 'col3' : ($row2 = 7) ? 'col3' : ($row2 = 3) ? 'col3' : ($row2 = 5) ? 'col9' : '';
$row2c2_class = ($row2 = 10) ? 'col3' : ($row2 = 6) ? 'col4' : ($row2 = 4) ? '' : ($row2 = 1) ? '' : ($row2 = 9) ? '' : ($row2 = 8) ? '' : ($row2 = 7) ? 'col3' : ($row2 = 3) ? 'col9' : ($row2 = 5) ? '' : '';
$row2c3_class = ($row2 = 10) ? 'col3' : ($row2 = 6) ? 'col4' : ($row2 = 4) ? 'col6' : ($row2 = 1) ? '' : ($row2 = 9) ? 'col3' : ($row2 = 8) ? 'col6' : ($row2 = 7) ? '' : ($row2 = 3) ? '' : ($row2 = 5) ? '' : '';
$row2c4_class = ($row2 = 10) ? 'col3' : ($row2 = 6) ? '' : ($row2 = 4) ? '' : ($row2 = 1) ? '' : ($row2 = 9) ? 'col3' : ($row2 = 8) ? 'col3' : ($row2 = 7) ? 'col6' : ($row2 = 3) ? '' : ($row2 = 5) ? 'col3' : '';

But if I do that for 12 rows that is a lot of repeated code.

Is there a way I can simplify it so the code is only used/entered once but it will provide results as if I was to use the code 12 times?

Thanks

GW

traq
07-25-2013, 04:33 AM
Hi all, I have some php code I have been working on that sets a specific class per a rows column which works fine for one row ...

The code you posted doesn't even parse.

"(If exists)", for example, is not something that exists.
I'm willing to bet a lot of the rest of it doesn't do what you expect ("($row1 = 10)", for example, will always be true - it simply assigns the value 10 to the variable $row1 and then checks if the result is truthy).
also, nesting the ternary operator ( ?: ?: ?: ... ) works exactly opposite from the way that most people expect.

Can you describe what the code is supposed to do, exactly? As a very general answer, you can put the code in a loop (perhaps for (http://php.net/for) or foreach (http://php.net/foreach)) to run it multiple times without rewriting it.

jscheuer1
07-25-2013, 04:43 AM
The code you posted doesn't even parse.

I was thinking a similar thing when I saw:



$row1c1_class = ($row1 = 10) ? 'col3'

Which will always be true.


Can you describe what the code is supposed to do, exactly? As a very general answer, you can put the code in a loop (perhaps for (http://php.net/for) or foreach (http://php.net/foreach)) to run it multiple times without rewriting it.

I was thinking more along the lines of making a prototype row and feeding it numbers and/or letters to get the various individual rows. I guess that could be done in a loop though.

traq
07-25-2013, 04:46 AM
I was thinking more along the lines of making a prototype row and feeding it numbers and/or letters to get the various individual rows. I guess that could be done in a loop though.

That could work too. I'm simply not clear enough on what the objective is.

gwmbox
07-25-2013, 06:32 AM
Hi sorry guys for the misunderstanding and my lack of php coding experience, I am trying to learn...

The code has not yet been run and tested it is just my first attempt and figuring out what I am trying to do.


$row1c1 = (If exists) ? 1 : 0);

The if exists is not the actual code used, it is just an if statement that checks for some code if it is there then the result is 1, 2, 3 or 4 depending on the row cell, if not then 0, for example


$row2c1 = if ($r1c1 != "") ? 1 : 0;
$row1c2 = if ($r1c2 != "") ? 2 : 0;
$row1c3 = if ($r1c3 != "") ? 3 : 0;
$row1c4 = if ($r1c4 != "") ? 4 : 0;

Based on what you have said the nested operators obviously I have not done that right so I will certainly take a look at it., I was just trying to simplify the if, then, else parts and must have got it wrong - sorry

Here is a more descriptive text of what I am trying to achieve

First I need to check if data exists in the four cells, if they do then the numbers 1, 2, 3 and 4 are applied.

The numbers 1, 2, 3 and 4 are used to calculate a number which is then assigned to a cell grid layout.


/* Total of items that exist */
$row1 = $row1c1 + $row1c2 + $row1c3 + $row1c4;

I then add up the if exists result being 1 + 2 + 3 + 4 or if one of them does not exist then 0 for that particular one, so it could be 1 + 0 + 0 +4 (as an example)

From that I want to have a class assigned to each row based on the total of the above (up to 10) and depending on what number it is a different class is assigned, some also have no classes assigned, e.g. col3 or col4 etc. There are 9 different possible outcomes for each cell (including blank ones)


$row1c1_class = if ($row1 = 10) ? 'col3' : if ($row1 = 6) ? 'col4' : if ($row1 = 4) ? 'col6' : if ($row1 = 1) ? 'col12' : if ($row1 = 9) ? 'col6' : if ($row1 = 8) ? 'col3' : if ($row1 = 7) ? 'col3' : if ($row1 = 3) ? 'col3' : if ($row1 = 5) ? 'col9';

But that may not be the right way to do it as you have said, can you show me the right way? The idea was to check if the $row1 equals 10, if so then apply class 'col3', if not then check if the $row1 = 6, if so apply class ' col4', if not then check if $row1 equals ... and do on.

Now as said there are 12 rows so I need to have the above applied to each row cell so in the end I will have a set of classes for $row1c1, $row1c2, $row1c3, $rwo1c4, $row2c1, #row2c2 ... all the way to $row12c4, (c1 to c4 are the four cells in each row) which is up to four cells per row. I was just looking for a way to not have to repeat the code for every row.

Does that make more sense now?

Cheers

traq
07-25-2013, 06:53 AM
Does that make more sense now?
a bit.


Based on what you have said the nested operators obviously I have not done that right ...
Maybe. Maybe not - what I meant was that it is unclear.


First I need to check if data exists in the four cells, if they do then the numbers 1, 2, 3 and 4 are applied.
The numbers 1, 2, 3 and 4 are used to calculate a number which is then assigned to a cell grid layout.
So, do I understand correctly that:
... $r1c1 ... $r1c4 are the values that will go in each row of your table
... $row1c1 ... $row1c4 are assigned "0" if the corresponding value is empty, and 1 ... 4 if not empty
... you are then choosing a class name for the row based on the sum of $row1c1 ... $row1c4
?


... based on the total of the above (up to 10) and depending on what number it is a different class is assigned, some also have no classes assigned, e.g. col3 or col4 etc
Can you tell us the class names that go with each total (including the empty class names)?

gwmbox
07-25-2013, 07:05 AM
So, do I understand correctly that:
... $r1c1 ... $r1c4 are the values that will go in each row of your table
... $row1c1 ... $row1c4 are assigned "0" if the corresponding value is empty, and 1 ... 4 if not empty
... you are then choosing a class name for the row based on the sum of $row1c1 ... $row1c4
?

Yes that is correct, the $r1c1 to $r1c4 are basically a 1 and 0 value, not exactly those values it is a case of if data is in that cell or not, if empty apply 0 if not empty apply 1, 2, 3, or 4


Can you tell us the class names that go with each total (including the empty class names)?

The class names are from col1 all the way up to col12 (not all may be needed I am still working that out), the empty class is going to be a null value as the cell is simply removed so for example there may be a row with only cell 1 and cell 3, here is a rough guide to the results depending on the total of $row1 (the value in the brackets () is that total number which assigns a rows grid layout) - I hope this does not make it more confusing...



+-----+-----+-----+-----+
| C1 | C2 | C3 | C4 | col3 col3 col3 col3 (10)
+-----+-----+-----+-----+
+-------+-------+-------+
| C1 | C2 | C3 | col4 col4 col4 (6)
+-------+-------+-------+
+-----------+-----------+
| C1 | C3 | col6 col6 (4)
+-----------+-----------+
+-----------------------+
| C1 | col12 (1)
+-----------------------+
+-----------+-----+-----+
| C2 | C3 | C4 | col6 col3 col3 (9)
+-----------+-----+-----+
+-----+-----------+-----+
| C1 | C3 | C4 | col3 col6 col3 (8)
+-----+-----------+-----+
+-----+-----+-----------+
| C1 | C2 | C4 | col3 col3 col6 (7)
+-----+-----+-----------+
+-----+-----------------+
| C1 | C2 | col3 col9 (3)
+-----+-----------------+
+-----------------+-----+
| C1 | C4 | col9 col3 (5)
+-----------------+-----+


So if you go back and have a look at my badly coded nested if statements, each row starts with something like (the ... meaning more code after that as above)


$row1c1_class = if ($row1 = 10) ? 'col3' : ...
$row2c1_class = if ($row1 = 10) ? 'col3' : ...
$row3c1_class = if ($row1 = 10) ? 'col3' : ...
$row4c1_class = if ($row1 = 10) ? 'col3' : ...

So because 10 is the value it assigns the classes col3 col3 col3 col3 for each of those $row1 cells (as shown in my diagram)

If the totalled value was 5 then the result would be that cell one will have a class col9, cell 2 and cell 3 would not exist and would be omitted as part of the first check and cell 4 would be col3

Is that making more sense?

traq
07-25-2013, 07:24 AM
Yes that is correct, the $r1c1 to $r1c4 are basically a 1 and 0 value, not exactly those values it is a case of if data is in that cell or not, if empty apply 0 if not empty apply 1
okay.


The class names are from col1 all the way up to col12 (not all may be needed I am still working that out), the empty class is going to be a null value as the cell is simply removed so for example there may be a row with only cell 1 and cell 3,
okay.


here is a rough guide to the results depending on the total of $row1 (the value in the brackets () is that total number which assigns a rows grid layout) - I hope this does not make it more confusing...



+-----+-----+-----+-----+
| C1 | C2 | C3 | C4 | col3 col3 col3 col3 (10)
+-----+-----+-----+-----+
+-------+-------+-------+
| C1 | C2 | C3 | col4 col4 col4 (6)
+-------+-------+-------+
+-----------+-----------+
| C1 | C3 | col6 col6 (4)
+-----------+-----------+
+-----------------------+
| C1 | col12 (1)
+-----------------------+
+-----------+-----+-----+
| C2 | C3 | C4 | col6 col3 col3 (9)
+-----------+-----+-----+
+-----+-----------+-----+
| C1 | C3 | C4 | col3 col6 col3 (8)
+-----+-----------+-----+
+-----+-----+-----------+
| C1 | C2 | C4 | col3 col3 col6 (7)
+-----+-----+-----------+
+-----+-----------------+
| C1 | C2 | col3 col9 (3)
+-----+-----------------+
+-----------------+-----+
| C1 | C4 | col9 col3 (5)
+-----------------+-----+

that's where you started losing me


So because 10 is the value it assigns the classes col3 col3 col3 col3 for each of those $row1 cells (as shown in my diagram)

If the totalled value was 5 then the result would be that cell one will have a class col9, cell 2 and cell 3 would not exist and would be omitted as part of the first check and cell 4 would be col3

Is that making more sense?
I don't understand the relationship between (for example) "3 + 3 + 3 +3" and "10". Is there a relationship, or are the patterns assigned numbers arbitrarily?

----------------------------------------------
unfortunately, it's past bedtime in my house. I will check in on this tomorrow, though.

gwmbox
07-25-2013, 07:46 AM
I don't understand the relationship between (for example) "3 + 3 + 3 +3" and "10". Is there a relationship, or are the patterns assigned numbers arbitrarily?.

No real relation ship other than they are used to assign the class values for each cell. so for the first row


+-----+-----+-----+-----+
| C1 | C2 | C3 | C4 | col3 col3 col3 col3 (10)
+-----+-----+-----+-----+

the 10 value is just an addition of the c#'s, so 1 + 2+ 3 + 4
col3 is merely a class value that is assigned to the cells for that row

Another example


+-----------+-----+-----+
| C2 | C3 | C4 | col6 col3 col3 (9)
+-----------+-----+-----+

Where the c# 2, 3 and 4 are totalled to equal 9 and this then applies the classes col6 to cell 2, and col3 to both cell3 and cell4

Thanks for your assistance - have a good rest :)

If anyone else is able to help in meantime that is great :)

Cheers

traq
07-25-2013, 03:24 PM
No real relation ship other than they are used to assign the class values for each cell.

Alright then; I'll leave that part to you.

- - - - - - - - - - - - - - - - - - - -

Changing this is going to be a lot of refactoring, and (judging from your current code examples) a lot of new concepts, which might be hard to wrap your head around at first. If you're willing, we'll go through it. If you're not, or if you don't have the time, you might want to simply repeat the code for each step.

First thing I would suggest is using associative arrays instead of a big collection of variables. For example, instead of using $r1c1, $r1c2, etc., use $r[1][1], $r[1][2], etc.. So:

<?php

// INSTEAD OF putting each value in its own, individual var...

$r1c1 = "hello, this text goes in row 1 column 1";
$r1c2 = "this goes in the next column!";
$r1c3 = "and so forth";
// . . .

// ...organize your data in an array:
// (like $data[row_number][column_number])

$data[1][1] = "hello, this text goes in row 1 column 1";
$data[1][2] = "this goes in the next column!";
$data[1][3] = "and so forth";
// . . .

// assign ALL row/column numbers, even if the value is blank
// (use "" (an empty string)).
// say there's no values for row 4, column 2:
$data[4][2] = "";
// . . .

// this will make it much easier to "loop" through the rows and columns,
// since they're all together in a "map."
Make sense? Let me know if you have any questions, then we'll start looping.

gwmbox
07-25-2013, 11:54 PM
You are right about it not making a lot of sense to me but I am willing to learn if you are willing to teach. Having said that I guess for me it is a case of weighing up what is the better way to do it that will result in the faster load time for the page, Since this will be part of a template the code is used and checked on every page.

The reason I was thinking it was not best to just repeat the code block 8 tiimes is I that I assumed it would take too much time to calculate each step and then load the results. My assumption was that if we could have the one code block which can then be used on each row would result in the page loading faster, or am I wrong on this?

i.e. if I repeated this code 8 times


/* Checkif data exists in each cell */
$row1c1 = if ($r1c1 != "") ? 1 : 0;
$row1c2 = if ($r1c2 != "") ? 2 : 0;
$row1c3 = if ($r1c3 != "") ? 3 : 0;
$row1c4 = if ($r1c4 != "") ? 4 : 0;

/* Total of items that exist */
$row1 = $row1c1 + $row1c2 + $row1c3 + $row1c4;

/* Use calculated total to match a class for each row/cell */
$row1c1_class = if ($row1 = 10) ? 'col3' : if ($row1 = 6) ? 'col4' : if ($row1 = 4) ? 'col6' : if ($row1 = 1) ? 'col12' : if ($row1 = 9) ? 'col6' : if ($row1 = 8) ? 'col3' : if ($row1 = 7) ? 'col3' : if ($row1 = 3) ? 'col3' : if ($row1 = 5) ? 'col9' : '';
$row1c2_class = if ($row1 = 10) ? 'col3' : if ($row1 = 6) ? 'col4' : if ($row1 = 4) ? '' : if ($row1 = 1) ? '' : if ($row1 = 9) ? '' : if ($row1 = 8) ? '' : if ($row1 = 7) ? 'col3' : if ($row1 = 3) ? 'col9' : if ($row1 = 5) ? '' : '';
$row1c3_class = if ($row1 = 10) ? 'col3' : if ($row1 = 6) ? 'col4' : if ($row1 = 4) ? 'col6' : if ($row1 = 1) ? '' : if ($row1 = 9) ? 'col3' : if ($row1 = 8) ? 'col6' : if ($row1 = 7) ? '' : if ($row1 = 3) ? '' : if ($row1 = 5) ? '' : '';
$row1c4_class = if ($row1 = 10) ? 'col3' : if ($row1 = 6) ? '' : if ($row1 = 4) ? '' : if ($row1 = 1) ? '' : if ($row1 = 9) ? 'col3' : if ($row1 = 8) ? 'col3' : if ($row1 = 7) ? 'col6' : if ($row1 = 3) ? '' : if ($row1 = 5) ? 'col3' : '';


I thought that repeating this code 8 times it would be too much code to go through and therefore result in a slowing of the pages loading time.

My request to simplify it was to try and have less code needed to do the same which would result in a much faster load time since the code block would be much smaller.

Or would the loop idea you have not really result in any time saving?

If both are just too much then maybe I need to abandon this and try something else or just apply the values in a more static approach.

Am I correct in that or not?

Oh can you recommend me a web site or book for learning php but written in a way that is easy to understand and learn from a non programmers knowledge, i.e. a noob to any programming language. I have a basic understanding but would like to learn much more.

Thanks

GW

traq
07-26-2013, 01:59 AM
The reason I was thinking it was not best to just repeat the code block 8 tiimes is I that I assumed it would take too much time to calculate each step and then load the results. My assumption was that if we could have the one code block which can then be used on each row would result in the page loading faster, or am I wrong on this?

It's more an issue of maintainability. While you'll probably never notice a difference in execution time (even horribly inefficient code is pretty dang fast), you will have less opportunity for errors and much more flexible code. It will also be easier to "figure out" if you ever need to make changes, add features, or (hint hint) explain it to somebody.


Oh can you recommend me a web site or book for learning php but written in a way that is easy to understand and learn from a non programmers knowledge, i.e. a noob to any programming language. I have a basic understanding but would like to learn much more.

mmm.... not specifically. Practice (practice, practice) reading the docs on php.net (http://php.net); they're a great resource. Books are tougher: most, if not all, are bad examples. Unfortunately, many online tutorials aren't that great either, and it takes a certain degree of experience to distinguish the good from the bad... it's a chicken-and-egg problem.

-----------------------------------------------------
moving on!

did you figure out how to get your data into an array?

once you do, let's put all of your class names in an array also. This will make them easier to access later:
<?php

// this is a sum => class name map, in the format
// "$class[sum] = class_name"

// as before, DON'T SKIP any indexes - use an empty string ( "" ) as the value instead

// (I took these values from your earlier code; you should check to make sure it's correct.)

$class[1] = "col12";
$class[2] = "";
$class[3] = "col3";
$class[4] = "col6";
$class[5] = "col9";
$class[6] = "col4";
$class[7] = "col3";
$class[8] = "col3";
$class[9] = "col6";
$class[10] = "col3";
$class[11] = "";
$class[12] = "";


just FYI, you can also make arrays all-at-once - I choose to show you the above method because, in this case, I think it's more straightforward.
But the following gives _exactly_ the same result:
$class = array(
1 => "col12"
,2 => ""
,3 => "col3"
,4 => "col6"
,5 => "col9"
,6 => "col4"
,7 => "col3"
,8 => "col3"
,9 => "col6"
,10 => "col3"
,11 => ""
,12 => ""
);

gwmbox
07-26-2013, 06:16 AM
It's more an issue of maintainability. While you'll probably never notice a difference in execution time (even horribly inefficient code is pretty dang fast), you will have less opportunity for errors and much more flexible code. It will also be easier to "figure out" if you ever need to make changes, add features, or (hint hint) explain it to somebody.

So what would you recommend, I repeat the code or persist with this and try and make it less code?


did you figure out how to get your data into an array?.

Ah sorry no, I had a look at what I have and could not work out how I would apply it as there is no specific content in each cell, the data that goes in the cell could be nothing or could be something, hence why I had a simple if not equal to no content then assign 1, 2,3 or 4 depending on what cell it was.


once you do, let's put all of your class names in an array also. This will make them easier to access later:

I need to read more as I am not quite understanding how that helps, is that list then looped through for each row of cells? I am not seeing the connection...

Thanks

traq
07-26-2013, 03:53 PM
So what would you recommend, I repeat the code or persist with this and try and make it less code?
Learning how to write the code more efficiently will definitely benefit you, not only with this particular code, but also with anything you try to write in the future. I would recommend giving it a shot (though it is of course up to you).


Ah sorry no, I had a look at what I have and could not work out how I would apply it as there is no specific content in each cell, the data that goes in the cell could be nothing or could be something, hence why I had a simple if not equal to no content then assign 1, 2,3 or 4 depending on what cell it was.
How do you generate the content of each cell now? It should be as simple as changing the assignment line (using something like "$data[1][2]" instead of "$r1c2").


I need to read more as I am not quite understanding how that helps, is that list then looped through for each row of cells? I am not seeing the connection...
Putting all the info together in an array make it easier for us to access/ search/ use later. For example, to print all the values in a row, you would currently need to do:
print $r1c1;
print $r1c2;
print $r1c3;
print $r1c4;
print $r1c5;
print $r1c6;
print $r1c7;
print $r1c8;
print $r1c9;
print $r1c10;
print $r1c11;
print $r1c12;

If the values were in an array, you could simply get the row you wanted and then loop through each column. For example, this loop prints the values of each column in row #1 (the same result as above):

foreach( $data[1] as $column ){
print $column;
}

gwmbox
07-30-2013, 01:24 AM
Sorry I have not replied sooner I have been ill

Thanks for what you have done so fare it is helping me get a better understanding and herping me get closer to the solution I am after.

Hopefully this makes it easier to understand how the $r1c1 cells are being populated. The code is being developed to be part of an overall set of templates for a range of different CMS systems, e.g QuickCMS, Joomla, Get Simple CMS, Project Wire CMS and Wolf CMS - to name a few. Each will have their own tweaks that are specific to each CMS but I am sure I can get this done if I can work this part out.

For us to work this out now I think is we work with Joomla it will be easier as it already has an easy built in method to check if content exists in a template position.

Joomla by default has a check process which uses code like


<?php if ($this->countModules('pos-row1c1')) : ?>

which then checks to see if any modules are assigned to that position, if it does then it has content in that cell, if no modules are assigned to that position then no content exists in that cell, hence my check for no content, if there is content then the result is 1, otherwise result is 0, as per this code


$row1c1 = if ($this->countModules('pos-row1c1') ? 1 : 0;

All the $data[1][2] will need to be $row[1][2] so it is identified as a row and it can be assigned a row number.

There is no specific data in each cell, it can be anything, however in Joomla it is a simple case of does a module exist in that position or not.

Does that help understand what i am trying to achieve?

Cheers

GW

traq
07-30-2013, 01:33 AM
I see. You might not be able to use what I had in mind, then - and I'm afraid I don't have much experience with Joomla.

There's a more complex approach I had thought of earlier, which involves variable variables (http://php.net/manual/en/language.variables.variable.php). It's something of an odd concept. Let me write an example.

gwmbox
07-31-2013, 07:25 AM
That is why I was trying to 'not' mention it was Joomla or any other specific CMS, as we need to take that out of the equation when we look at trying to resolve what I am doing. What I mean here is imagine I never mentioned it was CMS related, or in particular Joomla related.

The variables or the content of $row1c1 is still a case of if empty the result is 0, if it has ANY content then the result is 1, or for $row1c2 it is 2, for $row1c3 it is 3 and for $row1c4 it is 4. Is there a way we can do this without it having to be Joomla related?

Cheers

traq
08-01-2013, 04:31 AM
That is why I was trying to 'not' mention it was Joomla or any other specific CMS, as we need to take that out of the equation when we look at trying to resolve what I am doing. What I mean here is imagine I never mentioned it was CMS related, or in particular Joomla related.

Well, yes and no; I came up with my suggested refactoring based on the assumption that it was "plain PHP" - and the idea turned out to be unusable because it's inside Joomla. So, you didn't get "shut out" right away, but you did manage to waste some time :).

If you "never mentioned" it was Joomla, we'd still be milling around, wondering why you "can't use" this solution, that solution, etc. - it would be frustrating.

- - - - - - - - - - - - - - - - - - - - - - - - -
Here's the basics of the new plan:


<?php

// still recommending putting classnames in an array.
// $classnames[ row_sum ] = array( col_number=>classname ,col_number=>classname , . . . )

// these values taken from your earlier examples.
// double-check (some seem to be missing; row sum=2, for example).
$classnames[10] = array( 1=>'col3' ,2=>'col3' ,3=>'col3' ,4=>'col3' );
$classnames[6] = array( 1=>'col4' ,2=>'col4' ,3=>'col4' ,4=>'' );
$classnames[4] = array( 1=>'col6' ,2=>'' ,3=>'col6' ,4=>'' );
$classnames[1] = array( 1=>'col12',2=>'' ,3=>'' ,4=>'' );
$classnames[9] = array( 1=>'col6' ,2=>'' ,3=>'col3' ,4=>'col3' );
$classnames[8] = array( 1=>'col3' ,2=>'' ,3=>'col6' ,4=>'col3' );
$classnames[7] = array( 1=>'col3' ,2=>'col3' ,3=>'' ,4=>'col6' );
$classnames[3] = array( 1=>'col3' ,2=>'col9' ,3=>'' ,4=>'' );
$classnames[5] = array( 1=>'col9' ,2=>'' ,3=>'' ,4=>'col3' );

// loop through 12 rows
for( $row=1; $row<=12; $row++ ){

// this will hold the sum of all the values tallied
$row_sum = 0;

// loop through 4 columns
for( $col=1; $col<=4; $col++ ){

// "row-c" variable
$rc = "row{$row}c{$col}";

// if the column has content
if( $this->countModules( "pos-$rc" ) ){

// add the col number (1 ... 4) to the row total
$row_sum += $col;
}
}

// assign classname set for this row
$class[$row] = $classnames[$row_sum];
}

IIUC, this should be compatible with the Joomla methods you're using. When you're done, $class will be a map of classnames to apply to each row and column - for example,
<table>
<tr id=row1>
<td id=row1c1 class=<?= $class[1][1] ?>>whatever goes in col1</td>
<td id=row1c2 class=<?= $class[1][2] ?>>whatever goes in col2</td>
<td id=row1c3 class=<?= $class[1][3] ?>>whatever goes in col3</td>
<td id=row1c4 class=<?= $class[1][4] ?>>whatever goes in col4</td>
</tr>
<!-- etc. . . . -->
</table>

gwmbox
08-04-2013, 02:09 AM
Adrian. I do appreciate your time and will see if there is anything I can do with what you have provided. To stop wasting any more of your time I check to see if what you have provided can be used, if not I will drop this idea and go to a more manual approach. It seemed like a nice idea but seems to be far too complicated to get a fairly simple result, I guess with my limited php knowledge I thought it would be easier than it is hence why I asked for help initially.

Again thanks for your time.

GW

traq
08-04-2013, 06:02 PM
Adrian. I do appreciate your time and will see if there is anything I can do with what you have provided.
you are more than welcome. :)


To stop wasting any more of your time I check to see if what you have provided can be used, if not I will drop this idea and go to a more manual approach.
Please don't misunderstand - the "waste of time" was trying to create a solution without knowing all the details. "Helping you out" is not a waste of time.


It seemed like a nice idea but seems to be far too complicated to get a fairly simple result, I guess with my limited php knowledge I thought it would be easier than it is hence why I asked for help initially.
It's not that it's overly-complicated; just that it involves new concepts. Loops and multi-dimensional arrays can be hard to understand when you first encounter them, but, with practice, they get easier. And they're definitely "worth it." Let me know if you have any further questions!