I'm making a newbie webshop and I'm stuck on a problem. I've written a function to simply draw an item if it is defined. (so it will draw the image with its description, only if the item was actually added to my cart).
Note, this is obviously not the best way to do it. But it should work.
Setting my items. Will only work if they are actually defined at the start.
$item1 = $_SESSION['item1'];
$item2 = $_SESSION['item2'];
$item3 = $_SESSION['item3'];
$item4 = $_SESSION['item4'];
$item5 = $_SESSION['item5'];
Calling my function:
ValidateCart($item1, 'item1_name', 'item1_size', 'item1_price', 'item1_url');
My Function:
function ValidateCart($item, $itemname, $itemsize, $itemprice, $itemurl)
{
$arr = array($item1, $item2, $item3, $item4, $item5);
if (in_array($item, $arr)) {
echo '<br><hr>';
echo $_SESSION[$itemname]; echo '<br>';
echo $_SESSION[$itemsize]; echo '<br>';
echo '€ '; echo $_SESSION[$itemprice]; echo '<br><br>';
$itemurl1 = $_SESSION[$itemurl];
$itemurl2 = str_replace('url(', 'http://vintagefairytale.esy.es/', $itemurl1);
$itemurl3 = str_replace(')', '', $itemurl2);
echo '<img src='; echo $itemurl3; echo '></img>';
echo '<hr>';
}
}
Basically, in my ValidateCart()
I'm making an array with all the possible items, and if an item is matching my array, it will continue. However, it doesn't.
If I change if (in_array($item, $arr))
to if (in_array($item1, $arr))
it'll work but that's not what I'm trying to achieve.
I need it to work in a function, but to me it doesn't make sense when I call the function with the fit arguments, it won't show. But if I'd do my check inside my function, it works. Hence losing the point of using a function.
The problem was that my $item1 $item2 $item3... wont be used in the scope, not sure why but that was the issue. Calling the sessions inside my function has solved it. Thanks for the help!
why dont you use $arr outside the function and used in function as argument.
$arr = array($item1, $item2, $item3, $item4, $item5);
function ValidateCart($item, $itemname, $itemsize, $itemprice, $itemurl, $arr)
{
if (in_array($item, $arr)) {
echo '<br><hr>';
echo $_SESSION[$itemname]; echo '<br>';
echo $_SESSION[$itemsize]; echo '<br>';
echo '€ '; echo $_SESSION[$itemprice]; echo '<br><br>';
$itemurl1 = $_SESSION[$itemurl];
$itemurl2 = str_replace('url(', 'http://vintagefairytale.esy.es/', $itemurl1);
$itemurl3 = str_replace(')', '', $itemurl2);
echo '<img src='; echo $itemurl3; echo '></img>';
echo '<hr>';
}
}