Solved

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

Posted on 2007-11-16
6
298 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 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

726 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