I will close my first question and will asked here again, a little bit more structured.
I have a database (mysql/mariadb) from which I will show the datas in the tabulator-table.
When I store the incoming data at first in a json-file, it looks good. When I used the json_encode($output) directly I get an error.
First case:
My index.php:
<?php
require 'php/dbCon.php';
?>
<!DOCTYPE HTML>
<html>
<link rel="stylesheet"
href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-
ui.css">
<link rel="stylesheet" href="css/tabulator.css" type="text/css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js">
</script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.7.7/handlebars.min.js">
</script>
<script src="js/jquery-ui.js"></script>
<script src="js/tabulator.js"></script>
<script src="js/tabulator.min.js"></script>
<script src="js/jquery_wrapper.js"></script>
<script src="js/tt_kcal.js"></script>
<head>
<h2>Testseite</h2>
</head>
<body>
Tabellendaten:
<div id="example-table"></div>
</body>
</html>
My query php - case 1 -
<?php
include 'php/dbCon.php';
?>
<?php
$myQuery = "SELECT * FROM `kcaltab` WHERE `id`<=25";
$result = $conn->query($myQuery);
$output = array();
$output = $result->fetch_all(MYSQLI_ASSOC);
$sresult = json_encode($output);
$fp = fopen('json_kcaltab.json', 'w');
fwrite($fp, json_encode($output, JSON_PRETTY_PRINT));
?>
my js-file - case 1 -
$(document).ready(function() {
var table = new Tabulator("#example-table", {
data:"json_kcaltab.json",
layout:"fitColumns",
placeholder:"warte auf Daten",
columns: [
{title:"ID", field:"ID"},
{title:"Lebensmittel", field:"Lebensmittel", hozAlign:"left"},
{title:"Bemerkungen", field:"Bemerkungen"},
{title:"Einheit", field:"Einheit", hozAlign:"center"},
{title:"Kcal", field:"Einheit", hozAlign:"center"},
{title:"KH", field:"Einheit", hozAlign:"center"},
{title:"Eiweiss", field:"Einheit", hozAlign:"center"},
{title:"Fett", field:"Einheit", hozAlign:"center"},
{title:"ELgr", field:"Einheit", hozAlign:"center"},
{title:"TLgr", field:"Einheit", hozAlign:"center"},
{title:"eindat", field:"eindat", hozAlign:"center"},
],
});
});
the result:
enter image description here
By the second case, only the query-php-file and the js-file will changed a little bit:
case 2 - query:
<?php
include 'php/dbCon.php';
?>
<?php
$myQuery = "SELECT * FROM `kcaltab` WHERE `id`<=25";
$result = $conn->query($myQuery);
$output = array();
$output = $result->fetch_all(MYSQLI_ASSOC);
echo json_encode($output, JSON_PRETTY_PRINT);
?>
js-file - case 2 -
$(document).ready(function() {
var table = new Tabulator("#example-table", {
data:"mySql_kcalgesamt.php",
layout:"fitColumns",
placeholder:"warte auf Daten",
columns: [
{title:"ID", field:"ID"},
{title:"Lebensmittel", field:"Lebensmittel", hozAlign:"left"},
{title:"Bemerkungen", field:"Bemerkungen"},
{title:"Einheit", field:"Einheit", hozAlign:"center"},
{title:"Kcal", field:"Einheit", hozAlign:"center"},
{title:"KH", field:"Einheit", hozAlign:"center"},
{title:"Eiweiss", field:"Einheit", hozAlign:"center"},
{title:"Fett", field:"Einheit", hozAlign:"center"},
{title:"ELgr", field:"Einheit", hozAlign:"center"},
{title:"TLgr", field:"Einheit", hozAlign:"center"},
{title:"eindat", field:"eindat", hozAlign:"center"},
],
});
});
and the result comes with an error:
enter image description here
I would be very excited if someone can help me (and trying to explain the mistake I make to myself). Thanks a lot.
Dieter Ascheberg
Hi, now I have changed my js-file as Double H. said as follows:
".....
ajaxURL: "mySql_kcalgesamt.php",
ajaxConfig:"GET",
ajaxContentType:"json",
......"
and now I get the following error message in the web.
enter image description here
but in the "Rapid PHP Editor 2020" everything works fine.
You can see the site at wss-sh.de.
Had someone a solution for me?
2022.02.12
Now, everything works fine in my development area, only when I bring that to my server I get the json-error. Now I think it could be, that my json-file had a empty line or a space at the beginning (see the picture enter image description here, which is the reason for the error message.
Can anyone help me, to clean up this error?
Thanks in advance.
Dieter
Ok, all what I must do is to delete the
"<?php" and the "?>"
at the beginning of the query-php-file. Then the json had no space or empty line. Thanks TK1234 at the php.de/forum for the solution.
Related
I wish to add some script to a view and have it que and load at the bottom of the page within the default layout. At the moment it runs before query is loaded and causes an error.
I am using cakephp 2.5
Is there a way to buffer it?
myview.ctp:
<script type="text/javascript">
FWDEVPUtils.onReady(function(){
FWDEVPlayer.useYoutube = "yes";
FWDEVPlayer.videoStartBehaviour = "pause";
new FWDEVPlayer({
//main settings
instanceName:"player",
parentId:"player",
mainFolderPath:"content"
});
});
default.ctp
echo $this->Html->script('//code.jquery.com/jquery-1.10.2.min.js');
echo $this->Html->script('bootstrap.min');
echo $this->Html->script('FWDEVPlayer');
echo $this->fetch('script');
echo $this->Js->writeBuffer();
In myview.ctp:
<?php $this->start('script'); ?>
<script type="text/javascript">
FWDEVPUtils.onReady(function(){
FWDEVPlayer.useYoutube = "yes";
FWDEVPlayer.videoStartBehaviour = "pause";
new FWDEVPlayer({
//main settings
instanceName:"player",
parentId:"player",
mainFolderPath:"content"
});
});
<?php $this->end(); ?>
You script will then be included in your layout default.ctp where you wrote:
echo $this->fetch('script');
I'm turning a well-working piece of my CakePHP (v 2.44) application into a plugin, and I'm getting the strangest behavior when throwing exceptions from within a controller in my plugin: the exception handler/renderer is starting to use my main site's layout in app/View/Layouts/mylayout.ctp, and then interrupts it with the default layout from app/View/Layouts/error.ctp. Here's an extract:
<div><ul><li class="jsdnavpopup blog-menu-categories">
<a href='/blog/categories'>All Categories</a><nav>
<!DOCTYPE html PUBLIC "{trimmed for space}">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CakePHP: the rapid development php framework:Error Page</title>
If you notice where it looks like a new header is sent right in the middle of Cake's composing the layout. Right at the point where my layout is interrupted I load am element, and the element uses requestAction (no caching in my dev environment). If I remove the element, then my layout is rendered even more until the next element using requestAction is encountered. These requestAction calls aren't requesting an action from the plugin that raised the exception.
I'm using all of the default error handlers from app/Config/core.php. The only thing that's different is specifying $this->layout in the error views.
Now if I recreate the original error layouts and views in the plugin's View folder, things work like I would expect. But for experimental purposes I put a renamed copy of my main site's layout there, and the same thing. Any time an element using requestAction is encountered the exception interrupts the layout.
Any ideas?
I'll reiterate that everything works perfectly when throwing exceptions from anywhere in the app that's not a plugin.
Here's how I do it (as requested in the comments on the OP's question)
Inside my layout, I put the following piece of code where I want the top menu to appear:
<?php echo $this->element('Menu.top_navigation'); ?>
This is quite simple, and allows the HTML designers in my team to put the menu wherever they want to.
Of course there is more happening under the hood. Here are the other files you'll need to make this work:
Plugins/Menu/View/Elements/top_navigation.ctp:
$thisMenu = ClassRegistry::init('Menu.Menu');
$menus = $thisMenu->display_menu_widget(array('position'=> 'top'));
echo $this->element('Menu.display_ul', array('menus' => $menus));
Note that this is just a wrapper -- you can make more of these for footer navigation, or even make an element that allows you to pass in the position as an argument.
Plugins/Menu/View/Elements/display_ul.ctp:
<?php
if(empty($depth)){
$depth = 0;
}
switch($depth){
case 0:
$classes = "dropdown";
break;
default:
$classes = "";
break;
}
?>
<ul class="<?php echo $classes ?> depth_<?php echo $depth ?>">
<?php foreach ($menus as $menu): ?>
<li>
<a href="<?php echo $menu['MenuItem']['url']; ?>">
<?php echo $menu['MenuItem']['title']; ?>
</a>
<?php
if(count($menu['children']) > 0){
echo $this->element(
'Menu.display_ul',
array(
'menus' => $menu['children'],
'depth' => $depth + 1
)
);
}
?>
</li>
<?php endforeach; ?>
</ul>
Plugins/Menu/Model/Menu.php:
/**
* display_menu_widget method
*
* #param array $options
* #return void
*/
public function display_menu_widget($options = array()) {
$defaults = array(
'position' => 'top'
);
$settings = array_merge($defaults, $options);
$this->recursive = 0;
$menuItems = array();
$conditions = array(
'Menu.position' => $settings['position']
);
$menuDetails = $this->find('first', array('recursive' => -1, 'conditions' => $conditions));
$menuPosition = $menuDetails[$this->alias]['position'];
$parentId = $menuDetails[$this->alias][$this->primaryKey];
$conditions = array(
$this->MenuItem->alias . '.menu_id' => $parentId
);
$this->MenuItem->recursive = 0;
$cacheName = 'default' . 'ModelMenuItem';
$cacheKey = $this->generateCacheName(array('type' => 'threaded', 'conditions' => $conditions));
$menuItems = Cache::read($cacheKey, $cacheName);
if(empty($menuItems) || PWFunctions::forceCacheFlush()){
$menuItems = $this->MenuItem->find('threaded', array('conditions' => $conditions));
Cache::write($cacheKey, $menuItems, $cacheName);
}
return $menuItems;
}
There's probably more in that menu function than you need, I'm using some aggressive caching routines to reduce database load, I'd suggest only using the bare bones of that function before worrying about caching.
This solution works well for me, and I think that it's the solution that violates the MVC concepts the least -- when you, as a designer, are in HTML mode and think "Hey I need to display the top menu" then it's just one line in your view, and you're not having to worry about modifying controllers or models.
I've used this pattern throughout my current CakePHP projects, and it seems to work quite well.
I am trying to create the smallest possible GAE app to show the use of the channel api.
I have two handlers in the python, the first "TestPage" sends out the html shown below. The second "SendPage" tries to send a message over the channel to the test page. The code for the TestPage is
class TestPage(Handler):
def get(self):
token = channel.create_channel("1")
self.render("test.html", token = token)
It just creates the channel with an id of "1" and reders the page with the token passed back from create_channel().
The SendPage is just:
class SendPage(Handler):
def get(self):
channel.send_message("1", "hello")
self.write("sent hello to 1")
The html is as small as I could get it:
<!DOCTYPE HTML>
<html>
<body>
<br>Token is {{ token }}
<br>
<div id="debug">_</div>
<!--
<script src="https://talkgadget.google.com/talkgadget/channel.js"></script>
-->
<script src="static/channel.js"></script>
<script defer="defer">
function debug(s) {
document.getElementById("debug").innerHTML = s;
}
var channel = new goog.appengine.Channel( {{ token }} );
var socket = channel.open();
socket.onopen = function(e) {
debug("open");
}
socket.onclose = function(e) {
debug("close");
}
socket.onerror = function(e) {
debug("error");
}
socket.onmessage = function(e) {
debug("message");
}
debug("ready");
</script>
</body>
</html>
So, inside chrome I pull up TestPage and I see the "ready" message. Then I pull up the SendPage in another tab. And see the "sent message". Then when I go back to the TestPage I would expect to have "ready" replaced by "message". But this never happens. None of the socket handler functions are being called.
I'm stuck for the moment and would appreciate any help or suggestions.
Thank you.
Ok, I figured it out. There were two problems. First, the template line
var channel = new goog.appengine.Channel( {{ token }} );
should have been
var channel = new goog.appengine.Channel( "{{token}}" );
as it was token was something like "channel-2052893164-1373347311-1" which quietly evaluated to a number.
Second, the correct script for the channel.js was
<script type="text/javascript" src="/_ah/channel/jsapi"></script>
The other scripts I had referenced were from other stack overflow answers and I guess they did not properly apply to this problem.
Thanks.
hi all I'm trying to create an invoicing system however when i goto the related url i get a blank page and header just being the url. I've followed the following tutorial [http://bakery.cakephp.org/articles/kalileo/2010/06/08/creating-pdf-files-with-cakephp-and-tcpdf]
here is my viewPdf function in the invoices controller
function viewPdf($id = null)
{
if (!$id)
{
$this->Session->setFlash('Sorry, there was no property ID submitted.');
$this->redirect(array('action'=>'index_admin'), null, true);
}
Configure::write('debug',0); // Otherwise we cannot use this method while developing
$id = intval($id);
$property = $this->__view($id); // here the data is pulled from the database and set for the view
if (empty($property))
{
$this->Session->setFlash('Sorry, there is no property with the submitted ID.');
$this->redirect(array('action'=>'index'), null, true);
}
$this->layout = 'pdf'; //this will use the pdf.ctp layout
$this->render();
}
//End of Controller
}
and here is my viewPdf view
<?php
App::import('Vendor','xtcpdf');
$tcpdf = new XTCPDF();
$textfont = 'freesans'; // looks better, finer, and more condensed than 'dejavusans'
$fpdf->xheadertext = 'YOUR ORGANIZATION';
$tcpdf->SetAuthor("KBS Homes & Properties at http://kbs-properties.com");
$tcpdf->SetAutoPageBreak( false );
$tcpdf->setHeaderFont(array($textfont,'',40));
$tcpdf->xheadercolor = array(150,0,0);
$tcpdf->xheadertext = 'KBS Homes & Properties';
$tcpdf->xfootertext = 'Copyright © %d KBS Homes & Properties. All rights reserved.';
// add a page (required with recent versions of tcpdf)
$tcpdf->AddPage();
// Now you position and print your page content
// example:
$tcpdf->SetTextColor(0, 0, 0);
$tcpdf->SetFont($textfont,'B',20);
$tcpdf->Cell(0,14, "Hello World", 0,1,'L');
// ...
// etc.
// see the TCPDF examples
echo $tcpdf->Output('filename.pdf', 'D');
?>
to my knowledge it should be creating a pdf file that has the words 'hello world' written in it.
after following advice about that being outdated, I used the link to github and have this in my controller now
public function view($id = null) {
$this->set('title_for_layout', 'Invoices');
$this->set('stylesheet_used', 'homestyle');
$this->set('image_used', 'eBOXLogoHome.png');
$this->layout='home_layout';
$this->Invoice->id = $id;
if (!$this->Invoice->exists()) {
throw new NotFoundException(__('Invalid invoice'));
}
$this->pdfConfig = array(
'orientation' => 'potrait',
'filename' => 'Invoice_' . $id
);
$this->set('invoice', $this->Invoice->read(null, $id));
}
}
and my view.ctp
<html>
<head></head>
<title></title>
<body>
<?php $this->pdfConfig = array('engine' => 'CakePdf.WkHtmlToPdf') ?>
<?php echo $invoice; ?>
</body>
</html>
it prints out Array but does not render it as a pdf file
here is a link to the github pagw
https://github.com/ceeram/CakePdf
Use this plugin, setup is on github page ;)
https://github.com/ceeram/CakePdf
this is an obsolete tutorial, this is a tutorial for Cakephp 1.2 and you are using 2.1.
It's much better the code from the link of GitHub that Matus Edko gave you.
By the way, is always better to show all the error and notifications when you are developing and testing, by setting up de "debug" to 3.
Configure::write('debug',3);
I configured jenkins in order to automating the tests for my project. The unit tests work well. Now I want to do some test with my database. For example the login test: enter the username, the password (submit the form). Do you have an idea of how to do this? Thank you.
Here is the test I want to do but it does not work:
Get the home page, test if the link FAQ is present two times, click on the first FAQ link, test if the link RMK is present, click on login, test if forgot password link is present, enter my username and my password submit the form, get the profile page, test if My Profile link is present.
<?php
namespace RMK\SocialBundle\Tests\FunctionalTests\Controller\Website;
use RMK\SocialBundle\Controller\Website;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Bundle\FrameworkBundle\Controller\RedirectController;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser;
class HomeControllerTest extends WebTestCase
{
public function testViewFAQ(){
$client = static::createClient();
$crawler = $client->request('GET', '/home');
$this->assertEquals(2, $crawler->filter('a:contains("FAQ")')->count());
$this->assertEquals(2, $crawler->filter('a:contains("FAQ")')->count());
$faqlink = $crawler->filter('a:contains("FAQ")')->eq(0)->link();
$crawler = $client->click($faqlink);
$this->assertEquals(1, $crawler->filter('a:contains("RMK")')->count());
$loginLink = $crawler->filter('a:contains("Login")')->eq(0)->link();
$crawler = $client->click($loginLink);
$this->assertEquals(1, $crawler->filter('a:contains("Forgot Password?")')->count());
$form = $crawler->selectButton('_submit')->form();
$form['_username'] = 'username';
$form['_password'] = 'test';
$crawler = $client->submit($form);
$crawler = $client->followRedirect();
$crawler = $client->request('GET', '/profile');
$this->assertEquals(1, $crawler->filter('a:contains("My Profile")')->count());
}
}
?>
The last assert failed by saying: Failed asserting that 0 matches expected 1. When I put var_dump(print_r($client->getResponse()->getContent())); just before this last assert I got this:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="refresh" content="1;url=/home" />
<title>Redirecting to /home</title>
</head>
<body>
Redirecting to /home.
</body>
</html>bool(true)
I am sure I am missed something but I don't know what. Do you have an idea? Thx.
P.S: I am on Ubuntu 12.04.