#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0

    PHP Help - Consecutive Numbering


    I'm trying to hide some specific <tr> rows on the registration form in Virtuemart that are not needed, but there is no way to 'target' them as it uses the same piece of coding to create all the rows.

    What we have at the moment is:

    echo ' <table class="adminForm user-details">' . "\n";
    $_table = true;
    }
    echo ' <tr>' . "\n";
    echo ' <td class="key" title="'.$_field['description'].'" >' . "\n";
    echo ' <label class="' . $_field['name'] . '" for="' . $_field['name'] . '_field">' . "\n";
    echo ' ' . $_field['title'] . ($_field['required'] ? ' *' : '') . "\n";

    Where the td class is simply called "key" and all td are then called the same. We are trying to add in some consecutive lettering functionality so that each row then gets labelled with a consecutive number.

    We've tried adding this into the top of the page:

    <?php
    // array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i');
    foreach (range('a', 'z') as $letter)
    ?>
    Which does generate consecutive letters, and then this change to the code:

    echo ' <td class="key td-id-'.$letter.'" title="'.$_field['description'].'" >' . "\n";

    Any thoughts on how to get this to work?

    Thanks in advance!
  2. #2
  3. Confused badger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2009
    Location
    West Yorkshire
    Posts
    1,112
    Rep Power
    487
    The one thought that springs to mind is in the loop, have an incrementing counter and have an if statement determine if the row should be shown or not ... something like this:-

    PHP Code:
    // This goes inside the TR creation loop
    $rcounter++;
    if (
    $rcounter != && $rcounter != && $rcounter != 7)  {
        
    // Display the row
        
    echo "<tr ..... >";

    Obviously, change the numbers for the rows you don't wish to display and add/decrease the amount of 'and' conditions accordingly.
    "For if leisure and security were enjoyed by all alike, the great mass of human beings who are normally stupefied by poverty would become literate and would learn to think for themselves; and when once they had done this, they would sooner or later realise that the privileged minority had no function and they would sweep it away"
    - George Orwell, 1984
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0
    Thanks for the reply . . . but I don't know how to implement that to make it work . . .
  6. #4
  7. Confused badger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2009
    Location
    West Yorkshire
    Posts
    1,112
    Rep Power
    487
    Originally Posted by CMYKreative
    Thanks for the reply . . . but I don't know how to implement that to make it work . . .
    OK well you've not posted enough code to show the actual loop but consider this example and perhaps you'll have a better understanding of where and how to use it?

    PHP Code:
    // Lets create an array of data, from which we'll build the table ....
    $tarray = array(
      
    => "Badger"
      
    => "Fruit"
      
    => "Cat"
      
    => "Dog",
       
    => "Fish"
      
    => "Sausages"
      
    => "Bacon!!!!"
      
    => "Eggs"
      
    => "Tea, white with 3 sugars"
      
    => "My Wife"
      
    10 => "Ella"
      
    11 => "Jake"
      
    12 => "Jake dog"
    );

    // Now we want to make a table with each array element being the title ...
    // First, we define the non-repeating elements of the table (eg the headers)
    echo "<table width='100%' border=1>";
      echo 
    "<tr>";
        echo 
    "<td>";
          echo 
    "Header";
        echo 
    "</td>";
      echo 
    "</tr>";

    // Now we go through all the elements in the array, creating rows as we go .... 
    foreach ($tarray as $value) {
      echo 
    "<tr>";
        echo 
    "<td>";
          echo 
    $value;
        echo 
    "</td>";
      echo 
    "</tr>";
    }

    // And finally, we close the table neatly ...
    echo "</table><br />"
    OK, so the example above will give you an idea on the structure you have now. So now, let's use the same code above but with some modifications, we don't want to show all food items for example ... we know that they're in the array at positions 4,5,6,7, and 8 ...

    PHP Code:
    // Lets create an array of data, from which we'll build the table ....
    $tarray = array(
      
    => "Badger"
      
    => "Fruit"
      
    => "Cat"
      
    => "Dog",
       
    => "Fish"
      
    => "Sausages"
      
    => "Bacon!!!!"
      
    => "Eggs"
      
    => "Tea, white with 3 sugars"
      
    => "My Wife"
      
    10 => "Ella"
      
    11 => "Jake"
      
    12 => "Jake dog"
    );

    // Now we want to make a table with each array element being the title ...
    // First, we define the non-repeating elements of the table (eg the headers)
    echo "<table width='100%' border=1>";
      echo 
    "<tr>";
        echo 
    "<td>";
          echo 
    "Header";
        echo 
    "</td>";
      echo 
    "</tr>";

    // Now we go through all the elements in the array, creating rows as we go .... 
    // Here is the first change, we need to know the $key number (0 to 12) so we can determine if we're going to show it or not ...
    // Previously is was "foreach ($tarray as $value) {" but now it's "foreach ($tarray as $key => $value) {", it's a subtle change but an important one!
    foreach ($tarray as $key => $value) {

    // Here's the next change; the IF statement ... we need to check IF the $key is not equal to any of our "banned" item numbers
    if ($key != && $key != && $key != && $key != && $key != 8) {

    // If it's not, then it will do this bit
      
    echo "<tr>";
        echo 
    "<td>";
          echo 
    $value;
        echo 
    "</td>";
      echo 
    "</tr>";

    // Here is the end of the IF statement
    }

    }

    // And finally, we close the table neatly ...
    echo "</table><br />"
    As you can see, the table is created and happily skips over the "banned" row numbers.
    "For if leisure and security were enjoyed by all alike, the great mass of human beings who are normally stupefied by poverty would become literate and would learn to think for themselves; and when once they had done this, they would sooner or later realise that the privileged minority had no function and they would sweep it away"
    - George Orwell, 1984
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0
    Thanks again . . . I'll look into it and see if we can get it to work.

    This was the original code:

    echo ' <table class="adminForm user-details">' . "\n";
    $_table = true;
    }
    echo ' <tr>' . "\n";
    echo ' <td class="key" title="'.$_field['description'].'" >' . "\n";
    echo ' <label class="' . $_field['name'] . '" for="' . $_field['name'] . '_field">' . "\n";
    echo ' ' . $_field['title'] . ($_field['required'] ? ' *' : '') . "\n";
    echo ' </label>' . "\n";
    echo ' </td>' . "\n";
    echo ' <td>' . "\n";
    echo ' ' . $_field['formcode'] . "\n";
    echo ' </td>' . "\n";
    echo ' </tr>' . "\n";
    }
    }

    if ($_table) {
    echo ' </table><br>' . "\n";
  10. #6
  11. Confused badger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2009
    Location
    West Yorkshire
    Posts
    1,112
    Rep Power
    487
    CMYKreative, in the code you've copy/pasted, there are two "closing" curly braces but no "opening" ones so you're in a loop or a conditional statement already.
    But regardless, I'm sure with a bit of digging, you'll be able to impliment something along the lines of my examples!
    Good luck
    "For if leisure and security were enjoyed by all alike, the great mass of human beings who are normally stupefied by poverty would become literate and would learn to think for themselves; and when once they had done this, they would sooner or later realise that the privileged minority had no function and they would sweep it away"
    - George Orwell, 1984
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0
    Yes, I only snipped out the relevant area of the code . . . we'll give it a go and see what we can come up with.

    The full code is here:

    <?php


    // Check to ensure this file is included in Joomla!
    defined('_JEXEC') or die('Restricted access');



    $typefields = array('corefield', 'billto');
    $corefields = VirtueMartModelUserfields::getCoreFields();
    foreach ($typefields as $typefield) {
    $_k = 0;
    $_set = false;
    $_table = false;
    $_hiddenFields = '';

    // for ($_i = 0, $_n = count($this->userFields['fields']); $_i < $_n; $_i++) {
    for ($_i = 0, $_n = count($this->userFields['fields']); $_i < $_n; $_i++) {
    // Do this at the start of the loop, since we're using 'continue' below!
    if ($_i == 0) {
    $_field = current($this->userFields['fields']);
    } else {
    $_field = next($this->userFields['fields']);
    }

    if ($_field['hidden'] == true) {
    $_hiddenFields .= $_field['formcode'] . "\n";
    continue;
    }
    if ($_field['type'] == 'delimiter') {
    if ($_set) {
    // We're in Fieldset. Close this one and start a new
    if ($_table) {
    echo ' </table>' . "\n";
    $_table = false;
    }
    echo '</fieldset>' . "\n";
    }
    $_set = true;
    echo '<fieldset>' . "\n";
    echo ' <legend>' . "\n";
    echo ' ' . $_field['title'];
    echo ' </legend>' . "\n";
    continue;
    }



    if (($typefield == 'corefield' && (in_array($_field['name'], $corefields) && $_field['name'] != 'email' && $_field['name'] != 'agreed') )
    or ($typefield == 'billto' && !(in_array($_field['name'], $corefields) && $_field['name'] != 'email' && $_field['name'] != 'agreed') )) {
    if (!$_table) {
    // A table hasn't been opened as well. We need one here,
    if ( $typefield == 'corefield') {
    echo '<span class="userfields_info">' . $this->corefield_title . '</span>';
    } else {
    echo '<span class="userfields_info">' . $this->vmfield_title . '</span>';
    }


    echo ' <table class="adminForm user-details">' . "\n";
    $_table = true;
    }
    echo ' <tr>' . "\n";
    echo ' <td class="key" title="'.$_field['description'].'" >' . "\n";
    echo ' <label class="' . $_field['name'] . '" for="' . $_field['name'] . '_field">' . "\n";
    echo ' ' . $_field['title'] . ($_field['required'] ? ' *' : '') . "\n";
    echo ' </label>' . "\n";
    echo ' </td>' . "\n";
    echo ' <td>' . "\n";
    echo ' ' . $_field['formcode'] . "\n";
    echo ' </td>' . "\n";
    echo ' </tr>' . "\n";
    }
    }

    if ($_table) {
    echo ' </table><br>' . "\n";
    }
    if ($_set) {
    echo '</fieldset>' . "\n";
    }
    $_k = 0;
    $_set = false;
    $_table = false;
    $_hiddenFields = '';
    if(is_array($this->userFields['fields'])) {
    reset($this->userFields['fields']);
    }

    }

    echo $_hiddenFields;
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0
    OK, I found a quick and easy solution:

    echo ' <tr class="key td-id-' . $_field['name'] . '">' . "\n";

    I just added the existing ' . $_field['name'] . ' code to the tr and I was then able to target this in the way I wanted.

    Thanks for the help!

IMN logo majestic logo threadwatch logo seochat tools logo