Small product issue on success page Magento - php

I try to get product details on my checkout success page in Magento 1.9, but I don't know why I get the same product for two times. This is what I add in the app/design/frontend/base/default/template/checkout/success.phtml file
<tbody>
<?php
foreach ($items as $item):
$_product = Mage::getModel('catalog/product')->load($item->getProductId());
$productType = $_product->getTypeId();
$entityId = $_product->getEntityId();
$options = $item->getProductOptions();
if ($productType == "bundle") {
$bundled_product = new Mage_Catalog_Model_Product();
$bundled_product->load($entityId);
$selectionCollection = $bundled_product->getTypeInstance(true)->getSelectionsCollection( $bundled_product->getTypeInstance(true)->getOptionsIds($bundled_product), $bundled_product );
$bundled_items = array();
foreach ($selectionCollection as $option) {
$bundled_items[] = $option->product_id;
}?>
<tr>
<td rowspan="1">
<img class="product_img" src="<?php echo Mage::helper('catalog/image')->init($_product, 'thumbnail')->resize(75); ?>" alt="product-img" />
<?php echo $item->getName();
$customOptions = $options['options'];
if (!empty($customOptions)) {
foreach ($customOptions as $option) {?>
<span class="bottom-align">
<?php
echo '<b>' . $option['label'] . '</b> :';
echo $optionValue = $option['value'];
?>
</span>
<?php
}
}
?>
</td>
<td><?php echo $this->helper('checkout')->formatPrice($item->getPrice()); ?></td>
<td><?php echo $item->getQtyOrdered(); ?></td>
<td><?php echo $item->getSku(); ?></td>
<td><?php echo $this->helper('checkout')->formatPrice($item->getRowTotal()); ?></td>
</tr>
<?php
} else if (in_array($entityId, $bundled_items)) {
} else {
?>
<tr>
<td>
<img class="product_img" src="<?php echo Mage::helper('catalog/image')->init($_product, 'thumbnail')->resize(75); ?>" alt="product-img" />
<?php
echo $item->getName();
$customOptions = $options['options'];
if (!empty($customOptions)) {
foreach ($customOptions as $option) {
?>
<span class="bottom-align">
<?php
echo '<b>' . $option['label'] . '</b> :';
echo $optionValue = $option['value'];
?></span>
<?php
}
}
?>
</td>
<td><?php echo $this->helper('checkout')->formatPrice($item->getPrice()); ?></td>
<td><?php echo $item->getQtyOrdered(); ?></td>
<td><?php echo $item->getSku(); ?></td>
<td><?php echo $this->helper('checkout')->formatPrice($item->getRowTotal()); ?></td>
</tr>
<?php
}
?>
<?php endforeach ?>
</tbody>

This is the expected behaviour. There are several rows inserted into database for composite products: bundles, configurables.
In your case first one is a bundle product himself, second one is a selected simple product.
You can replace } else { with } elseif (!$item->getParentId()) { in your template to skip child simple product.

Related

Print variable in different php file

Hi I have shopping cart and i want print item name in different php file. I try this.
<?php
if(!empty($_SESSION["cart"]))
{
$total = 0;
foreach($_SESSION["cart"] as $keys => $values)
{
$product_cart = $values["item_name"];
?>
<tr>
<td><?php echo $values["item_name"]; ?></td> <?php session_start(); $_SESSION['produktik'] = $values["item_name"] ; ?>
<td><?php echo $values["item_quantity"] ?></td>
<td><?php echo $values["product_price"]; ?>€</td>
<td><?php echo number_format($values["item_quantity"] * $values["product_price"], 2); ?>€</td>
<td><span class="text-danger">X</span></td>
</tr>
and second php file is
<pre>
<?php
session_start();
echo $_SESSION['produktik'];
?>
</pre>

How to avoid duplicates in nested foreach loop php

Why are values are getting printed multiple times (see images below). I need to print them only once.
<?php foreach($patchData3 as $tes3){?>
<?php foreach($patchData1 as $tes){?>
<tr class="<?php if($tes->PATCH == $tes3->PATCH) {echo "red_color"; } ?>">
<td><?php echo $tes->HOSTNAME;?></td>
<td><?php echo $tes->VERSION;?></td>
<td><?php echo $tes->PATCH;?></td> <!--bgcolor="#FF0000"-->
</tr>
<?php } ?>
<?php }?>
<?php foreach($patchData1 as $tes){ ?>
<tr class="<?php if(checkfunction($tes->PATCH,$patchData3) == TRUE) { echo "red_color"; } ?>">
<td><?php echo $tes->HOSTNAME;?></td>
<td><?php echo $tes->VERSION;?></td>
<td><?php echo $tes->PATCH;?></td> <!--bgcolor="#FF0000"-->
</tr>
<?php } ?>
<?php
function checkfunction($patch,$patchData3){
foreach($patchData3 as $tes3){
if($patch == $tes3->PATCH){
return true;
}
}
}
?>
I used a function to overcome the duplication. Please comment if it does not work.

Displaying the nested foreach with if else in php codeigniter

Controller
public function admin()
{
// get the current date employee logged in details
$data['get_attendance'] = $this->attendance_model->get_attendance();
// get the active employee details
$data['get_employee'] = $this->attendance_model->get_employee();
//print_r($data['get_employee']); exit();
// attendance page
$data['header'] = "Employee";
$data['sub_header'] = "Attendance employee";
$data['main_content'] = 'attendance/admin_list';
$this->load->view('employeelayout/main',$data);
}
Model
public function get_attendance()
{
return $this->db->where('date',date('Y-m-d'))->get('attendance')->result_array();
}
public function get_employee()
{
return $this->db->where('is_active',1)->get('employee')->result_array();
}
Views
<?php $count = 1 ?>
<?php foreach ($get_employee as $employee) { ?>
<tr class="gradeX">
<td><?php echo $count++ ?></td>
<td><?php echo $employee['first_name'] . ' ' . $employee['last_name'] ?></td>
<td><?php echo $employee['employee_number'] ?></td>
<?php foreach ($get_attendance as $attendance) : ?>
<!-- if employee exists -->
<?php if($employee['employee_id'] == $attendance['employee_id'] ) { ?>
<td><?php echo date('M-Dj-Y',strtotime($attendance['date'])) ?></td>
<td><?php echo $attendance['mark_in_time'] ?></td>
<td>mark out going</td>
<td>Active</td>
<?php break; ?>
<?php } elseif($employee['employee_id'] != $attendance['employee_id'] ) { ?>
<td><?php echo "i'm absent" ?></td>
<?php break; ?>
<?php } ?>
<?php endforeach; ?>
</tr>
<?php } ?>
I want to display the currently logged in (present) employee and display the absent employees too. I'm getting the employee details from employee table.
and attendance details from attendance table. if an employee is absent and I want to display the absent else display the login details. where here foreach loop not displaying properly with if condition. what's wrong with the loop.
try this:
<?php foreach ($get_employee as $employee) : ?>
<tr class="gradeX">
<td><?php echo $count++ ?></td>
<td><?php echo $employee['first_name'] . ' ' . $employee['last_name'] ?></td>
<td><?php echo $employee['employee_number'] ?></td>
<?php foreach ($get_attendance as $attendance) : ?>
<!-- if employee exists -->
<?php if($employee['employee_id'] == $attendance['employee_id']) : ?>
<td><?php echo date('M-Dj-Y',strtotime($attendance['date'])) ?></td>
<td><?php echo $attendance['mark_in_time'] ?></td>
<td>mark out going</td>
<td>Active</td>
<?php elseif ($employee['employee_id'] != $attendance['employee_id']) : ?>
<td><?php echo "i'm absent" ?></td>
<?php else : ?>
<?php endif; ?>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
Apply left join in place of two different queries.
Because currently you have applied two loops and their comparisons, will consume a lot of execution time And I don't think this is the good code If you get thousands of data than definitely you can analyse the execution time.
So it's better to apply join query.

Prevent data from the same id to appear

I want to prevent amaun_caj , amaun_pelbagai , amaun_penalti , amaun_tunggakan from being repeated so if the id_akaun is the same the data will not appear, my code only works on amaun_caj, and for the rest, not a single data appear, what's the problem?
<?php
$i = 0; $id_akaun_old ="";
while($output = mysql_fetch_array($result)) {
$i++;
$id_akaun = $output["id_akaun"];
$lokasi = $output["lokasi"];
$amaun_caj = $output["amaun_caj"];
$amaun_tunggakan = $output["amaun_tunggakan"];
$amaun_penalti = $output["amaun_penalti"];
$amaun_pelbagai = $output["amaun_pelbagai"];
$jumlah_bayaran = $output["jumlah_bayaran"];
?>
<tr>
<td>
<?php echo $i; ?>
</td>
<td>
<?php echo $jenis; ?>
</td>
<td>
<?php echo $id_akaun;
?>
</td>
<td>
<?php echo $no_telefon; ?>
</td>
<td>
<?php echo $lokasi; ?>
</td>
<td align="center">
<?php
if($id_akaun != $id_akaun_old):
echo $amaun_caj;
$id_akaun_old = $id_akaun;
else: echo '';
endif;?>
</td>
<td align="center">
<?php
if($id_akaun != $id_akaun_old):
echo $amaun_pelbagai;
$id_akaun_old = $id_akaun;
else: echo '';
endif;?>
</td>
<td align="center">
<?php
if($id_akaun != $id_akaun_old):
echo $amaun_penalti;
$id_akaun_old = $id_akaun;
else: echo '';
endif;?>
</td>
<td align="center">
<?php
if($id_akaun != $id_akaun_old):
echo $amaun_tunggakan;
$id_akaun_old = $id_akaun;
else: echo '';
endif;?>
</td>
<td align="center">
<?php
if($id_akaun != $id_akaun_old):
echo $jumlah_bayaran;
$id_akaun_old = $id_akaun;
else: echo '';
endif;?>
</td>
<?php
}
?>
Using temporary variable $old_id_akaun might not be the best practice. If you still want to do it like that, i suggest you user ORDER BY id_akaun in your SQL syntax.
In my oppinion, you might get rid of temporary variable and follow these steps,
1. Create empty array outside your while loop. For the sake of easy
understanding, let's called it $list_id_akaun.
2. Inside your while loop, after you get $id_akaun, check whether $id_akaun
is inside $list_id_akaun
3. If not exists, insert it to $list_id_akaun and continue echoing your
table row
4. If exists, skip to the next row.
I don't know why you don't want to use a select distinct clause in this case, but just put them inside a container then check inside the loop:
<?php
$temp = array();
$i = 1;
while($output = mysql_fetch_array($result)):
$id_akaun = $output["id_akaun"];
$lokasi = $output["lokasi"];
$amaun_caj = $output["amaun_caj"];
$amaun_tunggakan = $output["amaun_tunggakan"];
$amaun_penalti = $output["amaun_penalti"];
$amaun_pelbagai = $output["amaun_pelbagai"];
$jumlah_bayaran = $output["jumlah_bayaran"];
?>
<tr>
<td><?php echo $i; $i++; ?></td>
<td><?php echo $jenis; ?></td>
<td><?php echo $id_akaun; ?></td>
<td><?php echo $no_telefon; ?></td>
<td><?php echo $lokasi; ?></td>
<?php if(!in_array($id_akaun, $temp)): ?>
<td><?php echo $amaun_penalti; ?></td>
<td><?php echo $amaun_tunggakan; ?></td>
<td><?php echo $jumlah_bayaran; ?></td>
<?php $temp[] = $id_akaun; ?>
<?php else : ?>
<td></td><td></td><td></td>
<?php endif; ?>
</tr>
<?php endhile; ?>

parsing XML web service

i have this code controler CI:
function cek_xml() {
$response = $_SESSION ['nim'];
// fetch data
$respons = $this->curl->simple_get($url = "http://localhost/restful/index.php/restful/buku/nim/$response/format/xml");
if (empty($response)) {
show_error('can`t access :' . $response);
}
$data['cuaca'] = $this->format->factory($respons, 'xml')->to_array();
$this->load->view('data_buku_XML', $data);
}
in view :
<?php $no=1;?>
<?php //$this->benchmark->mark('rest_start'); ?>
<?php foreach ($cuaca as $row) { ?>
<?php foreach ($row as $row) { ?>
<tr class="<?php echo ($no % 2 == 0) ?>">
<td><?php echo $no; ?></td>
<td><?php echo $row['title']; ?></td>
<td><?php echo $row['loan_date'] ?></td>
<td><?php echo $row['due_date'] ?></td>
<?php $no=$no+1;?>
</tr>
<?php } ?>
<?php } ?>
The problem is when i get the title, loan date, due date more than one variable it's okay.
But if get 1 title it's will show:
Message: Illegal string offset 'title'
But if i put // like in one foreach:
// foreach ($row as $row) {
It will show 1 variable title but error in more one variable ...
You are reusing same name for variables
Change the variable name
<?php foreach ($row as $r)
and use as
<td><?php echo $r['title']; ?></td>

Categories