Unset XML element function not working - php

This is what I have.
$filename= asql($_GET['filename']);
$fullfile = "xml/".$filename;
function delete_book_id($ids){
$data = simplexml_load_file($fullfile);
$data_count = count($data->item);
for($i = 0; $i < $data_count; $i++)
{
//basically what you want to remove
if(($data->item[$i]->id == $ids))
{
unset($data->item[$i]);
}
}
file_put_contents($fullfile, $data->saveXML());
}
lets say $fullfile is xml/name.xml and the file exists in our folder. Where the variable is called in the function it should work right?
If I replace the variable in the function with xml/name.xml it will work, but using the variable causes the page to break and not reload nor will it remove the line it is supposed to unset. Will the function not accept variables, or am I missing something here?
I have also tried using "xml/".$filename in place of the variable in the function. No luck there either.

$fullfile is defined outside of the function. It's undefined inside of it. Use global $fullfile; within function or define it there.

Related

how to require a php file passing parameters

I have a function that does something (and it is included in my files php).
That function should require a php file passing parameters, but it fails and I'm not able to go ahead...
following the initial code of the function:
<?php
function write_pdf($orientation, $initrow, $rowsperpage)
{
ob_start();
require "./mypage.php?orient=$orientation&init=$initrow&nrrows=$rowsperpage";
$html = ob_get_clean();
$dompdf = new Dompdf();
$dompdf->loadHtml($html);
...
...
the "mypage.php" returns the errors:
Notice:Undefined variable: orientation in
C:\wamp\www\htdocs\site\mypage.php on line 8
Notice:Undefined variable: initrow in
C:\wamp\www\htdocs\site\mypage.php on line 8
Notice:Undefined variable: rowsperpage in
C:\wamp\www\htdocs\site\mypage.php on line 8
is there a way to do something like that?
thanks!
You don't need to pass the parameters, as when you require the file is like its code where inside the function, so any variable defined in the function before the require, it will exists and be defined as well in your required file. So, you can directly use inside your required file the variables $orientation, $initrow, $rowsperpage.
Another way, quite ugly, is to add those vars to $_GET before require the file, assuming you're expecting to get them from $_GET:
$_GET['orient'] = $orientation;
$_GET['init'] = $initrow;
$_GET['nrrows'] = $rowsperpage;
require './mypage.php';
And my recommended way is to encapsulate your included file code in a function, so you can call it passing params. Even make a class, if included code is large and can be sliced in methods.
you can do so.
to_include.php
<?php
$orient = isset($_GET["orient"]) ? $_GET["orient"] : "portrait";
$init = isset($_GET["init"]) ? $_GET["init"] : 1;
echo "<pre>";
var_dump(
[
"orient"=>$orient,
"init"=>$init
]
);
echo "</pre>";
main.php
<?php
function include_get_params($file) {
$main = explode('?', $file);
$parts = explode('&', $main[1]);
foreach($parts as $part) {
parse_str($part, $output);
foreach ($output as $key => $value) {
$_GET[$key] = $value;
}
}
include($main[0]);
}
include_get_params("to_include.php?orient=landscape&init=100");
The method include_get_params, first separates the main part of the string, separates the file from the parameters, through the ?
After that he sets the parameters into pieces and puts all of this within the $_GET
In to_include, we retrieved the parameters from the $_GET
I hope I helped you.

PHP: how to check if a given file has been included() inside a function

I have a PHP file that can be include'd() in various places inside another page. I want to know whether it has been included inside a function. How can I do this? Thanks.
There's a function called debug_backtrace() that will return the current call stack as an array. It feels like a somewhat ugly solution but it'll probably work for most cases:
$allowedFunctions = array('include', 'include_once', 'require', 'require_once');
foreach (debug_backtrace() as $call) {
// ignore calls to include/require
if (isset($call['function']) && !in_array($call['function'], $allowedFunctions)) {
echo 'File has not been included in the top scope.';
exit;
}
}
You can set a variable in the included file and check for that variable in your functions:
include.php:
$included = true;
anotherfile.php:
function whatever() {
global $included;
if (isset($included)) {
// It has been included.
}
}
whatever();
You can check if the file is in the array returned by get_included_files(). (Note that list elements are full pathnames.) To see if inclusion occurred during a particular function call, check get_included_files before and after the function call.

Why my variable is undefined inside the function

It gives me this error Notice: Undefined variable: newfile and of course it also gives me fwrite() expects parameter 1 to be resource, null given cause of the first error. What I doing wrong?
$file = fopen("mycsv.csv", 'r');
$newfile = fopen("myjson.txt", 'w');
function write($text, $tab) {
$tabs = "";
for ($index = 0; $index < $tab; $index++) {
$tabs .= "\t";
}
fwrite($newfile, $text."\n".$tabs); //error here
}
Have a read about variable scope in the manual
$file and $newfile are global, therfore $newfile cannot be accessed locally in your function. Either move it into the function, pass it in as a parameter or as a last resort declare it as global in the function
You need to make $newfile global to be able to use it inside a function's scope:
function write($text, $tab) {
global $newfile;
// ... the rest
}
Any variable used inside a function is by default limited to the local function scope.
See: http://php.net/manual/en/language.variables.scope.php
You can do what you want if you make $newfile a global variable or pass it in as another function argument.
Function is out of scope. You could send the opened document direct to the function, that would allow it to be used within scope of the function.
function write($newfile,$text,$tab){
//the rest of your code
}
$returned = write($newfile,$text,$tab);

Is there a way to set Global Variables to be available by default within PHP functions?

Is there any way to do this / specify this in the php.ini configuration file? It would be really nice, at least for local server purposes, just to write functions without having to write the global keyword every time a global variable is used within the method.
Any way to do this?
EDIT:
What I mean is being to simply write this:
Example file "index.php":
$MY_ARRAY = array();
include("functions.php");
And then in "functions.php":
function addToArray($pMessage) {
$MY_ARRAY[] = "<a href='somelink.php'>$pMessage</a>";
}
Instead of:
And then in "functions.php":
function addToArray($pMessage) {
global $MY_ARRAY;
$MY_ARRAY[] = "<a href='somelink.php'>$pMessage</a>";
}
Yeah! You can setup a prepend file in PHP.
See: http://www.php.net/manual/en/ini.core.php#ini.auto-prepend-file
I still do not understand the purpose. But if I need to just remove the GLOBAL keyword from mentioning everytime, I would prefer to use a class with static array.
Something like this:
<?php
// Config.php
class Config {
public static $MY_ARRAY = array();
}
?>
And then include this file from your loader just call like this:
function addToArray($pMessage) {
Config::$MY_ARRAY[] = "<a href='somelink.php'>$pMessage</a>";
}
That will work.

PHP includes/require in a file and all functions inside it

I have a file which I include say:
require_once("../../constructor/database.php");
$stuff_inside_include_file->doThis() ; //ACCESSIBLE
function createUser(){
$stuff_inside_include_file->doThat(); //NOT-ACCESSIBLE (object not found)
}
function deleteUser(){
. ...
If I move the require statement into the scope of the function, then it works. Otherwise, it doesn't. Is there a better solution?
Thank you in advance.
You can try:
function createUser($stuff_inside_include_file){
$stuff_inside_include_file->doThat();
}
it should work, I think.
The best solution would be to not create global variables, but this would also work:
require_once("../../constructor/database.php");
$stuff_inside_include_file->doThis() ; //ACCESSIBLE
function createUser(){
global $stuff_inside_include_file;
$stuff_inside_include_file->doThat(); //ACCESSIBLE
}
function deleteUser(){
. ...
But really.. "Think globally, act in local scope".
I believe this is an issue with the variable not being global.
e.g.
$i = 10;
function pooppoop() {
$i = 11;
#this only sets the local variable i to 11
}
So you will want to put a global $i; at the top of your function. Obviously replace $i with the variable you are having trouble accessing.
I looks like the $stuff_inside_include_file variable is in the global scope, so you should be able to do this:
function createUser(){
global $stuff_inside_include_file;
$stuff_inside_include_file->doThat();
}

Categories