Solved

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

Posted on 2007-11-16
6
293 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Echo images using file system 2 31
object oriented programming comparison 5 54
what is best version of php to use 6 46
WP_Query multiple custom taxonomies 5 27
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…
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.

910 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

24 Experts available now in Live!

Get 1:1 Help Now