Smarty Undefined Index Variable - php

I am newbie to smarty and trying to output some results in my tpl file fetching from php. In my account.php file I have put Below code
<?php
include SMARTYLOADER;
$ad2 = $db->fetchRow("SELECT * FROM ip_ptc");
$smarty->assign("ad2", $ad2);
$smarty->display("account.tpl");
$db->close();
exit();
?>
and in my account.tpl file i have written below code to fetch the result
{$ad2.country}
but it is showing an undefined index variable ad2 what's wrong i am doing

Related

How to pass array/function to view page ? [OC]

Currently in my file
controller/common/home.php
$this->load->model('catalog/category');
$homepageProductTest = $this->model_catalog_category->homepageProductTest();
And when i echo $homepageProductTest , there is bunch of array.
What i wanted:
How do i pass the variable to my view file (home.tpl)
The error i always get (view/theme/xxx/template/common/home.tpl)
Undefined variable: homepageProductTest
if you want to use any variable in your view file from controller file then you have to pass your value in data variable like below,
like in your case, you want "homepageProductTest"
$data['homepageProductTest'] = $homepageProductTest; //write this code in your controller file
OR
$this->data['homepageProductTest'] = $homepageProductTest; //write this code in your controller
in .tpl file you can use this variable using PHP like,
<?php foreach($homepageProductTest as $producttest){ ?>
//your code here
<?php } ?>

Using $_SESSION to carry a php variable from one page to the next?

I created an app to receive and tally votes. I am trying to make the voter's selection carry over to a confirmation page so that I can display the name of the candidate for whom they voted on the confirmation page.
I am trying to use $_SESSION on the variable of the selected candidate from the voter's submission, and then call the variable on the confirmation page, however I continue to get undefined variable error.
voterSubmit.php:
<?php
$selectedCandidate = $_POST['candidateid'];
session_start();
$selection = $_SESSION[$selectedCandidate];
//Redirect to results page
header("Location: views/confirmation.php");
confirmation.php
<?php
session_start();
$_SESSION[$selectedCandidate] = $selection;
include "views/confirmation.php";
confirmation view:
<?php include "../partials/header.php"; ?>
<h1>Thanks For Your Vote!!</h1><br>
//This is where the error occurs (on my selection variable):
<h2>You voted for <?=$selection?>
View Results
<?php include "../partials/footer.php"; ?>
I want the name of the selected candidate to appear on the confirmation page by way of the $selection variable. However, all I receive on the front end is an "undefined variable" error. I also would like to note that instead of using the $selectedCandidate variable in my session, I have also tried grabbing the name directly by just using the name of the radio button selection as such:
$_SESSION['candidateid'] = $selection
I also would like to mention that i have tried the reverse:
on confirmation.php:
session_start();
$selection = $_SESSION[$selectedCandidate];
on voteSubmit.php:
session_start();
$_SESSION[$selectedCandidate] = $selection;
Your using the $_SESSION variable incorrectly.
Try:
voterSubmit.php:
<?php
$selectedCandidate = $_POST['candidateid'];
session_start();
$_SESSION['selectedCandidate'] = $selectedCandidate;
//Redirect to results page
header("Location: views/confirmation.php");
confirmation.php
<?php
session_start();
$selection = $_SESSION['selectedCandidate'];
include "views/confirmation.php";
voterSubmit.php looks ok... but I don't understand why you have include "views/confirmation.php"; in the confirmation.php file.
<?php
session_start();
$_SESSION[$selectedCandidate] = $selection;
include "views/confirmation.php";
Try coding your HTML/PHP this way:
<?php
/* confirmation.php */
session_start();
$_SESSION[$selectedCandidate] = $selection;
require_once("../partials/header.php");
//This is where the error occurs (on my selection variable):
echo <<<_STRT
<h1>Thanks For Your Vote!!</h1><br>
<h2>You voted for $selection</h2>
<p>View Results</p>
_STRT;
require_once("../partials/footer.php");
?>
I essentially eliminated a lot of start/stop code. The start of your PHP recalls the session and variable. Then I go into the HTML portion to provide the results to your site visitor. Give it a shot. Comment on it if it doesn't resolve your issue so we can rethink it.
I think session_start() needs to be the first line after the php tag.
$_SESSION is an array. So you need to assign it values like:
$_SESSION['keyname'] = $value;
voterSubmit.php:
<?php
session_start();
$_SESSION['selectedCandidate'] = $_POST['candidateid'];
//Redirect to results page
header("Location: views/confirmation.php");
confirmation.php
<?php
session_start();
$selection = $_SESSION['selectedCandidate'];
include "views/confirmation.php";
There are 2 issues in your code :
session_start() defined in the wrong place.
Wrong way of assigning value to your session variable.
Answer :
session_start() should be the first thing defined after your php
tag.
Correct way to declare session variables is : $_SESSION["selectedCandidate"] = $selectedCandidate; where, $selectedCandidate is the value to be assigned to your session variable, named selectedCandidate.
You have done 2 mistakes in your code:
1. Session should be defined in starting of page it means you have to define it like this:
<?php
session_start();
//php code goes here.
?>
2.wrong initialization of session variable .you should do it like this:
<?php
$_SESSION['selectedCandidate']=$selectedCandidate;
?>

How to get # variable from the url path using php

i want to get variable called #test from url and i want to show json results from database
its showing wrong results, this is the url am passing variable called #test like this
http://myinnos.in/read_all_filter_feed.php?filter=#test
this is my php code to get data from url and parsing JSON
<?php
header('Content-Type: application/json');
include("../db_config.php");
$linklist=array();
$link=array();
$filter=$_GET['filter'];
$percentage = "%";
$qr=mysql_query("
SELECT * from test where message LIKE '$percentage.$filter.$percentage'") or die(mysql_error());
while($res=mysql_fetch_array($qr))
{
$link['id']=$res['id'];
$link['name']=$res['name'];
array_push($linklist,$link);
}
//print_R($linklist);
echo json_encode($linklist);
?>

Problem while printing Hindi text through a user defined function!

I how two php files one is main.php and second is functions.php. If I print below line in main.php, its working fine.
print "देश विदेश";
but my reqirment is to call a function from functions.php in main.php that should do the same:
Mian.php
require_once('functions.php');
print func_headline();
functions.php
function func_headline()
{
//print html_entity_decode("देश विदेश", ENT_NOQUOTES,'UTF-8');
print "देश विदेश";
//utf8_encode("<a title='देश विदेश' href='#'><span>'देश विदेश'</span></a>");
}
why it doesnot works??
You need to just call the function in main.php. Change statement
print func_headline();
to
func_headline();

modify array value from php file in codeigniter or php

i have php file like below and i want to read this php file and get all array value in this php file and i can update value of array
<?php
$lang['country_name']="Country Name";
$lang['zip_code']="Zip Code";
$lang['flag']="Flag";
$lang['site_enabled']="Site Enabled";
$lang['save']="Save";
$lang['cancel']="Cancel";
$lang['country_info']="Country Information";
$lang['new_country']="New Country";
$lang['edit_country']="Edit Counry";
$lang['country']="Country";
$lang['home']="Home";
$lang['no']="No";
$lang['action']="Action";
$lang['show']="Show";
?>
If you want to include this PHP array in another script, you can use the include function.
For example, if you have another file, index.php in the same directory, you can include it and then you will have access to the variable. You can modify it like a normal variable, like so:
index.php
<?php
include `lang_array.php`; // will get the file with the array
echo $lang["no"]; // should output "No"
$lang["show"] = "value"; // update $lang["show"]
?>
Simply you just have include file using php include function.
<?php
include 'file_name.php';
$lang['new_country']="new country 1";
?>

Categories