php session error - session

I was trying to make a shopping cart and got a code from web..
<?php
session_start();
require_once 'class/Item.php';
$product_id = $_REQUEST['i_id'];
$action = $_REQUEST['action'];
$item= new Item();
if($product_id && !$item->productExists($product_id)) {
die("Error. Product Doesn't Exist");
}
switch($action) {
case "add":
$_SESSION['cart'][$product_id]++;
break;
case "remove":
$_SESSION['cart'][$product_id]--;
if($_SESSION['cart'][$product_id] == 0) unset($_SESSION['cart'][$product_id]);
break;
case "empty":
unset($_SESSION['cart']);
break;
}
?>
but durring adding the first item to the cart it goes error. How can I correct it.
Error:
Notice: Undefined index: cart in C:\wamp\www\website\store_esp\addtocart.php on line 16
Notice: Undefined index: 2 in C:\wamp\www\website\store_esp\addtocart.php on line 16

Looks like you may be trying to manipulate variables that aren't setup yet. Make sure you're checking that $_SESSION['cart'][$product_id] exists before you do something on it:
if(!isset($_SESSION['cart'][$product_id]))
$_SESSION['cart'][$product_id] = 0;
switch($action) {
...

Try changing this:
$_SESSION['cart'][$product_id]++;
to this:
if (isset($_SESSION['cart'][$product_id])) {
++$_SESSION['cart'][$product_id];
} else {
$_SESSION['cart'][$product_id] = 1;
}

Without knowing the error, it's impossible to tell for sure. But using my deductive powers, I think the problem is here:
$_SESSION['cart'][$product_id]++;
It should be this:
if (isset($_SESSION['cart'][$product_id])) {
$_SESSION['cart'][$product_id]++;
} else {
$_SESSION['cart'][$product_id] = 1;
}
And you need to change this:
session_start();
// add this part
if (!isset($_SESSION['cart'])) {
$_SESSION['cart'] = array();
}
require_once 'class/Item.php';
$product_id = $_REQUEST['i_id'];
$action = $_REQUEST['action'];

Related

implement update in save

$ahorroga = Ahorroga::findOrFail($id);
$ahorroga->junta_id = $request->junta_id;
$ahorroga->socio_id = $request->socio_id;
$ahorroga->ahorro = $request->ahorro;
$ahorroga->estado = $request->estado;
if ($ahorroga->isDirty()) {
$ahorroga->save();
toast('Ahorro Garantia Editado', 'success');
return redirect('ahorroga');
}
toast('No se detectaron cambios', 'error');
return redirect('ahorroga');
I know I can use and save lines of code, But how to apply the isDirty
Ahorroga::findOrFail($id)->update($request->all());
If I get it correct, then if you want to detect whether you updated stuff updated the table or not then below is the solution.
The best solution for this is to use update(), it returns true or false and based on this you can do your further logic. For ex:
$ahorroga = Ahorroga::find($id);
$ahorroga->junta_id = $request->junta_id;
$ahorroga->socio_id = $request->socio_id;
$ahorroga->ahorro = $request->ahorro;
$ahorroga->estado = $request->estado;
$id_updated = $ahorroga->update();
if($id_updated ) {
//if ture
}else{
//false
}
public function update($id)
{
$ahorroga->fill($request->only([
'junta_id', 'socio_id', 'ahorro','estado',
]);
if(!$ahorroga->isDirty()) {
toast('No se detectaron cambios', 'error');
return back();
}
$ahorroga->save();
toast('Ahorro Garantia Editado', 'success');
return redirect()->route();
}
In place of using isDirty(), you can also use isClean()
if($ahorroga->isClean()) {
// some error handling here
}
isDirty means there was changes. isClean means there was no change.

Why does this pattern not work in preg_match

I use preg_match in a function to prevent image extensions from being submitted!
Now I want to block " ~ " character also!
Can anyone tell me how I can do that?
function is_valid($url) {
$res = 1;
if (isset($url['path'])) {
if (preg_match('/\b.jpg\b/i', $url['path'])) { $res = 0; }
if (preg_match('/\b.gif\b/i', $url['path'])) { $res = 0; }
if (preg_match('/\b.png\b/i', $url['path'])) { $res = 0; }
if (preg_match('/\b.bmp\b/i', $url['path'])) { $res = 0; }
}
return $res;
}
I tried this, but it does not work:
if (strpos('~', $url['path'])) {
$res = 0;
}
First of all, you should really read something about regular expressions! If you have done that, read the manual for phps strpos.
You may try preg_match('/[^~]+\.(png|jpg|gif|bmp)/i', $url['path']) or if you want to stick to your version,
if (strpos($url['path'], '~') !== FALSE) {
$res = 0;
}
But anyway, your check will just not be very safe. Example: Someone renames a php file into png and uploads it, if you have mime_magic activated on your apache, the code will get executed. So it is much safer to check the mimetype of the file. See How do I find the mime-type of a file with php? as an example. The accepted answer there mentions a (now) deprecated function, but you can use http://de3.php.net/manual/en/function.finfo-file.php if you have PHP 5.3+

Adding a nav-selected to a dropdown menu for concrete5

I am trying to combine two things for a dropdown menu in Concrete5 and just can't seem to make it work and wonder if someone can help me.
I have a this code for the menu
<?
defined('C5_EXECUTE') or die(_("Access Denied."));
$aBlocks = $controller->generateNav();
global $c;
if ($c->isEditMode()) {
echo("<div class=\"menu\" style=\"position:inherit!important;\"><ul>");
}
else {
echo("<div class=\"menu\"><ul>");
}
$nh = Loader::helper('navigation');
foreach($aBlocks as $ni) {
$_c = $ni->getCollectionObject();
if (!$_c->getCollectionAttributeValue('exclude_nav')) {
$thisLevel = $ni->getLevel();
if ($thisLevel > $lastLevel) {
echo("<!--[if IE 7]><!--></a><!--<![endif]-->\n<!--[if lte IE 6]><table><tr><td><![endif]-->\n<ul>\n");
} else if ($thisLevel < $lastLevel) {
for ($j = $thisLevel; $j < $lastLevel; $j++) {
echo("</a></li>\n</ul>\n<!--[if lte IE 6]></td></tr></table></a><![endif]--></li>\n");
}
}
if ($thisLevel == $lastLevel && $i >0) {
echo "</a></li>\n";
}
$pageLink = false;
if ($_c->getCollectionAttributeValue('replace_link_with_first_in_nav')) {
$subPage = $_c->getFirstChild();
if ($subPage instanceof Page) {
$pageLink = $nh->getLinkToCollection($subPage);
}
}
if (!$pageLink) {
$pageLink = $ni->getURL();
}
if ($_c->getCollectionAttributeValue('placeholder')) {
$pageLink="javascript:void(0)";
}
echo '<li><a href="'.$pageLink.'">' . $ni->getName();
$lastLevel = $thisLevel;
$i++;
}
}
$thisLevel = 0;
for ($i = $thisLevel; $i <= $lastLevel; $i++) {
echo("</a></li></ul>");
}
echo '</div>';
?>
but haven't had any luck styling the nav-selected so I wanted to add under the line beginning foreach:
$classes = array();
if ($ni->isCurrent) {
//class for the page currently being viewed
$classes[] = 'nav-selected';
}
if ($ni->inPath) {
//class for parent items of the page currently being viewed
$classes[] = 'nav-path-selected';
}
from another menu where the nav-selected can be styled but that doesn't make a difference. Does anyone see what I am doing wrong or could someone give me a hint how to make this work? I would really appreciate the help. Thanks!
It appears as though you've taken code from two different places and pasted them together (which rarely works). I think your first code sample is from an old template, and the second code sample is from the newer one.
Your best bet here is to start from scratch with the newer, cleaner autonav template:
https://raw.github.com/concrete5/concrete5/master/web/concrete/blocks/autonav/view.php
It shouldn't be too difficult to modify that to do what you want. If you run into trouble with that, try posting as a new question, either here or in the concrete5 forums ( http://concrete5.org/community/forums ).

TYPO3 Extbase: How to render the pagetree from my model?

I want to create some kind of sitemap in extbase/fluid (based on the pagetree). I have loaded the pages table into a model:
config.tx_extbase.persistence.classes.Tx_MyExt_Domain_Model_Page.mapping.tableName = pages
I have created a controller and repository, but get stuck on the part wich can load the subpages as relation into my model.
For example:
$page = $this->pageRepository->findByPid($rootPid);
Returns my rootpage. But how can I extend my model that I can use $page->getSubpages() or $page->getNestedPages()?
Do I have to create some kind of query inside my model? Or do I have to resolve this with existing functions (like the object storage) and how?
I tried a lot of things but can simply figure out how this should work.
you have to overwrite your findByPid repository-method and add
public function findByPid($pid) {
$querySettings = $this->objectManager->create('Tx_Extbase_Persistence_Typo3QuerySettings');
$querySettings->setRespectStoragePage(FALSE);
$this->setDefaultQuerySettings($querySettings);
$query = $this->createQuery();
$query->matching($query->equals('pid', $pid));
$pages = $query->execute();
return $pages;
}
to get all pages. Than you can write your own getSubpages-method like
function getSubpages($currentPid) {
$subpages = $this->pagesRepository->findByPid($currentPid);
if (count($subpages) > 0) {
$i = 0;
foreach($subpages as $subpage) {
$subpageUid = $subpage->getUid();
$subpageArray[$i]['page'] = $subpage;
$subpageArray[$i]['subpages'] = $this->getSubpages($subpageUid);
$i++;
}
} else {
$subpageArray = Array();
}
return $subpageArray;
}
i didn't test this method, but it looks like this to get alle subpages.
i wonder that i couldĀ“t find a typo3 method that return the complete Page-Tree :( So i write a little function (you can use in an extbase extension), for sure not the best or fastes way, but easy to extend or customize ;)
first you need an instance of the PageRepository
$this->t3pageRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\Page\\PageRepository');
this->t3pageRepository->init();
make the init, to set some basic confs, like "WHERE deletet = 0 AND hidden = 0..."
then with this function you get an array with the page data and subpages in. I implement yust up to three levels:
function getPageTree($pid,$deep=2){
$fields = '*';
$sortField = 'sorting';
$pages = $this->t3pageRepository->getMenu($pid,$fields,$sortField);
if($deep>=1){
foreach($pages as &$page) {
$subPages1 = $this->t3pageRepository->getMenu($page['uid'],$fields,$sortField);
if(count($subPages1)>0){
if($deep>=2){
foreach($subPages1 as &$subPage1){
$subPages2 = $this->t3pageRepository->getMenu($subPage1['uid'],$fields,$sortField);
if(count($subPages2>0)){
$subPage1['subpages'] = $subPages2;
}
}
}
$page['subpages'] = $subPages1;
}
}
}
return $pages;
}

Bringing in value of Coupon Code into "tax" calculation.php in magento

Im trying to pull in the name of the discount code currently applied to the cart into the calculation.php file. The name of the discount code is optionalTax but Im having trouble passing it through or retrieving it directly. Its appears to be referenced as $quote->getCouponCode() in mage/sales/model/quote.php and I want to use it in
mage/tax/model/calculation.php
Anyone have any idea on how to call it in as I've tried using the model as per (which I think is correct)
public function calcTaxAmount($price, $taxRate, $priceIncludeTax=false, $round=true)
{
$taxRate = $taxRate/100;
if ($priceIncludeTax) {
$amount = $price*(1-1/(1+$taxRate));
} else {
$cModel = Mage::getModel('catalog/sales');
$thisDiscountCode = $cModel->$quote->getCouponCode();
die($thisDiscountCode);
$amount = $price*$taxRate;
}
if ($round) {
return $this->round($amount);
} else {
return $amount;
}
}
Chris
you need to get the quote from right model i guess:
Mage::getSingleton('checkout/session')->getQuote();

Resources