#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Posts
    61
    Rep Power
    15
    This seems so strange to me. I am saving a product in a shopping cart table named shopping. All needed variables are saved but the product variable always has 10 spaces before the true data. Product is currently a text type in the table.( have tried variations). I have even removed the field and reinstalled it with no changes. If I go in through PHPmyAdmin and edit out the spaces the view cart works. In the code below I have put in some echos ( echo $product ; ) at key points before AND after inserting or updating. All reports back correctly. I have another text field in this table that saves ok. Please, please help!

    add to shopping table code
    ***************************
    function check_item($table, $session, $product) {
    $query = "SELECT * FROM $table WHERE session='$session' AND product='$product' ";
    $result = mysql_query($query);

    if(!$result) {
    return 0;
    }

    $numRows = mysql_num_rows($result);

    if($numRows == 0) {
    return 0;
    } else {
    $row = mysql_fetch_object($result);
    return $row->quantity;
    }
    }

    function add_item($table, $session, $product, $quantity) {
    $qty = $this->check_item($table, $session, $product);
    if($qty == 0) {
    echo "<BR>inserted ".$product."<BR><BR>";
    $query = "INSERT INTO $table (session, product, quantity) VALUES ('$session', '$product', '$quantity') ";
    echo "<BR>inserted ".$product."<BR><BR>";
    mysql_query($query);
    } else {
    $quantity += $qty;
    echo "<BR>updated".$product."<BR><BR>";
    $query = "UPDATE $table SET quantity='$quantity' WHERE session='$session' AND product='$product' ";
    echo "<BR>updated".$product."<BR><BR>";
    mysql_query($query);
    }

    }

    *******************
    view cart code
    *******************
    function display_contents($table, $session) {
    global $contents;
    global $count;
    $count = 0;
    $query = "SELECT * FROM $table WHERE session='$session' ORDER BY id ";
    $result = mysql_query($query);
    while($row = mysql_fetch_object($result)) {
    $query = "SELECT * FROM inventory WHERE product='$row->product' ";
    $result_inv = mysql_query($query);
    $row_inventory = mysql_fetch_object($result_inv);

    $contents["product"][$count] = $row_inventory->product;
    $contents["price"][$count] = $row_inventory->price;
    $contents["quantity"][$count] = $row->quantity;
    $contents["total"][$count] = ($row_inventory->price * $row->quantity);
    $contents["description"][$count] = $row_inventory->description;
    $count++;
    }
    $total = $this->cart_total($table, $session);
    $total = number_format($total,2);
    $contents["final"] = $total;
    return $contents;
    return $count;
    }
    ***********************

    [This message has been edited by stujohnson (edited November 17, 2000).]
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    Perth West Australia
    Posts
    757
    Rep Power
    15
    not sure where your problem lies but you can always use trim($products) when inserting or echoing.

    although finding out why is of course better!

    ------------------
    Simon Wheeler
    FirePages -DHTML/PHP/MySQL
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Posts
    61
    Rep Power
    15
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by firepages:
    not sure where your problem lies but you can always use trim($products) when inserting or echoing.

    although finding out why is of course better!

    [/quote]

    Simon,
    Thanks for the help, it works. I found that by trimming before the function check_item it straightened out. I will check further to see what is happening there.

    I am still confused why the echo commands did not show the spaces though. If anyone can enlighten me on this it would be great.

    stujohnson
  6. #4
  7. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Brussels, Belgium
    Posts
    14,648
    Rep Power
    4493
    If you look in the source of the file generated by PHP, the spaces are probably there. HTML will only show one space though, unless something is surrounded by <pre> tags.

    so <space>word and <space><space><space>word will show up the same in HTML, but in the source, you'll see the spaces.

    Hope that helps.

    ---John Holmes...

IMN logo majestic logo threadwatch logo seochat tools logo