Variable set on true looses value after first if-statement ---? - php

This seems very bizarre to me.
At the beginning of my php-script I am defining a debug variable. In the script I am using this variable to test my functions etc. p.p. BUT:
After my first if-Statement, the variable $debug is not set on true but blank... what am I doing wrong:
Here is my code:
$debug=true;
if($debug){
echo 'Testausgabe: </br></br>';
include 'jsonData.php';
include 'database.php';
$ini_config=$ini_config = parse_ini_file("config.ini", true);
echo "Einbindung erfolgt!".LNBR;
$debug=true;
echo $debug;
echo LNBR;
}
function dropdownGen($link,$ini_config){
$drops=$ini_config['dropDowns'];
foreach($drops as $key => $liste){
$data=json_liste($link,$liste);
echo LNBR;
echo "droplist_parse=JSON.parse('".$data."');";
echo "ddslickNoImage(droplist_parse,'".$liste."');";
echo LNBR;
echo "ddslickNoImage_Toggle(droplist_parse,'".$liste."');";
echo LNBR;
}
}
function dropdownImgGen($link, $ini_config){
$images=$ini_config['image_links'];
print_r($images);
echo "<br><br>";
foreach($images as $key => $liste){
$link='..\\'.utf8_decode($liste);
echo $link;
echo LNBR;
$data=json_imageList($link);
echo $data;
echo LNBR;
echo "ddslickImageList(".$data.",'".$key."');";
}
}
if($debug){
echo $debug;
echo LNBR;
echo 'Testausgabe: </br></br>';
dropdownGen($link,$ini_config);
echo LNBR;
dropdownImgGen($link,$ini_config);
echo LNBR;
}

Related

Return value of key from associative array when key is known

So I am trying to echo the value of a key if the key is present in the array. I've go this code at the moment:
<?php
$lingos = array(
"en"=>"en_US",
"en-gb"=>"en_GB",
"nl"=>"nl_NL",
"de"=>"de",
"da"=>"da",
"es"=>"es",
"ca"=>"es_MX",
"fr"=>"fr",
"it"=>"it",
"pt-pt"=>"pt_PT",
"no"=>"no",
"sv"=>"sv",
"fi"=>"fi",
"et"=>"en_GB",
"is"=>"en_GB",
"cs"=>"cs",
"pl"=>"pl",
"lv"=>"en_GB",
"lt"=>"lt",
"hu"=>"hu",
"ro"=>"ro",
"sr"=>"en_GB",
"hr"=>"en_GB",
"bg"=>"bg",
"el"=>"el",
"uk"=>"en_GB",
"ru"=>"ru",
"tr"=>"tr",
"ar"=>"ar",
"zh-hans"=>"zh_CN",
"zh-hant"=>"zh_TW",
"ja"=>"ja",
"ko"=>"ko",
"id"=>"in",
"ms"=>"ms",
"th"=>"th",
"vi"=>"vi",
"pt-br"=>"pt_PT"
);
// foreach($lingos as $lingo => $x_lingo) {
// echo "Key=" . $lingo . ", Value=" . $x_lingo;
// echo "<br>";
// }
$wmpl_langcode = ICL_LANGUAGE_CODE;
echo $wmpl_langcode;
if (array_key_exists($wmpl_langcode, $lingos)) {
echo $lingos[1];
} else {
echo "not found";
}
?>
Thing is, $lingos[1] is not returning anything. What am I doing wrong?
Presumably $wmpl_langcode is something like en as you are checking for it with array_key_exists so use that as the index:
$wmpl_langcode = ICL_LANGUAGE_CODE;
echo $wmpl_langcode;
if (array_key_exists($wmpl_langcode, $lingos)) {
echo $lingos[$wmpl_langcode];
} else {
echo "not found";
}
Or simpler:
echo isset($lingos[$wmpl_langcode]) ? $lingos[$wmpl_langcode] : "not found";
//PHP 7+
echo $lingos[$wmpl_langcode] ?? "not found";

How to assign a PHP expression to a PHP variable

I need to do something like
$var = "Hello World ..the value is <?php echo 'XYZ' ?>";
Any help will be highly useful. I am stuck in this from the very long time but not able to acheive this. please help
Below is my PHP code that I need to assigned again to php variable (this variable will be passed to return of my ajax call)
<div class="photo-post">
<h4 class="h4-1">Some data...</h4>
<?php
$i=0;
$unique_seq="888094499";
$pic_counter=1;
foreach (glob("target/*".$unique_seq."*") as $filename)
{
$i++;
}
$total_pics=$i;
echo "<div id='post-photo-slider'>";
if($i>1)
{
echo "<a href='#' class='control_next_photo'>></a>";
echo "<a href='#' class='control_prev_photo'><</a>";
}
echo "<ul>";
foreach (glob("target/*".$unique_seq."*") as $filename)
{
echo "<li>";
echo "<div class='photos-slide-container'>";
echo "<img src='".$filename."' width='540px' height='225px'>";
echo "<span class='count-span-photos'>".$pic_counter." of ".$total_pics."</span>";
echo "</div>";
echo "</li>";
$pic_counter++;
}
if ($i==1)
{
echo "<li></li>";
}
echo "</ul>";
echo "</div><!-- post-photo-slider END -->";
echo "<hr class='hr4'>";
?>
</div><!-- photo end -->
you cannot do it in this way
$var = "Hello World ..the value is <?php echo 'XYZ' ?>";
when you return this string to your javascript php code cannot be execute on client side!!!!
why do not return do this simple way?
$var = "Hello World ..the value is " . "XYZ";

Call a php function to echo in place

I have this code
$css=0;
<?php
echo "<div>";
function style(){
global $css;
echo $css;
}
echo "</div>";
$css =5 ; style();
?>
what i want to happen is this
<div>5</div>
and what really happens is
<div></div>5
I need to declare the value of the variable then go back and echo it in the same place it intended to be in, and I can't change the order due to many reasons in the code structure .
UPDATE :: after reading all the answers it seems i wasn't clear enough .
<html>
<?php $css=0; ?>
<div id="<?php echo $css ?>"></div>
<div id ="K"></div>
<?php
$css=5;
?>
</html>
this is the situation
1- i can not declare $css before the echo.
2- i can not reposition anything from this structure .
3- i can not put #K after the declaring of the $css .
what i really need a way to put the value inside the variable and let echo where it's .
any ideas ?
you can try this as well using js
<?php
$css=0;
echo "<div id='tt'>";
/*function style(){global $css; echo $css;}*/
echo "</div>";
$css =5 ;
echo "<script>";
echo "document.getElementById('tt').innerHTML = $css";
echo "</script>";
?>
Your function is run when you call it, and as it is called below the you get the expected 5.
To fix this:
echo "<div>";
style();
echo "</div>";
<?php
echo "<div>";
function style() {
global $css;
echo $css;
}
$css =5 ;
style();
echo "</div>";
?>
or simply:
<?php
function style() {
global $css;
echo $css;
}
$css = 5;
echo "<div>" . style() . "</div>";
?>
Previously, you well calling the function to echo out the variable after closing the div.

codeigniter, passing object to view

I have the following in my codeigniter constructor:
$navbar= new stdClass();
$navbar->user_email = $this->user_email;
$navbar->vp = $this->vp;
When I try to access this in my index function:
public function index() {
var_dump($this->navbar);
this works.
I now tried to pass $this->navbar to the view with:
$this->load->view('buyers/navbar', $this->navbardata);
In the view I have
<?php echo 'in nav ';var_dump($this->navbar); exit; ?>
I get :
Message: Undefined property: MY_Loader::$navbar
How can I make this work?
Thanks in advance,
Try like this
$data['navbar'] = $this->navbardata;
$this->load->view('buyers/navbar', $data);
and in your view try like
<?php echo 'in nav ';var_dump($navbar); exit; ?>
First, create a controller like this
class Test extends CI_Controller
{
public function index()
{
$data['topics'] = "Views Testing";
$data['title'] = "Welcome To My Blog";
$data['myData'] = array("name" => "John Doe", "email" => "johndoe2020#gmail.com", "designation" => "CodeIgniter Developer");
$data['myObject'] = new MyClass("John Doe", "johndoe#gmail.com");
$this->load->view("blogview", array($data, "MySelf" => new MyClass("Doe John", "doejohn460#gmail.com"), "developer" => "Life at CodeIgniter Academy"));
}
}
class MyClass
{
public $name;
public $email;
public function __construct($name, $email)
{
$this->name = $name;
$this->email = $email;
}
}
Now Create Your blogview.php File In views folder
And Add the following code to Access The Data Passed from Controllers
echo "<h3>Variables :</h3> ";
echo var_dump($title);
echo $title;
echo var_dump($topics);
echo $topics;
echo "<h3>Array :</h3> ";
echo var_dump($myData);
print_r($myData);
echo "<br><br>";
echo $myData['name'];
echo "<br>";
echo $myData['email'];
echo "<br>";
echo $myData['designation'];
echo "<br> <br>";
foreach ($myData as $data)
{
echo $data."<br>";
}
echo "<h3>Object </h3>";
echo var_dump($myObject);
echo "<br>";
echo $myObject->name;
echo "<br>";
echo $myObject->email;
echo "<br><br>";
echo $MySelf->name;
echo "<br>";
echo $MySelf->email;
echo "<br><br>";
foreach($myObject as $Object)
{
echo $Object."<br>";
}
echo "<h3>Array key:Value Pair</h3>";
echo $developer;
I hope this will help.

php setcookie fails under php5

I created this simple script which will either set a cookie with three values or retrieve the cookies values if they are already set. On my server running PHP4, everything works. On my server with PHP 5 (5.2.11), the script fails to set the cookie in the browser. I already checked if output buffering is enabled in my php.ini and it is. Does anyone have any ideas as to why this fails to work?
<?php
echo "<!DOCTYPE html>";
echo "<body>";
if (!isset($_COOKIE['taeinv'])) {
echo "No cookie set... Attempting to set a new cookie.";
$user = "testuser";
$role = "admin";
$expire = "true";
$halfHour = 1800;
setcookie("websitename[Expire]", $expire, time()+$halfHour);
setcookie("websitename[User]", $user, time()+$halfHour);
setcookie("websitename[Role]", $role, time()+$halfHour);
}
if (isset($_COOKIE['websitename'])) {
echo "Cookie Values:";
echo "<br />";
foreach ($_COOKIE['websitename'] as $name => $value) {
echo "<b>$name</b> : $value <br />\n";
}
}
echo "<br />";
echo "<a href=logout.php>Logout</a>";
echo "</body>";
echo "</html>";
?>
You have to set the cookie before any output to the browser. Try moving all echo lines somewhere below the setcookie call. You could do something like this:
<?php
$set = false;
if (!isset($_COOKIE['taeinv'])) {
$set = true;
$user = "testuser";
$role = "admin";
$expire = "true";
$halfHour = 1800;
setcookie("websitename[Expire]", $expire, time()+$halfHour);
setcookie("websitename[User]", $user, time()+$halfHour);
setcookie("websitename[Role]", $role, time()+$halfHour);
}
echo "<!DOCTYPE html>";
echo "<body>";
if ($set) {
echo "No cookie set... Attempted to set a new cookie.";
}
if (isset($_COOKIE['websitename'])) {
echo "Cookie Values:";
echo "<br />";
foreach ($_COOKIE['websitename'] as $name => $value) {
echo "<b>$name</b> : $value <br />\n";
}
}
echo "<br />";
echo "<a href=logout.php>Logout</a>";
echo "</body>";
echo "</html>";
?>
That worked on my old PHP4 server, but not on PHP5.
Use output buffering – ob_start() and ob_end_flush().
Example:
<?php
ob_start();
echo '<p>Initializing…</p>';
setcookie('myLanguage', 'PHP');
ob_end_flush();
// you can continue your PHP code here…
?>
I had a similar problem but it was only in Chrome that the cookies disappeared. Firefox was fine.
Setting all the parameters in the setcookie function fixed it.
This sets the cookie but Chrome drops the cookie within a click:
setcookie('uname', 'Joe', time()+3600*24);
This sets the cookie and the browser retains it:
setcookie('uname', 'Joe', time()+3600*24, '/', 'www.domain.com', false, false);

Categories