The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
PHP-General - PHP Help - Consecutive Numbering
Discuss PHP Help - Consecutive Numbering in the PHP Development forum on Dev Shed. PHP Help - Consecutive Numbering PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 17th, 2012, 10:03 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 5
Time spent in forums: 15 m 12 sec
Reputation Power: 0
|
|
|
PHP-General - 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!
|

October 18th, 2012, 02:25 AM
|
 |
Confused badger
|
|
Join Date: Mar 2009
Location: West Yorkshire
|
|
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 != 1 && $rcounter != 3 && $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.
__________________
The number for UK Emergencies is changing, the new number is 0118 999 881 999 119 7253
"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
|

October 18th, 2012, 02:54 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 5
Time spent in forums: 15 m 12 sec
Reputation Power: 0
|
|
|
Thanks for the reply . . . but I don't know how to implement that to make it work . . .
|

October 18th, 2012, 03:17 AM
|
 |
Confused badger
|
|
Join Date: Mar 2009
Location: West Yorkshire
|
|
Quote: | 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(
0 => "Badger",
1 => "Fruit",
2 => "Cat",
3 => "Dog",
4 => "Fish",
5 => "Sausages",
6 => "Bacon!!!!",
7 => "Eggs",
8 => "Tea, white with 3 sugars",
9 => "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(
0 => "Badger",
1 => "Fruit",
2 => "Cat",
3 => "Dog",
4 => "Fish",
5 => "Sausages",
6 => "Bacon!!!!",
7 => "Eggs",
8 => "Tea, white with 3 sugars",
9 => "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 != 4 && $key != 5 && $key != 6 && $key != 7 && $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.
|

October 18th, 2012, 03:27 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 5
Time spent in forums: 15 m 12 sec
Reputation 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";
|

October 18th, 2012, 04:06 AM
|
 |
Confused badger
|
|
Join Date: Mar 2009
Location: West Yorkshire
|
|
|
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
|

October 18th, 2012, 04:44 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 5
Time spent in forums: 15 m 12 sec
Reputation 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;
|

October 18th, 2012, 05:36 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 5
Time spent in forums: 15 m 12 sec
Reputation 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!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|