Jump to content

Barand

Moderators
  • Posts

    24,565
  • Joined

  • Last visited

  • Days Won

    822

Everything posted by Barand

  1. It also gives a better structure to the resulting array, to make those with numbered keys a sub-group Array ( [products] => Array ( [77777] => Array ( [newQuantity] => 3 [newPrice] => 5 [usedQuantity] => 1 [usedPrice] => 3.99 [total] => 18.99 ) [88888] => Array ( [newQuantity] => 0 [newPrice] => 0 [usedQuantity] => 4 [usedPrice] => 12 [total] => 48 ) [44444] => Array ( [newQuantity] => 2 [newPrice] => 4 [usedQuantity] => 0 [usedPrice] => 3.99 [total] => 8 ) ) [date] => July 25 2021 [address] => 123 Anystreet Avenue [address2] => [zipcode] => 90210 [city] => Beverly Hills [state] => CA [planet] => Mars )
  2. PS It would better to change line 6 above to $sku['products'][$id][$k] = $v;
  3. I would ... foreach ($array as $k => $v) { $id = substr($k, -5); if (ctype_digit($id)) { $len = strlen($k); $k = substr($k, 0, $len-5); $sku[$id][$k] = $v; } else $sku[$k] = $v; } echo '<pre>' . print_r($sku, 1) . '</pre>'; giving... Array ( [77777] => Array ( [newQuantity] => 3 [newPrice] => 5 [usedQuantity] => 1 [usedPrice] => 3.99 [total] => 18.99 ) [88888] => Array ( [newQuantity] => 0 [newPrice] => 0 [usedQuantity] => 4 [usedPrice] => 12 [total] => 48 ) [44444] => Array ( [newQuantity] => 2 [newPrice] => 4 [usedQuantity] => 0 [usedPrice] => 3.99 [total] => 8 ) [date] => July 25 2021 [address] => 123 Anystreet Avenue [address2] => [zipcode] => 90210 [city] => Beverly Hills [state] => CA [planet] => Mars )
  4. When you create the original array, instead of $array['newQuantity77777'] = 3; use $array[77777]['newQuantity'] = 3; Job done
  5. No - I always use join. It's less typing.
  6. Here's a shortened version. It also uses json_decode without the "true" to give an object (containing objects) $all = json_decode($all_items_sold); $placeholders = $values = []; foreach ($all as $pid => $prod){ if (in_array($pid, $referred_by_Affiliate)) { $placeholders[] = "(?,?,?)"; array_push($values, $pid, $prod->item, $prod->price); } } $stmt = $db->prepare("INSERT INTO mytable (prod_id, item, price) VALUES " . join(',', $placeholders)); $stmt->execute($values); A PHP join has nothing to do with MySQL joins.
  7. Something like this, maybe (sorry it's more than 2 lines) $all = json_decode($all_items_sold, 1); $matched = array_filter($all, function($k) use ($referred_by_Affiliate) { return in_array($k, $referred_by_Affiliate); }, ARRAY_FILTER_USE_KEY ); $placeholders = $values = []; foreach ($matched as $pid => $pdata) { $placeholders[] = "(?,?,?)"; array_push($values, $pid, $pdata['item'], $pdata['price']); } $stmt = $db->prepare("INSERT INTO mytable (prod_id, item, price) VALUES " . join(',', $placeholders)); $stmt->execute($values);
  8. foreach() is good. Have you considered PDO (an OCI driver is available)? $curl_data = '{ "CONTACT_ID": "1793", "INV_SERIAL_NO": "345", "shipment_data": [ { "SHIP_SERIAL_NO": "11", "MASTER_NO": "11", "HOUSE_NO": "11" }, { "SHIP_SERIAL_NO": "22", "MASTER_NO": "22" }, { "SHIP_SERIAL_NO": "33", "HOUSE_NO": "33" } ] }'; $request = json_decode($curl_data, 1); $base = [ "CONTACT_ID" => $request['CONTACT_ID'], "INV_SERIAL_NO" => $request['INV_SERIAL_NO'], "SHIP_SERIAL_NO" => null, "MASTER_NO" => null, "HOUSE_NO" => null ]; $stmt = $db->prepare("INSERT INTO fl_shipment_data (contact_id, inv_serial_no, ship_serial_no, master_no, house_no) VALUES (:CONTACT_ID, :INV_SERIAL_NO, :SHIP_SERIAL_NO, :MASTER_NO, :HOUSE_NO) "); foreach ($request['shipment_data'] as $ship) { $data = array_merge($base, $ship); $stmt->execute($data); } results fl_shipment_data +----------------+------------+---------------+----------------+-----------+----------+ | fl_shipment_id | contact_id | inv_serial_no | ship_serial_no | master_no | house_no | +----------------+------------+---------------+----------------+-----------+----------+ | 1 | 1793 | 345 | 11 | 11 | 11 | | 2 | 1793 | 345 | 22 | 22 | NULL | | 3 | 1793 | 345 | 33 | NULL | 33 | +----------------+------------+---------------+----------------+-----------+----------+
  9. SELECT m.module_id , name , IFNULL(parent, 0) , CASE WHEN p.module_id IS NULL THEN 0 ELSE 1 END as permission FROM module m LEFT JOIN user_permission p ON m.module_id = p.module_id AND user_id = 1 ORDER BY m.module_id ;
  10. To me, checking the parent box indicates that all the submodules in that group are selected. But I can only suggest and give examples of how. You are completely at liberty to change things to how you want it to work, such as not storing permissons for parent modules. As I have stated before, I am not telepathic.
  11. Then validate the input in the php so only valid data gets to the database. PS... I would have it so that selecting the parent checkbox resulted in all the submodules of that parent being selected (checked) also. If they then uncheck a submodule, unset the parent (as they no longer have all)
  12. Your call to mysqli_query() failed and returned "false". Check mysqli_error to see why.
  13. If user selects only parent module and you set parent permission to 0 (ie remove permission record) how do you then know user has set parent permission?
  14. Yes - just like those sample links in my first reply
  15. Simple example products.php - list products with links containing the product's' id $res = $db->query("SELECT prod_id , description FROM featuredlist; "); $output = ''; foreach ($res as $row) { $output .= <<<HTML <div class='product'> <a href="detail.php?id={$row['prod_id']}"> {$row['description']} </a> </div>\n HTML; } detail.php $prod_id = $_GET['id'] ?? 0; // get product id from the query string if ($prod_id != 0) { $stmt = $db->prepare("SELECT description FROM featuredlist WHERE prod_id = ? "); $stmt->execute([$prod_id]); $row = $stmt->fetch(); // output product details here }
  16. You appear to use $_SESSION['prod_id'] every time. When does that get changed? You should be using the id from the product's link each time. PS USe code tags (<> button in toolbar)
  17. Suppose your product page has several images +------------------+ +------------------+ | | | | | Product A | | Product B | | | | | | | | | | | | | | href = | | href = | | detail.php?id=1 | | detail.php?id=2 | | | | | +------------------+ +------------------+ +------------------+ +------------------+ | | | | | Product C | | Product D | | | | | | | | | | | | | | href = | | href = | | detail.php?id=3 | | detail.php?id=4 | | | | | +------------------+ +------------------+ Apply a link from each image to the detail.php page passing the id of the product. In detail.php, get the id passed in the link query your database tables for the product info using that id display the product details
  18. Here's the single query. If the user has permission to use a module, the permission column = 1 otherwise 0. If the user has permission to use a module's parent module then the user can use all the children of the parent. SELECT m.module_id , name , IFNULL(parent, 0) , CASE WHEN p2.module_id IS NULL THEN CASE WHEN p.module_id IS NULL THEN 0 ELSE 1 END ELSE 1 END as permission FROM module m LEFT JOIN user_permission p ON m.module_id = p.module_id AND p.user_id = 2 LEFT JOIN user_permission p2 ON m.parent = p2.module_id AND p2.user_id = 2 ORDER BY m.module_id ;
  19. I am not a great fan of ENUM, preferring to have choices like your "media type" in a table with an id, and store the id in the data table as a foreign key. Having such values in a table makes it easy to form a list of choices for the user, a task which is a PITA if the values are stored as enum.
  20. I see you are using someone else's posted output image - do you have anything of your own so far for this project, such as code ? What bit are you having a problem with (apart from getting someone to give you free code)?
  21. I'd go for an array instead of separate variables created using $$. $fields = array( 'address' , 'email' , 'phone' , 'city' ); foreach ($fields as $name) { $data[$name] = "whatever"; } then you can subsequently $stmt = $pdo->prepare("INSERT INTO user (address, city, email, phone) VALUES (:address, :city, :email, :phone) "); $stmt->execute($data);
  22. Adding to dropdown... <script type='text/javascript'> $().ready(function() { $("#term").keyup(function() { var term = $(this).val() $.get( "", {"term":term}, function(resp) { $("#city").html("") $.each(resp, function(k,v) { var city = $("<option>", {"value":v.cityid, "text": v.cityname + ', ' + v.provincename + ', ' + v.countryname}) $("#city").append(city) }) }, "JSON" ) }) }) </script> </head> <body> <form> Search<br> <input type='text' name='term' id='term' style='width: 400px;'> <br> <select name='city' id='city' size='10' style='width: 400px;'></select> </form> </body> </html>
  23. BTW, over half your php code was creating unnecessary variables and transferring data from one array to another just to rename the keys. All you needed was $get_city_select = $db->prepare("SELECT c.city_id as cityid , c.city_name as cityname , p.province_name as provincename , co.country_name as countryname FROM city c LEFT JOIN province p ON c.province_id = p.province_id LEFT JOIN country co ON p.country_id = co.country_id WHERE city_name LIKE :param"); $get_city_select->bindParam(':param', $param_term); $get_city_select->execute(); $result_city_select = $get_city_select->fetchAll(PDO::FETCH_ASSOC); exit(json_encode($result_city_select));
  24. No peeping until you have tried again
  25. Having chunked your array of children, loop through the chunks putting the submodules from each chunk into a <td>..</td>. (You might want to pad the child array to 9 first to ensure you always get 3 columns of 3. Tables like their rows to have the same number of cells)
×
×
  • 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.