Opencart selected link has a class - php

So I have a class selected, which should only be active when you are on the page of the link you pressed, and I am trying to get it in the code with php and asking if the current page is equal to any of the links and the one link that is equal should get the class.
<?php if ($categories) { ?>
<div class="menu col-sm-5 noPadding">
<?php foreach ($categories as $category) {
$url = "http://$_SERVER[SERVER_NAME]$_SERVER[REQUEST_URI]";
$isIt = $category['href']; ?>
<?php if ($url == $isIt){ ?>
<a class="selected" href="<?php echo $category['href']; ?>"><?php echo $category['name']; ?></a>
<?php } else { ?>
<?php echo $category['name']; ?>
<?php } ?>
<?php } ?>
</div>
<?php } ?>
This is all in opencart and I would like to continue using foreach instead of having to code every link in by itself.
I've tried echoing them out and I get exactly the same things written.
Ok so this is the output from var_dump($categories)
array (size=4)
'name' => string 'Desktops' (length=8)
'children' =>
array (size=0)
empty
'column' => string '1' (length=1)
'href' => string 'http://localhost/openC/index.php?route=product/category&path=20' (length=67)
1 =>
array (size=4)
'name' => string 'Components' (length=10)
'children' =>
array (size=0)
empty
'column' => string '1' (length=1)
'href' => string 'http://localhost/openC/index.php?route=product/category&path=25' (length=67)
2 =>
array (size=4)
'name' => string 'Cameras' (length=7)
'children' =>
array (size=0)
empty
'column' => string '1' (length=1)
'href' => string 'http://localhost/openC/index.php?route=product/category&path=33' (length=67)
3 =>
array (size=4)
'name' => string 'MP3 Players' (length=11)
'children' =>
array (size=0)
empty
'column' => string '4' (length=1)
'href' => string 'http://localhost/openC/index.php?route=product/category&path=34' (length=67)

Try this:
<?php if ($categories) {
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; ?>
<div class="menu col-sm-5 noPadding">
<?php foreach ($categories as $category) { ?>
<?php if ($url == html_entity_decode($category['href'])){ ?>
<a class="selected" href="<?php echo $category['href']; ?>"><?php echo $category['name']; ?></a>
<?php } else { ?>
<?php echo $category['name']; ?>
<?php } ?>
<?php } ?>
</div>
<?php } ?>

Related

Display the member of one array depending on the other array

i have an array i and i want to show the array values if the name of same array repeat in the another array and have true value
my arrays like this
$array1 = [
array(
'name' => internal_evidence
'price' => 30
'course_id' => 3
),
array(
'name' => international_evidence
'price' => 450
'course_id' => 3
),
array(
'name' => internal_evidence
'price' => 10
'course_id' => 1
),
array(
'name' => technical_evidence
'price' => 134
'course_id' => 3
),
];
$array2 = [
array(
'id' => 3
'name' => graphic
'price' => 150
'attr' => array(
'internal_evidence' => 'true',
'international_evidence' => 'false',
'technical_evidence' => 'true'
)
),
array(
'id' => 5
'name' => 3dmax
'price' => 300
'attr' => array(
)
),
array(
'id' => 1
'name' => ICDL
'price' => 480
'attr' => array(
'internal_evidence' => 'true',
)
),
];
i want to showing this all attr selected with true value in like this
also I want to sum price of array2 member and array1
<h2>graphic</h2>
<p>internal_evidence</p>
<p>technical_evidence</p>
<small>course price: 150</small>
<small>314</small> <!-- Price with selected evidence -->
<h2>3dmax</h2>
<small>course price: 300</small>
<!-- its not have attr evidence -->
<h2>ICDL</h2>
<p>internal_evidence</p>
<small>course price: 480</small>
<small>490</small> <!-- Price with selected evidence -->
i try this but its don`t work properly
$priceOfAttr = 0;
foreach($array2 as $key => $cat):
echo "<h2>{$cat['name']}</h2>";
foreach($array1 as $pr):
if($pr['course_id'] == $cat['id']):
foreach($cat['attr'] as $m => $optionV):
if($m == $pr['name'] && $optionV == "true"){
echo $m .'<br>';
$priceOfAttr += $pr['price'];
// echo "<small>{$cat['price']}</small><br>";
// echo $cat['price'] + $pr['price']. "<br>";
}
endforeach;
echo $priceOfAttr + $cat['price'] . '<br>';
endif;
endforeach;
echo '<br>';
endforeach;
I'd use a combination array_reduce and array_map to transform your data into what you need, then simply loop over that to display your view:
<?php
// Index your $array1 by [id][name]
$array1ByIdAndName = array_reduce($array1, static function ($byIdAndName, $entry) {
$byIdAndName[$entry['course_id']][$entry['name']] = $entry;
return $byIdAndName;
});
// Transform $array2's `attr` entries into attribute list + compute total price
$array2 = array_map(static function ($entry) use ($array1ByIdAndName) {
$entry['total_price'] = $entry['price'];
$entry['attr'] = array_reduce(array_keys($entry['attr']), static function ($attrs, $attrName) use ($array1ByIdAndName, &$entry) {
if ($entry['attr'][$attrName] === 'true') {
$attrs[] = $attrName;
$entry['total_price'] += $array1ByIdAndName[$entry['id']][$attrName]['price'];
}
return $attrs;
}, []);
return $entry;
}, $array2);
// Display your view
?>
<?php foreach ($array2 as $entry): ?>
<h2><?= $entry['name'] ?></h2>
<?php foreach ($entry['attr'] as $attrName): ?>
<p><?= $attrName ?></p>
<?php endforeach ?>
<small>course price : <?= $entry['price'] ?></small>
<?php if ($entry['total_price'] > 0): ?>
<small><?= $entry['total_price'] ?></small>
<?php endif ?>
<?php endforeach ?>
Demo: https://3v4l.org/nS3Gl

PHP While loop an array

After I get all the results from my DB, I added some extra data ('Prio')
I used the $_POST method and store the following result in an array:
array (size=4)
'gereed' => string 'gereed' (length=6)
1 =>
array (size=7)
'prio' => string '1' (length=1)
'res' => string '456' (length=3)
'base' => string '190203' (length=6)
'lot' => string '101' (length=3)
'split' => string '0' (length=1)
'sub' => string '0' (length=1)
'seq' => string '10' (length=2)
2 =>
array (size=7)
'prio' => string '2' (length=1)
'res' => string '456' (length=3)
'base' => string '180676' (length=6)
'lot' => string '10' (length=2)
'split' => string '0' (length=1)
'sub' => string '0' (length=1)
'seq' => string '30' (length=2)
3 =>
array (size=7)
'prio' => string '3' (length=1)
'res' => string '456' (length=3)
'base' => string '180676' (length=6)
'lot' => string '10' (length=2)
'split' => string '0' (length=1)
'sub' => string '0' (length=1)
'seq' => string '60' (length=2)
Now I have only 3 rows, but it could happen that I have 10 or more rows.
I want to display these data like I did with the while loop.
So that the loop will go trough the rows (1, 2, 3, xx) and I can just dispaly them for example like echo $row['prio'];
Maybe it is an easy question, but I'am still learning.
EDIT!
At the input part I get the Prio value. I want to update this value at the right row.
UPDATE VMSCHRIP_SIM SET OPERATION_PRIORITY = $_POST['prio'] WHERE BASE = $_POST['base'] AND RESOURCE = $_POST['res'] AND blablabla
something like that...
Here is a part of my code.
if ($result > 1) {
$i = 1;
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$TrCode = transactionCodeDirect($Employee, $row["WORKORDER_BASE_ID"], $row["WORKORDER_LOT_ID"], $row["WORKORDER_SPLIT_ID"], $row["WORKORDER_SUB_ID"], $row["SEQUENCE_NO"], $row["RESOURCE_ID"]);
if ($TrCode <> ""){
$actief = "<p class=text-success>Actief</p>";
}else{
$actief = "<p class=text-warning>Klaar om te starten</p>";
}
if($row["DESCRIPTION"] == ""){
$description = strtok(wo_description($row["WORKORDER_BASE_ID"], $row["WORKORDER_LOT_ID"], $row["WORKORDER_SPLIT_ID"]), '(**');
}else{
$description = $row["PART_ID"]." : ".$row["DESCRIPTION"];
}
if($row["PREV_OP"] == "" and $row["MAT_REMAINING"] == '0'){
$row["PREV_OP_COMPLETED_QTY"] = $row["CALC_END_QTY"];
}
?>
<a href="#" class="list-group-item">
<div style="text-align:left">
<div class="row">
<div class="col-lg-1">
<br>
<span class="fa-stack fa-1x">
<i class="fa fa-circle-o fa-stack-2x"></i>
<span class="fa fa-stack-1x"><?php echo $row["OPERATION_PRIORITY"];?></span>
</span>
</div>
<div class="col-lg-1">
<br>
<input type="text" name="<?php echo $i; ?>[prio]" class="form-control" value="" autofocus placeholder="Prio">
<input type="hidden" name="<?php echo $i; ?>[res]" value="<?php echo $row['RESOURCE_ID'];?>">
<input type="hidden" name="<?php echo $i; ?>[base]" value="<?php echo $row['WORKORDER_BASE_ID'];?>">
<input type="hidden" name="<?php echo $i; ?>[lot]" value="<?php echo $row['WORKORDER_LOT_ID'];?>">
<input type="hidden" name="<?php echo $i; ?>[split]" value="<?php echo $row['WORKORDER_SPLIT_ID'];?>">
<input type="hidden" name="<?php echo $i; ?>[sub]" value="<?php echo $row['WORKORDER_SUB_ID'];?>">
<input type="hidden" name="<?php echo $i; ?>[seq]" value="<?php echo $row['SEQUENCE_NO'];?>">
</div>
<div class="col-lg-4">
<b><?php echo $row["NAME"];?></b><br>
<?php echo $row["WORKORDER_BASE_ID"];?>/<?php echo $row["WORKORDER_LOT_ID"];?>.<?php echo $row["WORKORDER_SPLIT_ID"];?>-<?php echo $row["WORKORDER_SUB_ID"];?>:<?php echo $row["SEQUENCE_NO"];?><br>
Vorige bewerking: <?php echo $row["PREV_OP"];?><br>
</div>
<div class="col-lg-6">
<?php echo $description;?><br>
<?php echo intval($row["COMPLETED_QTY"]);?> / <?php echo intval($row["CALC_END_QTY"]);?> (<?php echo intval($row["PREV_OP_COMPLETED_QTY"]);?>) Stuks
<div class="tooltip12"><i class="fa fa-question-circle"></i>
<span class="tooltip12text">Aantal gereed / Totaal (Beschikbaar)</span>
</div><br>
Volgende bewerking: <?php echo $row["NEXT_OP"];?><br>
</div>
<div class="col-lg-1">
<?php if (transactionCodeDirect($Employee, $row["WORKORDER_BASE_ID"], $row["WORKORDER_LOT_ID"], $row["WORKORDER_SPLIT_ID"], $row["WORKORDER_SUB_ID"], $row["SEQUENCE_NO"], $row["RESOURCE_ID"]) <> ""){ ?>
<i class="fa fa-rotate-right fa-4x" text-center"></i><br><b>Running</b>
<?php } ?>
</div>
</div>
</div>
</a>
<?php
$i++;
}
}else{
echo "Result is niet groter dan 1";
}
It could be like this way.
foreach($_POST as $value){
if(is_array($value)){
foreach($value as $key => $val){
// first iteration
echo $key; // prio
echo $val; // 1
// in second iteration, `$val` contains '2'
}
}
}
Since you have two dimensional array, you can use foreach function in PHP to loop over the array. In the below code, I used nested foreach function to loop over two dimensional array.
echo "<table><tr>th>Key</th><th>Value</th></tr>";
foreach($_POST['your_array'] as $value){
if(is_array($value)){
foreach($value as $key => $val){
echo "<tr>";
echo "<td>" . $key . "</td>"; // prio
echo "<td>" . $val . "</td>"; // 1
echo "</tr>"
}
}
}
echo "</table>";

htmlspecialchars on an array of arrays is not working

I have this form with a bunch of sections, and some of them have name of an array because they are supposed to add up dynamically. I'm trying to perform htmlspecialchars on them first and then once the submit button is clicked, echo them out on a next confirmation page, but it won't work for some reason. I did print_r on $clean, but it didn't show the input $value of them, so I don't know where I did something wrong.
It would be great if somebody could help me on this.
Thank you.
Here is a part of the htmlspecialchars code.
$clean = array();
if( !empty($_POST) ) {
foreach( $_POST as $key => $value ) {
if( is_array($key)){
foreach($key as $key2 => $value2)
$clean[$key2] = htmlspecialchars( $value2, ENT_QUOTES);
} else {
$clean[$key] = htmlspecialchars( $value, ENT_QUOTES);
}
}
}
This is a html part of it
<div class="seconf-h-form">
<label>Multiple</label>
<input type="radio" id="r2" name="team_select"
onchange="toggleFunc('ex_t_button');" value="Multiple"/>
</div>
<div class="element_wrap" id="box_2">
<input type="submit" name="add" id="add" value="add more">
<label>The name of your team</label>
<input type="text" name="ex_team_n[]" id="ex_team_n"/>
<select name="ex_amount[]">
<option value="">Select</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
<div id="add_section"></div>
and this is the part where I echo them out
<div class="element_wrap">
<label>The name of your team</label>
<p><?php echo $clean['ex_team_n']; ?></p>
</div>
<div class="element_wrap">
<label>The number of your team</label>
<p><?php echo $clean['ex_amount']; ?></p>
</div>
<input type="hidden" name="amount" value="<?php if(
$clean['team_select'] === "Multiple"){echo $clean['ex_team_n'];} ?>">
<input type="hidden" name="amount" value="<?php if(
$clean['team_select'] === "Multiple"){echo $clean['ex_amount'];} ?>">
You can use array_walk_recursive() to escape all data inside an array:
// Sample data, you can use $_POST instead or any other array
$array = array(
[
'a_key' => '<b>html</b>',
'b_key' => 'another code',
'c_key' => array('<script>alert(\'Hello\');</script>', 'No code, no change'),
],
[
'd_key' => '<small>ssup</small>',
'e_key' => 'stack',
'f_key' => 'overflow',
],
);
// Function to escape the value, you must pass the item by reference using the & operator
function html_escape(&$item){
$item = htmlspecialchars($item, ENT_QUOTES);
}
// Dump data before escaping
var_dump($array);
// Walk recursively through the array and call our function
array_walk_recursive($array, 'html_escape');
// Dump data after escaping
var_dump($array);
The data dumped before escaping
array (size=2)
0 =>
array (size=3)
'a_key' => string '<b>html</b>' (length=11)
'b_key' => string 'another code' (length=46)
'c_key' =>
array (size=2)
0 => string '<script>alert('Hello');</script>' (length=32)
1 => string 'No code, no change' (length=18)
1 =>
array (size=3)
'd_key' => string '<small>ssup</small>' (length=19)
'e_key' => string 'stack' (length=5)
'f_key' => string 'overflow' (length=8)
The data dumped after escaping
array (size=2)
0 =>
array (size=3)
'a_key' => string '<b>html</b>' (length=23)
'b_key' => string '<a href="http://example.com/">another code</a>' (length=68)
'c_key' =>
array (size=2)
0 => string '<script>alert('Hello');</script>' (length=54)
1 => string 'No code, no change' (length=18)
1 =>
array (size=3)
'd_key' => string '<small>ssup</small>' (length=31)
'e_key' => string 'stack' (length=5)
'f_key' => string 'overflow' (length=8)
Documentation for array_walk_recursive()
You're not iterating over the right object and not creating the inner array.
Replace the lines:
if( is_array($key)){
foreach($key as $key2 => $value2)
$clean[$key2] = htmlspecialchars( $value2, ENT_QUOTES);
with
if( is_array($value)){
foreach($value as $key2 => $value2) {
if (!isset($clean[$key])) $clean[$key] = array();
$clean[$key][$key2] = htmlspecialchars( $value2, ENT_QUOTES);
}
And then it should work properly.
function sanitizeMyArray($array) {
array_walk_recursive($array, 'standard');
return $array;
}
function standard(&$item, $key) {
//You must return this to $item for it to work.
$item = htmlspecialchars($item, ENT_QUOTES);
return $item;
}
$results = sanitizeMyArray($array);
print_r($results)

Multidimensional Array - foreach, within a foreach

I have a multi dimensional array that is printing out exactly how I want it, however, I have become stuck on figuring out how I can construct it into the for each loop that i'm looking for.
Please Note : $handle is a field the client entered in the backend.
<?php
$gp = Mage::getStoreConfig('social_code/social_group/google_field');
$ld = Mage::getStoreConfig('social_code/social_group/linkedin_field');
$tw = Mage::getStoreConfig('social_code/social_group/twitter_field');
$fb = Mage::getStoreConfig('social_code/social_group/facebook_field');
$social_array = array(
"facebook" => array(
'class' => "facebook",
'url' => 'https://www.facebook.com/',
'handle' => $fb
),
"twitter" => array(
'class' => "twitter",
'url' => 'https://www.twitter.com/',
'handle' => $tw
),
"linked-in" => array(
'class' => "linked-in",
'url' => 'http://www.linkedin.com/company/',
'handle' => $ld
),
"google-plus" => array(
'class' => "google-plus",
'url' => 'https://plus.google.com/',
'handle' => $gp
)
);
?>
Now i wish to spit this out as an unordered list so i have the below, but its still not working for me.
<ul>
<?php foreach ($social_array as $name => $group) :?>
<?php foreach ($group as $class => $url) :?>
<li><?php echo ("$group"); ?></li>
<?php endforeach; ?>
<?php endforeach; ?>
</ul>
I would like it so that it loops through all the social array and prins something similar to this
<li><?php echo $name; ?></li>
or so understood better
<li>Facebook</li>
Also if I'm making this over complicated for myself please let me know.
<ul>
<?php foreach ($social_array as $name => $group) :?>
<li><?php echo $name; ?></li>
<?php endforeach; ?>
</ul>
I think this is what you're looking for if I've understood correctly.
<ul>
<?php foreach ($social_array as $name => $group) :?>
<li><a href="<?php echo $group['url'].$group[handle']; ?>" class="<?php echo $group['class']; ?>"><?php echo $name
<?php endforeach; ?>
</ul>
There is no need to inner foreach.
<?php foreach ($social_array as $name => $group) :?>
<li><?php echo $group['class']; ?></li>
<?php endforeach; ?>
See http://3v4l.org/3cH6f for the example working below. Just one foreach.
<?php
$social_array = array(
"facebook" => array(
'class' => "facebook",
'url' => 'https://www.facebook.com/',
'handle' => 'Mage::getStoreConfig(\'social_code/social_group/twitter_field\')'
),
"twitter" => array(
'class' => "facebook",
'url' => 'https://www.facebook.com/',
'handle' => 'Mage::getStoreConfig(\'social_code/social_group/twitter_field\')'
),
"linked-in" => array(
'class' => "facebook",
'url' => 'https://www.facebook.com/',
'handle' => 'Mage::getStoreConfig(\'social_code/social_group/twitter_field\')'
),
"google-plus" => array(
'class' => "facebook",
'url' => 'https://www.facebook.com/',
'handle' => 'Mage::getStoreConfig(\'social_code/social_group/twitter_field\')'
)
);
$html = '<ul>';
foreach ($social_array as $name => $class_array){
$html .= '<li>'. $name. '</li>';
}
$html .= '</ul>';
print $html;
?>
Give this a shot:
<ul>
<?php foreach ($social_array as $name => $properties) :?>
<li><?php echo ucfirst($name); ?></li>
<?php endforeach; ?>
</ul>
In this foreach, the syntax is foreach($array as $key => $value), so here $name is the key from $social_array, and $properties is the array associated with that key.
The thing you are forgetting is that the inner foreach is processing each item i.e. class,ulr,handle one at a time.
You therefore need to store the information you require as you pass over each of the 3 items so it can be used once you have the 2 items you actually need.
So this may help.
<ul>
<?php
foreach ($social_array as $name => $group) :
$class = NULL;
$url = NULL;
$handle = NULL;
foreach ($group as $name => $value) :
switch ($name) :
case 'class' : $class = $value; break;
case 'url' : $url = $value; break;
case 'handle' : $handle = $value; break;
endswitch;
if ( isset($class) AND isset($url) AND isset($handle) ) :
echo '<li>' . $group . '</li>';
$class = NULL;
$url = NULL;
$handle = NULL;
endif;
endforeach;
endforeach;
?>
</ul>

How to loop a foreach on an object/array

My DB returns an array of objects with arrays within.
Var_dump
array (size=2)
0 =>
object(stdClass)[22]
public 'customer_id' => string '10' (length=2)
public 'cart' => string 'a:1:{s:32:"f9bb1d342b1c2a0bfe982ef405369ec0";a:9:{s:5:"rowid";s:32:"f9bb1d342b1c2a0bfe982ef405369ec0";s:2:"id";s:9:"101_30524";s:3:"qty";s:1:"1";s:5:"price";s:5:"104.5";s:4:"name";s:13:"Business Card";s:5:"image";s:18:"business-cards.gif";s:4:"ship";a:3:{s:6:"Ground";d:9.730000000000000426325641456060111522674560546875;s:11:"2nd Day Air";d:18.53999999999999914734871708787977695465087890625;s:9:"Overnight";d:26.269999999999999573674358543939888477325439453125;}s:7:"options";a:2:{s:17:"Print Description";s:16'... (length=761)
public 'shipping_type' => string 'Ground' (length=6)
public 'shipping_cost' => string '9.73' (length=4)
public 'order_sub_total' => string '104.50' (length=6)
public 'order_total' => string '114.23' (length=6)
public 'id' => string '28' (length=2)
public 'timestamp' => string '2012-10-12 20:10:30' (length=19)
1 =>
object(stdClass)[23]
public 'customer_id' => string '10' (length=2)
public 'cart' => string 'a:2:{s:32:"22d2d3d8584f6e0819c4e46af4d2fda2";a:9:{s:5:"rowid";s:32:"22d2d3d8584f6e0819c4e46af4d2fda2";s:2:"id";s:9:"101_94980";s:3:"qty";s:1:"1";s:5:"price";s:2:"64";s:4:"name";s:13:"Business Card";s:5:"image";s:18:"business-cards.gif";s:4:"ship";a:3:{s:6:"Ground";d:9.730000000000000426325641456060111522674560546875;s:11:"2nd Day Air";d:18.53999999999999914734871708787977695465087890625;s:9:"Overnight";d:26.269999999999999573674358543939888477325439453125;}s:7:"options";a:2:{s:17:"Print Description";s:164:"'... (length=1506)
public 'shipping_type' => string 'Ground' (length=6)
public 'shipping_cost' => string '19.46' (length=5)
public 'order_sub_total' => string '148.25' (length=6)
public 'order_total' => string '167.71' (length=6)
public 'id' => string '29' (length=2)
public 'timestamp' => string '2012-10-12 20:29:10' (length=19)
Notice cart is a multidimensional array. How do I loop through these objects and arrays and create a table?
<?php foreach($all_orders as $key => $val) : ?>
<?php echo $key; ?> <?php echo $val; ?>
<?php endforeach; ?>
This causes the following error: A PHP Error was encountered Severity: 4096 Message: Object of class stdClass could not be converted to string
Access the properties of the stdClass object using the -> operator:
<?php foreach($all_orders as $key => $val) : ?>
Customer ID <?php echo $val->customer_id ?> has a total of <?php echo $val->order_total ?><br />
<?php endforeach ?>
You can try
echo "<pre>";
foreach ( $cart as $all_orders ) {
foreach ( $all_orders as $key => $value ) {
echo $key, " = ", $value, PHP_EOL;
}
}
PHP 5 allows one to iterate over an object's public properties using a foreach loop.
For more information (and code examples): http://php.net/manual/en/language.oop5.iterations.php
I use PHP's ArrayObject in my MVC:
us3.php.net/manual/en/class.arrayobject.php
$arrayobject = new ArrayObject($dataRows);
for ($iterator = $arrayobject->getIterator(); $iterator->valid(); $iterator->next()) {
}

Categories