I am trying to cycle through the results of my query to display in table format on my page.
What am I missing in my foreach and my echo to cycle through the results in my display table.
{source 0}
<?php
$user =& JFactory::getUser();
if (!$user->guest)
$name = $user->username;
$db =& JFactory::getDbo();
$query = $db->getQuery(true);
$query = "
SELECT image_url
FROM #__image_data
WHERE user_name = '$name'";
$db->setQuery($query);
$list = $db->loadObjectList();
foreach ($list as $item){
$item->image_url;
}
?>
<td><?php echo $item->image_url;?></td><br/>
<td><?php echo $item->image_url;?></td><br/>
<td><?php echo $item->image_url;?></td><br/>
{/source}
{source 0}
<?php
$user =& JFactory::getUser();
if (!$user->guest)
$name = $user->username;
$db =& JFactory::getDbo();
$query = $db->getQuery(true);
$query = "
SELECT image_url
FROM #__image_data
WHERE user_name = '$name'";
$db->setQuery($query);
$list = $db->loadObjectList();
foreach ($list as $item){
echo "<td>".$item->image_url."</td><br/>";
}
?>
{/source}
You can easily loops through and get the <td> as below.
Method 1 - Here you don't have to concatenate since you are using ""
$list = $db->loadObjectList();
foreach ($list as $item){
echo "<td> $item->image_url </td><br/>";
}
Method 2
$list = $db->loadObjectList();
foreach ($list as $item){
echo '<td>'.$item->image_url'.</td><br/>';
}
Related
I try write this code
public function allsalesblock(){
echo 'other block ';
$orders = Mage::getResourceModel('sales/order_collection')
->addFieldToSelect('*')
->addFieldToFilter('customer_id', Mage::getSingleton('customer/session')->getId());
->addAttributeToSort('created_at', 'DESC');
$order = Mage::getModel("sales/order")->load($orders); //load order by order id
$ordered_items = $order->getAllItems();
foreach($ordered_items as $item){
echo $item->getItemId(); //product id
echo $item->getSku();
echo $item->getQtyOrdered(); //ordered qty of item
echo $item->getName();
}
}
But this doesn't work - i see white screen. I found this code(and modification) here How can I display all products bought by a customer in magento?
You can get it using below code,
<?php
require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app();
$email = 'albert#example.com';
$_orders = Mage::getModel('sales/order')->getCollection()->addFieldToFilter('customer_email',$email);
foreach ($_orders as $order) {
$id[]= $order->getId();
}
//print_r($id);
foreach ($id as $order_id) {
$order = Mage::getModel('sales/order')->load($order_id);
#get all items
$items = $order->getAllItems();
$itemcount= count($items);
$data = array();
$i=0;
#loop for all order items
foreach ($items as $itemId => $item)
{
$data[$i]['name'] = $item->getName();
$data[$i]['price'] = $item->getPrice();
$data[$i]['sku'] = $item->getSku();
$data[$i]['id'] = $item->getProductId();
$data[$i]['qty'] = $item->getQtyToInvoice();
}
#displaying the array in order to see the products
echo '<pre/>';print_r($data);
}
here change the email address as you want to get products which that customer bought,
$email = 'albert#example.com';
I have made a query that gets customer's orders. Can I get customer gender based on this query? If not, how can I get customer gender? This is the code I have so far:
<?php
require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app();
$productId = 297;
$orders = Mage::getModel('sales/order')->getCollection();
$orders
->getSelect()
->joinLeft(
array(
'order_item' =>Mage::getSingleton('core/resource')->getTableName('sales/order_item')),
'order_item.order_id = main_table.entity_id',
array('product_id'=>'product_id'))
->where('product_id=?', $productId)
->group('main_table.entity_id')
->order('main_table.entity_id DESC');
$fields = "date,company,name,country,city,address,postcode,itemname,quantity,price,sku \n";
foreach($orders as $order):
$order->getData();
$order->getIncrementId();
$billingAddress = $order->getBillingAddress();
$shippingAddress = $order->getShippingAddress();
$billingStreet = $billingAddress->getStreet();
$countryCode = $billingAddress->getCountry();
$country = Mage::getModel('directory/country')->loadByCode($countryCode);
$fields .=
date('Y-m-d',strtotime($order->getCreatedAt())).','.
$billingAddress->getCompany().','.
$billingAddress->getName().','.
$country->getName().','.
$billingAddress->getCity().','.
$billingStreet[0].','.
$billingAddress->getPostcode().','.
$order->getShippingDescription() .',';
foreach ($order->getAllItems() as $itemId => $item):
$itemname = $item->getName();
$itemname = str_replace('&', " ", $itemname);
$fields .=
$itemname. ','.
$item->getQtyOrdered().','.
$item->getPrice().','.
$item->getSku().',';
endforeach;
$fields = rtrim($fields, ',');
$fields .= "\n";
endforeach;
$name = date('d-m-Y-H-i-s');
$output = fopen('backend/assets/csv/' . $name . '.csv', 'w');
fwrite ($output,$fields);
fclose ($output);
foreach($orders as $order){
$customer = $order->getCustomer();
Mage::log($customer->getGender());
}
if this does not work then:
foreach($orders as $order){
$customer = $order->getCustomer();
$customerObj = Mage::getModel('customer/customer')->load( $customer->getEntityId());
Mage::log($customerObj->getGender());
}
I am using $data = $query->result_array(); but I would need to change my fields name from first_name to first name.
I looked up found this....
foreach ($query->result_array() as $row)
{
echo $row['title'];
echo $row['name'];
echo $row['body'];
}
would I want to do something like this?
foreach ($query->result_array() as $row)
{
$row = str_replace("_", " ", $row);
}
$data = $row;
if you need then a new array you can easly do:
foreach ($query->result_array() as $key=> $value)
{
$data[] = array(
str_replace("_", " ", $key)=>$value
);
}
I am working on getting list of order numbers(name) ordered by a customer. I have tried using
Mage::getModel('sales/order')->load($order_id);
but didn't works for me? Actually i am working on help desk module and trying to assign orders to the tickets.
Ok friends thanks for your hints, I got this by using this
require_once 'app/Mage.php';
Mage::app();
$orders = Mage::getResourceModel('sales/order_collection')
->addFieldToSelect('*')
->addFieldToFilter('customer_id', Mage::getSingleton('customer/session')->getCustomer()->getId())
->addFieldToFilter('state', array('in' => Mage::getSingleton('sales/order_config')->getVisibleOnFrontStates()))
->setOrder('created_at', 'desc')
;
$this->setOrders($orders);
foreach ($orders as $order):
echo $order->getRealOrderId().' at '.$this->formatDate($order->getCreatedAtStoreDate()).' ('.$order->formatPrice($order->getGrandTotal()).')';
endforeach;
you can try this:
$yourCustomerId = '123123';
$field = 'customer_id';
$collection = Mage::getModel("sales/order")->getCollection()
->addAttributeToSelect('*')
->addFieldToFilter($field, $yourCustomerId);
echo "<pre>";
print_r($collection);
echo "</pre>";
// if the customer is logged in you can add this
if(!Mage::getSingleton('customer/session')->isLoggedIn()){
$yourCustomerId = Mage::getSingleton('customer/session')->getCustomer()->getId();
$field = 'customer_id';
$collection = Mage::getModel("sales/order")->getCollection()
->addAttributeToSelect('*')
->addFieldToFilter($field, $yourCustomerId);
echo "<pre>";
print_r($collection);
echo "</pre>";
}
You can try this as well,
$customer = Mage::getSingleton('customer/session')->getCustomer();
$email = $customer->getEmail();
Now you have customer email address in your $email variable. So you can easily get order collection using this email address as following:
$orderCollection = Mage::getModel(‘sales/order’)->getCollection();
$orderCollection->addFieldToFilter(‘customer_email’, $email);
foreach ($orderCollection as $_order)
{
echo $_order->getRealOrderId() ;
echo $this->formatDate($_order->getCreatedAtStoreDate()) ;
echo $_order->getShippingAddress() ? $this->escapeHtml($_order->getShippingAddress()->getName()) ;
echo $_order->formatPrice($_order->getGrandTotal());
echo $_order->getStatusLabel();
}
$orders = Mage::getModel('sales/order')->getCollection();
$orders->getSelect()->where('e.customer_id ='.$customer_id);
i want to show count of only configurable products of current category on category page. for that I have written following code...
<?php $cate = Mage::registry('current_category')->getName();
$total=0;
$category = Mage::registry('current_category');
$products = $category->getProductCollection();
foreach ( $products as $_product )
if ($_product->isConfigurable())
{
$total++;
}
echo $cate."(".$total.")"; ?>
my problem is the code is showing the total count of configurable products of all child categories... can anyone help me with this?
try this
<?php
$cate = Mage::registry('current_category')->getName();
$total = 0;
$category = Mage::registry('current_category');
$products = $category->getProductCollection();
foreach ( $products as $_product ){
if ($_product->getType_id()=="configurable"){
$total++;
}
}
echo $cate."(".$total.")";
?>
$category = Mage::registry('current_category');
$products = $category->getProductCollection()
->addAttributeToFilter('type_id', 'configurable');
$total = $products->getSize();
echo $this->__('%s (%d)', $category->getName(), $total);