I have a bunch of data in an array which has been collected in a session. I am able to print_r this and show all the contents but I am struggling to show the product name, id, image etc.
I am showing the using:
<?php
echo '<pre>';
print_r($_SESSION);
echo '</pre>';
?>
It prints out everything I need but not in the format I require. So I know it is collecting my data and storing it in the array. I have set the data in another files like so:
if (isset($_POST['Submit'])) {
$_SESSION['product_img'][] = $_POST['product_img'];
$_SESSION['product_id'][] = $_POST['product_id'];
$_SESSION['product_name'][] = $_POST['product_name'];
$_SESSION['product_price'][] = $_POST['product_price'];
$_SESSION['product_sku'][] = $_POST['product_sku'];
$_SESSION['product_description'][] = $_POST['product_description'];
}
And it is all of the above I want to output.
**Either you stick with your proposal : $_SESSION['...'][] **
if (isset($_POST['Submit'])) {
$_SESSION['product_img'][] = $_POST['product_img'];
$_SESSION['product_id'][] = $_POST['product_id'];
$_SESSION['product_name'][] = $_POST['product_name'];
$_SESSION['product_price'][] = $_POST['product_price'];
$_SESSION['product_sku'][] = $_POST['product_sku'];
$_SESSION['product_description'][] = $_POST['product_description'];
}
and format it like so:
foreach($_SESSION as $elements)
{
foreach($elements as $element) {
echo $element . '<br />';
}
}
Or use this other method:
if (isset($_POST['Submit'])) {
$_SESSION['product_img'] = $_POST['product_img'];
$_SESSION['product_id'] = $_POST['product_id'];
$_SESSION['product_name'] = $_POST['product_name'];
$_SESSION['product_price'] = $_POST['product_price'];
$_SESSION['product_sku'] = $_POST['product_sku'];
$_SESSION['product_description'] = $_POST['product_description'];
}
and format it like so:
foreach($_SESSION as $element)
{
echo $element . '<br />';
}
A simple way to output this as HTML is to use a foreach loop, which goes through each item in the array.
To make things easier, I would suggest changing your POST code so that each item is a single array, like so.
if (isset($_POST['Submit'])) {
$_SESSION['products'][] = array(
'img' => $_POST['product_img'],
'id' => $_POST['product_id'],
'name' => $_POST['product_name'],
'price' => $_POST['product_price'],
'sku' => $_POST['product_sku'],
'description' => $_POST['product_description']
);
}
Now you can iterate through $_SESSION['products'] and get the information for each product in the session. For example:
foreach ($_SESSION['products'] as $product) {
$name = $product['name'];
$id = $product['id'];
$price = $product['price'];
$img = $product['img'];
$sku = $product['sku'];
$description = $product['description'];
echo "<h1>Product: $name</h1>";
echo "<p>Price: $price | ID: $id</p>";
echo "<img src='$img'>";
echo "<p>$description</p>";
echo "<hr />";
}
Related
With api I get a multidimensional array php. I want to extract data from it with a simple code php.
I extract data from an array this way:
var_dump ($obj['product']);
echo $prise = ($obj['product']['return']['ProductsItem']['0']['Article']);
echo $Brand = ($obj['product']['return']['ProductsItem']['0']['Brand']);
echo $Currency = ($obj['product']['return']['ProductsItem']['0']['Currency']);
echo $Name = ($obj['product']['return']['ProductsItem']['0']['Name']);
echo $StockItem = ($obj['product']['return']['ProductsItem']['0']['Stock']['StockItem']['0']['Price']);
echo $StockItem = ($obj['product']['return']['ProductsItem']['0']['Stock']['StockItem']['0']['TransferTime']);
echo $StockItem1 = ($obj['product']['return']['ProductsItem']['0']['Stock']['StockItem']['0']['Count']);
echo $StockItem2 = ($obj['product']['return']['ProductsItem']['0']['Stock']['StockItem']['1']['Count']);
How do I simplify the code? How can the data in the array change?
<?php
foreach ($obj['product']['return']['ProductsItem'] as $productsitem) {
$article = $productsitem['Article'];
$brand = $productsitem['Brand'];
$currency = $productsitem['Currency'];
$name = $productsitem['Name'];
foreach ($productsitem['Stock']['StockItem'] as $stockitem) {
$price = $stockitem['Price'];
$transfertime = $stockitem['TransferTime'];
$count = $stockitem['Count'];
}
}
I have array with already existing values:
$existingValues = array();
now i get new values in xml file (is a import script) but i have to avoid insert of already existing values, my question is, how can i do a if check in foreach where i list all new values from xml?
$i = 1;
foreach($node->children() as $child) :
$attribute2 = $child->attributes();
$productcode = $attribute2['sku'];
$productvariant = $attribute2['variantid'];
$productprice = $attribute2['price'];
if ($attribute2['sku']) :
echo $productcode . ' - ' . $productvariant . '<br>';
endif;
$i++;
endforeach;
I tried with in_array() but it's not correct.
You can create an array wich stores the products and test if the current product is already in the array:
$i = 1;
$products = array();
foreach($node->children() as $child) :
$attribute2 = $child->attributes();
$productcode = $attribute2['sku'];
$productvariant = $attribute2['variantid'];
$productprice = $attribute2['price'];
if ($attribute2['sku'] && !in_array($productcode, $products)) :
echo $productcode . ' - ' . $productvariant . '<br>';
endif;
$products[] = $productcode;
$i++;
endforeach;
You can create a $existingSKU array, extracting sku from existing elements. Then you can compare current sku with existing, and if not present, you can add element to existing.
// build existing sku array
$existingSKU = array();
foreach($existingValues as $value)
{
array_push($existingSKU, $value['sku']);
}
// add element to existing, if not present
foreach($node->children() as $child)
{
$attribute2 = $child->attributes();
if(!in_array($attribute2['sku'], $existingSKU))
{
array_push($existingValues, $attribute2);
}
}
Since you don't use it, you can remove $i.
I have a foreach loop and have a problem with it. My loop show duplicate some item and I want show item only once and don't duplicate item. My loop it is.
foreach($result as $res){
$id = $res->id;
$title = $res->title;
echo $title;
}
$partial=array();
foreach($result as $res){
if (!in_array($res->id, $partial)) {
$id = $res->id;
$title = $res->title;
echo $title;
array_push($partial, $res->id);
}
}
try this one..
$present_array = array();
foreach($result as $res){
if(!in_array($res->title, $present_array)){
$present_array[] = $res->title;
$id = $res->id;
$title = $res->title;
echo $title;
}
}
<?php
function remove_dup($matriz) {
$aux_ini=array();
$entrega=array();
for($n=0;$n<count($matriz);$n++)
{
$aux_ini[]=serialize($matriz[$n]);
}
$mat=array_unique($aux_ini);
for($n=0;$n<count($matriz);$n++)
{
$entrega[]=unserialize($mat[$n]);
}
return $entrega;
}
foreach(remove_dup($result) as $res){
$id = $res->id;
$title = $res->title;
echo $title;
}
another solution is, if you are getting this data from database then use DISTINCT in your select query.
Do a fast check & make your code execute faster like this:
$check_dup = array();
foreach ($items as $item){
if (in_array($item['id'], $check_dup))
continue;
else {
array_push($check_dup, $item['sid']);
/* your code in foreach loop */
}
}
Try this:
$result = array(
array(
'id' => "1",
'title' => "My title"
),
array(
'id' => "2",
'title' => "My title 2"
)
);
foreach($result as $res){
$id = $res['id'];
$title = $res['title'];
echo $title . "<br>";
}
Hi i have a script which fetch all product data from magento , there is one problem occur which is there are some products whose name is same but sku is different , i want to append product name who has the same value other who has the unique value should not append...
<?php
#ob_start();
#session_start();
ini_set('display_errors', 1);
include '../../../../app/Mage.php';
umask(0);
Mage::app('default');
function empty_pk($data){
if($data!=''){return $data;}
else {return " ";}
}
$collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('status', 1);
//arsort($collection);
//echo'<pre>';
//print_r($collection);die();
echo 'count===='.count($collection).'<br/>';
$i=0;
foreach ($collection as $product_all) {
//echo $product_all->getId().'<br/>';
if($i==10) break;
$id = $product_all->getId();
$neew = Mage::getModel('catalog/product')->load($id);
//echo'<pre>';
$product_id = $neew->getId();
$created_at = ' 2013-01-26 00:53:46';
$description = $neew->getdescription();
$short_description = $neew->getshort_description();
$sku = $neew->getsku();
$size_fit = $neew->getsize_fit();
$style_ideas = $neew->getstyle_ideas();
$name = $neew->getname();
how can i do this
You can use temporary array for keeping names' counts and then filter by it:
function empty_pk($data){
if($data!=''){return $data;}
else {return " ";}
}
$collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('status', 1);
//arsort($collection);
$names = array();
foreach ($collection as $product_all) {
$name = $product_all->getName();
if (array_key_exists($name)) {
$names[$name]++;
} else {
$names[$name] = 1;
}
}
//echo'<pre>';
//print_r($collection);die();
echo 'count===='.count($collection).'<br/>';
$i=0;
foreach ($collection as $product_all) {
//echo $product_all->getId().'<br/>';
if($i==10) break;
$id = $product_all->getId();
$neew = Mage::getModel('catalog/product')->load($id);
//echo'<pre>';
// Exit if unique.
if ($names[ $neew->getName() ] == 1) {
break;
}
$product_id = $neew->getId();
$created_at = ' 2013-01-26 00:53:46';
I am trying to expand on my question asked here: Creating a multi-dimensional array from query
I have added a category description to my categories table but I cannot figure out how to add it to the code below and display each category description for each category.
Here is the code I am using to display the items by category:
$itemcategories = array();
while ($row = mysqli_fetch_array($result))
{
$head = $row['category'];
$itemcategories[$head][] = array(
'id' => $row['id'],
'title' => $row['title'],
'itemdesc' => $row['itemdesc'],
'price' => $row['price'],
'special' => $row['special']
);
}
<?php foreach ($itemcategories as $head => $items) { ?>
<h3><?php echo $head; ?></h3>
<table class="chart">
<?php foreach ($items as $item) { ?>
<tr><td><?php echo $item['itemdesc']; ?></td></tr>
<?php } ?>
</table>
<?php } ?>
// to initial variable holder
$categories = $items = array();
// looping mysql result
while ($row = mysqli_fetch_array($result))
{
// to get category name
$head = $row['category'];
// avoid excessive setting of $categories
if (!isset($categories[$head]))
{
$categories[$head] = $row['descrption'];
}
// assign mysql result to $items
$items[$head][] = $row;
}
// free mysql result
mysql_free_result($result);
// loop all $categories
foreach ($categories as $head=>$desc)
{
// this is my example for printing HTML
// you can use heredoc syntax
// or stick to your existing format
echo "<h3>{$head} - {$desc}</h3><table class="chart">";
foreach ($items[$head] as $item)
{
// print item description
echo "<tr><td>{$item['itemdesc']}</td></tr>";
}
echo "</table>";
}