Function Call and variables problems - php

I would like to ask a question about php . That's related php function , there are two different function but whenever call this function , doesnt work below highlight(Doesnt Work Area) place .By the way , I tried call content variable from out of function and also I wrote global variable but I couldnt.
Do you know , why ?
Java () {
$res=mysql_query("SELECT * FROM candidate WHERE candidate_id=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);
if(isset ($_POST['language'])) {
$deneme=$_POST['language'];
}
$file= $userRow['candidate_name'];
touch($file.'.java');
if ( isset ($_POST ['content']) )
{
file_put_contents ($file.'.java',$_POST ['content']); **Doesnt Work HERE!**
}
exec("C:\Java\java\bin\javac -verbose $file.java 2>&1" , $output, $resultCode);
.
.
.
.
.
.
}
Is there any advice about it ?
Thank you in advance

Do you sure that if ( isset ($_POST ['content']) ) is true?
Check write permissions
Execute after writing.

Instead of
Java () {
You should use:
function Java() {
// Function contents...
}

Related

Get a variable in a php file from a function in another file

I have a php file in the root and I would like to retrieve a variable that is in a function on another php file. I need it to make a SQL query
My php file in the root
<?php
require dirname(__FILE__) . '/config/config.inc.php';
require 'modules/pricefrom/pricefrom.php';
$lowestPrice = Db::getInstance()->getValue('
SELECT MIN(`price`)
FROM `' . _DB_PREFIX_ . 'product_attribute`
WHERE `id_product` = ' . (int)$id_product
);
I need the $id_product from another file who is required and who is in a function and use a parameters of this function
<?php
...
public function hookDisplayPriceBlock($param) {
$id_product = $param['id_product'];
$id_product_attribute = $param['id_product_attribute'];
$product = new Product();
...
I know there is an easy way to make it but I'm searching for hours and I can't find, can you help me guys ?
In root use
require_once('<path to file2.php>');
$id_product = hookDisplayPriceBlock(<params>);
.....
File2
public function hookDisplayPriceBlock($param) {
...
return $id_product;
}

Drupal 8 : more suggestions for input forms

I'm making a form and when I want to custom my input, I can see only one suggestion already used for another form .
There is a solution to have others suggestion without module if it's possible?
I'm using this function to have input suggestions:
function terreal_theme_suggestions_input_alter(array &$suggestions, array &$variables, $hook)
{
if (isset($variables['element']['#id'])) {
$id = str_replace("-", "_", $variables['element']['#id']);
$suggestions[] = $hook . '__' . $id;
}
}
my suggestion used is : input--edit-field-lastname-0-value.html.twig
thanks

Adding script to certain nodes and pages on drupal 7

I am trying to add a script code on all pages of my drupal except a few. Is there any conditionals I can use in html.tpl.php ? or any functions on the template.php to achieve this?
I tried the code below on template.php but no luck :(
Thanks in advance!
function THEME_preprocess_page(&$variables) {
if (isset($variables['node']->type)) {
$variables['theme_hook_suggestions'][] = 'page__' .
$variables['node']->type;
}
//this is what I am trying
if ($variables['nid'] == '77') {
drupal_add_js(drupal_get_path('theme', 'THEME') .'/scripts/path.js');
}
}
You can use hook_page_build(&$page)
function MYMODULE_page_build(&$page){
$nids = array(123, 1234); // your nids
if(($node = menu_get_object('node', 1)) && in_array($node->nid ,$nids) ){
drupal_add_js(drupal_get_path('theme', 'mythemename') .'/scripts/path.js');
}
}
Clear all cache after create this function to see result , also ensure of path script is correct ;)

Accessing variable stored in class from another file

I would like to access the $new_id variable in the method below (from public class youth_teams) from an outside file but I can't figure out how. I have it printing the lastInsertID correctly from inside the file which contains the method but would like to be able to access the variable in other files also.
public function addTeam(
$team_name,
&$error
) {
$query = $this->pdo->prepare('INSERT INTO `' . $this->table . '` (
`team_name`
) VALUES (
:team_name
)');
$query->bindParam(':team_name', $team_name);
$query->execute();
print_r($query->errorInfo());
print $this->pdo->lastInsertID();
$new_id = $this->pdo->lastInsertID();
return $new_id;
}
Here's the code I've tried from the OTHER FILE:
sw::shared()->youth_teams->addTeam (
$team_name,
$error
);
$temp_two = sw::shared()->youth_teams->addTeam->pdo->lastInsertID();
echo "new id: " . $temp_two . "<br>";
Of course that is not working... What's the correct path to access $new_id?
It should be:
$temp_two = sw::shared()->youth_teams->addTeam($team, $error);
addTeam() is a function that returns $new_id, so you need to call it with ().
If you really want to be able to access $new_id directly, you can declare it global:
public function addTeam(
$team_name,
&$error
) {
global $new_id;
...
What was wrong with this?
$sw = new sw();
$temp_two = $sw->addTeam( $team, $error );
echo "new id: " . $temp_two . "<br>";
If you can call sw::shared() from the outside file, you can assign the object.
I probably am not fully understanding your code, if not, please fully explain the following:
sw::shared()->youth_teams->addTeam( $team, $error );
// 1. What does the shared() method do?
// 2. What is the youth_teams property?
If it's needed, might I suggest adding the assignment directly into the addTeam() function and use the above format to return the id only.

Display php function with smarty

I'm beginer in smarty My php function is:
public function list_special() {
Session::set('was_in_special', 'true');
if (isset($_GET['showall']) && $_GET['showall'] == 'yes') {
TPL::$s->assign('products', $this->proc->getSpecialProducts('true'));
} else {
TPL::$s->assign('products', $this->proc->getSpecialProducts());
}
TPL::$s->assign('title', 'Specialus katalogas - norėdami papildyti specialiųjų kainų sąraša, susisiekite su savo vadybininku.');
//TPL::$s->assign('products', $this->proc->getSpecialProducts());
TPL::$s->assign("pages_num", $this->proc->get_special_products_pages());
TPL::$s->assign("parameters", Request::$params[1] . "/" . Request::$params[2] . "/");
echo TPL::$s->fetch('catalog.products.html');
}
I want call this function in one page , I try use {products} but this nothing display for me
Maybe you should register the object in before display, then call method in template like this:
{object->method p1='xx' p2='xx'}
look here.
http://www.smarty.net/docsv2/en/advanced.features.tpl

Categories