Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Setting up variable id’s for <tr> tags within PHP
 
sherrie
Moderator
 
Avatar
Total Posts:  1655
Joined:  2007-12-14
Waterloo, ON
 

Alright, I’m trying to add the following code to Fieldset.php

for($i 0$i <= 100$i++) {
        
if ($element->getHtmlId() == "carriers_multiflat"{
        
if ($html.= '<tr>'{
        $html
.= '<tr id="'.div.$i.'">';
        
}
        }
        }

but all it does is adds the 100 <tr id="div1" /> (div2, div3, etc) tags before the actual <tr> tags being used

<tbody>
<
tr/>
<
tr id="div0"/>
<
tr/>
<
tr id="div1"/>
...
<
tr>
<
td class="label"Enabled</td>
<
td class="value">
<
select id="carriers_multiflat_active" class="select" type="select" name="groups[multiflat][fields][active][value]">
<
option selected="selected" value="1">Yes</option>
<
option value="0">No</option>
</
select>
</
td>
</
tr>

I’m not sure why it’s adding the slash or putting empty ones in between either.

What I am trying to get it to do is to assign id="div1" (with 1 being a variable) to all the <tr> tags within the fieldset with the id="carriers_mutliflat" ... it is only adding the tags to that fieldset, so I know I got that part right, but something with assigning it to the <tr> tags is not working properly.  I’m sure this is a simple oversight ... can anyone point me in the right direction?

 
Magento Community Magento Community
Magento Community
Magento Community
 
nikefido
Guru
 
Avatar
Total Posts:  481
Joined:  2008-07-11
New Haven, CT
 

What are you testing for with “if(’<tr>’)” ??

 
Magento Community Magento Community
Magento Community
Magento Community
 
sherrie
Moderator
 
Avatar
Total Posts:  1655
Joined:  2007-12-14
Waterloo, ON
 

I want it to replace all instances of <tr> (within the carriers_mutliflat fieldset) with <tr id="div1"> (etc).

 
Magento Community Magento Community
Magento Community
Magento Community
 
nikefido
Guru
 
Avatar
Total Posts:  481
Joined:  2008-07-11
New Haven, CT
 

within “if” statements, you need to test with an “is equal” oporator, for instance

if($apple 'yummy'{

}
Will test if $apple was successfully assigned the string “yummy” while

if($apple == 'yummy'{

}
will test if $apple IS ‘yummy’
so…
for($i 0$i <= 100$i++) {
        
if ($element->getHtmlId() == "carriers_multiflat"{
        
if ($html == '<tr>'{
        $html
.= '<tr id="'.div.$i.'">'//this sytnax is going to give you an error when PHP doesn't find a constant "div" - you need to do $html = '<tr id="div' . $i . '">
        
}
        }
        } 
//this still will not work, because $html does not equal just "<tr>" but might contain that tag somewhere.

check out php.net - has a lot of GOOD documentation

You are looking for a str_replace (string replace).  http://us2.php.net/str_replace

$string 'my apple is yummy';
$new_string str_replace('apple''pear'$string);

echo 
$new_string//'my pear is yummy'
 
Magento Community Magento Community
Magento Community
Magento Community
 
sherrie
Moderator
 
Avatar
Total Posts:  1655
Joined:  2007-12-14
Waterloo, ON
 

Ok, that makes sense, but I’m not totally understanding the link ...

What would the string be to replace <tr> with <tr id="’.div.$i.‘“> ?

 
Magento Community Magento Community
Magento Community
Magento Community
 
nikefido
Guru
 
Avatar
Total Posts:  481
Joined:  2008-07-11
New Haven, CT
 

that’s a problem, because str_replace will replace ALL instance of what you are searching for (in your case, ‘<tr>’ with another string...so you can’t add a counter variable (such as $i i your case) that increments for each <tr> tag being replaced.

My confusion is how you are coming about this, as I’m sure there is another way to go about it.
Can you explain what files you are editing and to what final purpose? I think judging by this, you are editing a certain .phtml file?

If you can share your files or show the whole thing, I’d be glad to offer assistance

 
Magento Community Magento Community
Magento Community
Magento Community
 
sherrie
Moderator
 
Avatar
Total Posts:  1655
Joined:  2007-12-14
Waterloo, ON
 

Sure thing. smile

All I’m editing is app/code/core/Mage/Adminhtml/Block/System/Config/Form/Fieldset.php which currently looks like this:

class Mage_Adminhtml_Block_System_Config_Form_Fieldset
    
extends Mage_Adminhtml_Block_Abstract
    
implements Varien_Data_Form_Element_Renderer_Interface
{

    
/**
     * Render fieldset html
     *
     * @param Varien_Data_Form_Element_Abstract $element
     * @return string
     */
    
public function render(Varien_Data_Form_Element_Abstract $element)
    
{
        $html 
$this->_getHeaderHtml($element);

        foreach (
$element->getElements() as $field{
            $html
.= $field->toHtml();
        
}

        $html 
.= $this->_getFooterHtml($element);

        return 
$html;
    
}

    
/**
     * Enter description here...
     *
     * @param Varien_Data_Form_Element_Abstract $element
     * @return string
     */
    
protected function _getHeaderHtml($element)
    
{
        $default 
= !$this->getRequest()->getParam('website') && !$this->getRequest()->getParam('store');

        
$html '<div  class="entry-edit-head collapseable" ><a id="'.$element->getHtmlId().'-head" href="#" onclick="Fieldset.toggleCollapse(\''.$element->getHtmlId().'\', \''.$this->getUrl('*/*/state').'\'); return false;">'.$element->getLegend().'</a></div>';
        $html.= '<input id="'.$element->getHtmlId().'-state" name="config_state['.$element->getId().']" type="hidden" value="'.(int)$this->_getCollapseState($element).'">';
        $html.= '<fieldset class="config collapseable" id="'.$element->getHtmlId().'">';
        $html.= '<legend>'.$element->getLegend().'</legend>';

        if ($element->getComment()) {
            $html .= '<div class="comment">'.$element->getComment().'</div>';
        }
        // field label column
        $html.= '<table cellspacing="0" class="form-list"><colgroup class="label"/><colgroup class="value"/>';
        if (!$default) {
            $html.= '<colgroup class="default"/>';
        }
        $html.= '<tbody>';
        for($i = 0; $i <= 40; $i++) {
        if ($element->getHtmlId() == "carriers_multiflat") {
        if ($html.= '<tr>') {
        $html.= '<tr id="'.div.$i.'">';
        }
        }
        }
        return $html;
    }

    /**
     * Enter description here...
     *
     * @param Varien_Data_Form_Element_Abstract $element
     * @return string
     */
    
protected function _getFooterHtml($element)
    
{
        $html 
'</tbody></table></fieldset>' Mage::helper('adminhtml/js')->getScript("Fieldset.applyCollapse('{$element->getHtmlId()}')");
        return 
$html;
    
}

    
protected function _getCollapseState($element)
    
{
        $extra 
Mage::getSingleton('admin/session')->getUser()->getExtra();
        if (isset(
$extra['configState'][$element->getId()])) {
            
return $extra['configState'][$element->getId()];
        
}
        
return false;
    
}
}

The reason is because I am trying to add a show/hide element within my shipping module xml so that I can show/hide the 10 different shipping options individually as needed.  There seems to be no simple way to do this without assigning an id for the show/hide script to reference, which is why I was attempting to add it in the Fieldset.php file.  As I showed in a previous post, I was getting it to work, but it was adding the <tr> tags instead of replacing the ones already there.

My system.xml file for my module looks like this:

<name1 translate="label"
              <
label>Event Method 1 >></label>
              <
frontend_type>text</frontend_type>
              <
sort_order>3</sort_order>
              <
show_in_default>1</show_in_default>
              <
show_in_website>1</show_in_website>
              <
show_in_store>1</show_in_store>              
            </
name1
            <
price1 translate="label">
              <
label• Price (0.00) <![CDATA[ <br /><a href="#" onclick="showhide('div1');">show/hide details</a]]></label>
              <
frontend_type>text</frontend_type>
              <
sort_order>5</sort_order>
              <
show_in_default>1</show_in_default>
              <
show_in_website>1</show_in_website>
              <
show_in_store>1</show_in_store>
            </
price1>
            <
details1 translate="label">
              <
label>• Details</label>
              <
frontend_type>textarea</frontend_type>
              <
sort_order>6</sort_order>
              <
show_in_default>1</show_in_default>
              <
show_in_website>1</show_in_website>
              <
show_in_store>1</show_in_store>
            </
details1>

My original thought was to make a div surrounding all of method 1 (which all 3 of those belong to) to show/hide it, but I was going to settle for just using a show/hide on the details.

Does this help clarify?  I really appreciate any way you can help as I have been stumbling through this for awhile now!

 
Magento Community Magento Community
Magento Community
Magento Community
 
sherrie
Moderator
 
Avatar
Total Posts:  1655
Joined:  2007-12-14
Waterloo, ON
 

Oh, and I have the following js in my app/design/adminhtml/default/default/template/page/head.phtml file:

<script language="javascript"
<!-- 
var 
state 'none'

function 
showhide(layer_ref

if (state == 'block'
state 
'none'

else 
state 
'block'

if (document.all//IS IE 4 or 5 (or 6 beta) 
eval( "document.all." layer_ref ".style.display = state")

if (document.layers//IS NETSCAPE 4 or below 
document.layers[layer_ref].display state

if (document.getElementById &&!document.all
hza 
document.getElementById(layer_ref); 
hza.style.display state


//--> 
</script>
 
Magento Community Magento Community
Magento Community
Magento Community
 
nikefido
Guru
 
Avatar
Total Posts:  481
Joined:  2008-07-11
New Haven, CT
 

mmm i guess im still abit confused, but here’s my shot at trying to accomplish what you want (maybe rasberry )

for($i 0$i <= 40$i++) {
        
if ($element->getHtmlId() == "carriers_multiflat"{
                $html
.= '<tr id="div'.$i.'"><td>something to go here??</td></tr>';
        
}
 }

So that will create 40 full rows (with one “cell") within that <tbody>. I don’t know what you want to do within that <tbody>...you need the opening and closing <tr> tags, a long with the <td> tags to make a complete table cell. (<tr>’s do the rows, <td>’s do the columns...each row will contain one or many column...not sure if you are familiar with it, im sure you are, but you never know rasberry )

 
Magento Community Magento Community
Magento Community
Magento Community
 
sherrie
Moderator
 
Avatar
Total Posts:  1655
Joined:  2007-12-14
Waterloo, ON
 

Nope, but thanks for trying.  I need it to replace the <tr> tags that already exist in that location, not create new ones.

 
Magento Community Magento Community
Magento Community
Magento Community
 
sherrie
Moderator
 
Avatar
Total Posts:  1655
Joined:  2007-12-14
Waterloo, ON
 

I’m attaching a screen shot of my admin details - maybe that will help explain.  See how there are 2 methods?  There are actually 10, so I want to start out with (at the very least) the detail sections of each of them hidden (only showing the name and price fields) but have the ability to show them if they are needed.  I can get it to show/hide anything on the lefthand column but I want to it to show/hide the whole row (both Details and the text area). My thought in this thread was that if I can get it to assign id’s to the <tr> tags I can use those id’s to show/hide with.  An alternative method would be to somehow get it to set divs around that area, but I was having a more difficult time figuring that out in the xml.

Does that make more sense?

I tried this

for($i 0$i <= 40$i++) {
        
if ($element->getHtmlId() == "carriers_multiflat"{
        $original 
= array("'<tr>'");
        
$new = array("'<tr id=\"'.div.$i.'\">'");
        
$change str_replace($original$new$html);
        
}
        }
and it doesn’t cause any errors but it doesn’t seem to be changing anything either.  My guess is the $html command is not what I was looking for.
Image Attachments
shippingadmin.jpg
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top