Page 2 of 2 FirstFirst 12
Results 11 to 20 of 20

Thread: Help simplify repeated code

  1. #11
    Join Date
    Mar 2005
    Location
    Western Australia
    Posts
    148
    Thanks
    24
    Thanked 4 Times in 4 Posts

    Default

    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
    PHP Code:
    /* Checkif data exists in each cell */
    $row1c1 = if ($r1c1 != "") ? 0;
    $row1c2 = if ($r1c2 != "") ? 0;
    $row1c3 = if ($r1c3 != "") ? 0;
    $row1c4 = if ($r1c4 != "") ? 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
    Last edited by gwmbox; 07-26-2013 at 12:03 AM. Reason: better info and php code wrap
    1st rule of web development - use Firefox and Firebug
    2nd rule - see the first rule
    --
    I like Smilies

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

    Default

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

    Quote Originally Posted by gwmbox View Post
    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; 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 Code:
    <?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] = "";
    Edit: 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:
    PHP Code:
    $class = array(
        
    1  => "col12"
       
    ,2  => ""
       
    ,3  => "col3"
       
    ,4  => "col6"
       
    ,5  => "col9"
       
    ,6  => "col4"
       
    ,7  => "col3"
       
    ,8  => "col3"
       
    ,9  => "col6"
       
    ,10 => "col3"
       
    ,11 => ""
       
    ,12 => ""
    ); 
    Last edited by traq; 07-26-2013 at 02:07 AM. Reason: example

  3. #13
    Join Date
    Mar 2005
    Location
    Western Australia
    Posts
    148
    Thanks
    24
    Thanked 4 Times in 4 Posts

    Default

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

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

    Quote Originally Posted by traq View Post
    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
    1st rule of web development - use Firefox and Firebug
    2nd rule - see the first rule
    --
    I like Smilies

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

    Default

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

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

    Quote Originally Posted by gwmbox View Post
    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:
    PHP Code:
    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):
    PHP Code:
    foreach( $data[1] as $column ){
        print 
    $column;


  5. #15
    Join Date
    Mar 2005
    Location
    Western Australia
    Posts
    148
    Thanks
    24
    Thanked 4 Times in 4 Posts

    Default

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

    PHP Code:
    $row1c1 = if ($this->countModules('pos-row1c1') ? 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
    1st rule of web development - use Firefox and Firebug
    2nd rule - see the first rule
    --
    I like Smilies

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

    Default

    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. It's something of an odd concept. Let me write an example.

  7. #17
    Join Date
    Mar 2005
    Location
    Western Australia
    Posts
    148
    Thanks
    24
    Thanked 4 Times in 4 Posts

    Default

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

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

    Default

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

  9. #19
    Join Date
    Mar 2005
    Location
    Western Australia
    Posts
    148
    Thanks
    24
    Thanked 4 Times in 4 Posts

    Default

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

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

    Default

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

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

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

  11. The Following User Says Thank You to traq For This Useful Post:

    gwmbox (08-06-2013)

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
  •