Solved

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

Posted on 2007-11-16
6
296 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

809 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