Jump to content

javanoob

Members
  • Posts

    31
  • Joined

  • Last visited

Everything posted by javanoob

  1. Ok. I'll make note of that and make the changes. Thank you!
  2. Thanks. I'll replace all instances of ( $row['gm'] ) with ( $row['gm'] ?? 'X' ). Em$ and Gm$ are the game's currency. Em$ is the premium currency, it can be bought with real money. Gm$ is awarded for completing missions and harvesting hunted animals. I assume it might stand for Evergreen Money and Game Money but I don't know for sure. The knowledge base for the game doesn't define them if they are acronyms. Evergreen Hunting Reserve is the name of the game's world.
  3. Thanks! I'll give that a try. Learning from that SQL link is on my to do list. Some items can only be bought with EM. For those items I have an X appearing in the gm field. The X messes up the totals calculation. How can that be prevented? let X = 0;?
  4. Doesn't that require indexes so it knows where to look? Or is that what the rfl.id = rs.rifle_id in the sql is doing? There's only a few different prices for ammo. That's what I intended the a_em and a_gm columns in the database and the number input like this is for under Ammo Qty on the front-end table. I already have the columns filled in. It's not difficult to delete the whole column (it's excel/calc) if it's better to do as you suggest.
  5. Thanks! I was confident you'd know of a way to do that. I think I found a script that'll populate the 'scope' menus based on what rifle's selected. I want to get the database completed with all the items in it before I give it a try.
  6. ยท Scopes for the weapons. Each weapon doesn't mount the same scope. Some weapons have 4 available scopes they can mount, some have 2, some have 8 and they're not all the same scope sets for every weapon. There are 42 different optics in the game including binoculars, spotting scopes, and range finding binoculars. Vice versa for the scopes them selves. Same thing though. This weapon can mount 5 different scopes. This one can only mount 2 different scopes that are available. Click on one of the scopes on either of those pages then click on this scope to see the weapons it can be mounted to. It's not all perfectly the same. It would be a nice to have a feature that a weapon populated a selection menu of the scopes that fit. I was considering giving every item that has an accessory an accessory id (acs_id) and making a different table for that: I'm working on putting that data together. I'm thinking if I make a table of sets with scope_set_id's and a table of individual scopes with a scope_id's to handle that it should work out. Suggestions welcome and appreciated.
  7. That's what I was missing. It works now. Thank you! Is it possible to design the layout like this with how the code is now?: I'd like to fill the page if that's possible with how the code is now. I'll try making separate php files and use <? include 'file_name.php' ?> to do that. If I can't be done with the code as it is that's fine. It works. I'm so very extremely grateful for the help! Thank you so very much, Barand!!! It does need to calculate totals and balance from user input. That and a [ - ] button to remove rows that are added are my next attempt at trying to write javaScript.
  8. How do I define pdoConnect()? I didn't do that. This is my connect.php file (local WAMP): <?php $servername = "localhost"; $dbport = '3306'; $dbuser = 'root'; $ddbname = 'barands_code'; $bpass = ''; try { $pdo = new PDO("mysql:host=$servername;dbname=$dbname;charset=utf8;'", $dbuser, $dbpass, array(PDO::ATTR_EMULATE_PREPARES => false,PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); } catch(PDOException $e) { echo "Connection failed: " . $e->getMessage(); } ?> That's all of it. There's nothing else in it. I added these 10 lines to the file. That's what pushed 67 to 77. $servername = "localhost"; $dbport = '3306'; $dbuser = 'root'; $ddbname = 'barands_code'; $bpass = ''; catch(PDOException $e) { echo "Connection failed: " . $e->getMessage(); } The line 77 error went away when I started using the connect.php file. The only line being reported as erroring now is line 3 which is the $pdoConnect('db_name'); line. As sure as I can be. Every time I tried doing this differently I made a new folder. I compared the code I have to the code you posted here in this thread side by side line for line. I'll run them in WinMerge if my connect.php isn't missing something it needs.
  9. The only thing I can think of is I have the $pdoConnect('code'); wrong. <?php require 'connect.php'; $pdo = pdoConnect('barands_code'); I didn't change anything else. 'barands_code' is the name of the database. The error is on line 3 now. So...I have that wrong. What am I supposed to have $pdoConnect('HERE');?
  10. Here's line 1 to the end of </script> : <?php require 'connect.php'; // contains db credentials and connection function code $pdo = pdoConnect('barands_code'); // YOU WILL NEED YOUR OWN PDO CONNECT CODE // YOU WILL NEED YOUR OWN PDO CONNECT CODE ################################################################################ ## ## ## Handle AJAX request when + button is clicked ## ## ## ################################################################################ if (isset($_GET['ajax'])) { if ($_GET['ajax'] == 'newrow') { exit( tableItemRow($pdo, $_GET['catid']) ); } } ################################################################################ ## ## ## Query db item table to get all the categrories for the output table ## ## ## ################################################################################ $res = $pdo->query("SELECT id , itemname , em , gm , cat_id , cat_name FROM item i JOIN category c USING (cat_id) WHERE cat_id IN (1,2,3,4) ORDER BY cat_id, itemname "); $data = []; foreach ($res as $r) { if (!isset($data[$r['cat_id']])) { $data[$r['cat_id']] = [ 'catname' => $r['cat_name'], 'items' => [] ]; } $data[$r['cat_id']]['items'][] = array_slice($r, 0, 4); } ################################################################################ ## ## ## Loop through the data array to build the output table ## ## ## ################################################################################ $tdata = ''; foreach ($data as $cid => $cdata) { $tdata .= "<tbody data-cat='$cid'> <tr> <td class='cat-title' colspan='4'> {$cdata['catname']} <div class='addmore' data-cat='$cid'><i class='fa fa-plus'></i></div> </td> </tr> <tr> <td><select class='item-menu' name='item[]' onchange='item_menu_changed(this)'> <option value='' data-em='0' data-gm='0'>Choose from {$cdata['catname']}</option> "; foreach ($cdata['items'] as $row) { $tdata .= "<option value={$row['id']} data-em='{$row['em']}' data-gm='{$row['gm']}'>" . htmlspecialchars( $row["itemname"] ) . "</option>"; } $tdata .= "</select></td> <td class='em ca'>0</td> <td class='gm ca'>0</td> </tr> </tbody>"; } /******************************************************************************* * generate html for additional output row * * @param PDO $db * @param int $cat_id */ function tableItemRow(PDO $db, $cat_id) { $res = $db->prepare("SELECT id , itemname , em , gm , cat_name FROM item i JOIN category c USING (cat_id) WHERE cat_id = ? ORDER BY itemname "); $res->execute([$cat_id]); $opts = ''; $data = $res->fetchAll(); foreach ($data as $row) { $opts .= "<option value={$row['id']} data-em='{$row['em']}' data-gm='{$row['gm']}'>" . htmlspecialchars( $row["itemname"] ) . "</option>"; } $rowdata = "<tr> <td><select class='item-menu' name='item[]' onchange='item_menu_changed(this)'> <option value='' data-em='0' data-gm='0'>Choose from {$data[0]['cat_name']}</option>" . $opts . "</select></td> <td class='em ca'>0</td> <td class='gm ca'>0</td> </tr>"; return $rowdata; } ?> <!--#ffa500--> <!DOCTYPE html> <html lang='en'> <head> <title>sample</title> <meta http-equiv='Content-Type' content='text/html; charset=utf-8'> <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script type='text/javascript'> $().ready( function() { $(".addmore").click( function() { let cid = $(this).data("cat") $.get ( "", {"ajax":"newrow", "catid":cid}, function(resp) { $("tbody[data-cat="+cid+"]").append(resp) }, "TEXT" ) }) }) function item_menu_changed(menu) { // // update values in current row // let therow = $(menu).parent().parent() let em = parseInt($(menu).find("option:selected").data("em")) let gm = parseInt($(menu).find("option:selected").data("gm")) $(therow).find(".em").html(em) $(therow).find(".gm").html(gm) // // calculate totals // let totem = 0 let totgm = 0 $(".em").each(function(k,v) { totem += parseInt($(v).html()) }) $(".gm").each(function(k,v) { totgm += parseInt($(v).html()) }) $("#totem").html(totem) $("#totgm").html(totgm) } </script> <style type='text/css'> I did one of these when I realized I should be editing line 2 instead of removing it. I'm not positive what I should be entering as the $pdoConnect('code'); That may be all I have whoopsa-futzed now? When I reload it with what I think it should be Opera GX and Chrome say: With $pdo = pdoConnect('barands_code'); included FireFox loads a blank, white, page. Local WAMP says: Line 17 is just a } that matches up with and open {. Everything's all the same everywhere I'm trying to run it. This is the local WAMP's db the code I posted is from:
  11. The line numbers are moved forward because I added my db credentials directly to the file. https://nwdb.42web.io/barand_rocks/ (it's a free host, might have security certificate issues until it verifies.) Here's a video showing what I did to run it.
  12. I get this when I try to run Barand's code: Notice: Undefined index: em in index.php on line 77 Notice: Undefined index: gm in index.php on line 77 The error above repeats a bunch of times. The table displays on the page under the errors and the dropdown menu's populate but, of course since it can't find em and gm it doesn't do it's thing to the input fields. The em and gm columns and fields are there in the database. It finds everything else, the database is connected, it finds 'itemname' but not other fields in that same table. Line 77 is: $tdata .= "<option value={$row['id']} data-em='{$row['em']}' data-gm='{$row['gm']}'>" . htmlspecialchars( $row["itemname"] ) . "</option>"; The weird thing is if I press the add row button it adds a row and menu (it's slow to do it like it's struggling for some reason but works). The menu it adds populates the input fields correctly. How in the world is a row it's adding working but the row that displays by it self doesn't???? I tried sorting it out but I'm swinging blind.
  13. Yes. It is. Thank you! I tried so hard to get mac_gyver's suggestion to work, I'm sure it does work, but I wasn't able to get it working with a database no matter what I tried. I don't understand what the categories thing that keeps being mentioned is. I very much appreciate all the help and input, from everyone that attempts helping! To shorten my code I've resorted to making separate files out of each section: table_header.php <html> <head></head> <body> <table><tr><td> <table border="1" border-color="orange" id="items_table"><tr> <thead> <tr><th colspan="3" class="category_top">Items</th></tr> <th>Categories</th> <th> em</th><th>gm</th><th style="border:none;"></th> </thead> rifles.php <!-- Rifles --> <div> <tr><td colspan="3" class="categoryth"><span class="text">Rifles</span></td><td><button type="button">+</button></td></tr> </tr><tr> <td><select id="rmenu" name="selection"> <?php $data = $pdo->query("SELECT id, rifleName, em, gm FROM rifles ORDER BY id"); foreach ($data as $row) { echo "<option value={$row['id']} data-em='{$row['em']}' data-gm='{$row['gm']}'>" . $row["rifleName"] . "</option>"; } ?> </select></td> <td><!-- Rifle EM--><input class="cal_em" type="text" id="rem" value="0" disabled size="5"/></td> <td><!-- Rifle GM--><input type+"text" id="rgm" value="0" disabled size="5"/></td> </tr> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script type='text/javascript'> $().ready(function() { $("#rmenu").change( function() { let em = $(this).find("option:selected").data("em") let gm = $(this).find("option:selected").data("gm") $("#rem").val(em) $("#rgm").val(gm) }) }) </script> <!-- End Rifles --> </div> shotguns.php <div> <tr><td colspan="3" class="categoryth">Shotguns</td><td><button type="button">+</button></td></tr> </tr> <tr><td> <select id="smenu" name="selection"> <?php $shotguns = $pdo->query("SELECT id, shotgunName, em, gm FROM shotguns ORDER BY id"); foreach ($shotguns as $row) { echo "<option value={$row['id']} data-em='{$row['em']}' data-gm='{$row['gm']}'>" . $row["shotgunName"] . "</option>"; } ?> </select> </td> <td><!--Shotgun EM--><input class="cal_em" type="text" id="sem" value="0" disabled size="5"/></td> <td><!--Shotgun GM--><input type="tect" id="sgm" value="0" disabled size="5"/></td> </tr> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script type='text/javascript'> $().ready(function() { $("#smenu").change( function() { let em = $(this).find("option:selected").data("em") let gm = $(this).find("option:selected").data("gm") $("#sem").val(em) $("#sgm").val(gm) }) }) </script> </div> </tr> and so on.....I'm using includes to put it all together (example....this isn't the actual file it was one I grabbed quick to example): index.php <?php include ('secFol/c.php'); ?> <html> <head> <link rel="stylesheet" href="style/page.css"></link> <link rel="stylesheet" href="style/items.css"></link> </head> <body> <table border="0" width="100%"> <tr><td><?php include ('pages/rifles.php');?></td> <tr><td><?php include ('pages/bows.php');?></td> <tr><td><?php include ('pages/shotguns.php');?></td> <tr><td><?php include ('pages/optics.php');?></td></tr> </table> </body> </html> I understand and I'm thinking to myself doing it this way is only making it more difficult but my OCD gets upset if my code isn't nice, neat, organized, and as few lines as possible. I get bothered by having to scroll through a 90 mile long file of code that's the same thing (slightly different) over and over again. Awesome!! You got the totals working! Thank you! That was my next task I was probably going to have a hard time figuring out that I'll be asking about. The plus buttons I intend to duplicate each div so users can add an additional item of that type, because players often do that - buy more than one thing of each item type. That's another thing I'll be figuring out. I have a script that duplicates the div but...getting the totals to work with that, I assume, is going to be a difficult task for me to figure out on my own. I'll copy what you did and use it! Thank you so very much! And now I finally see what you and mac_gyver mean: item +----+---------------------+------+------+--------+ | id | itemname | em | gm | cat_id | +----+---------------------+------+------+--------+ | 1 | .308 Bolt action | 225 | 1350 | 1 | | 2 | 7mm magnum | 300 | 1575 | 1 | | 3 | .243 LeverAction | 215 | 8725 | 1 | | 4 | 6ft Longbow | 135 | 1120 | 2 | | 5 | 5ft Flatbow | 235 | 1345 | 2 | | 6 | Recurve | 215 | 2525 | 2 | | 7 | Purdy 12 bore | 135 | 1120 | 3 | | 8 | Beretta 12 bore | 235 | 1345 | 3 | | 9 | Small bore rook gun | 215 | 2525 | 3 | | 10 | Glock 9mm | 125 | 1050 | 4 | | 11 | Colt .38 | 200 | 1175 | 4 | | 12 | Derringer .22 | 115 | 3125 | 4 | +----+---------------------+------+------+--------+ category +--------+----------+ | cat_id | cat_name | +--------+----------+ | 1 | Rifles | | 2 | Bows | | 3 | Shotguns | | 4 | Pistols | +--------+----------+ I knew whatever you and mac meant was a better way of doing it. I wasn't translating what, exactly, you mean. This explains it! I can and will do that. I don't mind doing any amount of work if it results in things working better, and less code. I'll fix my .ods file and make a new database structure so it's like that. Thanks!
  14. I had my first experience with php in 1997. I consider myself an 'oldie' because I'm proud to share I was a member of the teams behind php-nuke CMS packages. I didn't do much coding, I don't and never did know much about the code. I did front-end, graphics, themes, created a module or two. I understood a lot about how those packages worked and was able to help people install, use, and modify their site. I was on the tech support teams on futurenuke, nukeplanet, and others. It was rewarding being able to help people. I'm curious. This domain has been around since somewhere around 1995-ish? It's always been a php reference and help site as far as I know. Is it the same group of people as it was in the late 90s, early 2000s?
  15. Result: Fatal error: Uncaught PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'ehr_bt.category' doesn't exist in C:\Program Files (x86)\EasyPHP-Devserver-17\eds-www\ehr_tracker\try13\mac_gyver1.php:26 Stack trace: #0 C:\Program Files (x86)\EasyPHP-Devserver-17\eds-www\ehr_tracker\try13\mac_gyver1.php(26): PDO->query('SELECT c.name, ...') #1 {main} thrown in C:\Program Files (x86)\EasyPHP-Devserver-17\eds-www\ehr_tracker\try13\mac_gyver1.php on line 26 I got Barand's suggestion to work. It just worked. I'll stick with using that. Thank you so much for the help!
  16. I can't get the SQL/PHP code to run. I'm not sure how to use that php/sql code. I doubt it's the code, it's how I'm trying to use it. Unexpected character. (near "?" at position 1) Unexpected character. (near "$" at position 54) Unexpected character. (near "[" at position 64) Unexpected character. (near "]" at position 65) Unexpected beginning of statement. (near "?" at position 1) Unexpected beginning of statement. (near "php" at position 2) Unrecognized statement type. (near "get" at position 12) Is this all I have to do / Is this what you mean?: And the script mac_gyver posted will work?: $(document).ready(function() { $(".populate_em_gm").change( function() { let em = $(this).find("option:selected").data("em") let gm = $(this).find("option:selected").data("gm") $(this).closest('tr').find('.em').val(em) $(this).closest('tr').find('.gm').val(gm) }) }) I'll attempt that on a new database.
  17. Yes. Every character. Multiple times. I tend to write lengthy threads. It's only fair I read every character other people write and share. I don't understand what's you mean by "categories". My database already has categories. That's all it is is categories. I based the design of my database off of phpbb3's newest forum package and WordPress's database. The only difference in those and mine is mine doesn't have a prefix to each table. Please try to explain what you mean. A roadmap image, a screenshot of one like you mean? I shared my database file. It can easily be downloaded and edited to show me what you mean. Change one of the sheets to show me what you mean and I'll do the rest? Did you actually look at my database? There's more on it than just an id and a quantity. I believe you it's different. I don't have a database with info on it besides the one I made to look at one, that has info on it. Share how it's different please. I don't see a difference between mine and any other I have access to to look at. I don't have a clue what all that is or what it's doing. I don't see much difference in yours, mac_gyver: $(document).ready(function() { $(".populate_em_gm").change( function() { let em = $(this).find("option:selected").data("em") let gm = $(this).find("option:selected").data("gm") $(this).closest('tr').find('.em').val(em) $(this).closest('tr').find('.gm').val(gm) }) }) and the one Barand posted: $().ready(function() { $("#menu").change( function() { let em = $(this).find("option:selected").data("em") let gm = $(this).find("option:selected").data("gm") $("#em").val(em) $("#gm").val(gm) }) }) I haven't tried running the mac_gyver script in my project's index.php file. I'll sigh and swat at myself for you if it just works.
  18. I have to repeat that question because holy boat load of re-doing a boat load of work I already did collecting and organizing the data. That process took a few years to do. The database is already categories. Doing it the way you suggest, mac_gyver, adjusting the database to the script, is a sh*t ton of work that will take me several days or weeks to do when it seems a few-line script or adjusting the script you posted can be created in a few minutes. Ouch to all that work. Why can't it be done the way I already have the database?
  19. Why can't code be adjusted to a database instead of the database being adjusted to the code? Seems to be a lot more work that way. Or is it less work to change the database? I'm going to struggle with changing the database really hard because I don't see how to create it like this, is this the kind of layout for the database you mean?: Categories Rifles Rifle 1 ammo Rifle 1 ammo type 1 Rifle 1 ammo type 2 rilfe 1 ammo type 3 scopes r1 scope posibility 1 r1 scope possibility 2 3 4 5 6 7 6 Species allows 1 2 3 4Rifle 2 Rifle 2 ammo Rifle 2 ammo type 1 Rifle 2 ammo type 2 rilfe 2 ammo type 3 scopes r2 scope posibility 1 r2 scope possibility 2 3 4 5 6 7 6 Species allows 1 2 3 4 Bows bow 1 ammo 1 2 3 scope/sight 1 2 3 4 animals allows to shoot 1 2 3 4 Bow 2 ammo 1 2 3 scope/sight 1 2 3 4 animals allows to shoot 1 2 3 4 The way I have the database already is pretty much set up that way. It's just categories. I excluded making a "categories" main category and putting everything in it because this part of the project only has the categories. Why can't the code be adjusted without everything being in a main category? I'd be interested to learn how to make sub-categories in a database like that. Please share how to do that! That would be cool, and useful. I don't agree it needs to be structured like that for this project but others, I do agree.
  20. I can't edit my post above anymore. It doesn't work with my database. I'll work on it a bit to see what I have to change with both the code and the database to get it working that way. I moved a ways forward with the project with how Barand suggested doing it. Your suggestion mac_gyver, seems like a great way of doing it. Hard drive space filling up! lol
  21. I've done that. Works great! I'll see if I can figure out how to get it working with the database I have. I don't understand this: $sql = "SELECT c.name category_name, i.id, i.name, i.em, FROM items i JOIN category c ON i.category_id = c.id ORDER BY c.name, i.name"; $stmt = $pdo->query($sql); can that be changed to something like: $sql = "SELECT id, rifleName, em, gm, FROM rifles i JOIN category c ON i.category_id = c.id ORDER BY c.name, i.name"; $stmt = $pdo->query($sql); and work for the rifles category and menu? I'm completely lost (at the moment) as to how you did that. Brain farts due to my limited knowledge of all this. I'm figuring it out slowly. I'm very grateful for all the help!
  22. I get this error when trying to run what you shared. Fatal error: Uncaught PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'ehr_tracker.items' doesn't exist in C:\Program Files (x86)\EasyPHP-Devserver-17\eds-www\ehr_tracker\try13\mac_gyver1.php:26 Stack trace: #0 C:\Program Files (x86)\EasyPHP-Devserver-17\eds-www\ehr_tracker\try13\mac_gyver1.php(26): PDO->query('SELECT c.name c...') #1 {main} thrown in C:\Program Files (x86)\EasyPHP-Devserver-17\eds-www\ehr_tracker\try13\mac_gyver1.php on line 26 How you did all that is beyond what I understand so, right, I didn't touch it. The database doesn't have a table named `items`. The items aren't all in one table they're in separate tables. I did it that way because it made the most sense to me. One table per category. One dropdown menu per item category. I did consider putting all of the items in one table called 'items' like that but I don't know how to make the database have a tree structure doing it that way needs. I'm not aware we can make a mySQL database have a tree structure like that. Is it possible to make the database that way? Even if it was possible to do it that way I'm not sure that would be an ideal way of setting it up for this. I don't know enough php (and can't find a reference that's of any help to _me_) to edit how you wrote the code you shared so it works. This project is based off of the layout map below. The roadmap below is based off of another spreadsheet program I made I'm trying to make a web app version of. It's easier for the game's community to access a web app than a spreadsheet program. The spreadsheet program I made won't run on google sheets or MS office "live" because I made it with Libre Calc. Not compatible or, yeah, I would just upload the spreadsheet program to MS live or google sheets and be done. This is the database. I import it as a file through phpMyAdmin to convert it to mySQL: https://drive.google.com/drive/u/0/folders/16StspK1rSDKGDgKaEEuA9SBiP6FrY3Hw
  23. Thank you! I can only write the script once? I'll play around with your sample here and test that out. Very much appreciated! I think I may have miscommunicated how many categories there are or I'm mis-reading "//the following assumes I want all category data". I'm confused on that actually. Yes, I will be using all the category data but not in the same `<select>` menu. I made the tables separate as you see on the left because each one is going to have it's own `select` menu: The shorter the code can be, the less I have to repeat code, the better. I agree! And as I said I'd rather have the JS in it's own file all together. I'd rather not publish all the code in one file. Thank you very much again, for the help and advice! I saw your //comment about the database stuff. I'll apply what you suggested to that too. Thanks!
  24. Thanks! Doing them all like this seems to be working. <!-- Rifles --> </div> <tr> <td colspan="2">Rifles</td><td></td><td><button type="button">+</button></td></tr> </tr> <tr> <td> <select id="menu" name="selection"> <option value="1" selected>Choose a Rifle</option> <?php $data = $pdo->query("SELECT id, rifleName, em, gm FROM rifles ORDER BY rifleName"); foreach ($data as $row) { echo "<option value={$row['id']} data-em='{$row['em']}' data-gm='{$row['gm']}'>" . $row["rifleName"] . "</option>"; } ?> </select></td> <td><!-- Rifle EM--><input type="text" id="rem" value="0" disabled size="5"/></td> <td><!-- Rifle GM--><input type+"text" id="rgm" value="0" disabled size="5"/></td> <td>+</td> </tr> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script type='text/javascript'> $().ready(function() { $("#menu").change( function() { let em = $(this).find("option:selected").data("em") let gm = $(this).find("option:selected").data("gm") $("#rem").val(em) $("#rgm").val(gm) }) }) </script> </div> <!-- End Rifles --> <!-- Shotguns --> <div> <tr><td colspan="2">Shotguns</td><td></td><td><button type="button">+</button></td></tr> </tr> <tr><td> <select id="smenu" name="selection"> <option value="#">Choose a Shotgun</option> <?php $shotguns = $pdo->query("SELECT id, shotgunName, em, gm FROM shotguns ORDER BY shotgunName"); foreach ($shotguns as $row) { echo "<option value={$row['id']} data-em='{$row['em']}' data-gm='{$row['gm']}'>" . $row["shotgunName"] . "</option>"; } ?> </select> </td> <td><!--Shotgun EM--><input type="text" id="sem" value="0" disabled size="5"/></td> <td><!--Shotgun GM--><input type="tect" id="sgm" value="0" disabled size="5"/></td> </tr> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script type='text/javascript'> $().ready(function() { $("#smenu").change( function() { let em = $(this).find("option:selected").data("em") let gm = $(this).find("option:selected").data("gm") $("#sem").val(em) $("#sgm").val(gm) }) }) </script> </div> <!-- End Shotguns --> They are all the same in the database. id, [something]Name, em, gm. I was hoping when I get it all together I would be able to put the script in one file and the db connection info in another. I'd like to keep the code as minimal as possible. Wishful thinking?
  25. For the life of me, I've been trying to for decades, I cannot get a grip on JavaScript. What am I doing wrong that I can't get the other menus working? It's the same thing, repeated. I tried renaming everything making it individual with the bows menu. No go. The shotgun and bows menu don't work. There's other categories I'd like to add and do this with. Then I'll be looking in to how to add all the values from the em and gm columns in the html table together to get a total that I'll probably be wrestling with too <html> <head> <style> body { background: black; color: white; } input { background: black; color: white; border: 1px solid orange; text-align: center; } select, selection { background: black; color: white; border: 1px solid orange; } table { border: 1px solid gray; border-collapse: collapse; } </style> </head> <body> <?php $servername = "localhost"; $dbport = '3306'; $dbname = 'ehr_tracker'; $dbuser = 'root'; $dbpass = ''; try { $pdo = new PDO("mysql:host=$servername;dbname=$dbname;charset=utf8;'", $dbuser, $dbpass, array(PDO::ATTR_EMULATE_PREPARES => false,PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); } catch(PDOException $e) { echo "Connection failed: " . $e->getMessage(); } $smt = $pdo->prepare('select id, rifleName, em, gm From rifles'); $smt->execute(); $data = $smt->fetchAll(); $data = $pdo->query("SELECT id , rifleName , em , gm FROM rifles ORDER BY rifleName "); $bows = $pdo->query("SELECT bow_id , bowName , bow_em , bow_gm FROM bows ORDER BY bowName "); $ShotGuns = $pdo->query("SELECT id , shotgunName , em , gm FROM shotguns ORDER BY shotgunName "); ?> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script type='text/javascript'> $().ready(function() { $("#menu").change( function() { let em = $(this).find("option:selected").data("em") let gm = $(this).find("option:selected").data("gm") $("#em").val(em) $("#gm").val(gm) // Bows $("#bow_menu").change( function() { let bow_em = $(this).find("option:selected").data("bow_em") let bow_gm = $(this).find("option:selected").data("bow_gm") $("#bow_em").val(bow_em) $("#bow_gm").val(bow_gm) }) }) }) </script> <table border="1"><tr> <th>Items</th> <th> em</th><th>gm</th><th>+</th> </th> <tr> <!-- Rifles --> <td colspan="2">Rifles</td><td></td><td><button type="button">+</button></td></tr> </tr> <tr> <td> <select id="menu" name="selection"> <option value="#">Choose a Rifle</option> <?php foreach ($data as $row) { echo "<option value={$row['id']} data-em='{$row['em']}' data-gm='{$row['gm']}'>" . $row["rifleName"] . "</option>"; } ?> </select></td> <td><!-- Rifle EM--><input type="text" id="em" value="0" disabled size="5"/></td> <td><!-- Rifle GM--><input type+"text" id="gm" value="0" disabled size="5"/></td> <td>+</td> </tr> <!-- Bows --> <tr> <td colspan="2">Bows</td><td></td><td><button type="button">+</button></td></tr> </tr> <tr> <td> <select id="bow_menu" name="selection"> <option value="1">Choose a Bow</option> <?php foreach ($bows as $row) { echo "<option value={$row['bow_id']} data-em='{$row['bow_em']}' data-gm='{$row['bow_gm']}'>" . $row["bowName"] . "</option>"; } ?> </select> </td> <td><!--Bow EM--><input type="text" id="bow_em" value="0" disabled size="5"/></td> <td><!--Bow GM--><input type="text" id="bow_gm" value="0" disabled size="5"/></td> </tr> <!-- ShotGuns --> <tr> <td colspan="2">Shotguns</td><td></td><td><button type="button">+</button></td></tr> </tr> <tr> <td> <select id="menu" name="selection"> <option value="#">Choose a Shotgun</option> <?php foreach ($ShotGuns as $row) { echo "<option value={$row['id']} data-em='{$row['em']}' data-gm='{$row['gm']}'>" . $row["shotgunName"] . "</option>"; } ?> </select> </td> <td><!--Bow EM--><input type="text" class="charge-type" name="em" id="em" value="0" disabled size="5"/></td> <td><!--Bow GM--><input class="charge-type" name="gm" id="gm" value="0" disabled size="5"/></td> </table> </body> </html>
×
×
  • 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.