Zend Studio code formatter problem with comments - zend-studio

i have Zend Studio 7.x, mostly use it for Zend Framework PHP and OOP JavaScript.
When i run formatter on *.php file, often space is added after block of inline comments
Before formatter run:
<?php
function someAction() {
// some note
// second line
if ($foo) {
}
}
?>
After:
<?php
function someAction() {
// some note
// second line
if ($foo) {
}
}
?>
If i run formatter one more time next empty line after // is added,
Code is formatted in right way but blank lines are frustrating.
Something similar happens in *.js files.
Any idea?

Related

Put JavaScript variable into PHP with AJAX?

Here's a some of my JavaScript
function myFunction() {
var finalMove = "a1";
$.post("index.php", {postFinalMove:finalMove});
];
Here is my PHP code. The commented lines are just two of the things I've tried.
<?php
session_start();
$db = mysqli_connect("localhost","myUsername","myPassword","abraDB");
if (mysqli_connect_errno())
{
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
//if (isset($_POST['postFinalMove']))
//{
// $turn = ($db, $_POST['postFinalMove']);
// echo $turn;
//}
//if ($_POST['postFinalMove'])
//{
// $turn = ($db, $_POST['postFinalMove']);
// echo $turn;
//}
if ($_POST['logout'])
{
session_start();
$_SESSION = array();
session_destroy();
}
mysqli_close($db);
?>
As soon as I uncomment some of the code I've tried, my entire webpage is blank with no errors and no source code, which makes this hard for me to debug. Javascript function fires just fine, its my index.php page that crashes. PHP code was working great until this. Thanks in advance
You should be able to do the following in your PHP script:
if(isset($_POST['postFinalMove'])){
echo $_POST['postFinalMove'];
}
This line is probably causing the error:
// $turn = ($db, $_POST['postFinalMove']);
That statement does not make any sense. What are you trying to store in $turn? If you want to store the $_POST['postFinalMove'] you would have:
$turn = $_POST['postFinalMove'];
Take a look at the GIF image and you will see for yourself..!
Replace your code with this line as and you are good to go then :
$turn = $_POST['postFinalMove'];
echo $turn;
It's just a simple Syntax Issue nothing more than that Dear..! :D

Using Blade directives outside of templates

Laravel 5.1: I defined a few custom directives inside a BladeServiceProvider (example below). Now I would like to use them outside of a view template to format strings (I am writing an EXCEL file with PHPExcel in a custom ExportService class). Is it possible to reuse my directives?
Blade::directive('appFormatDate', function($expression) {
return "<?php
if (!is_null($expression)) {
echo date(\Config::get('custom.dateformat'), strtotime($expression));
}
else {
echo '-';
}
?>";
});
The BladeCompiler has a compileString method, which allows you to use the Blade directives outside the views. :)
So, you can do things like this:
$timestamp = '2015-11-10 17:41:53';
$result = Blade::compileString('#appFormatDate($timestamp)');
You can use this:
use Illuminate\Support\Facades\Blade;
$timestamp = '2023-01-28 12:41:53';
Blade::render("#appFormatDate({$timestamp})");

Laravel 4: include template with parameters in single line

I'm trying to save a blade template into a javascript variable but I can't figure out how to remove the line breaks from the template.
I looked at extending blade and created the following code based on BladeCompiler:compileInclude() which removes all line breaks, but only for templates without parameters.
Blade::extend(function($view, $compiler)
{
$pattern = $compiler->createMatcher('include_string');
return preg_replace($pattern, '$1<?php echo str_replace(array("\r","\n"), "", $__env->make($2, array_except(get_defined_vars(), array(\'__data\', \'__path\')))->render()); ?>', $view);
});
I know I could simply remove all line breaks from the template manually but I'm hoping there's a better way.
Has anyone done this before? Any help is appreciated.
I haven't found a way to create your own blade functions that can receive multiple values (or an array) and I don't think it is even possible by using "only" Blade::extend. Of course you could extend Laravel's BladeCompiler class and write much more powerful macros.
But instead I suggest you create a macro for removing the line breaks only.
Blade::extend(function($view, $compiler)
{
$pattern = $compiler->createMatcher('singleline');
return preg_replace($pattern, '$1<?php echo str_replace(array("\r","\n"), "", $2); ?>', $view);
});
And the use it either like this:
#singleline(View::make('view-name', array('foo' => 'bar')))
Or if you prefer the $__env syntax:
#singleline($__env->make('view-name', array('foo' => 'bar')))

php codeigniter uploading photos

So I was watching a video on how to upload photos using codeigniter. The link I used is here and the code is also at this site http://code.tutsplus.com/tutorials/codeigniter-from-scratch-file-uploading-and-image-manipulation--net-9452. I got everything to work however, when I tried to use the code on my own website I ran into a problem.
Basically all I changed was that I created a template for the view to be loaded in instead of just loading a single view. There is a controller Gallery.php file that looks like the follows.
<?php
class Gallery extends CI_Controller {
function index() {
$this->load->model('Gallery_model');
if ($this->input->post('upload')) {
$this->Gallery_model->do_upload();
}
$data['images'] = $this->Gallery_model->get_images();
$this->load->view('gallery_view',$data);
}
}
I simply changed this code to. Really only changing the last line and replacing it with those new three bottom lines.
<?php
class Gallery extends CI_Controller {
function index() {
$this->load->model('Gallery_model');
if ($this->input->post('upload')) {
$this->Gallery_model->do_upload();
}
$data['images'] = $this->Gallery_model->get_images();
$var = $this->load->view('gallery_view',$data,true);
$data['center_content'] = $var;
$this->load->view('includes_main/template',$data);
}
}
Now I get an strange error that I do not understand and no error shows up in the console log. Here is a picture of the error. http://i.imgur.com/tsKNj9W.png. The error says "unable to load the requested file" then doesn't have any file name after it. then there is a .php at the bottom of the page. I just don't have a clue what is giving me this error. I checked my template over again and again, but don't see anything wrong.
My template is as follows. I commented out everything just to make sure nothing else was giving me this error. So I am just left with one line.
<?php $this->load->view($center_content); ?>
Thanks for reading. Sorry I just have been stuck on this for a while and still haven't been able to fix it.
In this case there there are many mistakes. you did not load upload library and you also did not set config array. you can find on codeigniter user guide how to upload a file. I recommend to you read this link to upload a file in codeigniter
https://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html

Laravel 4: if statement in blade layout works strange

Could someone explain me why I get blank screen with printed string "#extends('layouts.default')" if I request page normally (not ajax)?
#if(!Request::ajax())
#extends('layouts.default')
#section('content')
#endif
Test
#if(!Request::ajax())
#stop
#endif
I'm trying to solve problem with Ajax, I don't want to create 2 templates for each request type and also I do want to use blade templates, so using controller layouts doesn't work for me. How can I do it in blade template? I was looking at this Laravel: how to render only one section of a template?
By the way. If I request it with ajax it works like it should.
Yes #extends has to be on line 1.
And I found solution for PJAX. At the beginning I was not sure this could solve my problem but it did. Don't know why I was afraid to lose blade functionality if you actually can't lose it this way. If someone is using PJAX and needs to use one template with and without layout this could be your solution:
protected $layout = 'layouts.default';
public function index()
{
if(Request::header('X-PJAX'))
{
return $view = View::make('home.index')
->with('title', 'index');
}
else
{
$this->layout->title = 'index';
$this->layout->content = View::make('home.index');
}
}
Try moving #extends to line 1 and you will see the blade template will render properly.
As for solving the ajax problem, I think it's better if you move the logic back to your controller.
Example:
…
if ( Request::ajax() )
{
return Response::eloquent($books);
} else {
return View::make('book.index')->with('books', $books);
}
…
Take a look at this thread for more info: http://forums.laravel.io/viewtopic.php?id=2508
You can still run your condition short handed in the fist line like so
#extends((Request::ajax())?"layout1":"layout2")

Resources