Help simplify repeated code
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
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
Code:
/* 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