Jump to content

code not executing - no error messages received


hance2105

Recommended Posts

i have a myList.php which should list all products added to my favourites and compute the total price of products.

 

here is the code

 

<?php                 include 'navigation.php'     ?>        <div class='sectionContents'>    <?php    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 = mysql_query("SELECT prod_id, prod_name, prod_price FROM tbl_product WHERE prod_id IN ('$ids')") or die(mysql_error());                      $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>";    }    ?>

 
i use the add_to_fav.php to add the products to my favourites
 
<?phpsession_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 addif(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 arrayelse{    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);} ?>

 
i am having "No products found in your favourites. :(" when i try to view the favourites
 
i have a counter like thing which shows the number of products in my favourites as well and it stays to 0.
 
have i erred somewhere? which mistake should i correct?

 

Link to comment
Share on other sites

<?php 
        
        include 'navigation.php' 
    ?>
    
    <div class='sectionContents'>
    <?php
    if(isset($_GET['action']) && $_GET['action']=='removed'){
        echo "<div>" . $_GET['prod_name'] . " was removed from favourites.</div>";
    }
    
    if(isset($_SESSION['fav'])){
        $ids = array( )
        foreach($_SESSION['fav'] as $prod_id){
            $ids[] = $prod_id; // i see how you were doing it now, sorry mate just woke up 
        }
        
        include "db_connect.php";
 
        $ids = implode(",", $ids);

        $query = mysql_query("SELECT prod_id, prod_name, prod_price FROM tbl_product WHERE prod_id IN ('{$ids}')") or die(mysql_error());
              
        $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);// i dont know where ur extract function it, but im assuming it is taking the key and making that a var, with its correct value
                    foreach($row as $k => $v){
                         $var = $k;
                         ${$var} = $v;
                    }// this should work I havnt tested it
                    
                    $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>";
    }
?>

Im not sure it works but i hope so.  When you were adding your favorites you were just over righting the line you currently added.  

Edited by chriscloyd
Link to comment
Share on other sites

You bump a thread after 2 minutes? Really! That is against the rules.

 

You do not have a session_start() in the code showing the favorites.

 

Also, $ids = implode(',', $_SESSION['fav']); would be much less code than looping through the array.

Link to comment
Share on other sites

The code you posted does not show a session_start().

 

Turn on error reporting so you can see what is happening.

 

Also dump the session at the start of the page to see what is there.

 

Put as the very first line in the script.

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

session_start();
printf('<PRE>%s</PRE>', print_r($_SESSION, true));
Link to comment
Share on other sites

This

Array
(
[fav] => Array
(
)

)
is the output from the print_r. And that indicates that the session array is empty. So the problem appears to be in the code that is building the list of favorites.

 

If you turn on error reporting in that script and add an item, what messages do you get?

Link to comment
Share on other sites

this is the code where the add to favourites button is found. the code i had given at start of this thread is the add_to_fav.php

 

this one is called the prod_list.php

 

<?phpif(isset($_GET['action']) && $_GET['action']=='add'){    echo "<div>" . $_GET['prod_name'] . " was added to your favourites.</div>";} if(isset($_GET['action']) && $_GET['action']=='exists'){    echo "<div>" . $_GET['prod_name'] . " already exists in your favourites.</div>";} include "db_connect.php"; $query =mysql_query("SELECT prod_id, prod_name, prod_price FROM tbl_product"); $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>";                while ($row = mysql_fetch_assoc($query)){            extract($row);                        //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='add_to_fav.php?prod_id={$prod_id}&prod_name={$prod_name}' class='customButton'>";                        echo "<img src='shopping-cart-in-php/images/add-to-cart.png' title='Add To Favourites' />";                    echo "</a>";                echo "</td>";            echo "</tr>";        }            echo "</table>";} // no products in the databaseelse{    echo "No products found.";}?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.