Getting Array data into a variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • iannorton@gmail.com

    Getting Array data into a variable

    Hi,

    I've spent most of today trying to solve this problem, but sadly no
    luck.
    I have an shopping basket based on an array which stores the name,
    product id, quantity and price for products, i want to take the
    Information in the array and output the Quantity and Product ID
    information into variables so that i can pass them into a Select
    Statement and submit the order to the database.

    The Current Code to Show the cart is listed below: -

    function show_cart() {
    if (isset($_SESSIO N["item_count "]) && $_SESSION["item_count "] > 0) {
    echo "<table class=\"basket\ "border=\"0\">" ;
    echo
    "<tr><td>It em</td><td>Price</td><td>Amount</td><td>Subtotal </td></td></tr>";

    $total = 0;
    for($i=0; $i<$_SESSION["item_count "]; $i++) {

    echo "<td><a
    href=insertself pagehere.php?de leteitem=1&Prod ID=".$_SESSION["items"][$i][0].">";

    $item=$_SESSION["items"][$i][0];
    echo $_SESSION["items"][$i][1]."</a></td>";
    echo "<td>".$_SESSIO N["items"][$i][2]."</td>";
    echo "<td>*".$_SESSI ON["items"][$i][3]."</td>";
    $subtotal = $_SESSION["items"][$i][2] * $_SESSION["items"][$i][3];
    echo "<td align=\"right\" >$subtotal</td>";
    echo "</tr>";
    $total += $subtotal;
    }
    echo "<tr><td><b>tot al</b></td><td align=\"right\"
    colspan=\"3\">$ total</td></tr></table>";
    }

    else {
    echo "No items";
    }
    }

    Any help would be much appreciated, i seem to have a mental block with
    PHP at the moment.

    Kind Regards,

    Ian

  • Ken Robinson

    #2
    Re: Getting Array data into a variable

    iannor...@gmail .com wrote (in part):[color=blue]
    > Hi,
    >
    > I've spent most of today trying to solve this problem, but sadly no
    > luck.
    > I have an shopping basket based on an array which stores the name,
    > product id, quantity and price for products, i want to take the
    > Information in the array and output the Quantity and Product ID
    > information into variables so that i can pass them into a Select
    > Statement and submit the order to the database.
    >
    > The Current Code to Show the cart is listed below: -
    >
    > function show_cart() {
    > if (isset($_SESSIO N["item_count "]) && $_SESSION["item_count "] > 0) {
    > echo "<table class=\"basket\ "border=\"0\">" ;
    > echo
    >[/color]
    "<tr><td>It em</td><td>Price</td><td>Amount</td><td>Subtotal </td></td></tr>";[color=blue]
    >
    > $total = 0;
    > for($i=0; $i<$_SESSION["item_count "]; $i++) {
    >
    > echo "<td><a
    >[/color]
    href=insertself pagehere.php?de leteitem=1&Prod ID=".$_SESSION["items"][$i][0].">";[color=blue]
    >
    > $item=$_SESSION["items"][$i][0];
    > echo $_SESSION["items"][$i][1]."</a></td>";
    > echo "<td>".$_SESSIO N["items"][$i][2]."</td>";
    > echo "<td>*".$_SESSI ON["items"][$i][3]."</td>";
    > $subtotal = $_SESSION["items"][$i][2] * $_SESSION["items"][$i][3];
    > echo "<td align=\"right\" >$subtotal</td>";
    > echo "</tr>";
    > $total += $subtotal;
    > }
    > echo "<tr><td><b>tot al</b></td><td align=\"right\"
    > colspan=\"3\">$ total</td></tr></table>";
    > }
    >
    > else {
    > echo "No items";
    > }
    > }[/color]

    First let's start by cleaning up your code a little bit. You can
    eliminate all of your escaped quotes by using single quotes:

    function show_cart() {
    if (isset($_SESSIO N['item_count']) && $_SESSION['item_count'] > 0) {
    echo '<table class="basket" border="0">';
    echo
    '<tr><td>Item</td><td>Price</td><td>Amount</td><td>Subtotal </td></td></tr>';

    $total = 0;
    for($i=0; $i<$_SESSION['item_count']; $i++) {

    echo '<td><a
    href=insertself pagehere.php?de leteitem=1&Prod ID='.$_SESSION['items'][$i][0].'>';

    $item=$_SESSION['items'][$i][0];
    echo $_SESSION['items'][$i][1].'</a></td>';
    echo '<td>'.$_SESSIO N['items'][$i][2].'</td>';
    echo '<td>*'.$_SESSI ON['items'][$i][3].'</td>';
    $subtotal = $_SESSION['items'][$i][2] * $_SESSION['items'][$i][3];
    echo '<td align="right">' . $subtotal . '</td>';
    echo '</tr>';
    $total += $subtotal;
    }

    echo '<tr><td><b>tot al</b></td><td align="right" colspan="3">' . $total
    .. '</td></tr></table>';

    }
    else echo "No items";
    }

    Now a few questions:
    1) Since you are using sessions, why are you passing the value of
    "$_SESSION['items'][$i][0]" in your link, why not just "$i"?

    2) What's your database look like?
    Are looking for a statement like:
    $q = "Update tablename set quantity='" . $_SESSION['items'][$i][3] . "'
    where product_id = '" . $item . "'";

    Ken

    Comment

    • Richards Noah \(IFR LIT MET\)

      #3
      Re: Getting Array data into a variable

      <iannorton@gmai l.com> wrote in message
      news:1105296684 .673441.276640@ c13g2000cwb.goo glegroups.com.. .[color=blue]
      > Hi,
      >
      > I've spent most of today trying to solve this problem, but sadly no
      > luck.
      > I have an shopping basket based on an array which stores the name,
      > product id, quantity and price for products, i want to take the
      > Information in the array and output the Quantity and Product ID
      > information into variables so that i can pass them into a Select
      > Statement and submit the order to the database.
      >
      > The Current Code to Show the cart is listed below: -
      >
      > function show_cart() {
      > if (isset($_SESSIO N["item_count "]) && $_SESSION["item_count "] > 0) {
      > echo "<table class=\"basket\ "border=\"0\">" ;
      > echo
      >[/color]
      "<tr><td>It em</td><td>Price</td><td>Amount</td><td>Subtotal </td></td></tr>";[color=blue]
      >
      > $total = 0;
      > for($i=0; $i<$_SESSION["item_count "]; $i++) {
      >
      > echo "<td><a
      >[/color]
      href=insertself pagehere.php?de leteitem=1&Prod ID=".$_SESSION["items"][$i][0].
      ">";[color=blue]
      >
      > $item=$_SESSION["items"][$i][0];
      > echo $_SESSION["items"][$i][1]."</a></td>";
      > echo "<td>".$_SESSIO N["items"][$i][2]."</td>";
      > echo "<td>*".$_SESSI ON["items"][$i][3]."</td>";
      > $subtotal = $_SESSION["items"][$i][2] * $_SESSION["items"][$i][3];
      > echo "<td align=\"right\" >$subtotal</td>";
      > echo "</tr>";
      > $total += $subtotal;
      > }
      > echo "<tr><td><b>tot al</b></td><td align=\"right\"
      > colspan=\"3\">$ total</td></tr></table>";
      > }
      >
      > else {
      > echo "No items";
      > }
      > }
      >
      > Any help would be much appreciated, i seem to have a mental block with
      > PHP at the moment.
      >
      > Kind Regards,
      >
      > Ian
      >[/color]

      I'd love to help you, but I'm not quite sure what the "problem" is. There
      doesn't appear to be any coding errors (PHP wise) here, although you are
      missing some quotes in certain places:

      echo '<td><a>
      href="insertsel fpagehere.php?d eleteitem=1&Pro dID='.$_SESSION["items"][$i][0]
      ..'">';

      You remembered them with the <table> tag, I'm sure you just overlooked this
      one.

      Also, if "insertselfpage here.php" is meant to denote, well, whatever page
      you are at, you can always use:

      $_SERVER['php_self']

      as a better alternative (for example, if the page name changes, you don't
      need to change it within the script).

      But, like I said, I'm not sure where your problem is. If you could post a
      description of the problem and the error output (or the incorrect output and
      what it should be), I'll do my best to help out.

      -Noah


      Comment

      • iannorton@gmail.com

        #4
        Re: Getting Array data into a variable

        Thanks for the replies,
        The problem is that i really don't know too much about arrays.
        I know how to use varialbes and mysql with PHP but i'm stuck with
        Arrays.
        All i need to do is get the contents of the array into variables, after
        this i need to add them to a mysql DB so that the order can be saved,
        processed or used as a quote at a later point.

        Ken mentioned: -
        2) What's your database look like?
        Are looking for a statement like:
        $q = "Update tablename set quantity='" . $_SESSION['items'][$i][3] . "'
        where product_id = '" . $item . "'";


        This seems like what i'm after, only this would require X amounts of DB
        lookups (X being the number of different items in the DB)
        Is this the best way to do it?

        Kind Regards,

        Ian

        Comment

        • Chris Newey

          #5
          Re: Getting Array data into a variable


          <iannorton@gmai l.com> wrote in message
          news:1105564155 .335161.135570@ z14g2000cwz.goo glegroups.com.. .
          | Thanks for the replies,
          | The problem is that i really don't know too much about arrays.
          | I know how to use varialbes and mysql with PHP but i'm stuck with
          | Arrays.
          | All i need to do is get the contents of the array into variables, after
          | this i need to add them to a mysql DB so that the order can be saved,
          | processed or used as a quote at a later point.
          |
          | Ken mentioned: -
          | 2) What's your database look like?
          | Are looking for a statement like:
          | $q = "Update tablename set quantity='" . $_SESSION['items'][$i][3] . "'
          | where product_id = '" . $item . "'";
          |
          |
          | This seems like what i'm after, only this would require X amounts of DB
          | lookups (X being the number of different items in the DB)
          | Is this the best way to do it?
          |
          | Kind Regards,
          |
          | Ian
          |

          Orders eh - presumably you have order header and order detail tables to
          enable you to have 1->n items per order. If this is the case then you have
          to have one insert/update for each unique row.

          rtfm springs to mind. http://dev.mysql.com/doc/mysql/en/INSERT.html and
          http://dev.mysql.com/doc/mysql/en/UPDATE.html would be good places to start.

          Chris


          Comment

          Working...