Solved

$_SESSION[cart][$product_name]  returns  { Array[Bowl_in_Green_glaze] }

Posted on 2007-11-16
6
292 Views
Last Modified: 2010-04-21
$_SESSION[cart][$product_name]  returns  { Array[Bowl_in_Green_glaze] }
Is there a Mid$ function and a Left$ function in PHP so I can trim off the first 8 and the last 3 characters?
(or is there a way to filter that intrinsically?)

Oh, and for the extra 9 points, why is this failing as "Fatal error: Unsupported operand types" ?
     $total += ( $_SESSION['cart'][$product_name]['price'] * $count );
it also fails like this:
     $total = $total + ( $_SESSION['cart'][$product_name]['price'] * $count );

Thanks,
David
0
Comment
Question by:DavidWare
  • 4
  • 2
6 Comments
 
LVL 2

Expert Comment

by:guston76
ID: 20303401
// LEFT FUNCTION
function left($string, $length) {
      return substr($string, 0, $length);
}

// RIGHT FUNCTION
function right($string, $length) {
      return substr($string, strlen($string)-$length);
}

// FUNCTION TO CUT STRING FROM LEFT AND RIGHT
function cut_string($string, $chars_from_left, $chars_from_right) {
      return substr($string, $chars_from_left, strlen($string)-$chars_from_left-$chars_from_right);
}

// FOR YOUR SITUATION...
$s = cut_string ( $_SESSION[cart][$product_name], 8, 3);
0
 

Author Comment

by:DavidWare
ID: 20304049
Hello guston76,
Thanks for the help.

This returns an error
Notice: Array to string conversion in /var/www/sites/mysite.com/cart.php on line 125

Thanks,
David
0
 
LVL 2

Accepted Solution

by:
guston76 earned 84 total points
ID: 20307124
Oops... I forgot the array :   Array[Bowl_in_Green_glaze]

If it is an associative array with a key "Bowl_in_Green_glaze", then you should update the previous code as :
$s = cut_string ( $_SESSION[cart][$product_name]['Bowl_in_Green_glaze'], 8, 3);

Now about the "unsupported operand type" error. Your code was :
$total += ( $_SESSION['cart'][$product_name]['price'] * $count );

It doesn't matter if you use "$total += ..."  or  "$total = $total + ...", I'm sure that's not the cause.
Please check the type of $_SESSION['cart'][$product_name]['price'] and $count, using gettype() function :

print gettype ( $_SESSION['cart'][$product_name]['price'] ); print "<br>";
print gettype ( $count );

Both types must be numeric in order to make your code works.

Good luck.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:DavidWare
ID: 20317485
Is there a Mid$ function in PHP?
This is an important part of the question.
I'm adding a few points for this clarification.
Thanks!
0
 

Author Closing Comment

by:DavidWare
ID: 31409709
Oh, substr()
Thanks for your help!
0
 

Author Comment

by:DavidWare
ID: 20317508
Oh, substr()
Thanks!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now