I followed one of the basic tutorials on the knockout.js website and now I'm just trying to make it work on localhost. In the head, I have included two files: knockout.js and misc_form.js.
<head>
<script src="/knockout.js"></script>
<script src="/misc_form.js"></script>
</head>
I'm going to omit the code for misc_form.js because I didn't modify it and it works 100% on the live knockout.js tutorial.
Here's the HTML
//form.php
function display_form(){
?>
<table>
<thead><tr>
<th>Passenger name</th><th>Meal</th><th>Surcharge</th><th></th>
</tr></thead>
<!-- Todo: Generate table body -->
<tbody data-bind="foreach: seats">
<tr>
<td><input data-bind="value: name" /></td>
<td><select data-bind="options: $root.availableMeals, value: meal, optionsText: 'mealName'"></select></td>
<td data-bind="text: formattedPrice"></td>
<td>Remove</td>
</tr>
</tbody>
</table>
<button data-bind="click: addSeat">Reserve another seat</button>
<?
}
Edit - I changed the path to the files and now I'm getting the following error
Uncaught TypeError: Cannot read property 'nodeType' of null //in reference to knockout.js
Check the paths for your script files, those look strange. Try visiting http://localhost/Applications/XAMPP/xamppfiles/htdocs/misc_form.js in a web browser, do you see your JavaScript code?
From the urls, I'm guessing they should be:
<head>
<script src="/knockout.js"></script>
<script src="/misc_form.js"></script>
</head>
since htdocs is likely the folder which gets served up as /.
Related
Is is possibile to extract a piece of a twig template?
I need to manage a table ajax refresh, I have a twig template for example:
<html>
<body>
<table>
<thead>
<tr><th></th></tr>
</thead>
<tbody id="mypiece">
<tr><th></th></tr>
<tr><th></th></tr>
</tbody>
</table>
</body>
</html>
In the first load I need the whole template, via ajax I need just the #mypiece content, is it possibile to extract it from twig using the DOM id or with some other markers?
The only solution I found is to divide this in two different template and use an include steatment.
whole.html
<html>
<body>
<table>
<thead>
<tr><th></th></tr>
</thead>
<tbody id="mypiece">
{% include 'content.html' %}
</tbody>
</table>
</body>
</html>
content.html
<tr><th></th></tr>
<tr><th></th></tr>
But I think this is really a bad solution...
Well, I personally prefer do actually divide them, but if you intend to get something with AJAX as well - try using embeded controllers (in this case, specifically for your XHR request), for example:
<tbody id="mypiece">
{{ render(controller(
'SomeBundle:SomeController:someAction',
{ 'someParameter': "something" }
)) }}
</tbody>
This is a lot better then to parse some rendered template to get part of it, because to me it seems like design flow.
Even better solution is to return specified json data on ajax call and render it in one of the javascript template engines on the client side.
Hope this helps, cheers.
I'm trying to call a php snippet to load a miniblog.
The code provided by the site is:
<?php
define('IN_BLOG', true);
define('PATH', '../miniblog');
include('includes/miniblog.php');
?>
I have tried calling it both in index.php and sidebar.php in the table cell I want it to go in, in the header, and in the body.
I keep getting warnings like:
Warning: include(includes/miniblog.php) [function.include]: failed to open stream: No such file or directory in /home/a1822199/public_html/index.php on line 32
EDIT: I was asked to include some code, this is my index page. I have installed the plug in as the site says, and it's accepting blog posts.
<body>
<table width="75%" align="center" id="container">
<tr>
<td>
<?php include 'header.php'; ?>
</td>
</tr>
<tr>
<td width="25%">
<?php
define('IN_BLOG', true);
define('PATH', '../miniblog');
include('includes/miniblog.php');
?>
<?php include 'sidebar.php'; ?></td>
<td width="75%"></td>
</tr>
<tr>
<td colspan="3">
<?php include 'footer.php'; ?>
</td>
</tr>
</table>
</body>
EDIT 2:
It seems to be searching in index.php as if IT ITSELF is a folder.
You must use include('miniblog/includes/miniblog.php');
see /home/a1822199/public_html/index.php this is your path to index.php and I think
`/home/a1822199/public_html/`
will be the path to includes folder. And you says that the file situated in
`miniblog/includes/miniblog.php`
pleasse give the absolute path like
`http://localhost/miniblog/includes/miniblog.php`
If the directory structure is like this.
For example my site is mysite.com. Here is source of this site:
<html>
<head>
<title>site</title>
//here is many javascript and css codes
</head>
<body>
<div>
<table border="1">
<tr>
<td><a href="somthing.html">Here is a text</td>
<td><img src="image.gif" alt="this is image"/></td>
</tr>
</table>
</div>
</body>
</html>
How can I using php get only text and image without all tags(javascript codes, links, tables and other)?
I want get only "here is a text" and "image.gif".
Use PHP cURL if the file is on the internet else you can use the file_get_contents() function if the file on the local machine.
To get rid of the extra tags you can use the code:
$contents - file_get_contents('file.html');
$contents = strip_tags( $contents, '<img>' ); //other than <img> you can specify more tags also
Alternatively you can use the DOM approach also.
I need some help. I am working on a backend of a website and I want to create functions where I can re-use the same code instead of duplicating it.
So I will have certain "sections" that contain information, such as:
getQuickInfo
function getQuickInfo() {
echo '
<div class="portlet">
<div class="portlet-header">
<h4>Quick Info</h4>
</div>
<div class="portlet-content">
<table cellspacing="0" class="info_table">
<tbody>
<tr>
<td class="value">'.getCount("total_users").'</td>
<td class="full">Total Users</td>
</tr>
<tr>
<td class="value">'.getCount("count_open_requests").'</td>
<td class="full">Open Support Requests</td>
</tr>
</tbody>
</table>
</div>
</div>
';
}
Now I know I am approaching this the wrong way, that's why I am posting this question. But that is just an example of a Quck Info section I would like to re-use on other pages, so I can sitck it wherever I want and just do getQuickInfo()
For this example, that function works fine. I would like suggestions on a better way to do it, and also for some other sections it won't be that easy it will have some Mysql queries and grabbing information from a database, which I can't store that within an echo.
How does everyone else accomplish stuff like this?
My main goal is to be able to output sections wherever I would like:
getQuickInfo()
getAdminNotes()
getSupportRequests()
etc.
Thanks!
Much as I am loathed to mix raw HTML with PHP code in templates, I know others disagree with me and this is one place where it might be a valid use.
So you would create a file that looks like this:
quickinfo.php
<div class="portlet">
<div class="portlet-header">
<h4>Quick Info</h4>
</div>
<div class="portlet-content">
<table cellspacing="0" class="info_table">
<tbody>
<tr>
<td class="value"><?php echo getCount("total_users"); ?></td>
<td class="full">Total Users</td>
</tr>
<tr>
<td class="value"><?php echo getCount("count_open_requests"); ?></td>
<td class="full">Open Support Requests</td>
</tr>
</tbody>
</table>
</div>
</div>
...and in you main page, you can just do:
include('quickinfo.php');
You can easily adapt this to use the result of queries. You would simply perform the query in the main page, and assign the results to a named variable. Then in the template page you use the data in the named variable to generate the page. This means that you can use the same code to generate different results based on different queries - as long as the result are held in the same named variable.
If you're using php 5 you should considered using PHP's object orientation support.
http://php.net/manual/en/language.oop5.php
This way you can create a class which has methods that print out stuff and hold values and then instantiate this class and call it's methods as needed.
I'm no php guru, but maybe you should create separate .php files for each of those sections (something like quickinfo.php, adminnotes.php, etc) put your html layout inside, and maybe even the php functions you need (or you could put them in a separate file, this depends on whether the logic you need is too complex, if it's fairly simple you can put everything in the same file)
Then you would do something like include('quickinfo.php') wherever you want to use those sections.
I'm developing a site that's pretty lightweight on the interface and mostly database driven (equal amounts read and write). It's written in PHP and I've found the easiest way to make each page is:
Page:
<?php include("header-nav.php"); ?>
<table>
<tr>
<th>Column 1</th>
<th>Column 2</th>
</tr>
<tr>
<td>Data Point 1</td>
<td>Data Point 2</td>
</tr>
<tr>
<td>Data Point 3</td>
<td>Data Point 4</td>
</tr>
</table>
<?php include("footer.php"); ?>
header-nav.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Welcome</title>
<link rel="stylesheet" type="text/css" href="assets/style.css" />
</head>
<?php include("db_connect.php"); ?>
<body>
<div id="wrapper">
<h1>Welcome</h1>
<ul id="nav">
<li>Home</li>
<li>Data</li>
</ul>
footer.php:
</div>
</body>
<?php mysql_close($dbc); ?>
</html>
All of these pages by themselves are not valid and may produce errors. Working together to create a whole page, they look great.
My question is: Would this be an efficient and easy to maintain solution?
It seems that just adding a couple includes (one at the top and one at the bottom) makes sense. Especially because the site is so lightweight that I don't need a web framework. It just feels uncomfortable to create pages without starting with <html><head>...etc.
This is definitely an okay thing. I would highly recommend it. This way if you need to change the header or anything you can do so in once place easily. And the read time for hitting the file system for the include really isn't that big of a concern. So I would say that this is definitely acceptable.
"All of these pages by themselves are not valid" - I'm not sure what you mean by this. You mean a HTML Validator wouldn't pass them? Well of course not - they are fragments of pages. What matters is what the validator says when ran against the HTML the executed PHP generates.
This is one approach, and depending on the size of the problem you're tackling it's a valid one.
Yes, IMO this is a perfectly good way to do things, especially for a small site. Done it myself many times.
Get over your discomfort. Most IDEs (Dreamweaver comes to mind) actually support this way of developing sites, and will display content correctly and honor the includes if you prefer a WYSIWYG.
I develop sites using includes, like so:
site-header.inc:
require_once 'html-header.inc';
<div id="header">
/* menus, navigation, etc. */
</div>
<div class="content">
site-footer.inc:
</div>
<div id="header">
/* menus, navigation, etc. */
</div>
require_once 'html-footer.inc';
Where "html-header.inc" and "html-footer.inc" are your HTML header and footer tags and elements (title, meta, etc.). I then have functions to allow me to add CSS, JavaScript, titles, anywhere on the page, and use ob_start() and ob_end_flush() to handle these in the footer, actually. e.g.
stylesheet_register($path, $media="screen", $type="text/css");
javascript_register($path, $type="text/javascript");
title_set($title, $overwrite=true);
It your basic concept of abstraction: Don't write the same "header" and "footer" HTML code twice. Same applies to any PHP functionality which can be easily abstracted away and decoupled. Best of luck.