Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: Help simplify repeated code

  1. #1
    Join Date
    Mar 2005
    Location
    Western Australia
    Posts
    132
    Thanks
    19
    Thanked 3 Times in 3 Posts

    Default 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
    1st rule of web development - use Firefox and Firebug
    2nd rule - see the first rule
    --
    I like Smilies

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by gwmbox View Post
    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).
    Edit: 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 or foreach) to run it multiple times without rewriting it.
    Last edited by traq; 07-25-2013 at 04:58 AM.

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,926
    Thanks
    43
    Thanked 3,190 Times in 3,154 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by traq View Post
    The code you posted doesn't even parse.
    I was thinking a similar thing when I saw:

    Quote Originally Posted by gwmbox View Post
    Code:
    $row1c1_class = ($row1 = 10) ? 'col3'
    Which will always be true.

    Quote Originally Posted by traq View Post
    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 or 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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by jscheuer1 View Post
    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.

  5. #5
    Join Date
    Mar 2005
    Location
    Western Australia
    Posts
    132
    Thanks
    19
    Thanked 3 Times in 3 Posts

    Default

    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.

    Code:
    $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

    Code:
    $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.

    Code:
    /* 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)

    Code:
    $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
    Last edited by gwmbox; 07-25-2013 at 06:52 AM. Reason: More info hopefully more helpful
    1st rule of web development - use Firefox and Firebug
    2nd rule - see the first rule
    --
    I like Smilies

  6. #6
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by gwmbox View Post
    Does that make more sense now?
    a bit.

    Quote Originally Posted by gwmbox View Post
    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.

    Quote Originally Posted by gwmbox View Post
    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
    ?

    Quote Originally Posted by gwmbox View Post
    ... 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)?

  7. #7
    Join Date
    Mar 2005
    Location
    Western Australia
    Posts
    132
    Thanks
    19
    Thanked 3 Times in 3 Posts

    Default

    Quote Originally Posted by traq View Post
    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

    Quote Originally Posted by traq View Post
    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...

    Code:
    +-----+-----+-----+-----+
    | 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)

    Code:
    $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?
    Last edited by gwmbox; 07-25-2013 at 07:25 AM. Reason: more helpful info
    1st rule of web development - use Firefox and Firebug
    2nd rule - see the first rule
    --
    I like Smilies

  8. #8
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by gwmbox View Post
    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.

    Quote Originally Posted by gwmbox View Post
    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...

    Code:
    +-----+-----+-----+-----+
    | 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

    Quote Originally Posted by gwmbox View Post
    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.
    Last edited by traq; 07-25-2013 at 02:56 PM.

  9. #9
    Join Date
    Mar 2005
    Location
    Western Australia
    Posts
    132
    Thanks
    19
    Thanked 3 Times in 3 Posts

    Default

    Quote Originally Posted by traq View Post
    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

    Code:
    +-----+-----+-----+-----+
    | 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

    Code:
    +-----------+-----+-----+
    | 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
    1st rule of web development - use Firefox and Firebug
    2nd rule - see the first rule
    --
    I like Smilies

  10. #10
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by gwmbox View Post
    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 Code:
    <?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.

Similar Threads

  1. How to simplify this simple code?
    By smansakra in forum Flash
    Replies: 2
    Last Post: 07-19-2011, 06:09 AM
  2. Replies: 1
    Last Post: 10-06-2009, 11:40 AM
  3. please help me simplify this code!
    By guidoo in forum JavaScript
    Replies: 1
    Last Post: 04-08-2009, 01:00 PM
  4. can anyone simplify this
    By bonesten in forum HTML
    Replies: 2
    Last Post: 10-16-2006, 10:10 PM
  5. can anyone simplify this
    By bonesten in forum The lounge
    Replies: 0
    Last Post: 10-16-2006, 02:23 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •