Sublime Text 3 custom PHP auto-completion does not work - php

Here's the default auto-completion for "switch":
switch (variable) {
case 'value':
# code...
break;
default:
# code...
break;
}
but I wish to turn it into just:
switch ()
{
case '':
break;
case '':
break;
}
because I don't like to modify "#code here..." every time.
I navigated to
"C:\Users\USER\AppData\Roaming\Sublime Text 2\Packages\PHP"
and opened "switch(-).sublime-snippet" and modified it into:
<snippet>
<content><![CDATA[switch ($0)
{
case '$0':
break;
case '$0':
break;
}]]></content>
<tabTrigger>switch</tabTrigger>
<scope>source.php</scope>
<description>switch …</description>
But nothing works.
Is there any syntax error? Or do I modify the wrong file?

If you tagged your question correctly, you modified the wrong file - you need to edit the Sublime Text 3 version. This is a bit more difficult to do directly, since the file is wrapped up in a .sublime-package zip archive. To get around this, install Package Control (if you haven't already), then install the PackageResourceViewer plugin. Open the Command Palette, type prv to bring up the PackageResourceViewer options, select Open Resource, then navigate down to PHP and select the switch(-).sublime-snippet option. Edit it to your liking, save it, and you should be all set.
You probably also want to set your tab stops differently. Try this instead:
<snippet>
<content><![CDATA[switch ($1)
{
case '$2':
$3
break;
case '$4':
$5
break;
${0:default:}
}]]></content>
<tabTrigger>switch</tabTrigger>
<scope>source.php</scope>
<description>switch …</description>
</snippet>
Now, you can tab through the different areas, filling in the info as you go, ending up at the bottom with a default option, that you can just hit Delete on to erase if you don't want it. With your original version, after typeing switchTab, you would have ended up with 3 different cursors, one at each of the $0 locations. Check out the snippets reference for more information.

Related

Forms action field in html form - not redirecting to correct php code

Adding further explanation. I am pretty new to php but not programming in general.
I have a plugin for creating templates in wordpress and adding to the WP page editing screen
these templates are in a folder see below
C:\Wamp64\www\cbjaxon\wp-content\plugins\cgdmaintroutines\templates
these php file templates are file maintenance routines
there are sometimes a 00 and a 01 file that I need to move back and forth between
then there are 'non page' php files for inserting, deleting records as well as
things like db_connect.php, error handling, etc. these files are located in a folder
below the website folder cbjaxon i.e. cbjaxon/Code
when add, update etc button is clicked I need to call(?) these in the Code folder.
Also need to loop back from insert.php back to the 01 maintenance program.
I would have thought from what I've seen in Stackoverflow that if in a file in the
C:\Wamp64\www\cbjaxon\wp-content\plugins\cgdmaintroutines\templates
directory I could ust $url = 'filename' and put that in the action field in the form and
it would work. But it doesn't.
I know I'm rambling but I have been fighting this for days.
Using Wordpress to create pages.
File actually located in "c:/wamp64/www/cbjaxon/Code/schoolInsert.php"
Trying to set $url to appropriate url so can insert, delete and update. All the code files(insert, update, delete) located in same folder "c:/wamp64/www/cbjaxon/Code/?????.php.
Wordpress page code located in
C:\Wamp64\www\cbjaxon\wp-ontent\plugins\cgdmaintroutines\templates\schoolMaint01-tpl.php
So trying to set action field in form to the appropriate $url(and load into action field) to either insert, delete or update.
I have tried setting to $url to all kinds of things with ../'s, etc. as well as explicitly setting to actual file location as in the last one below.
The issue is that I do not know how to get the action field to navigate to my php code. I'm fairly new to php.
The version shown(is for add) and is the one I'm using as a test as it is the easiest to test.
The JS function validateFormOnSubmit is working as it does not produce an error.
As you can see from path below this is all being done at the moment on my local computer.
$url = "/cgdschoolInsert.php"; #
$url = "c:/wamp64/www/cbjaxon/Code/cgdschoolInsert.php";
?>
<div class="maintScreen">
<form action="<?php echo $url ?>" name="schoolMaintForm"
onsubmit="return(validateFormOnSubmit());" method="post">
It seems that you are trying to use the form to send a post request to cgdschoolInsert.php. A lot of information is missing from your question, but my assumptions are:
Based on the destination PHP file, I am assuming that this PHP code is your INSERT action and you are having similar cgdschoolDelete.php for DELETE action and so on.
The above code snippet with the form is published in some kind of web site on a web server (I am going to be using http://example.com/mydoc.php as a working example here).
First things first:
I am not sure about your templating and other setup, but the key is knowing the location of cgdschoolInsert.php on your website:
cgdschoolInsert.php must be directly accessible from your browser on your web site
Your $url must reflect the location of cgdschoolInsert.php on your web site, not in your server's file system.
In other words, you should be able to type the URL for cgdschoolInsert.php in your browser and access it. For example, if it is located in the same directory as mydoc.php (the document with your form), you should be able to access it from that form as cgdschoolInsert.php (no slash). If it is in the root of the web site, you can access it as http://example.com/cgdschoolInsert.php, or, alternatively as /cgdschoolInsert.php. If it is in a different directory, you will have to figure out the actual URL of it yourself. I am not familiar with your setup, so I cannot help there any further.
Please try that in your browser. If unsure, you can put a big <?php echo '<h1>I am here</h1>' ?> on the top of the cgdschoolInsert.php script to make sure your browser loads the right script.
Then:
Action URL may or may not contain domain. If, say, your mydoc.php script is served from http://example.com/mydoc.php, $url = "/cgdschoolInsert.php" would be having the same effect as $url = "http://example.com/cgdschoolInsert.php". (The former would probably be better, saving you a lot of re-work if you decide to move your site to a different domain in the future.)
Lastly:
You want to implement different actions. You can do this in several ways. One suggested in your script is a different PHP script for INSERT, DELETE, UPDATE. In that case, you can use PHP switch statement to set the right $url "based on passed in variable" (as you mentioned):
switch($passedVariable) {
case "INSERT":
$url = "/cgdschoolInsert.php"; // Change per your finding from the first point
break;
case "UPDATE":
$url = "/cgdschoolUpdate.php"; // Change per your finding from the first point
break;
case "DELETE":
$url = "/cgdschoolDelete.php"; // Change per your finding from the first point
break;
default:
throw new Exception('Bad form action');
}
?>
<div class="maintScreen">
<form action="<?php echo $url ?>" name="schoolMaintForm" onsubmit="return(validateFormOnSubmit());" method="post">
...
OR ALTERNATIVELY
You can have one script for all actions, e.g. cgdSchoolCRUD.php. You can add a hidden form field into your form:
<form ...>
<input type="hidden" id="myAction" name="myAction" value="<?php echo $passedVariable ?>">
...
</form>
Inside the cgdSchoolCRUD.php you can use the switch statement to process the action:
$myAction = $_POST["myAction"];
switch($myAction) {
case "INSERT":
// ... code to insert school
break;
case "UPDATE":
// ... code to update school
break;
case "DELETE":
// ... code to delete school
break;
default:
throw new Exception('Bad form action');
}
Another small tip:
Depending whether it is enabled in your system, you may consider using PHP short tags, which could make your HTML more readable. Instead of <?php echo $url ?> you can use the shorthand of <?=$url ?>.

Why won't the key combination Ctrl-K Ctrl-F work for php visual code?

I'm intending to format the selection (indentation) for the PHP code, but it does not work.
I already made sure that there aren't duplicate shortcuts.
I also disabled all extensions.
I changed the keyboard shortcut from Ctrl+K Ctrl+f to Ctrl+k Ctrl+y.
None of these helped.
Is the only language that does not work for me to format selection
Eye. It's not because I'm missing the closing tag (?>).
To see menu bar if not present press
Left Alt
then go:
Preferences > Keyboard Shortcuts
type in the search bar
ctrl+k ctrl+f
you should see
perhaps you have a collision and other command has the same shortcut defined or your shortcut is not defined at all.
You can double click on shortcut to edit it.
Note at the picture When this is when the command works because one shortcut may work only if you are currently editing document and other when you are browsing files so once you set a shortcut make sure your checking it in different places of editor to see if its working or not.
If you use shortcut:
Ctrl+Shift+P
and select command:
You'll see whole bunch of shortcuts and there should be there one you are missing:
{ "key": "ctrl+k ctrl+f", "command": "editor.action.formatSelection",
"when": "editorHasDocumentSelectionFormattingProvider && editorHasDocumentSelectionFormattingProvider && editorTextFocus && !editorReadonly" },
I think that you can just copy the one above, paste to your file if it is not present and save that file, restart your Code and all should be working. Remember that the file is JSON so keep its format - look how other keys are presented there and your pasting should not make JSON invalid.
.vue file doesn't have formate selection.
This function depends on your file type.
I checked that this key binding was indeed still specified, there was no duplicate key binding, etc.; still, Visual Studio Code refused to recognize the key combination. Then I quit visual studio code and restarted it, and the key combination started working again.
Sometimes, the basic quit-and-restart is the answer!
If nothing is working you can create your own "format selection" with multiple commands. You would need a code formatter and a macro extension to run multiple commands from one keybinding,I'm using "prettier" and "multi-command" extension.
You can use this keybinding in your keybindings.json (Click File -> Preferences -> Keyboard shortcuts. Use the tab that opens up to edit and find available key bindings) with the multi-command extension - no need for anything in settings.json:
{
"key": "Shift+Alt+A", // or whatever keybinding you wish
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
"editor.action.formatDocument",
"editor.action.clipboardCopyAction",
"undo",
"editor.action.clipboardPasteAction",
]
},
"when": "editorHasDocumentFormattingProvider && editorTextFocus && !editorReadonly"
}
I use it because "format selection" is not working with "*.vue" files.

Explode url page with switch case

Solved, this is my temporary solution for the 404 error page.
if (sizeof($url) != 1) {
header('Location: ../404');
}
else {
// nothing
}
default:
$page_name = 'Error';
$page_file = 'pages/errors/404.php';
break;
Change the code in your default: section to return HTTP 404 code and generate a proper template. See this question as reference.
PS. You seem to check only the first part of the URL, i.e. $_GET[0], but looks like you don't process any subsequent ($_GET[1],...) parts of it. In general I would advise you to either build your app on any mainstream framework with proper routing package in it or if you don't feel like using any fullstack PHP framework, you can install just any routing package via composer. E.g. symfony/routing

Locale is switching/not loading properly (sometimes)

I have a very annoying problem with the language/locale for our site. I've been looking around here but haven't found anything similar.
So we support a few different languages and have everything set up with .po and .mo files which works fine most of the time. Sometimes it switches language when you load something or refresh a page. There's not really a pattern to it either, sometimes 6 'en' in a row and then 'sv' and sometimes every second 'sv' and 'en'.
Ex. If you want to edit a user you get a popup which starts by including authorize.php where we check the language and set it accordingly with this function:
function _set_locale ($lang){
switch($lang) {
case 'sv': $locale = "sv_SE"; break;
case 'en': $locale = "en_SG"; break;
case 'nl': $locale = "nl_NL"; break;
case 'dk': $locale = "da_DK"; break;
case 'vi': $locale = "vi_VN"; break;
case 'zh': $locale = "zh_CN"; break;
default: $locale = "en_SG"; break;
}
putenv("LANGUAGE=$locale");
setlocale(LC_ALL, "$locale.UTF-8");
bindtextdomain("messages", "./locale/");
textdomain("messages");
I was running an account with Swedish, 'sv' set and refreshed the edit user popup 10 times and debugged this function, $lang was 'sv' and I logged setlocale(LC_ALL, 0) which returned 'sv_SE.UTF-8' every time but the page was displayed in English 5-6 times.
It seems like it's only switching from the current language to English which is the original language, so I figure that it's not getting translated. It doesn't look like we're setting the wrong/default language, it just ignores/don't have time? to set the language.
The language is not saved in cookies and some accounts just have 1 language (which is not English) and still gets this.
In the above ex. when we return from authorize.php we don't use any language variable in the actual 'edit user' page. So it shouldn't be able to change it there. (I also debugged and checked the language when returning from auth. and it was Swedish every time).
I don't expect anyone to be able to solve this by the info I've given I'm just interested if someone has experienced this or have any idea why it sometimes 'switches' please let me know if I can attach some more code to sort this out.
Thanks!
I had a similar intermittent problem PHP gettext and vagrant running ubuntu, it was showing the right text on the 3rd request.
Try one of the following, I think it will depend how you have PHP running with Apache
sudo service php5-fpm restart
sudo service apache2 restart
I think it stemmed from me playing around with the value passed to setLocale()

PHP SWITCH entering DEFAULT even if there is a valid CASE?

I have this SWITCH block:
switch ($_GET['page']) {
case "listaOferteTest":
include("php_views/lista_oferte_test.php");
break;
case "categorieOferte":
include("php_views/categorie_oferte.php");
break;
case "pagina":
include("php_views/pagina.php");
break;
default:
include("php_views/page_not_found_redirect.php");
break;
}
and some php links [they are dinamicaly generated, but i'll paste the html]:
<a href='/pagina/termeni-si-conditii/'>
Termeni si conditii
</a>
<a href='/pagina/informatii-utile/'>
Informatii utile
</a>
<a href='/pagina/contact/'>
Contact
</a>
I have a .htaccess where i treat the link like this:
RewriteRule (.*)/(.*)/(.*)/ index.php?page=$1&subPage=$2&subSubPage=$3 [L]
The problem: When I tested the links above I noticed a strange behavior - from about 10 clicks on random links one gets also the default. How is this possible? Thanks!
My first instinct would be to get the debugger out and step through the code so you can see exactly what's going on. If you don't have a debugger installed (why not? you should, it'll change your life!) or if you can't recreate the problem with a debugger given it's seemingly random nature, then next best thing would be to add some logging to the default block so you can work out why it's getting there. My guess would be that $glob['page'] doesn't match 'pagina' exactly; maybe it's got a leading/trailing space or slash, or maybe it's empty because there's a bug in how that value is being extracted. Something like Monolog would do the job in terms of logging.
Then u can use, if's instead of switch.
For ex.
if (isset($_GET['page'])) {
if($_GET['page']=='something') {...}
if($_GET['page']=='something2') {...}
...
}

Categories