Jump to content

makamo66

Members
  • Posts

    64
  • Joined

  • Last visited

Everything posted by makamo66

  1. This is what I'm using and it's not working: foreach($cart_items as $item) { foreach($item["id"] as $key => $val) { foreach($item["quantity"] as $i => $value) { if ($key == $i){ if(empty($item["quantity"])){ $item["quantity"] = $value; } elseif (isset($_REQUEST["quantity_$i"]) ) { $item["quantity"] = $value + $_REQUEST["quantity_$i"]; $value = $item["quantity"]; echo "Same product"; }}}}
  2. The last product in the SESSION array is the product with the non-updated quantity. When I update the quantity, I get the previous product and also the updated product. I have to remove the non-updated product.
  3. I'm doing something like that. My quantity is updating when I add the same product but I need to delete the product that's the last item in the SESSION array. So far I've tried array_pop and unset but I can't get rid of the non-updated product. These two don't work to remove the last product added to the session: unset($_SESSION["cart_item"]["element_id"]); array_pop($_SESSION["cart_item"]);
  4. If you were working on a PHP shopping cart using SESSION variables for the cart item's product id and quantity, how would you update the cart if the same product were chosen? If the same product id was posted, how would you add the quantities and show just the same product?
  5. What is or where do I find this simple cart definition?
  6. This is the code that I'm using for a shopping cart in order to update the quantity if the same product id is chosen. $cart_items is a two-dimensional session array composed of the id session array and the quantity session array. While looping through $cart_items, I create a temporary array $temp_array so that I can execute some logic on the array without changing the normal output. $temp_array is the $cart_items array without the last items which would be the $_REQUEST id and the $_REQUEST quantity. So the two $_REQUESTs are popped off with array_pop and then the remaining $_SESSIONs are compared to the $_REQUEST id with $j = array_search($_REQUEST["element_id_$i"], $temp_array); $j is now the key that corresponds to the id in $temp_array where the $_REQUEST id has been found. if( $j==$temp_array[$i]) If $j is the key of the item in $temp_array, then the $_REQUEST is unset and the quantities for the item are added together. There are two problems with this code. First of all the var_dump of the $temp_array is always an empty array but I am expecting to get an array with just the last element popped off. Secondly, I get the error "Undefined offset" for the $i variable in this line of the code: if( $j==$temp_array[$i]) foreach($cart_items as $item) { foreach($item["id"] as $key => $val) { foreach($item["quantity"] as $i => $value) { if ($key == $i){ $temp_array = $cart_items; $array = array_pop($temp_array); var_dump($temp_array); if (isset($_REQUEST["element_id_$i"]) && isset($_REQUEST["quantity_$i"])&& isset($value)){ $j = array_search($_REQUEST["element_id_$i"], $temp_array); if( $j==$temp_array[$i]) { echo "<b>SAME PRODUCT ADDED</b>"; $value += $_REQUEST["quantity_$i"]; unset($_REQUEST["element_id_$i"]); }}}}}}
  7. I retract my original question because I don't need to make my multidimensional-array any larger after all. Please ignore my post.
  8. The inner arrays of the multidimensional $_SESSION arrays aren't single variables, they're $_SESSION arrays defined like below. for($i=1; $i<=6; $i++){ if (isset($_REQUEST["element_id_$i"]) && ($_REQUEST["element_id_$i"] != NULL) ) { $_SESSION["element_id_$i"] = $_REQUEST["element_id_$i"]; $id = $_SESSION["element_id_$i"]; array_push($_SESSION["element_id"],$id); } $id = $_SESSION["element_id"]; if (isset($_REQUEST["product_$i"]) && ($_REQUEST["product_$i"] != NULL) ) { $_SESSION["product_name_$i"] = $_REQUEST["product_$i"]; $product_name = $_SESSION["product_name_$i"]; array_push($_SESSION["product_name"],$product_name); } $product_name = $_SESSION["product_name"]; }
  9. <?php $_SESSION["cart_item"] = array( 'cart_item' => array( 'id' => $id, 'product_name' => $product_name )); } $cart_items = $_SESSION["cart_item"]; foreach ($cart_items as $cart_item) { echo $cart_item["id"] . $cart_item["product_name"]; } ?> I have tried several variations of the foreach loop like the one above and I mostly get the error message: Notice: Array to string conversion. When I use: var_dump($cart_items); I get the following output: array(1) { ["cart_item"]=> array(2) { ["id"]=> array(2) { [0]=> string(1) "2" [1]=> string(1) "3" } ["product_name"]=> array(2) { [0]=> string(19) "Adult Female Bike" [1]=> string(18) "Kids Unisex Bike" } } }
  10. I want to use array_pop to remove the last item added to the SESSION array. So it would remove the REQUEST item. The problem is that nothing is left in the session array because I pop off the last item each time that the form is submitted. Maybe I need to use array_push to push the popped items back on to the SESSION array after the logic has been applied. I don't understand how array_pop is supposed to work on a SESSION array. Can anyone explain to me what behaviour to expect? I expected to get a SESSION array with the REQUEST item removed but I thought that I would still have all of my SESSION items.
  11. The solution posted to a different forum was: $sql = "INSERT INTO comment_table (name,date_col,website,comment) VALUES (?, ?, ?, ?)";
  12. I changed the code so that it is using prepared statements but I just get new errors now. I altered the table so that the date_col column has the data type of varchar. I get the following warning: mysqli_stmt_bind_param(): Number of variables doesn't match number of parameters in prepared statement if(isset($_REQUEST["submit"]) ) { $date = new DateTime('', new DateTimeZone('America/New_York')); $name = mysqli_real_escape_string($connection, $_REQUEST["name"]); $date_col = $date->format('M d, Y H:i'); $website = $_REQUEST["website"]; $comment = mysqli_real_escape_string($connection, $_REQUEST["comment"]); $sql = "INSERT INTO comment_table (name,date_col,website,comment) VALUES ('$name', '$date_col','$website','$comment')"; if($stmt = mysqli_prepare($connection, $sql)){ // Bind variables to the prepared statement as parameters mysqli_stmt_bind_param($stmt,'ssss',$name,$date_col,$website,$comment); // Attempt to execute the prepared statement if(mysqli_stmt_execute($stmt)){ echo "Records inserted successfully."; } else{ echo "ERROR: Could not execute query: $sql. " . mysqli_error($connection); } } else{ echo "ERROR: Could not prepare query: $sql. " . mysqli_error($connection); } // Close statement mysqli_stmt_close($stmt); // Close connection mysqli_close($connection);
  13. I added a column to my database with SQL as follows: ALTER TABLE comment_table ADD date_col Datetime NOT NULL; I try to insert into the database with the following PHP but nothing gets inserted. if(isset($_POST["submit"])) { $date_col = "test";//this will be DateTime later $name = $_POST["name"]; mysqli_query($connection, "INSERT INTO comment_table (name, date_col) VALUES ('$name', '$date_col')"); } $comsql = "SELECT * FROM comment_table"; $comres = mysqli_query($connection, $comsql); while($comr = mysqli_fetch_assoc($comres)){ ?> <div class="row"> <p>Name: <strong><?php echo $comr['name']; ?></strong>This is the code that is being pointed to as undefined index. <?php echo $comr['date_col']; ?> </p> <?php } ?> </div> The data I'm using for the date_col column is varchar and it should be Datetime but I don't know if that's the reason for the error. I would like to set $date_col equal to a Datetime expression for testing purposes but the formatting I chose wasn't working either.
  14. The code didn't actually make the call to unset the variables until after the not empty conditional was asked. I was misled because var_dump still printed out an empty session array. Once I changed the order of my code, it worked.
  15. EDIT: When I use the following code, it doesn't print out "else test" until after I refresh the page. unset($_SESSION["product"]); if ( !empty($_SESSION["product"]) && ($_SESSION["product"] != NULL)) { echo "if test"; } else { echo "else test"; } EDITED POST TO CHANGE ELSE IF INTO ELSE
  16. EDIT: When I use the following code, it doesn't print out "else if test" until after I refresh the page. unset($_SESSION["product"]); if ( !empty($_SESSION["product"]) && ($_SESSION["product"] != NULL)) { echo "if test"; } else if { echo "else if test"; }
  17. When I use the following code, it doesn't print out "test" until after I refresh the page. unset($_SESSION["product"]); if ( !empty($_SESSION["product"]) && ($_SESSION["product"] != NULL)) { echo "test"; } When I use: var_dump($_SESSION["product"]); It shows: array(0) { } I'm wondering it that's the problem. That it's array(0) when it should be array().
  18. In the html: <div id="simpliest-usage" class="box"></div><br /> <form method="POST" action="http://beta.opentemp.local/user/dashboard" accept-charset="UTF-8" id="submit_mdp"><input name="_token" type="hidden" value="9jlMjOzit5sZBGPkYQfwXwSerdzSnZXYVnEyr9Yx"><input class="mdp_button" type="submit" value="Submit"></form> In the javascript $( "#submit_mdp" ).submit(function( event ) { $.ajax({ url: 'http://beta.opentemp.local/user/dashboard', data: $("form#submit_mdp").serialize(), type: 'POST', 'beforeSend': function(xhr, settings) { console.log( $("form#submit_mdp").serialize() ); console.log('ABOUT TO SEND'); }, 'success': function(result, status_code, xhr) { console.log('SUCCESS!'); }, 'complete': function(xhr, text_status) { console.log('Done.'); }, 'error': function (XMLHttpRequest, textStatus, errorThrown) { alert("Error with ajax Function: "+ textStatus+" "+errorThrown); } }); }); in the php print_r($_POST); The multidatespicker is a modal.
  19. This is a very basic question. I don't know how to get the post data from the jquery multidatespicker. When I submit the page I want to echo back the post variables but I don't see how they're sent with jquery.
  20. I'm trying to get the user/dashboard page to submit as ajax. My console shows ABOUT TO SEND SUCCESS! Done. so that means the jquery post was sent but I keep getting "No ajax" on the user/dashboard page. How do I submit a page as ajax? added to routes.php Route::post('user/dashboard', array('before' => 'suth', 'uses' => 'UserController@calendar')); added to UsersController.php public function calendar(){ print_r($_POST); if (Request::ajax()) { return "yeahhhh"; } return "<br />No ajax"; return View::make('user.dashboard.index'); } added to main.js $.ajax({ url: 'http://beta.opentemp.local/user/dashboard', data: {} + "&_token=" + $("input[name=_token]").val(), type: 'POST', 'beforeSend': function(xhr, settings) { console.log('ABOUT TO SEND'); }, 'success': function(result, status_code, xhr) { console.log('SUCCESS!'); }, 'complete': function(xhr, text_status) { console.log('Done.'); }, 'error': function (XMLHttpRequest, textStatus, errorThrown) { alert("Error with ajax Function: "+ textStatus+" "+errorThrown); } });
  21. I was asked at another forum why I want a sub_tile_id and this is my explanation. I'd like to know if it is even really necessary after all. The jquery at http://myownmealplanner.com/mealplans/add contains the following code (see the view source): for (var i=1;i<100;i++){ $( "#draggable" + i ).draggable(); } Each draggable div uses the primary key of the tiles table to create its own name, for example draggable1, draggable2,..., and on up to draggable100. The tile id (primary key) gives the draggable div its name and I am looping through 100 of these. If I have five users who each have 20 meal tiles then I have already exhausted all of the names available at 100 (5 times 20 being 100). Of course I could just loop over 200 meal tiles instead, keep adding users and keep looping over ever more meal tiles but it seems like a bad idea. Wouldn't the jquery slow down quite a bit if I loop through for example 500 tiles? If instead each user has his own set of meal tiles then I would grab the user id and the sub_tile_id so it would never be more than maybe 10 or 20 to loop through.
  22. This didn't work CREATE TABLE `tiles` ( `sub_tile` ENUM('1','2','3') NOT NULL, `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , `user_id` INT(22) )
  23. I need to create a tiles table that has a structure like this for http://www.myownmealplanner.com: user_id sub_tile_id 1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3 etc. I can't just create new tables for new users because I'm using cakephp and that would require new models, views, and controllers for every new table. How do I get the sub_tile_id to auto_increment starting at every new user id? According to the manual I can't restart auto-increment with a lower value than it has already displayed so this needs to be done with php somehow.
×
×
  • 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.