Jump to content

List not printing out correctly


djforema

Recommended Posts

I am trying to print out information from a list.  I thought I had this figured out but realized It was just printing out the same information over and over.
This is from a Joomla website and I am using the J2store extension, just to give you the information from where this is coming from.

So this is suppose to print out all the attributes from a single order. Each item in the order has any number of attributes that make it up.  I am using it as an assembly site.  So each assembly will have different attributes that make it up. (Hope that explains it good enough.)

<?php
  	JModelLegacy::addIncludePath(JPATH_SITE.'/components/com_j2store/models');
  	$model = JModelLegacy::getInstance( 'OrderItemAttributes', 'J2StoreModel' );
  ?>	
  
  <?php foreach (@$items as $item) : ?>
  
  <?php
 	//these are just printed to show the 5 different items in the same order
 
 	echo $item->order_id;
 	echo '<br />';
 	echo $item->orderitem_id;
 	echo '<br />';
 	echo $item->orderitem_name;
 	echo '<br />';
 
 
 	$model->setState( 'filter_orderitemid', $item->orderitem_id);	
 	$attributes = $model->getList();
 	
 	
 	
 	foreach($attributes as $attribute) {
 		if(!empty($attribute->orderitemattribute_value)) {
 			
 			echo $attribute->orderitemattribute_name.' : '.$attribute->orderitemattribute_value;
 			
 			echo '<br />';
 			
 		}	
 		
 	}
 	echo '<br />';
 	?>	
 
 <?php endforeach; ?>

This ends up printing out this:

--------------------------------------------------------

1402100770
49
16" Box to Stud Bracket with Open Back Box + Modular Duplex
BRACKET : B-Line BB7-16
BOX : Crouse Hinds TP40DPF
Mud Ring (Choose an Option) : 3/4'' Rise Single Gang
MC Cable (Choose an Option) : 12/2 (black,white,green) x 25' L
Device (Choose an Option) : Hubbell 20A SnapConnect Duplex with Connector
Device Color (Choose an Option) : White
MC Connector (x2) : Crouse Hinds 38MCQ
12/2 Anti-Short (x2) : 12/2 Anti Short
11" Cable Tie (x2) : 11'' Cable Tie
4 Port Wago (x3) : Ideal Model 88
Device Protector Plate : B-Line BPR1

1402100770
52
3/4" Conduit Masonry Stub
BRACKET : B-Line BB7-16
BOX : Crouse Hinds TP40DPF
Mud Ring (Choose an Option) : 3/4'' Rise Single Gang
MC Cable (Choose an Option) : 12/2 (black,white,green) x 25' L
Device (Choose an Option) : Hubbell 20A SnapConnect Duplex with Connector
Device Color (Choose an Option) : White
MC Connector (x2) : Crouse Hinds 38MCQ
12/2 Anti-Short (x2) : 12/2 Anti Short
11" Cable Tie (x2) : 11'' Cable Tie
4 Port Wago (x3) : Ideal Model 88
Device Protector Plate : B-Line BPR1

1402100770
53
3/4" Conduit Masonry Stub
BRACKET : B-Line BB7-16
BOX : Crouse Hinds TP40DPF
Mud Ring (Choose an Option) : 3/4'' Rise Single Gang
MC Cable (Choose an Option) : 12/2 (black,white,green) x 25' L
Device (Choose an Option) : Hubbell 20A SnapConnect Duplex with Connector
Device Color (Choose an Option) : White
MC Connector (x2) : Crouse Hinds 38MCQ
12/2 Anti-Short (x2) : 12/2 Anti Short
11" Cable Tie (x2) : 11'' Cable Tie
4 Port Wago (x3) : Ideal Model 88
Device Protector Plate : B-Line BPR1

1402100770
51
MC Cable Whip Assembly
BRACKET : B-Line BB7-16
BOX : Crouse Hinds TP40DPF
Mud Ring (Choose an Option) : 3/4'' Rise Single Gang
MC Cable (Choose an Option) : 12/2 (black,white,green) x 25' L
Device (Choose an Option) : Hubbell 20A SnapConnect Duplex with Connector
Device Color (Choose an Option) : White
MC Connector (x2) : Crouse Hinds 38MCQ
12/2 Anti-Short (x2) : 12/2 Anti Short
11" Cable Tie (x2) : 11'' Cable Tie
4 Port Wago (x3) : Ideal Model 88
Device Protector Plate : B-Line BPR1

1402100770
50
Span Bracket with 4 SQ. Deep 1/2''-3/4'' KO's + Modular 2 Single
BRACKET : B-Line BB7-16
BOX : Crouse Hinds TP40DPF
Mud Ring (Choose an Option) : 3/4'' Rise Single Gang
MC Cable (Choose an Option) : 12/2 (black,white,green) x 25' L
Device (Choose an Option) : Hubbell 20A SnapConnect Duplex with Connector
Device Color (Choose an Option) : White
MC Connector (x2) : Crouse Hinds 38MCQ
12/2 Anti-Short (x2) : 12/2 Anti Short
11" Cable Tie (x2) : 11'' Cable Tie
4 Port Wago (x3) : Ideal Model 88
Device Protector Plate : B-Line BPR1

--------------------------------------------------------

The number '1402100770' is the order number.  Which in that order there were 5 items purchased.  The numbers for the orderitem_id are the correct numbers for the order number.  The name is also the correct for the items that were in the order.
The problem is when the attributes print out. It prints the same attributes for all 5 items purchased.
It seems to be printing the first items attributes for all the items in the order.

I can't figure out why the orderitem_id and orderitem_name all prints out correctly but when it prints of the attributes it just prints the same attributes for all the items in the order.

Link to comment
Share on other sites

the following two lines would be responsible for getting the correct data -

    $model->setState( 'filter_orderitemid', $item->orderitem_id);    
    $attributes = $model->getList();

since no one here likely has experience with the extension you are using, about the only way we could help is if you posted a link to the documentation that defines those two methods.

Link to comment
Share on other sites

Alright here is the link to the documentation for J2Store.

 

http://j2store.org/support/user-guide.html

 

I don't think this is what you were wanting.

Keep in mind I am still pretty new with PHP so I am trying to help you help me as best as I can.

I also went through the code and found where these functions/methods were defined.

 

 

getList():

	/**
	 * Retrieves the data for a paginated list
	 * @return array Array of objects containing the data from the database
	 */
	public function getList($refresh = false)
	{
		if (empty( $this->_list ) || $refresh)
		{
			$query = $this->getQuery($refresh);
			$this->_list = $this->_getList( (string) $query, $this->getState('limitstart'), $this->getState('limit') );
		}
		return $this->_list;
	}

I couldn't find the setState() function.  I did a little research and found this on a joomla site for components.

function setState( $property, $value=null )
{
        return $this->_state->set($property, $value);
}

Does this give you what you need?

Link to comment
Share on other sites

that does help since it shows what getList() is doing (though i would expect that setState() would clear previous data.) give this a try for the getList() method call -

$attributes = $model->getList(true); // force refresh - i.e. get new data

edit: actually, that may not produce the expected result since the $refresh value is used by getQuery() to get the $query value. the form of getList() with a parameter would imply you tell it which list you want. that would take knowing the name(s) of the available lists.

 

this is the problem of code that's not documented.

 

how did you come up with the two lines of code in question? perhaps if there is an example showing using the getList('with some list name here...') method, that we can determine how to relate it to your code.

Edited by mac_gyver
Link to comment
Share on other sites

i did a global search through that extension's code, and there's no instance of using getList() with a parameter. there is however a getItem() method in ...\components\com_j2store\models\orders.php file that looks like it is retrieving the item attribute information you are trying to access.

 

if your purpose in this thread is to just change how information that's already being retrieved is being displayed, you would modify the 'view' code for that data, not write your own data retrieval code.

Link to comment
Share on other sites

Here is the getItem() function that you were talking about.

public function getItem($pk=null, $refresh=false, $emptyState=true )
	{
		if (empty( $this->_item ))
		{

			JModelLegacy::addIncludePath( JPATH_SITE.'/components/com_j2store/models' );

			$query = $this->getQuery();

			// TODO Make this respond to the model's state, so other table keys can be used
			// perhaps depend entirely on the _buildQueryWhere() clause?
			$keyname = $this->getTable()->getKeyName();
			$value	= $this->_db->Quote( $this->getId() );
			$query->where( "tbl.$keyname = $value" );
			$this->_db->setQuery( (string) $query );
			$item = $this->_db->loadObject();
			if ($item)
			{

				//retrieve the order's items
				$model = JModelLegacy::getInstance( 'OrderItems', 'J2StoreModel' );
				$model->setState( 'filter_orderid', $item->order_id);
				$model->setState( 'order', 'tbl.orderitem_name' );
				$model->setState( 'direction', 'ASC' );
				$item->orderitems = $model->getList();
				foreach ($item->orderitems as $orderitem)
				{
					$model = JModelLegacy::getInstance( 'OrderItemAttributes', 'J2StoreModel' );
					$model->setState( 'filter_orderitemid', $orderitem->orderitem_id);
					$attributes = $model->getList();
					$attributes_names = array();
					$attributes_codes = array();
					$attributes_csv = array();
					foreach ($attributes as $attribute)
					{
						// store a csv of the attrib names
						$attributes_names[] = JText::_( $attribute->orderitemattribute_name );
						if($attribute->orderitemattribute_code)
							$attributes_codes[] = JText::_( $attribute->orderitemattribute_code );

						if($attribute->orderitemattribute_type == 'select' || $attribute->orderitemattribute_type == 'radio') {
							$attributes_csv[] = $attribute->productattributeoptionvalue_id;
						}

					}
					$orderitem->attributes_names = implode(', ', $attributes_names);
					$orderitem->attributes_codes = implode(', ', $attributes_codes);
					sort($attributes_csv);
					$orderitem->attributes_csv = implode(',', $attributes_csv);

					// adjust the price
					$orderitem->orderitem_price = $orderitem->orderitem_price + floatval($orderitem->orderitem_attributes_price);
				}


			}

			$this->_item = $item;
		}

		return $this->_item;
	}

I see that the code that I had is pretty much in this.

 

I get that this returns '$this->_item'.  I'm assuming this is an array.

 

So in place of my original code how would I call this function?

 

I would still need a for loop to print out the list of attributes, right?

 

So I came up with this:

<?php
$newarray = getItem();
?>

<?php foreach ($newarray as $list) : ?>
<?php
echo $list->orderitemattribute_name.' : '.$list->orderitemattribute_value;
echo '<br />';
?>
<?php end foreach; ?>

Which didn't end up working.  Can you give me a some pointers on what I'm doing wrong?

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.