add to favourites code not executing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hance
Forum Newbie
Posts: 10
Joined: Wed Jun 19, 2013 9:28 am

add to favourites code not executing

Post by hance »

hello guys,
i have an add _to_favourites.php and myList.php which are not executing.

code for add_to_favourites.php

Code: Select all

<?php
session_start();

// get the product id
$prod_id = $_GET['prod_id'];
$prod_name = $_GET['prod_name'];

/* 
 * check if the 'fav' session array was created
 * if it is NOT, create the 'fav' session array
 */
if(!isset($_SESSION['fav'])){
    $_SESSION['fav'] = array();
}

// check if the item is in the array, if it is, do not add
if(in_array($prod_id, $_SESSION['fav'])){
    // redirect to product list and tell the user it was added to favourites
    header('Location: prod_list.php?action=exists&prod_id' . $prod_id . '&prod_name=' . $prod_name);
}

// else, add the item to the array
else{
    array_push($_SESSION['fav'], $prod_id);
    
    // redirect to product list and tell the user it was added to cart
    header('Location: prod_list.php?action=add&prod_id' . $prod_id . '&prod_name=' . $prod_name);
}

?>
code for myList.php

Code: Select all

<?php

	error_reporting(E_ALL);
	ini_set('display_errors', 1);

	session_start();
	printf('<PRE>%s</PRE>', print_r($_SESSION, true));
	
    if(isset($_GET['action']) && $_GET['action']=='removed'){
        echo "<div>" . $_GET['prod_name'] . " was removed from favourites.</div>";
    }
    
    if(isset($_SESSION['fav'])){
        $ids = "";
        foreach($_SESSION['fav'] as $prod_id){
            $ids = $ids . $prod_id . ",";
        }
        
        // remove the last comma
        $ids = rtrim($ids, ',');
        
        include "db_connect.php";

        $query = "SELECT prod_id, prod_name, prod_price FROM tbl_product WHERE prod_id IN ('$ids')";
		
		/*echo $query;*/
        
		$result = mysql_query($query) or die(mysql_error());
		
		$num = mysql_num_rows($result);      
        /*$num = mysql_num_rows($query);*/

        if($num>0){
            echo "<table border='0'>";//start table
            
                // our table heading
                echo "<tr>";
                    echo "<th class='textAlignLeft'>Product Name</th>";
                    echo "<th>Price (MUR)</th>";
                    echo "<th>Action</th>";
                echo "</tr>";
                
                //also compute for total price
                $totalPrice = 0;
                
                while ($row = mysql_fetch_assoc($query)){
                    extract($row);
                    
                    $totalPrice += $prod_price;
                    
                    //creating new table row per record
                    echo "<tr>";
                        echo "<td>{$prod_name}</td>";
                        echo "<td class='textAlignRight'>{$prod_price}</td>";
                        echo "<td class='textAlignCenter'>";
                            echo "<a href='remove_favourite.php?prod_id={$prod_id}&prod_name={$prod_name}' class='customButton'>";
                                echo "<img src='shopping-cart-in-php/images/remove-from-cart.png' title='Remove from favourite' />";
                            echo "</a>";
                        echo "</td>";
                    echo "</tr>";
                }
                
                echo "<tr>";
                    echo "<th class='textAlignCenter'>Total Price</th>";
                    echo "<th class='textAlignRight'>{$totalPrice}</th>";
                    echo "<th></th>";
                echo "</tr>";
                
            echo "</table>";
            echo "<br /><div><a href='#' class='customButton'>Home</a></div>";
        }else{
            echo "<div>No products found in your favourites. :(</div>";
        }

    }else{
        echo "<div>No products in favourites yet.</div>";
    }
    ?>
on my page i have a view products button which when clicked displays all products in my database with an add to favourites button just beside. when i click on the add to favourites, i have the message that product has been added. but when i see that my count stays at 0 and my favourites displays this

Array
(
[fav] => Array
(
)

)
No products found in your favourites. :(
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: add to favourites code not executing

Post by mecha_godzilla »

Hi,

You could try commenting out the redirections in your "add_to_favourites.php" script and see what $_SESSION['fav'] contains at that point - the problem is either that the values are not being saved to the array as expected (are the $_GET values definitely being received?) or the session is not persisting between pages. You could also just try manually setting $_SESSION['fav'] for testing purposes by assigning a pre-defined array to it - if the values are no longer there when you return back to the main page, this would indicate that the session is not persisting.

A final option would be to just echo out your query - perhaps the session value *is* being set correctly, but the query is empty.

HTH,

Mecha Godzilla
Post Reply