?
Solved

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

Posted on 2007-11-16
6
Medium Priority
?
301 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 336 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
Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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 look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

752 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