Jump to content

Barand

Moderators
  • Posts

    24,566
  • Joined

  • Last visited

  • Days Won

    822

Everything posted by Barand

  1. I rarely use sets so I keep forgetting about the FIND_IN_SET() function
  2. If there are are 10 values in your array then you need 10 placeholders i.e. NOT IN (?,?,?,?,?,?,?,?,?,?) then $stmt->execute( $yourarray );
  3. That line clears the data each time through the loop. Put it before the foreach() loop.
  4. Here's an example // // set default values // $senderid = $_GET['sender'] ?? '0'; $recipid = $_GET['recipient'] ?? '0'; $page = $_GET['page'] ?? 1; if (isset($_GET['btnSub'])) $page = 1; // new search was requested $total_recs = 0; $search_results = ''; if (isset($_GET['page'])) { // was form submitted? // // build query WHERE clause // $whereclause = ''; $where = []; $params = []; if ($senderid != 0) { $where[] = "(sender_id = ?)"; $params[] = $senderid; } if ($recipid != 0) { $where[] = "(recipient_id = ?)"; $params[] = $recipid; } if ($where) { $whereclause = "WHERE " . join(' AND ', $where); } // get total records from the search $count = $db->prepare("SELECT COUNT(*) FROM notes n INNER JOIN user s ON n.sender_id = s.user_id INNER JOIN user r ON n.recipient_id = r.user_id $whereclause"); $count->execute($params); $total_recs = $count->fetchColumn(); // now get the search results if (intval($page) == 0) $page = 1; $offset = ($page - 1) * PERPAGE; $limit = PERPAGE; $stmt = $db->prepare("SELECT n.id , s.username as sender , r.username as recipient , n.message , date_format(time_sent, '%a %b %D %l:%i %p') as sent FROM notes n INNER JOIN user s ON n.sender_id = s.user_id INNER JOIN user r ON n.recipient_id = r.user_id $whereclause ORDER BY time_sent LIMIT $offset, $limit "); $stmt->execute($params); foreach ($stmt as $rec) { $search_results .= " <div class='result'> <div class='fromto'> <div class='label'>From:</div>". esc($rec['sender']) . "<br> <div class='label'>To:</div>". esc($rec['recipient']) . "<br><br> <div class='label'>Sent:</div>". esc($rec['sent']) . "<br> </div> <div class='msg'>". esc($rec['message']) . "</div> <div style='clear:both'></div> </div>\n"; } }
  5. NOTE TO READERS: The above statement applies only to those using PDO. If you are using mysqli then prepared statements are a completely different ballpark to those using query()
  6. Have you checked the error log?
  7. Yes, but what have you tried so far?
  8. HTML knows nothing about php variables. If it's in the HTML section (as this is) it needs embedding inside php tags Either <a href='test.php?id=<?php echo $uri; ?>'>link to page2</a> or use short tags <a href='test.php?id=<?=$uri?>'>link to page2</a>
  9. In the first post the href was in the middle of a double-quoted php string I.E. echo "<tr> <td> <a href='Details/21/index.php?id=$id'>$id</a> </td> </tr>"; We have no context for this latest post - is it in the HTML portion or PHP portion of your code?
  10. Don't rely on table position (last four). Add a "dropdown" flag (1 = used in dropdown, 0 = not)
  11. I've just looked at the data in your sql dump files. Did I say the design was terrible? I apologise for the gross understatement and for referring to it as a database As I hope I demonstrated in my earlier posts, if you get the data right the processing becomes a whole lot easier. Nothing in the other tables matches those column names in your prices table, so all of my solutions go out of the window. There might have been some hope if the first column in prices mapped to id #1 and the 2nd column to id #2 but even that isn't the case. I'll check if dropping "Price" suffix from the column names can save the day. I'll have a look at your code now, but first I need a stiff drink, or several - a large gin and tonic should help.
  12. The solution should work for those products in the product table whose name matches a column name in the price table. Basically you just need +----+-------+-------+ | id | name | price | +----+-------+-------+ | 1 | prod1 | 10.00 | | 2 | prod2 | 20.00 | | 3 | prod3 | 30.00 | +----+-------+-------+ You can Use the id/name columns to get your product dropdown. Use id/price columns to create your prices array. I don't know what SS Req, MS Req etc are, or how they relate to products, so I can't comment on those No, they are not required. The array elements are your variables If you want to display the price for product 2 then you don't need $ESSPrice, just echo $prices[ $prods[2] ]
  13. Look at the colours in your posted code. It has all gone green. This is indicating it is all a single string value because a closing quote is missing somewhere.
  14. Cut-down version echo "<tr> <td> <a href='Details/21/index.php?id=$id'>$id</a> </td> </tr>";
  15. Even with your terrible (polite description) table design it can still be done relatively easily without all those variables I set up a couple of tables to replicate your DB TABLE: product TABLE: price +----+-------+ +----+-------+-------+-------+ | id | name | | id | prod1 | prod2 | prod3 | +----+-------+ +----+-------+-------+-------+ | 1 | prod1 | | 1 | 10.00 | 20.00 | 30.00 | | 2 | prod2 | +----+-------+-------+-------+ | 3 | prod3 | +----+-------+ code const HOST = 'localhost'; const USERNAME = '????'; const PASSWORD = '????'; const DATABASE = '????'; function pdoConnect($dbname=DATABASE) { $db = new PDO("mysql:host=".HOST.";dbname=$dbname;charset=utf8",USERNAME,PASSWORD); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); return $db; } $db = pdoConnect(); // create prices array with prod names as the keys $res = $db->query("SELECT * FROM price"); $prices = $res->fetch(PDO::FETCH_ASSOC); // create an array of prod names (key = prod id) $res = $db->query("SELECT id, name FROM product"); foreach ($res as $r) { $prods[ $r['id'] ] = $r['name']; } $selected_id = 2; $price = $prices[ $prods[$selected_id] ]; echo $price; // 20.00 * * * In future, all you would need to do is add a "price" column to your product table and put the prices in. This will do it for you (use same connection code first) <?php // CONNECTION CODE HERE // $db->exec("ALTER TABLE `product` ADD COLUMN `price` DECIMAL(10,2) NULL AFTER `name`"); $res = $db->query("SELECT * FROM price"); $prices = $res->fetch(PDO::FETCH_ASSOC); $stmt = $db->prepare("UPDATE product SET price = ? WHERE name = ? "); foreach ($prices as $name => $price) { $stmt->execute( [ $price, $name ] ); } // JOB DONE - check the results... $res = $db->query("SELECT id , name , price FROM product "); echo '<pre>'; foreach ($res as $r) { vprintf("%3d | %-20s | %10.2f\n", $r); }
  16. That is not what you should be doing. You should either create a prices array direct from the DB where the keys are the product ids and values are the prices, or When user selects product id, query the DB to get the price for the product If your data looks like +---------------+----------+ | product_id | price | +---------------+----------+ | 1 | 10.00 | | 2 | 12.50 | | 3 | 10.00 | +---------------+----------+ get the data and store in aray $res = $db->query("SELECT product_id, price FROM product"); foreach ($res as $rec) { $prices[ $rec['product_id'] ] = $rec['price']; } then the array will be $prices = [ 1 => 10.00, 2 => 12.50, 3 => 10.00 ]; and to get the price $selected_id = 2; $price = $prices[$selected_id]; //--> 12.50
  17. We still don't know what you are trying to do. All you've told us is how you are attempting to do something. Where are the prices stored Why do need to hard code them into an array What does the dropdown look like - you mentioned selecting a product. (Does that give you the 1, 2, or 3 or the price) Are you trying to find the price for a product or trying to find all products that have a specific price. Give us a clue and we can help.
  18. So long as it's only the id you need to access.
  19. Array keys need to be unique. Build your array the other way round $array = [ 1 => $TSPrice, 2 => $ESSPrice, etc ]; the it doesn't matter if two prices are the same.
  20. How are you calling the onclick now? onclick = "myfunction(this)"
  21. does menuItemId have a value?
  22. Does using quotes help?
  23. PS I always quote my variable names EG data: {"ajax" : 'two', "id": menuItemId},
  24. Does the developer tool's network tab show the id being sent? Code seems OK.
  25. That's the easiest way. The other way is to put the code defining the handler inside the ajax call's response handling function (which can get a bit messy)
×
×
  • 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.