Jump to content

aebstract

Members
  • Posts

    1,105
  • Joined

  • Last visited

Posts posted by aebstract

  1. Well I got this:

      foreach ($_SESSION['cart'] as $id => $array2){
      foreach ($array2 as $material => $array3){
      foreach ($array3 as $key => $key2){
    
    
        echo "$id $material $key $key2<br />";
    
        }
    
    
    }
    }
    

    Which turns out to be:

     

    8 mildsteel qty 1

    8 mildsteel size 4.5

    8 stainless qty 1

    8 stainless size 4.5

     

    Which is obviously not right, need it to do something like

     

    8 mildsteel qty 1 size 4.5

    8 stainless qty 1 size 4.5

     

    Still trying to get it under control though, anyone?

  2. Using:

    $_SESSION['cart'][$id][mildsteel] = array(qty => 1, size => 4.5);

     

    I get this array:

    Array
    (
        [8] => Array
            (
                [mildsteel] => Array
                    (
                        [qty] => 1
                        [size] => 4.5
                    )
    
                [stainless] => Array
                    (
                        [qty] => 1
                        [size] => 4.5
                    )
    
            )
    
    )
    
    

     

    Now I am trying to control it so that I can display the cart correctly, which for this let's say there's a table displaying each item. For each material of each item I need to show a new row. The foreach I have below will not work correctly.

      foreach ($_SESSION['cart'] as $id){
      foreach ($id as $material){
      foreach ($material as $row){
    
    print "ID: $id, Meterial: $material, Qty: $row[0], Size: $row[1]";
    
    }
    
    }
    
            }

     

    Basically need to somehow use that array to do something like this:

     

    ID: 8, Material: mildsteel, QTY: 1, Size, 4.5

    ID: 8, Material: stainless, QTY: 1, Size, 4.5

     

    and for it to be able to do this for every item ID and each different Material under each id. Hope this makes sense, and I'm still playing with it to see if I can control it.

     

  3. Is this method:

      foreach ($_SESSION['cart'] as $id => $array2){
        	print "item: $id<br />";
    foreach ($array2 as $row => $item){
    
    		if ($row == 0){
    		print "Qty: $item<br />";
    		}
    		if ($row == 1){
    		print "Material: $item<br />";
    		}
    
    }
    
      }
    
    

     

    The best for getting and using the items? I don't think an if statement should be necessary, but I'm not entirely sure. This resulted with:

     

    item: 7

    Qty: 1

    Material: mild steel

  4. I've tried a few different things, I'm getting more comfortable with arrays, but..

    $_SESSION['cart'] = array($specialid => '1', 'mild steel');

    All this will do is make my array like this:

    Array

    (

        [7] => 1

        [8] => mild steel

    )

    Somehow I want more values for [7] without creating 8, since I had to reserve all of the keys for ids. Trying to make it possible something like:

    Array

    (

        [7] => '1', 'mild steel', 'etc'

        [8] => '2', 'stainless steel'

    )

    Also I'm sure there might be a special way to go about getting the extra values, if something like this is possible? Possibly inside the foreach loop.

  5. Right. So instead use this structure:

     

    $_SESSION['cart'][ItemID] = Quantity;

     

    $_SESSION['cart'][4] = 4;
    $_SESSION['cart'][3] = 1;

     

    So something like,

    $_SESSION['cart'][$specialid] = 1;

    right? With that, how would I work with the id and qty for each item? Like how can I set em as a variable or something?

     

  6. Okay, I'm gonna work with that in the morning. Currently all I am doing to make the cart is add the id of the item on to the end of a string. So I have $_SESSION['cart'] = "4,4,4,4"; and just if I wanted to add item 3 it would just get added to the end: 4,4,4,4,3. This leaves me pretty much option-less to do anything else ;(

  7. My current array when printed looks like this:

     

    array(7) {

      [0]=>

      string(1) "8"

      [1]=>

      string(1) "8"

      [2]=>

      string(6) "11;4.2"

      [3]=>

      string(1) "7"

      [4]=>

      string(6) "11;4.3"

      [5]=>

      string(6) "11;4.2"

      [6]=>

      string(6) "11;4.2"

    }

     

    Needs to be formatted something like

     

    array (

    '11;4.2' => 3,

    '11;4.2' => 3,

    '11;4.2' => 3,

    '8' => 2,

    '7' => 1

    )

     

    Though I'm not sure how to set it up to work like that.

  8. Well if I have 3 of the same item and want to update it to 10, or 1... I'm having trouble figuring out how to easily update my quantity because of the way it is listed.

     

    edit: I see what you mean but I am pretty stupid with arrays, gonna fiddle around and see if I can figure it out.

  9. Just want some input on your thoughts for an array structure. Right now, for a shopping cart, each item is added as it's own id to a session and then split up in to an array. So 4,4,4,5,3. Then after splitting that up a      foreach ($contents as $id=>$qty) {

    will tell us that we have qty 3 of item 4, 1 of 5 and 1 of 3. Would it be better to somehow make this a deeper array, something like:

     

    array('item1' => array('1', '1', '1'),

            'item2' => array('1', '1'));

     

    Then I can display item 1 and know I have 3 of it and item2 I have 2 of it? If so how do I go about storing something like this in a variable and how do I go about grabbing those results?

  10. You told me to change it from what it is to what it is? O.o

    I think the problem originates from this line;

     

    $specialid = $id.";".$sizenum;

     

    Try changing it to this for now...

     

    $specialid = $id.";".$sizenum;

     

    This solved it, I wonder why it mattered that I was using the html version of ;. I mean, it recognized it to explode it and all, weird stuff.. Thanks everyone for their time and input ;)

  11. Right now it is getting set with:

    <?php
    
    
    
    $product_cat = $_GET['product_cat'];
    $checknum = 1;
    $result = mysql_query("SELECT * FROM `p_products` WHERE category='$product_cat'") or DIE(mysql_error());
    while($r = mysql_fetch_array($result))
    {
      $id = $r['id'];
      $partnumber = $r['partnumber'];
      $partname = $r['partname'];
      $description = $r['description'];
      $price = $r['price'];
      $option1 = $r['option1'];
    $specialid = $id;
    
    $postname = image.$id;
    $postsize = size.$id;
    
    if (isset($_POST[$postname])) {
    $error = 0;
    if (isset($_POST['hideme'])) {
    if (empty($_POST[$postsize])){
    $error = 1;
    $errormsg = "You must fill in a Pipe Size for $partname.";
    } else {
    $sizenum = $_POST[$postsize];
    $specialid = $id.";".$sizenum;
    }
    }
    
    
    if ($error == 1){
    $errordisplay .= "<div id=\"error_box\">   $errormsg</div>";
    } else {
    
    
    $cart = $_SESSION['cart'];
    if ($cart) {
    $cart .= ','.$specialid;
    } else {
    $cart = $specialid;
    }
    $_SESSION['cart'] = $cart;
    header("Location: index.php?page=cart");
    exit;
    }
    
    
    
    }
    
    
    if (!empty($option1)){
    $clicker = "
    Pipe Size: <input type=\"text\" size=\"4\" value=\"\" name=\"size$id\" style=\"background-color: #FFFFFF; border: solid 1px #000000; height: 20px; font-size: 10px;\" /><br />
    <div class=\"product_image\">
    <input type=\"hidden\" name=\"hideme\" value=\"needsize\" />
    <input type=\"image\" value=\"submit$id\" src=\"addtocart.jpg\" width=\"144\" height=\"34\" border=\"0\" alt=\"Add to Cart\" name=\"image$id\">
    </div>";
    } else {
    $clicker = "<br /><div class=\"product_image\">
    <input type=\"image\" value=\"submit$id\" src=\"addtocart.jpg\" width=\"144\" height=\"34\" border=\"0\" alt=\"Add to Cart\" name=\"image$id\">
    </div>";
    }
    
    if( $odd = $checknum%2 ) {
    	$content .= "
    	<form action=\"index.php?page=product_page&product_cat=$product_cat\" method=\"post\" name=\"add_product$id\">
    	<div class=\"product_left\">
    	<div class=\"product_image\"><img src=\"products/$partnumber-cart.jpg\" class=\"jkimagelarge\" title=\"products/$partnumber.jpg\" height=\"100\"/></div>
    	<div class=\"product_info\">
    	$partname
    	<br />
    	$partnumber
    	<br />
    	$$price
    	<br />
    
    $clicker
    </div>
    	</div></form>";
    } else {
    	$content .= "<div class=\"product_right\">
    	<form action=\"index.php?page=product_page&product_cat=$product_cat\" method=\"post\" name=\"add_product$id\">
    	<div class=\"product_image\"><img src=\"products/$partnumber-cart.jpg\" class=\"jkimagelarge\" title=\"products/$partnumber.jpg\" height=\"100\"/></div>
    	<div class=\"product_info\">
    	$partname
    	<br />
    	$partnumber
    	<br />
    	$$price
    	<br />
    $clicker
    </div>
    	</div></form>";	}
    
    
    $checknum ++;
    }
    
    
    
    
    ?>
    
    
    

     

    But.. it is set correctly, let me add a few items to the cart without the value and then throw one in the middle with it and show you the echo results:

     

    1,9,8,11;4.5,7,8
    
    array(6) {
      [0]=>
      string(1) "1"
      [1]=>
      string(1) "9"
      [2]=>
      string(1) "8"
      [3]=>
      string(10) "11;4.5"
      [4]=>
      string(1) "7"
      [5]=>
      string(1) "8"
    }
    
    ID is 1 and there is no size!
    ID is 9 and there is no size!
    ID is 8 and there is no size!
    ID is 11; and Size is 4.5
    ID is 7 and there is no size!
    ID is 8 and there is no size! 
    

     

    You can see it is set correctly, and it's splitting them all correctly but leaving that ;. What section of my code do you think you will need to look at? Not sure if what I posted is all, but it's where it gets set at.

  12. that is strange, just to give it a try, pass the variable to the function rather than using the $_SESSION inside the function;

     

    function showCart2($cart) {
       #$cart = "11;5 5/8";
    echo "$cart";
    $var = explode(',',$cart);
    echo '<pre>';
    var_dump($var);
    echo '</pre>';
    $cnt = count($var);
    
    for ($i=0; $i<$cnt; $i++) {
        list($id, $size) = explode(";", $var[$i]);
    
    if (isset($size)) {
       echo "ID is $id and Size is $size <br />";
    } else {
       echo "ID is $id and there is no size! <br />";
    }
    
    }
    }
    
    showCart2("11;5 5/8");
    

     

    This worked fine.

  13. well in that code your form doesn't have an action or method or anything. This is why it is good to have actual code presented, and if this is.. then there is problem 1. Also, if you're gonna have a submit button for each bin item, each bin item is also going to have to have its own form, so the actual form will have to be included in to the loop. If this is true, each form will need it's own name, I would suggest using your id to uniquely name each form.

×
×
  • 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.