Jump to content

nodirtyrockstar

Members
  • Posts

    108
  • Joined

  • Last visited

Everything posted by nodirtyrockstar

  1. Each reference is new! I see it now. I will try to mess around with it some more now that I finally understand what you are doing. My server is down at the moment.
  2. Maybe it will help to take a step backt. I noticed that you are not declaring the initial array with references, and that you are instead only using the ampersand here: Can you explain the difference?
  3. I understand what is happening with my code in theory. The reference variables are set to point to the most recent result set. What I don't understand is why your solution works. Why does &$data[$i][$col] get around duplicating the reference and actually assign just a value?
  4. Okay, not fully understanding it, I did my best to implement your solution. Now it is not copying my array keys. I would really benefit from an explanation of each step you take to copy the value without carrying over the reference so that I can make it work in my own script.
  5. It is my understanding that assigning a variable with a referenced variable actually copies the reference.
  6. Does my algorithm use the deprecated call time pass by reference? I was reading about that, too. I am also looking at the ReflectionClass in order to pass a dynamic array to bind_params.
  7. Thank you for your response. I feel like the issue with my code is how I am creating the $results array. I see that you did it differently, but I was wondering if you could explain how some of your changes work. More specifically, I am having a bit of trouble with understanding passing as reference. How can I assign a value to a variable from another variable that is passed as reference, without having the assigned variable change when the referenced variable changes?
  8. I keep researching this online, and could still use some help. Ultimately what I need to learn is how to pass a reference variable as a value. In other words, $results is passed by reference. I need to assign the values of the $results array to another array, and not have those values reference $results, just the values of $results at the time of the assignment.
  9. No that didn't seem to work. I have updated the code a bit. Here's the newest version: function prodArr() { if (!empty($_POST['cart'])) { global $dbType; $prodDetail = Array(); //connect to database $mysqli = Database::getInstance(); //Retrieve product data if ($dbType === 'distro') { $results = array('img' => &$img, 'artist' => &$artist, 'title' => &$title, 'label' => &$label, 'year' => &$year, 'price' => &$price, 'qty' => &$qty); $tbl = 'products'; } elseif ($dbType === 'releases') { $results = array('img' => &$img, 'artist' => &$artist, 'title' => &$title, 'year' => &$year, 'price' => &$price, 'qty' => &$qty); $tbl = 'products'; } elseif ($dbType === 'merch') { $results = array('img' => &$img, 'title' => &$title,'size' => &$size, 'color' => &$color, 'sex' => &$sex, 'price' => &$price, 'qty' => &$qty); $tbl = 'merch'; } $query = 'SELECT '; $query .= '`'.implode('`, `', array_keys($results)).'`'; $query .= ' FROM ' . $tbl . ' WHERE (`id` = ?) AND (`qty` > 0) AND (`agedOff` <> 1);'; foreach ($_POST['cart'] as $key => $reqQty) { if (!$stmt = $mysqli->prepare($query)) { echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error; } if (!$stmt->bind_param('s', $key)) { echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error; } if (!$stmt->execute()) { echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error; } if (!call_user_func_array(array($stmt, 'bind_result'), $results)) { echo "Binding results failed: (" . $stmt->errno . ") " . $stmt->error; } while($stmt->fetch()){ $tmp = array(); // Attempt to store $results into temporary array $tmp = $results; $prodDetail[$key] = $tmp; } //If requested quantity is greater than inventory, put total qty available into cart if($reqQty > $qty) { echo "You requested $element copies of $title, and we only have $qty in stock. We put our remaining stock into your cart.<br /><br />"; $prodDetail[$key]['reqQty'] = $qty; } else { $prodDetail[$key]['reqQty'] = $reqQty; } //$prodDetail[$key] = array_merge($results,$prodDetail[$key]); printArray($prodDetail); $stmt->free_result(); } $stmt->close(); Any thoughts as to how I can capture each new iteration of the $results array without changing all of my variables would be a big help!
  10. Do you think the solution is to store each new iteration of $results in a new array, so therefore it won't matter that $results changes?
  11. If you visit the page I am working on here, and try to put ten of each product into your basket, it will print the $prodDetail array through each iteration of the foreach loop and you can see the progression...
  12. I have a cart function that is supposed to use the post array (with product numbers as indices and the associated value is the customer's requested quantity) and populate product information in the session by accessing the database. I would like this function to add a $key and $value pair to $prodDetail for each item in the post array. Instead, it is replacing all the $value entries with the most recently selected product data. I do not see why it is resetting all of the $value fields upon each new iteration. Please let me know if this is still unclear after you have read the following. Here is the function that I believe is the culprit: function prodArr() { if (!empty($_POST['cart'])) { global $dbType; $prodDetail = Array(); //connect to database $mysqli = Database::getInstance(); //Retrieve product data if ($dbType === 'distro') { $results = array('img' => &$img, 'artist' => &$artist, 'title' => &$title, 'label' => &$label, 'year' => &$year, 'price' => &$price, 'qty' => &$qty); $tbl = 'products'; } elseif ($dbType === 'releases') { $results = array('img' => &$img, 'artist' => &$artist, 'title' => &$title, 'year' => &$year, 'price' => &$price, 'qty' => &$qty); $tbl = 'products'; } elseif ($dbType === 'merch') { $results = array('img' => &$img, 'title' => &$title,'size' => &$size, 'color' => &$color, 'sex' => &$sex, 'price' => &$price, 'qty' => &$qty); $tbl = 'merch'; } $query = 'SELECT '; $query .= '`'.implode('`, `', array_keys($results)).'`'; $query .= ' FROM ' . $tbl . ' WHERE (`id` = ?) AND (`qty` > 0) AND (`agedOff` <> 1);'; foreach ($_POST['cart'] as $elKey => $element) { if (!$stmt = $mysqli->prepare($query)) { echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error; } if (!$stmt->bind_param('s', $elKey)) { echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error; } if (!$stmt->execute()) { echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error; } if (!call_user_func_array(array($stmt, 'bind_result'), $results)) { echo "Binding results failed: (" . $stmt->errno . ") " . $stmt->error; } if (!$stmt->fetch()){ echo "Fetching results failed: (" . $stmt->errno . ") " . $stmt->error; } $prodDetail[$elKey] = $results; printArray($prodDetail); I do not show the entire function, because I believe this is where the problem lies. Through dumping and echoing variables, I show that the $results array changes through each iteration of the foreach loop, but unfortunately it is setting all of the child arrays in $prodDetail to match the current $results array. All I want to do is add each new result set to the array. Maybe I have been staring at this for too long, but I can't seem to see the problem. Can someone please point how to fix this?
  13. If it were my site, I would add the tooltip text in a new field to the database. Then when you are fetching from the DB, retrieve that and bind it as an additional result. while($stmt->fetch()){ $format = "<img src='%s' title='%s' />" // where %s is a placeholder for a variable string printf($format, $imgSrc, $imgTooltip) // $format is the intended format, the following two parameters are the strings that you will place into the image placeholders during each iteration of results. }
  14. I actually reexamined the architecture of the database. Essentially I had two fields that were not unique, but what would be unique is the combination of the two fields. I created another field which is a combination of the session ID and product ID. This creates a singular unique key and allows me to use the ON DUPLICATE KEY UPDATE command.
  15. This is a PHP forum. You will probably have better luck in the MYSQL thread.
  16. I think you want to use the 'ON DUPLICATE KEY UPDATE' clause. This is an add-on to the INSERT command which will update rows with matching primary keys, and insert when there is no match. (You will probably get a better response in the MYSQL thread.)
  17. It would appear that your question belongs in an HTML forum.
  18. Thanks everyone! I never used implode before, and this really helped to improve my code.
  19. Doh!! Thank you! I thought for sure I got the call_user_func_array part wrong.
  20. Here is my mysqli query: SELECT `artwork`,`artist`,`title`,`label`,`year`,`price`,`id` FROM `products` WHERE (`qty` <> 0) AND (`agedOff` <> 1); I know this query works. I typed it into the mysqladmin window and it returned the desired results. Here is the variable setup & database query: $artwork = ''; $artist = ''; $title = ''; $label = ''; $year = 0; $price = 0.00; $id = ''; $vars = array(&$artwork, &$artist, &$title, &$label, &$year, &$price, &$id); //I placed the ampersands before the array values, because bind_results requires that you pass as reference if (!$stmt = $mysqli->prepare($query)) { echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error; } if (!$stmt->execute()) { echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error; } if (!call_user_func_array(array($stmt, 'bind_result'), $vars)) { echo "Binding results failed: (" . $stmt->errno . "( " . $stmt->error; } if($stmt->fetch) { //this part does not execute } The error message I receive is: "Notice: Undefined property: mysqli_stmt::$fetch in /hermes/waloraweb073/b2264/moo.katc/cobra/lib/distro.php on line 75 Fetching results failed: (0)" Can someone point me in the right direction please?
  21. First, Jessica is right. Second, if a user selects a checkbox field, and then submits it to PHP, it will be set to 'on' in the post array. You don't have to create an additional variable to reflect that it has been set, because PHP provides it for you. Thus, here is an example using your code: [code<input type="submit" name="submit[]" value="Update"/>[/code] Note the square brackets after "submit" in the name field. This is what will give you an array of checkbox values. Then you can reference their values like so: if (!empty($_POST['submit'])) //though I would recommend changing this variable name, I have left it as you originally marked it up { foreach($_POST['submit'] as $key => $value) { if ($_POST['submit'][$key] === 'on') { Now you can set new variables based on what you find within your post array. } } }
×
×
  • 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.