Jump to content

maxxd

Gurus
  • Posts

    1,662
  • Joined

  • Last visited

  • Days Won

    52

Posts posted by maxxd

  1. I don't know if it's applicable to your situation (or why it's happening, honestly) but recently I've come across a couple situations where using get_template_directory() or get_template_directory_uri() from a child theme will return the parent theme's directory. Which, obviously, breaks a lot of things. I wrote the following method into one of my functions classes to get around it:

    private function getTemplateDirectory(){
    	$curTheme = \wp_get_theme();
    	return str_replace(strtolower($curTheme->get('Template')),strtolower($curTheme->get('Name')),\get_template_directory_uri());
    }
    
    

    Not sure if it helps, just thought I'd throw that out there.

  2. Your prepared statement is correct - you don't need to put the quotes around the question marks.

     

    Single vs. double quotes is a deceptively large conversation... When are where to use either or both is largely a question of taste, though there are some hard and fast rules to remember. For the base of it, remember that you can't mix and match the two, but you can nest them. So, if your string assignment starts with a single quote, it has to end with a single quote. That string can include double quotes without problem, but any single quotes in that string will need to be escaped. The same goes with nesting double quotes inside double quotes. Also, php will parse most variables contained within a string using double quotes, but will not when using single quotes. Hopefully this will make some sense...

    $string = 'This starts with a "single quote" and ends with it, too...'; //this is fine
    $string = 'This starts with a 'single quote' and will cause your script to barf'; //nope
    $string = "This starts with a 'double quote' and ends with it, too..."; //also fine
    $string = "Look! more "barfing scripts"!"; //still nope
    $string = 'Starting with a single quote means I\'ll have to escape any other single quotes inside this string.'; //note the backslash before the single quote - that's necessary
    $string = "Same thing with \"double quotes\""; //again, escaping...
    
    $variable = "Hidee ho, matey!";
    $string = '$variable will not parse...'; //contains the string $variable will not parse...
    $string = "$variable will parse..."; //contains the string Hidee ho, matey! will parse...
    

    As for the question about $link, yes, you can use the same connection multiple times. If you need to run several different queries, you should use the same connection object and pass the different query strings to that connection object. If you're concerned about multiple instances of your database connection object being created and used, you can look at using a Singleton pattern for the the connection object, which theoretically will enforce there being at most one instantiated object of the class at a time. Now, you could have an issue if $stmt gets overwritten before you're done using the original value. For that, you just need to format your code and approach logic trips carefully. Make sure you've gotten all the use you need out of the original $stmt query before you write a different value to that variable. Some folks will argue that using the same variable names leads to confusion within a script, and I agree with that, most of the time. However, other times it makes perfect sense to do so, so just think about what you're doing, and document your code like it was your second job.

  3. First and foremost, you'll want to turn on error checking at the top of your script

    error_reporting(-1);
    ini_set('display_errors',true);
    

    Secondly, and I don't know if this is a forum-related thing or not, but the lack of indentation in the code makes it difficult to read and follow what's actually happening. I thought for a second you had an improperly nested if-else loop, but I missed an opening bracket earlier in the code. Also - just as a side-note - mac_gyver is absolutely correct about the issue with the repetition in the code. You're typing way too much, but that's a thing for later. Right now, turn on error reporting and see what that has to say.

  4. Hey y'all - I have a quick question. [edit - As I type, it's becoming a not-so-quick question. Sorry about that, but I'd very much appreciate any input.]

     

    At work (we're on osX) we've recently switched to using Bitbucket with SourceTree for our version control system. I've got many project directories, already populated with working files. They're under SVN control, but honestly our SVN repo is a bit of a mess and I'm hoping to start over. My process for creating GIT repositories for each of these project directories using SourceTree is as follows:

     

    In the bookmarks window that pops up when I launch SourceTree, I click '+ New Repository' > 'Create Local Repository', then I use finder to navigate to the populated project directory and select it. I click 'Create Remote Repository', then click 'Create' to create the local repo.

     

    After filling out the remote repository owner and description, and marking it private, I click 'Create'. Then, in the browser window for the newly created repository that appears when I double click on the bookmark, I stage all the files in the local project directory and commit them. I then push that to the remote master branch.

     

    My question is this - is using the SourceTree 'Create Local Repository' option as described above equivalent to using the terminal window and 'git init' in the project directory? Everything seems to be working correctly (I've successfully committed and pushed files to the remote repositories), but I've only been using this setup for 2 days and haven't done any real heavy lifting with it yet - no branching or merging or anything of the sort. I have successfully cloned one of the repos on a secondary system, but haven't had a chance to make changes on that secondary system and commit then push them to make sure everything is actually working correctly.

     

    It certainly seems like all is well, and I have done more thorough testing on my home system, but that's a Windows box and the Windows version of SourceTree is quite different from the Macintosh version. Really, I decided to go with ST because I just don't like the command line and the price was right (free), and I'm trying to learn it as I go. I just don't want to be five or six months down the line and have to make a major change to a project only to discover that I messed up something very simple with the repository initialization and nothing's working like it should...

     

    Hopefully this is a dumb question and I'm just being paranoid, but I'd very much appreciate any input anyone with SourceTree experience has and is willing to offer.

  5. First and foremost, add_action() and add_filter() are WordPress hook functions - they're specialized global functions that allow the developer to inject a custom function into the core functionality at specific points. So, in your case, at the point that the WordPress core calls after_switch_themes, it will call the user-defined function after_switch_theme_example(). This, by the way, a simplified explanation...

     

    Keep in mind as you're learning: WordPress - from a modern programming point of view - it's really not very well written. The reliance on global variables and functions, and some of the ways in which what classes there are are used is just backwards and old-fashioned. From an end-user point of view, it's great - it allows you to do a lot of things for very little effort. However, it can become rather frustrating quickly as you develop for it. And as far as PHP books and tutorials go, there's a literal ton of them out there but you need to be a little careful because a lot of them are old and PHP has changed a lot in the last several years. If you see a lot of $_REQUEST variables, the keyword global, or any kind of hashing using md5, just assume it's out of date and won't be of much practical help to you. I don't know of any good beginner tutorials or books right off the top of my head, but hopefully someone can kick in with an example or two for you.

  6. You're not using the $sql_user variable in your update queries, you've got it as a string. So, change the following lines:

    $data1 = mysqli_query($connection, "UPDATE member SET rank= '1' WHERE username='sql_user'") or die (mysql_error($connection));
    $data2 = mysqli_query($connection, "UPDATE member SET rank= '2' WHERE username ='sql_user'") or die (mysql_error($connection));
    

    to

    $data1 = mysqli_query($connection, "UPDATE member SET rank= '1' WHERE username='$sql_user'") or die (mysql_error($connection));
    $data2 = mysqli_query($connection, "UPDATE member SET rank= '2' WHERE username ='$sql_user'") or die (mysql_error($connection));
    

    That's the first thing I see right off the bat...

  7. Wordpress is an ever changing complicated mess of their own renamed functions.

     

    Yes, yes it is...

     

    @codexpower - the biggest thing is that WordPress is huge, and asking for tutorials on how to <airquotes>program for it</airquotes> really isn't the way to go. If you've got a specific issue with a theme or plugin you're currently using that you want to fix or improve upon, start by working on just that issue. Basically, give yourself a project and set yourself a goal, then use PHP.net, the WP codex, and the many tutorials on the web to work towards that goal. When you get stuck, then ask a specific question here.

     

    We're all more than willing to help, but we need to know what we're helping with, you know?

  8. You're still using a style sheet with Barand's suggestion. The only thing the code he presented is doing is creating a dynamic class name depending on the day's schedule. Parse the code and read the helpful comments - if there's a booking on that day, the code checks to see if there are free time slots. If so, it applies the 'partial' class tag, because the day is partially booked. If there are no free time slots on that day, it applies the 'full' class tag, because the day is fully booked. In your style sheet, create the .day{}, .partial{}, and .full{} class style definitions and you should be good to go. There's not a lot to understand with this part of it.

  9. I'd be interested to know why it won't work. What you describe with the 5 or 6 table set-up all joined on UserID is exactly how a relational database should work - hence the 'relational' in the name. If a DATETIME field is conflicting with what you're doing, then honestly you're handling that data incorrectly. Serialized data crammed into a VARCHAR or TEXT field may seem like it works when it's set up, but it will almost always come back to bite you in the butt later on.

  10. While it is possible to avoid using multiple languages in this case, I'd advise against doing so.

     

    Use JavaScript to back up your HTML5 validation on the client, then use php on the server to validate the data that's already passed the client-side validation. You can check for simple things like blank values and malformed e-mail addresses and whatnot using JavaScript to give your users immediate feedback and avoid wasting their time with a page refresh if something is obviously wrong. Then you do a deeper validation and make sure that not only is all the necessary data present, but that it's in a format and made up of values you expect using php on the server side.

     

    Always assume that your user is malicious and trying to mess with your application - it's a cynical as all get-out but completely and totally necessary point of view...

  11. You've got a syntax error and a couple logical errors in the code you've posted. Here's an example of what I think you're trying to do:

    function AutoPost() {
    	$.ajax({
    		type: "POST",
    		url: "<?php echo AJAX_URL; ?>",
    		data:$('#lastviewer').serialize(),
    	}).done(function(data) {
    		if(data.success == true){
    			$("#lastviewer").html(data.processed) ;
    		}else{
    			$('#lastviewer').html('<p>Sorry, there was an error</p>');
    		}
    	}).fail(function(jqXHR, status){
    		$('#lastviewer').html('<p>Server error: ' + status + '</p>');
    	});
    }
    
    <?php
    function doAjax($data){
    	$rnd = rand(0,2);
    	if($rnd == 0 || $ret == 2){
    		$ret['success'] = true;
    		$ret['processed'] = 'This is your data after processing';
    	}else{
    		$ret['success'] = false;
    	}
    	print(json_encode($ret));
    	die();
    }
    ?>
    

    Obviously I'm skipping any actual processing and just checking a random number, but other than that...

     

    First off, your success() callback was improperly placed inside the AJAX object as opposed to chained to it. It's also been deprecated, so I've replaced it with the done() callback. Now, the done() callback will fire regardless the outcome of the php processing - as long as the AJAX object receives any legitimate return value(s), AJAX considers that success. So add a 'success' index to the return array that gets set to true if the php processes successfully or false if it doesn't, and check that in the done() callback before any processing takes place. Along those lines, you're going to want to check for server errors as well - that's where the fail() callback comes in.

     

    Using WordPress, your AJAX_URL constant is going to be a set path that resolves to

    /whatever_your_content_path_is/wp-admin/admin-ajax.php

    so don't set that as the action parameter of your form. WordPress will use the contents of the hidden 'action' field in your form to know what php function to call, as long as you've added the following lines in your functions.php file (assuming THEMEX_PREFIX is 'mine_'):

    add_action('wp_ajax_mine_update_user', 'doAjax');
    

    WordPress uses a front-end pattern approach, so everything is going to go through index.php - just leave the action parameter of your form empty.

     

    That will hopefully get you pointed in the right direction and makes any sense at all and is close to what you were looking for...

  12. Where are you instantiating $user_level and $active? I see them as query parameters for your select query, then they're assigned values returned from the the query, but they're not set before you use them in the bind_param() statement.

  13. You're missing quotes right and left, it looks like. Start with the line where you echo the timezone definition (any reason you're outputting that instead of actually defining the constant?) and go from there. If your code editor doesn't support syntax coloring, take a look at one that does - I think maybe Notepad++ does? The system I'm using at work doesn't have it installed and I can't remember off the top of my head.

  14. There's a few things going wrong in the code you've posted. First and foremost, you don't want to create buttons in the foreach() loop, you want to create options. The value of the option should be only $cal->cal_id, and there shouldn't be a link tag in there.

     

    Looking at your code, it looks like you want a listing of open time slots on each day, right? I'd recommend building a table listing the dates down the left, then having a drop-down with the open time slots for each of those days in a column to the right. A third column in the row with a submit button would be good - that way you don't have to write the onChange() javascript to submit the form when a time slot is chosen from the drop-down.

     

    Basically, each row is a separate form with the following hidden fields: dtstart (for the date), task (always going to be 'create', according to your code), and option (always going to be 'com_pbbooking' according to your code). Name the select element 'cal_id', and when you're creating the options (your foreach() loop), put $cal->cal_id in the value parameter. If you assign the action parameter of your form 'index.php' and the method parameter 'get', the form should work just like the link you posted originally once you click the 'submit' button next to the day you've selected.

     

    Obviously I've not tested this, but theoretically it should work.

  15. Personally, I'd go ahead and document the exception with a @throws tag because when another method calls that method, there's a chance it's going to have to catch and deal with the exception the child method throws. I guess there are arguments you could make depending on the visibility or scope of each of the methods, but I like my documentation thorough and have a bit of a tendency to overdo it some times.

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