-
Posts
24,433 -
Joined
-
Last visited
-
Days Won
807
Posts posted by Barand
-
-
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?
-
Don't rely on table position (last four). Add a "dropdown" flag (1 = used in dropdown, 0 = not)
-
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.
- 2
-
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
33 minutes ago, Maihes said:what i meant about the variables for the prices is that they are required anyway for them to be displayed
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] ]
-
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.
-
Cut-down version
echo "<tr> <td> <a href='Details/21/index.php?id=$id'>$id</a> </td> </tr>";
- 1
-
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); }
-
20 minutes ago, Maihes said:
prices are stored in db too but each have an assigned variable
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.
-
3 minutes ago, Adamhumbug said:
I have chnaged the function to just be a function rather than looking for a click. I have put an onclick on the button that is being created.
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.
-
38 minutes ago, Adamhumbug said:
The del button is coming from the modal that was created by the first function.
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>
Href - use id of href & concatenation inside loop.
in PHP Coding Help
Posted · Edited by Barand
HTML knows nothing about php variables. If it's in the HTML section (as this is) it needs embedding inside php tags
Either
or use short tags