Jump to content

maxxd

Gurus
  • Posts

    1,655
  • Joined

  • Last visited

  • Days Won

    51

Posts posted by maxxd

  1.     $sql2 = "SELECT id, amount_charged_each, quantity, chargable_units from quote_items where quote_id = :qId";
        $stmt2 = $pdo -> prepare($sql2);
        $stmt2 -> execute([
            ':id' => $_GET['quoteId']
        ]);

    There's also a fairly obvious problem here in that place marker names need to match the index of the values.

  2. This is completely untested, but it makes sense...

    add_action( 'admin_print_styles', 'add_order_notes_column_style' );
    function add_order_notes_column_style() {
    	$css = '.post-type-shop_order table.widefat.fixed { table-layout: auto; width: 100%; }';
    	$css .= 'table.wp-list-table .column-order_notes { min-width: 280px; text-align: left; }';
    	$css .= '.column-order_notes ul { margin: 0 0 0 18px; list-style-type: disc; }';
    	$css .= '.order_customer_note { color: #ee0000; }'; // red
    	$css .= '.order_private_note { color: #0000ee; }'; // blue
    	$css .= ".order_private_note.toto { color: #00ee00; }"; // green
    	wp_add_inline_style( 'woocommerce_admin_styles', $css );
    }
    
    // Add order notes to the "Order Notes" column
    add_action( 'manage_shop_order_posts_custom_column', 'add_order_notes_content' );
    function add_order_notes_content( $column ) {
    	if( $column != 'order_notes' ) return;      
    	global $post, $the_order;
    	if( empty( $the_order ) || $the_order->get_id() != $post->ID ) {
    		$the_order = wc_get_order( $post->ID );
    	}    
    	$args = array();
    	$args['order_id'] = $the_order->get_id();
    	$args['order_by'] = 'date_created';
    	$args['order'] = 'ASC';
    	$notes = wc_get_order_notes( $args );
    	if( $notes ) {
    		print '<ul>';
    		foreach( $notes as $note ) {
    			if( $note->customer_note ) {
    				print '<li class="order_customer_note">';
    			} else {
    				print '<li class="order_private_note';
    				if($note->added_by == 'toto'){
    					print ' toto';
    				}
    				print '">';
    			}
    			$date = date( 'd/m/y H:i', strtotime( $note->date_created ) );
    			print $date.' by '.$note->added_by.'<br>'.$note->content.'</li>';
    		}
    		print '</ul>';
    	}
    } // end function

     

  3. Totally get that about reinventing things to understand them, so I'm not going to give you any crap for going down that road if you choose to.

    I will say that given that and your plans to take up a framework later on, you may want to (at this time) look into Twig as a templating language instead of Smarty - I'm sure someone here will correct me if I'm wrong but the impression that I get is that Smarty isn't all that used any more, while several frameworks either support Twig or use a dialect based on it. Heck, from looking at the Smarty repo it looks like they've taken that in a more Twig-based direction.

    Another choice is to simply use PHP in your HTML files and skip a templating language entirely; this is a technique still widely used in the wild and is obviously easily extensible in plain PHP. It's not an approach I personally recommend or use these days, but again my last four or five years worth of jobs have been Laravel based, so I've gotten rather used to that.

    Rounding back to your original question, the benefit of using a framework is that a framework usually takes care of the front-controller aspect of things, and you won't have to do any extra work in the .htaccess file to get the pretty URLs it sounds like you were originally asking about (before I derailed the entire conversation - sorry about that).

  4. Omitting the action attribute from a form tag is the current best practice, yes. As far as data/form security goes, it takes more than just that - it takes things like using a nonce and validating and sanitizing any user-submitted data before you do anything with it. Even then, you'll need to use prepared statements for any database interactions that involve said user-submitted data.

  5. Not gonna lie, I didn't realize Smarty was still in active development but looking at the github repo it clearly is, so if it's something you're invested in it doesn't seem like a lost cause. As I said, it's been a bit since I've explored it - I moved to Twig for a bit, then my jobs moved me to Laravel with Blade and/or Vue.

    All that aside, whatever template language you're using won't affect your routes. I'm sure others will kick in on this, but if you're just learning and starting fresh and you want to adhere to MVC patterns while keeping things DRY and using pretty URLs for routing, it might not be a bad idea to go straight to a framework just so you don't have to recreate the wheel.

    It's always good to understand the things a framework is doing at the base level, but honestly these days the most popular and most used frameworks take care of so much of the crap work that it's kinda worth learning backwards IMO. If you're working on updating an existing plain PHP code base then obviously ignore that opinion entirely; your situation is going to supersede any bullshit I may spout about the ease of adopting a framework. On the other hand, if you're planning on rewriting everything anyway, why not make it a bit easier on yourself?

    There are several frameworks out there, so if you decide to go that route do some research. Laravel is the most popular PHP framework and once you get used to its opinions it is pretty easy to deal with and offers some nice sugar out of the box, but there's a lot of magic there. In my experience CodeIgniter 4 is quite good, but offers less default functionality (and thereby less magic). I've heard good things about the latest version of Cake though I've never tried it. And from early experience with the latest major version and looking at the repo, Yii is ... not awesome. However, maybe that's changed?

  6. It sounds like you're looking for a pretty URL? If so, this is typically set up as a rewrite rule. I haven't looked at Smarty in a long time, and a quick Google search didn't return anything it specifically does to enable pretty URLs, so it's probable you'll have to update your .htaccess file (assuming you're using Apache - I'm not sure where or how this happens if you're using nginx or another server) with the rule to make it happen.

    If you're looking to go with more of a single page application style pattern, there are several to many other considerations to take into account.

  7. You're also ending the if statement in the middle of the loop - you can't do that. If the conditional applies to each row pulled from the database (it doesn't look like it does) then put the if statement inside the loop. Otherwise, end it after the loop. And as mac_gyver pointed out, you'll be better served going with PDO over mysqli.

  8. Works for me - what exactly are you seeing?

    <!DOCTYPE html>
    <html lang="en">
    <head>
    	<meta charset="UTF-8">
    	<meta name="viewport" content="width=device-width, initial-scale=1.0">
    	<title>Testing before</title>
    	<style>
    		.color-top-border-pink-green:before {
    			background: linear-gradient(to right,#e0218a 17.85%,#bff010 53.28%,#96BD0C 100%);
    			content: "";
    			height: 4px;
    			width: 100%;
    			display: block;
    		}
    		.color-top-border-pink-green{
    			height: 350px;
    			width: 99vw;
    			background: url('https://images.unsplash.com/photo-1507525428034-b723cf961d3e?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=2073&q=80') center center no-repeat;
    		}
    	</style>
    </head>
    <body>
    	<div class="color-top-border-pink-green"></div>
    </body>
    </html>

    Note that there is a small chance I'm breaking copyright rules with that background image - I honestly don't know. It comes from here: https://unsplash.com/photos/KMn4VEeEPR8

  9. It really doesn't matter - I personally like OOP, even in plain PHP projects. However, when I start something new I reach for Laravel because my jobs have used Laravel for the past 4 or so years and by now I'm somewhat familiar with it. I also liked CodeIgniter 4, though there's not as much built-in functionality there as Laravel (but that can be nice as there's also not as much magic to deal with).

    Biggest problem I find I have nowadays jumping into a procedural or functions-based system is keeping track of what function comes from where. When everything is in the global namespace I find that things can get messy and confusing.

  10. That's a completely different form in the JS Fiddle than what you've described already. Post the code and we can help.

    Some more things right off the top, don't set the form 'action' attribute based on what button's clicked - use the JS to set a hidden field in the form and leave the action attribute empty. That way the form will submit to the current URL. Set up your page in this order from top to bottom:

    PHP
    HTML
    JavaScript

    This allows you to handle the form submission first and display any errors to the user. It also allows you to not have to maintain three separate files for very similar functionality. Also - as I said before - prepare the query before the loop, then set $paramValue and execute the query in the loop. This way you're not wasting cycles preparing the same statement on every iteration - it only needs to be done once.

  11. You don't have a form input named 'users' and you never actually execute the query. That's a couple of several issues I see with this code - first, I assume your php is being called via ajax from the JS setUpdateAction() function? If so, you'll want to echo output and let your javascript handle the return instead of issuing a location header. Also, one of the joys about prepared statements is that you can prepare them once and use them multiple times - if you're doing the update in a loop prepare the statement before the loop, then bind and execute it in the loop. Also, if you're using mysqli I recommend switching to PDO now - it's just a better interface. Beyond that, you don't need two consecutive require_once statements for the same file; the name pretty much says it all (require once), and there's no need to jump through the hoops you're doing to assign evenRow or oddRow class names - just use CSS's nth-child() selectors.

  12. No matter what, by passing no third parameter to the filter_input function no filtering will actually happen and all of this is pointless. From the docs:

     filter_input(
        int $type,
        string $var_name,
        int $filter = FILTER_DEFAULT,
        array|int $options = 0
    ): mixed

    and

    Quote

    filter

    The ID of the filter to apply. The Types of filters manual page lists the available filters.

    If omitted, FILTER_DEFAULT will be used, which is equivalent to FILTER_UNSAFE_RAW. This will result in no filtering taking place by default.

    It's not clear what's happening with the submitted data (except for the one random output of $preApprovalAmount), but the OP needs to actually handle the input in a manner appropriate for the usage. If it's for a DB query, use prepared statements. If it's for output, use htmlspecialchars() or htmlentities().

  13. I could be remembering incorrectly, but I feel like this used to be available before google announced the move to GA4 and away from its Universal Analytics. In my experience, the move to GA4 made everything more complex, time consuming, and difficult. In my last job even extremely experienced data scientists were having trouble figuring out how to map and track user interactions, and this was after extensive meetings with the dev team about implementation.

    This is probably a jaded and unfair opinion, but I feel like as long as a company/user has enough money, google will find a away to show them the data they're looking for.

    Grumpy rant over...

  14. Or, if you just really like to over-engineer things:

    echo (new DateTime('now', new DateTimezone('America/New_York')))->sub(new DateInterval('P1M'))->format('M j, Y');

    As dumb as it may sound given the length of this statement versus requinix and Barand's answers, this is easier for my brain to read.

×
×
  • 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.