It is possible to pass a reference in a PHP variable that is taken from inside scope of an instantiated object class (PHP) into the JQuery function, in order for it render to a number or string to display the number of items added? I have a code here something like this:
<!--in cart.php-->
$(document).ready(function() {
$('div#cart').replaceWith('<div id=\'cart2\'><span><?php echo ('.**$itemcount**.'); ?> </span> <a href=\'viewcart.html\' class=\'view-car开发者_如何学Ct\'>View Cart</a><a class=\'checkout\' href=\'checkout.html\'>Checkout</a></div>');
});
where $itemcount
is a stored variable in PHP external file called store.php
many thanks for assistance!
An interesting question to say the least...
Lets say store.php looks like this:
<?php
class store {
private $_itemcount = 0;
public function getItemCount(){
return $this->_itemcount;
}
}
?>
From there we go like this in cart.php:
<?php
$store = new store();
$itemcount = $store->getItemCount();
echo <<<html
$(document).ready(function() {
$('div#cart').replaceWith("<div id='cart2'><span>$itemcount</span><a href='viewcart.html' class='view-cart'>View Cart</a><a class='checkout' href='checkout.html'>Checkout</a></div>");
});
html;
?>
This way, we can return the value and use it in the javascript.
store.php
<?php $itemcount = 10; ?>
in cart.php
$(document).ready(function() {
$('div#cart').replaceWith('<div id=\'cart2\'><span><?php require 'store.php'; echo $itemcount; ?> </span> <a href=\'viewcart.html\' class=\'view-cart\'>View Cart</a><a class=\'checkout\' href=\'checkout.html\'>Checkout</a></div>');
});
There are a number of answers here that will work with a string or number, but if your requirements develop any more complexity you'll run into problems.
If needed you can serialize a PHP object to JSON which will allow you to create a JavaScript object that can be used directly by JavaScript / JQuery code.
Assuming your cart.php script has access to the object from store.php represented by $phpObject:
e.g.
...
$(document).ready(function() {
var jsPhpObject = <?php echo json_encode($phpObject); ?>; // doesn't need to be inside document.ready if required elsewhere
$('div#cart').replaceWith("<div id='cart2'><span>" + jsPhpObject.property + "</span><a href='viewcart.html' class='view-cart'>View Cart</a><a class='checkout' href='checkout.html'>Checkout</a></div>");
});
...
store.php
<?PHP
class SomeClass
{
public $insideMemberNum;
public function __construct()
{
global $outsideNum;
$insideNum = 2;
$this->insideMemberNum = 2;
// 1. $outsideNum = $insideNum; (this will make $outsideNum = 2, even outside this class)
// 2. $outsideNum = &$insideNum; (this will make $outsideNum = 2, but only wihtin this scope, after it will go back to 1)
// so youre options are...
// 1. pass by value
// 2. create a public variable and store the number in there
// 3. create a private variable and a public function to return the value of that variable like someone else suggested
}
}
?>
incart.php
<?PHP
$outsideNum = 1;
echo $outsideNum . "<br />"; // prints out 1
include("store.php"); // include the external file
$obj = new SomeClass(); // instansiate the object
echo $outsideNum . "<br />"; // option 1 prints out 2, option 2 prints out 1
echo $obj->insideMemberNum . "<br />"; // prints out 2
?>
精彩评论