Using MVC I have a model directory inside of which are two files; menu.xml and the following model.php.
<?php
$xml = simplexml_load_file('menu.xml');
print_r ($xml);
?>
In my views directory I have an index.php files as follows:
<?php
include '../model/model.php';
print_r($xml);
foreach ($xml->children() as $option)
{
?>
<li><a href='<?= $option->getName() ?>'><?= $option['name'] ?></li>
<?php
}
?>
</ul>
In another directory - this time html - i have the following:
<!-- This is the controller -->
<?php
require_once('../includes/helpers.php');
// determine which page to render
if (isset($_GET['page']))
$page = $_GET['page'];
else
$page = 'index';
// show page
switch ($page)
{
case 'index';
render('templates/header', array('title' => 'Main Menu'));
render('index');
render('templates/footer');
break;
}
?>
This is supposed to show a list of the children of the menu.xml file. If I access model.php directly:
<pre>
<?php
$xml = simplexml_load_file('menu.xml');
print_r ($xml);
?>
</pre>
I can see the contents of the $xml object.
So why doesn't the include '../model/model.php command work?
Any tips appreciated!
Thanks
$xml = simplexml_load_file('menu.xml');
This path is relative to the current executed script. That’s why it works when your accessing model/model.php directly, but not when including it from ./index.php. Use absolute path instead:
$xml = simplexml_load_file(__DIR__ . '/menu.xml');
Edit by IMSoP:
For PHP versions older than 5.3:
$xml = simplexml_load_file(dirname(__FILE__) . '/menu.xml');
Related
I'm trying to parse the site, but the cat doesn't output anything
<?php
include_once 'simple_html_dom.php';
$html = file_get_html('https://teleprogramma.pro/headlines');
foreach($html->find('.text-part') as $element) {
echo $element->outertext;
}
?>
There are no Elements in the document which match the class .text-part. You can look at the source code when you save the HTML into a file.
<?php
include_once 'simple_html_dom.php';
$html = file_get_html('https://teleprogramma.pro/headlines');
file_put_contents('htmlData.html', $html);
When you try for example to find .block-top-section-posts you'll get a result.
<?php
include_once 'simplehtmldom_1_9_1/simple_html_dom.php';
$html = file_get_html('https://teleprogramma.pro/headlines');
foreach($html->find('.block-top-section-posts') as $element) {
echo $element->outertext;
}
// Outputs
/*
<div class="vue-container block-top-section-posts"> <div id="vuetag-top-section-posts" class="vue-tag news-line" data-url="/rest/term-additional-loading/section/76217/0/0" data-max-width="0" data-min-width="0" data-information=""> </div> </div>
*/
When you lookup the Site in a Browser you will get redirected to another URL. If you want to use that, have a look at php get url of redirect from source url to get the final address.
I'm currently having an issue, where I have some php code in a file and calling that file from another file.
so:
index.php is calling file.php and file.php has some php code in it.
The reason I'm using file_get_contents, is that I have code in index.php that only needs to read some of the content in file.php, designated by tags. Based on teh section tag trigged by the ($_GET['identifier']); that section within the file.php is the only section displayed
Example code in file.php:
<?php
//simplified php code
var $item = "hello";
var $item2 = "Hola";
var $item3 = "おはよ";
?>
<section id="content">
<?php echo $item1; ?>
</section>
<section id="content2">
<?php echo $item2; ?>
</section>
<section id="content3">
<?php echo $item3; ?>
</section>
?>
code in index.php:
$content = file_get_contents('file.php');
if (isset($_GET['item'])) {
$section = $_GET['item'];
} else {
$section = 'content';
}
$delimiter = '<section id="' . $section . '">';
$start = explode($delimiter, $content);
$end = explode("</section>", $start[ 1 ] );
echo $end[0];
so if the browser url shows index.php?item=content2 , it should show the content from section ID named content2, along with the PHP code in that file, executed.
currently, if I do this, nothing is displayed, and a view source, show the php code
To execute the code before you get the contents, you either need to include and capture the output:
ob_start();
include('file.php');
$content = ob_get_clean();
Or get the file by URL (probably not the best idea and not portable):
$content = file_get_contents('http://example.com/file.php');
So I would like to print name of the current page in the title tag of the head.
I include my head in every page like this:
include 'includes/head.php';
This is my head:
<head>
<?php $page = basename(__FILE__, '.php'); ?>
<title><?php echo ucfirst($page); ?><title>
</head>
I thought this would work, but now it just shows "Head" on every page.
I know I can make it work by just putting the $page variable on every page but I would like to prevent this.
So is there any way to print the name of the current page through the included head.php file without adding anything to every page?
Thanks
EDIT
This is how I fixed it:
$page = pathinfo($_SERVER['SCRIPT_NAME'],PATHINFO_FILENAME);
If you were to create a file head.php with the following content
<?php
$xpage = pathinfo( $_SERVER['SCRIPT_NAME'],PATHINFO_BASENAME );
$ypage = pathinfo( $_SERVER['SCRIPT_NAME'],PATHINFO_FILENAME );
echo "
<!--
with extension
{$xpage}
without extension
{$ypage}
-->";
?>
and include in your regular php pages using include '/path/to/head.php' you should get the desired result ~ you will see two options - with or without file extension.
To add this to the document title simply echo whichever option is preferrable
<title><?php echo $ypage;?></title>
Try this enclosing the $page variable in php tags:
<head>
<?php $page = basename(__FILE__, '.php'); ?>
<title><?php echo ucfirst($page); ?><title>
</head>
You have several problems, first one is curly bracket in front of the echo, the second one is that end title tag is missing forward slash and probably last one is that page variable is not inside php tags...
So your code should look like:
<?php $page = basename(__FILE__, '.php'); ?>
<title><?php echo ucfirst($page); ?></title>
You could use a function like this:
head.php
<?php
function head($page) {
echo "<head>";
echo "<title>".ucfirst($page)."<title>";
echo "</head>"
}
index.php
<?php
include 'includes/head.php';
$page = basename(__FILE__, '.php');
head(page);
I was trying to follow the tutorials found on: https://www.ibm.com/developerworks/xml/library/x-phpwikipedia/index.html
All tutorials use Zend framework. I was wondering how this code can be adapted to avoid the use of Zend (or any other prerequisites) if possible? Please see example below. Thanks.
<?php
// load Zend classes
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Rest_Client');
// define category prefix
$prefix = 'hollywood';
try {
// initialize REST client
$wikipedia = new Zend_Rest_Client('http://en.wikipedia.org/w/api.php');
// set query parameters
$wikipedia->action('query');
$wikipedia->list('allcategories');
$wikipedia->acprefix($prefix);
$wikipedia->format('xml');
// perform request
// iterate over XML result set
$result = $wikipedia->get();
} catch (Exception $e) {
die('ERROR: ' . $e->getMessage());
}
?>
<html>
<head></head>
<body>
<h2>Search results for categories starting with
'<?php echo $prefix; ?>'</h2>
<ol>
<?php foreach ($result->query->allcategories->c as $c): ?>
<li><a href="http://www.wikipedia.org/wiki/Category:
<?php echo $c; ?>"><?php echo $c; ?></a></li>
<?php endforeach; ?>
</ol>
</body>
</html>
Something like this should work:
<?php
$prefix = 'allcategories';
$url = "http://en.wikipedia.org/w/api.php?action=query&list={$prefix}&format=json&continue=";
$res = file_get_contents($url);
$data = json_decode($res);
?>
<html>
<head></head>
<body>
<h2>Search results for categories starting with
'<?php echo $prefix; ?>'</h2>
<ol>
<?php foreach ($data->query->allcategories as $c):
?>
<li><?php echo $c->{'*'}; ?></li>
<?php endforeach; ?>
</ol>
</body>
</html>
In this case request will be using json format, its easier to live with :)
As the IBM article mentions, the Wiki API by default returns results in XML but can also return results in JSON, WDDX, YAML, or PHP serialized.
Basically, you just need to make an HTTP request that requests the correct information and parses the returned results. For example click this link http://en.wikipedia.org/w/api.php?action=query&list=allcategories&acprop=size%20&acprefix=hollywood&format=xml and you see it returns XML results in the browser which you can pretty much use any language to loop over and parse.
To request the contents of the xml data in PHP you could use something file_get_contents($target_url); and then you wouldn't have to rely on creating a zend rest client.
Ok so I am making a website on each page where I want to include a file sidebar.php .
In sidebar.php I would like to echo the name of file that included sidebar.php .
Below are contents of sidebar.php, they return 'sidebar'.
<?php
$file = basename(__FILE__, '.php');
echo $file;
;?>
I found a similar question but the whole point is that I don't have to make no variables in on each page.
Excuse me for vague use of word 'include', I am using the it as the statement in php.
You could pass it to the sidebar page through a session variable:
<?php
session_start();
$_SESSION['filename'] = "thisFilesName.php";
include('../sidebar.php');
?>
sidebar.php:
<?php
session_start();
echo $_SESSION['filename'];
?>