-
Posts
24,604 -
Joined
-
Last visited
-
Days Won
830
Everything posted by Barand
-
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.
-
Href - use id of href & concatenation inside loop.
Barand replied to JonnyDriller's topic in PHP Coding Help
Cut-down version echo "<tr> <td> <a href='Details/21/index.php?id=$id'>$id</a> </td> </tr>"; -
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); }
-
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
-
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.
-
So long as it's only the id you need to access.
-
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.
-
How are you calling the onclick now? onclick = "myfunction(this)"
-
does menuItemId have a value?
-
Does using quotes help?
-
PS I always quote my variable names EG data: {"ajax" : 'two', "id": menuItemId},
-
Does the developer tool's network tab show the id being sent? Code seems OK.
-
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)
-
Your code was if $prod is in the array loop through the array and output each value endif As your values are 10, 20 and 30 then the output of "102030" is doing exactly what you asked it to do. Why don't you tell us what you need - we aren't psychics.
-
PS Your processing sequence is FUBAR. You should Handle ajax requests - any output in an ajax call is returned in the response, so you need to process and exit asap Handle POST processing - then after updating db reload the page with a location header and exit. Handle GET processing and other processing required for page output. HTML
-
Page loads and the <script> at bottom of page runs, defining the .delButton click handler. User clicks "Manage" button and a delButton is created, but the handler has already been defined When a handler is defined for a class it is attached to existing members of that class. Your delButton does not exist when the page is initially loaded.
-
When does the $('.delButton').click(function () run to define what happens when it is clicked? Is it before the delButton has been created? If the answer to the second is "Yes" then the button object will not receive the click handler.
-
Now you have added an input field to edit the comment you need to add the comment column to the database update. At present it only updates status in your above code..
-
in_array() checks if a value exists in an array. You are looking for a key. if (isset($array1[$prod]) )
-
You cannot just blindly copy/paste without thought about context and what it is doing. Lines 35 to 45 should be <tr> <td>{$row['id']}</td> <td>{$row['details']}</td> <td>{$row['location']}</td> <td>{$row['status']}</td> <td><textarea name='comment' cols='50' rows='5'>{$row['comment']}</textarea></td> <td><button name='status' value='Approved' class='w3-button w3-green'>Approve</button></td> <td><button name='status' value='Rejected' class='w3-button w3-red'>Reject</button></td> </tr>
-
Then add one, for example <form method='post'> <textarea name='comment' cols='50' rows='5'><?=$comment?></textarea> <button name='status' class='w3-button w3-green' value='Approved'>Approve</button> <button name='status' class='w3-button w3-red' value='Rejected'>Reject</button> </form>
-
You should be normalizing your data and put the miles in a separate table +---------------------+ | invoice | +---------------------+ | inv_id (PK) | | order_no (UQ) |-----+ | order_date | | +---------------------+ | +----------------------+ | | state_miles | | +----------------------+ | | state_miles_id (PK) | +-----<| order_no (FK) | | state | | miles | +----------------------+ Are you getting any pdo errors? - I don't see you checking for any (have you set the ERRMODE attribute when connecting?) [edit] PS Why are you getting the last insert id before you have executed the insert? Why do you need the last insert id? Why is there a subsequent update immediately after the insert?
-
Works fine for me mysql> select * from pup; +----+-------+--------+---------------------+ | id | name | amount | transaction_date | +----+-------+--------+---------------------+ | 1 | Curly | 10.00 | 2020-01-01 10:00:00 | | 2 | Larry | 20.00 | 2020-01-15 12:30:00 | | 3 | Mo | 15.00 | 2020-02-01 09:00:00 | | 4 | Peter | -5.00 | 2020-02-01 10:30:00 | | 5 | Paul | 10.00 | 2020-02-02 11:30:00 | | 6 | Mary | 5.00 | 2020-02-02 12:15:00 | +----+-------+--------+---------------------+ 6 rows in set (0.00 sec) mysql> select MAX(id) as maxid FROM pup; +-------+ | maxid | +-------+ | 6 | +-------+ 1 row in set (0.01 sec) or <?php $res = $conn->query("SELECT MAX(id) as maxid FROM pup"); $row = $res->fetch_assoc(); echo $row['maxid']; //--> 6 ?>
-
You have an "undefined variable (out) error where you are concatenating. ajax call "two" is triggered when something with class="delButton" is clicked. I can find no reference to delButton in your html