Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation since 06/27/2022 in all areas

  1. Personally I always use lowercase for var names. Never have any confusion that way.
    1 point
  2. it means only write code that contributes something useful to what your application is trying to accomplish. for connection errors and errors from query(), prepare(), and most execute() calls (except for duplicate or out of range errors when inserting/updating user submitted data), there's nothing the visitor to your site can do to recover from the error, they are due to programming mistakes or a database server that's not running, ... therefore, there's no point in writing any code for these cases. just let php catch the exceptions in these cases and display (when developing code/query(ies)) or log (when on a live/public server) the actual error information, so that you, the programmer/developer, will know when these type of errors are occurring, so that you can find and fix what's causing them. the only types of database errors that the visitor to a site can do anything about are when inserting/updating duplicate or out of range visitor submitted data. this is the only case where having database error handling code in your application will do anything useful. you would catch the database exception, test the error number, setup a message telling the visitor exactly what was wrong with the data that they submitted, so that they can potentially submit a different value that will succeed.
    1 point
  3. there's no good reason to have a try/catch block for the connection code. this is just typing for nothing. a connection error is a fatal problem for a database dependent web page. just let php catch any connection exception, where php will 'automatically' display/log the connection error for you. Keep It Simple (KISS.)
    1 point
  4. You are missing the TRY for your CATCH
    1 point
  5. That's what confused me - your video of exactly what you did didn't show that bit. They aren't necessary as that is what your included code does. You don't need pdoConnect(). Your "connect.php" defines the $pdo connection varaiable. My db_inc.php contains const HOST = 'localhost'; const USERNAME = '????'; const PASSWORD = '????'; const DATABASE = 'test'); // default db 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; } function myConnect($database=DATABASE) { mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT); $db = mysqli_connect(HOST,USERNAME,PASSWORD,$database); $db->set_charset('utf8'); return $db; } I use the pdoConnect() and myConnect() functions to create a connection as I have dozens of databases and, depending on who I am helping, I may need a PDO or a mysqli connection. This lets me use the same include file yet allows flexibility in my connections.
    1 point
  6. Dont be afraid of that. We have many years ahead of us. I try hard and get back to where I was 12-15 years ago, both in business and knowledge. There have been many tragedies along the way. It started with a business fall from 1000 meters and lots of lost money, many deaths in close family and relationships, one broken marriage, close personal bankruptcy, and ended up drinking two liters of vodka or whiskey straight up a day for 4-5 years, one brain stroke, one daughter (now 25) nearly left me (too), one grandchild 2 years old in October (she's the reason i was waking up ++ etc, etc. + I was exposed to a traffic accident 34 years ago that broke my body in small pieces and which was close to taking my life + ADHD that can not be medicated. Of course, it has given me a lot of life experience. This has among other things taught me to listen to others and share what I have. is knowledge, money, etc. One should never give up. Life is fragile and life is what one makes it. We must live and fulfill our dreams while we can. One day it is too late. So now my goal is to come back to where I was in knowledge and business. It's not easy as 50 years and with so many scars r, a lot of new on the front we work, but there I go. When it comes to business Barand knows a little about what a ride I had in the last half of the 90s and early 2000s. As time goes on, you will notice that Leon can also help, not just ask questions. I appreciate all of you and what you do for me. Thanks! I want to continue and be here. For me, this is the beginning of something long. 1.5 years have already passed. Take care guys 😊
    1 point
  7. the posted code contains a php syntax error (a closing ?> tag inside the function definition) and doesn't run at all, so that's not the actual code producing that error. a few possibilities why the function definition is not present in the current program scope - the function definition is inside of a conditional statement, and so isn't defined until the code where that statements is at has been executed and the conditional statement is true. the function definition is inside of another function definition, and so isn't defined until the parent function has been called. the function definition is being required (you should use require for things your program just have) using a url, not a file system path, and is not in the same instance of your program as the function call. after you correct the php syntax error, the posted code produces an undefined constant error because the function call is before the point where the constants are defined. it would take seeing the actual code producing the error to help with what is wrong with it.
    1 point
  8. I'm guessing that's a browser feature. For example, Google Chrome automatically shows an icon to translate a website when you view a page written in a foreign language. More information can be found here: https://support.google.com/chrome/answer/173424 The translate attribute sounds like it can be used to control how these automatic translation services work. For example, you can say that a particular word shouldn't be translated since it is a brand name.
    1 point
  9. More information can be found here: https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/translate
    1 point
  10. Just an FYI: Pretty pictures are nice and all but we cant paste an image into our editors. In the future, please post actual code using the code tags on the post menu <>. 😃
    1 point
  11. gmail has recently stopped accepting emails that are being sent by unknown, unauthenticated email clients/systems, such as a sending mail server at some random web hosting. you must generate an OAuth token and use it to authenticate when sending to gmail. see the following post and thread it is in - prior to this, you were required to use smtp authentication against your gmail mailbox, which the php mail() function does not support, so you need to use either the phpmailer or swiftmailer class in any case.
    1 point
  12. as a separate post. this code is typical misuse of OOP. someone took a few lines of main code, building and executing an insert query, threw a class definition around it, then to make it 'work' had to add $this-> in front of everything, add a bunch more code to build the input properties, code to instantiate the class, code to call the various methods, code (if any) to test the returned values, and code (if any) to use the output properties. if this method gets called and there isn't a database connection (what the first few lines of code are testing), it means that the connection code doesn't have error handling that would have stopped the whole database dependent application from being executed upon a connection error, and for the rare cases where a connection was successfully made, but lost during execution of the code, the fatal php error that would have occurred when the non-existent connection was used should be getting logged to let you know what type of problems are occurring. edit: since the code is just returning in this case, and it's doubtful that the returned value is causing any sort of logging/reporting to occur, you will never know if this is the cause of the missing inserted data. this brings up an additional point for debugging the problem. php's error_reporting should be set to E_ALL and log_errors should be set to ON, preferably in the php.ini on the system, so that all php errors will get reported and logged. if the php/web server error log is empty, either this is not the case or the log file settings are not setup.
    1 point
  13. It's unfortunate, for someone who wants to minimize their code, that you have used a data model which considerably increases your code. +---------------------------------------------------------------+ +--------------------------------------------------+ | | | | | YOUR DATA | | MY DATA | | | | | +---------------------------------------------------------------+ +--------------------------------------------------+ rifles item +----+------------------+------+------+ +----+---------------------+------+------+--------+ | id | rifleName | em | gm | | id | itemname | em | gm | cat_id | +----+------------------+------+------+ +----+---------------------+------+------+--------+ | 1 | .308 Bolt action | 225 | 1350 | | 1 | .308 Bolt action | 225 | 1350 | 1 | | 2 | 7mm magnum | 300 | 1575 | | 2 | 7mm magnum | 300 | 1575 | 1 | | 3 | .243 LeverAction | 215 | 8725 | | 3 | .243 LeverAction | 215 | 8725 | 1 | +----+------------------+------+------+ | 4 | 6ft Longbow | 135 | 1120 | 2 | bows | 5 | 5ft Flatbow | 235 | 1345 | 2 | +----+-------------+------+------+ | 6 | Recurve | 215 | 2525 | 2 | | id | bowName | em | gm | | 7 | Purdy 12 bore | 135 | 1120 | 3 | +----+-------------+------+------+ | 8 | Beretta 12 bore | 235 | 1345 | 3 | | 1 | 6ft Longbow | 135 | 1120 | | 9 | Small bore rook gun | 215 | 2525 | 3 | | 2 | 5ft Flatbow | 235 | 1345 | | 10 | Glock 9mm | 125 | 1050 | 4 | | 3 | Recurve | 215 | 2525 | | 11 | Colt .38 | 200 | 1175 | 4 | +----+-------------+------+------+ | 12 | Derringer .22 | 115 | 3125 | 4 | shotguns +----+---------------------+------+------+--------+ +----+---------------------+------+------+ | id | shotgunName | em | gm | category +----+---------------------+------+------+ +--------+----------+ | 1 | Purdy 12 bore | 135 | 1120 | | cat_id | cat_name | | 2 | Beretta 12 bore | 235 | 1345 | +--------+----------+ | 3 | Small bore rook gun | 215 | 2525 | | 1 | Rifles | +----+---------------------+------+------+ | 2 | Bows | pistols | 3 | Shotguns | +----+---------------+------+------+ | 4 | Pistols | | id | pistolName | em | gm | +--------+----------+ +----+---------------+------+------+ | 1 | Glock 9mm | 125 | 1050 | | 2 | Colt .38 | 200 | 1175 | | 3 | Derringer .22 | 115 | 3125 | +----+---------------+------+------+ +---------------------------------------------------------------+ +--------------------------------------------------+ | | | | | YOUR CODE | | MY CODE | | | | | +---------------------------------------------------------------+ +--------------------------------------------------+ A separate block of code for each category Single block of code for all categories (For 20 categories that's a 95% reduction in coding time) If you add 4 more categories you have to write If I add more categories the code remains unchanged 4 more code blocks My code's output My code for you to try <?php require 'db_inc.php'; // contains db credentials and connection function code $pdo = pdoConnect('javanoob'); // connect setting javanoob as default db // 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'> body { font-family: calibri, sans-serif; } select { background-color: black; color: white; font-size: 14pt; width: 100%; padding: 4px; border: none; } table { background-color: black; color: white; border-collapse: collapse; border-color: #ffa500; margin: 50px auto; } th { font-size: 20pt; color: #ffa500; } td { font-size: 14pt; } .cat-title { background: linear-gradient(to right top, #ffa500, black); font-size: 20pt; font-weight: 600; color: black; padding: 4px 2px 4px 16px; width: 600px; } .ca { text-align: center; } .addmore { width: 20px; height: 20px; display: inline-block; padding: 2px; text-align: center; font-size: 14pt; background: linear-gradient(to right top, #666, #FFF, #666); color: black; border: 1px solid #ffa500; float: right; } </style> </head> <body> <table border='1'> <tr><th>Items</th><th>em</th><th>gm</th><th>&plus;</th></tr> <?= $tdata ?> <tbody> <tr style='border-top: 8px solid #ffa500;'> <th>Totals</th> <td class='ca' id='totem'>0</td> <td class='ca' id='totgm'>0</td> <td>&nbsp;</td> </tr> </tbody> </table> </body> </html> and the SQL code to create the test data tables -- -- Table structure for table `category` -- DROP TABLE IF EXISTS `category`; CREATE TABLE `category` ( `cat_id` int(11) NOT NULL AUTO_INCREMENT, `cat_name` varchar(45) DEFAULT NULL, PRIMARY KEY (`cat_id`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; INSERT INTO `category` VALUES (1,'Rifles'),(2,'Bows'),(3,'Shotguns'),(4,'Pistols'); -- -- Table structure for table `item` -- DROP TABLE IF EXISTS `item`; CREATE TABLE `item` ( `id` int(11) NOT NULL AUTO_INCREMENT, `itemname` varchar(50) DEFAULT NULL, `em` int(11) DEFAULT NULL, `gm` int(11) DEFAULT NULL, `cat_id` int(11) DEFAULT NULL, PRIMARY KEY (`id`), KEY `idx_item_cat_id` (`cat_id`) ) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8; INSERT INTO `item` VALUES (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);
    1 point
  14. Ok, you got my respect for that. When I think about it, I think you're right. And now I even don't dare to say more, not even ask another question ( 😉 ). I feel really stupid. I saw the solution. But (as we say in Norway in Norwegian of course) I just had to shit on my own leg. I feel so embarrassed. Before I was never afraid to try new things or find solutions. It must be my age. I am getting older and the Alzheimer's is closing up on me. Requinix, SORRY FOR ASKING STUPID SELF EXPLAINED QUESTIONS. But, thank you for listening
    1 point
  15. Alternatively $str = 'FSOWPDHJEOSKJ3D2DF5R3FG51'; echo join('-', str_split($str, 5)); //--> FSOWP-DHJEO-SKJ3D-2DF5R-3FG51
    1 point
  16. echo "<a class='knil' href='high.php?id={$game['high']}'>{$game['high']}</a>";
    1 point
  17. If $id contains "Shooter" as a string value it need to be in quotes otherwise SQL thinks it is a column name $sql = "SELECT * FROM games WHERE genre = '$id' "; ^ ^
    1 point
  18. rifleName is incorrect That's what happens when you use multiple cases....
    1 point
  19. An optional parameter would be like public static function render($post, $withPost = true) { which will default to true if not provided. You would then use $withPost to decide whether to do that ->push or not.
    1 point
  20. Why not query your database and put it all in an array like the one Barand showed you. Very little code doing it that way.
    1 point
  21. Use data attributes for the options <?php $data = [ [ 'id' => 1, 'rifleName' => '.308 Bolt action', 'em' => '225', 'gm' => '1350' ], [ 'id' => 2, 'rifleName' => '7mm magnum', 'em' => '300', 'gm' => '1575' ], [ 'id' => 3, 'rifleName' => '.243 LeverAction', 'em' => '215', 'gm' => '8725' ] ]; ?> <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) }) }) </script> <select class='nameItems' 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']}'>" . htmlspecialchars( $row["rifleName"] ) . "</option>"; } ?> </select> em:<input type="text" class="charge-type" name="em" id="em" value="0" disabled size="5"/> gm: <input class="charge-type" name="gm" id="gm" value="0" disabled size="5"/>
    1 point
  22. Don't use a redirect. Half of the point of a 404 page is that it returns a 404, and using a redirect makes it do something different. Why not just change the 404.php to be this new page? Wouldn't that be easier?
    1 point
This leaderboard is set to New York/GMT-04:00
×
×
  • 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.