This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
It is display $job as undefined. What is wrong with my switch statement?
in a switch case we have to pass only one variable but how can i append the array here.
list_questatus is also values of array
if(isset($_POST['list_queuestatus']))
{
$selected= $_POST['list_queuestatus'];
switch($selected[$job]){
case 'r':
if($state[$jid]=="r"){
$jobs[$j]=$jid;
}
break;
}
}
$j=0;
$jobs=array();
for($i = 5; $i < count($output); ++$i) {
$jid=trim(substr($output[$i],0,30));
$jobid[$jid]=trim(substr($output[$i],0,30));
$username[$jid]=trim(substr($output[$i],30,15));
$queue[$jid]=trim(substr($output[$i],47,15));
$jobname[$jid]=trim(substr($output[$i],63,15));
$sessionid[$jid]=trim(substr($output[$i],79,8));
$nds[$jid]=trim(substr($output[$i],88,4));
$tsk[$jid]=trim(substr($output[$i],93,5));
$reqmem[$jid]=trim(substr($output[$i],99,6));
$reqtime[$jid]=trim(substr($output[$i],106,5));
$state[$jid]=trim(substr($output[$i],112,2));
$elaptime[$jid]=trim(substr($output[$i],114,8));
if(isset($_POST['list_queuestatus']))
{
$selected= $_POST['list_queuestatus'];
switch($selected[$job]){
case 'R':
if($state[$jid]=="R"){
$jobs[$j]=$jid;
}
break;
case 'Q':
if($state[$jid]=="Q"){
$jobs[$j]=$jid;
}
break;
case 'H':
if($state[$jid]=="H"){
$jobs[$j]=$jid;
}
break;
case 'S':
if($state[$jid]=="S"){
$jobs[$j]=$jid;
}
break;
case 'W':
if($state[$jid]=="W"){
$jobs[$j]=$jid;
}
break;
case 'E':
if($state[$jid]=="E"){
$jobs[$j]=$jid;
}
break;
case 'A':
$jobs[$j]=$jid;
}
}
else{
$jobs[$j]=$jid;
break;
}
}
$selected='';
function get_options($select)
{
$list_queuestatus=array("--Select Status--"=>"SS","All"=>"A","Running"=>"R","Queued"=>"Q","Held"=>"H","Suspened"=>"S","Waiting"=>"W","Error"=>"E");
$options='';
while(list($k,$v)=each($list_queuestatus))
{
if($select==$v)
{
$options.='<option value"'.$v.'"selected>'.$k.'</option>';
}
else{
$options.='<option value"'.$v.'">'.$k.'</option>';
}
}
return $options;
}
echo '<form action="main.php?page=queuestatus&';
echo '" method="POST">';
echo '<select name="list_queuestatus" onchange="this.form.submit();">';
echo get_options($selected);
echo '</select>';
echo '</form>';
Please use $_POST instead of $_post. And please declare the $job variable before it used or the outer scope.
It seems that that the job is not a variable its a index for the array. So please use 'job' instead of $job.
Hope this will resolve your issue.
Related
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 6 years ago.
So i've got a rather complex bit of code that i'm trying to make work (actually; it works fine on my test server but not so much my live server. so i'm trying to make it rework) -- it keeps telling me that a variable i'm asking it to check with isset is not a defined variable. I'm probably over thinking this whole method and there's probably a simplier way. but i need you guys to bash me over the head with it apparently
include('bootstra.php');
include('includes/parts/head.php');
if(!isset($_SESSION['user']['uid']))
{
echo $html;
include('includes/parts/login.php');
if(isset($_GET['mode']))
{
$file = $_GET['mode'];
$path = "includes/parts/{$file}.php";
if (file_exists($path)) {
require_once($path);
} else {
die("The Page REquested could not be found!");
}
}
else
{
include('includes/parts/frontpage.php');
}
}
else
{
if(!isset($_SESSION['x']) || !isset($_SESSION['y']))
{
if(isset($_GET['x']) && isset($_GET['y']))
{
$_SESSION['x'] = $_GET['x'];
$_SESSION['y'] = $_GET['y'];
}
else
{
$_SESSION['x'] = 0;
$_SESSION['y'] = 0;
}
header("location:index.php?x={$_SESSION['x']}&y={$_SESSION['y']}");
}
else
{
if($_SESSION['x'] != $_GET['x'] || $_SESSION['y'] != $_GET['y'] )
{
header("location:index.php?x={$_SESSION['x']}&y={$_SESSION['y']}");
}
else
{
echo $html;
echo $base->displayMap($_GET['x'], $_GET['y']);
include('includes/context_menu.php');
}
}
}
Here is the exact error:
Notice: Undefined index: x in /home/****/public_html/reddactgame/index.php on line 27
Change it
<pre>
if( !isset($_SESSION['x']) || !isset($_SESSION['y']) )
</pre>
to
<pre>
if( !( isset($_SESSION['x']) && isset($_SESSION['y']) ) )
</pre>
There is no error, that is a Warning...
It is saying that $_SESSION['x'] was never setted, so, when you do that isset it tells you that it was never declared.
Example:
This gives you the same warning
empty($x);
This does not give you warning
$x = 0;
empty($x);
EDIT
I see this is a common question, so here is a better explanation.
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 8 years ago.
I have a problem in using of array as a variable in nested if..else.. conditions. Its used to create custom web-service APIs. Here is the structure of code:
if (condition) {
// code to be executed in case of true
if (condition) {
// code to be executed in case of true
$result['key1'] = $variable1;
$result['key2'] = $variable2;
$result['key3'] = $variable3;
} else {
// code to be executed in case of false
}
} else {
$result['error'] = 'Something went wrong!!!';
}
echo json_encode($result); // line 121
On execution of code it displays the following error:
Notice: Undefined variable: result in C:\xampp.. on line 121
Variables need to be declared before using it inside functions like json_encode()! Do it this way:
$result = array();
if (condition) {
// code to be executed in case of true
if (condition) {
// code to be executed in case of true
$result['key1'] = $variable1;
$result['key2'] = $variable2;
$result['key3'] = $variable3;
} else {
// code to be executed in case of false
}
} else {
$result['error'] = 'Something went wrong!!!';
}
echo json_encode($result);
U need to declare your variable first if you are directly entering the value to an array.
$result = array();
if (condition) {
// code to be executed in case of true
if (condition) {
// code to be executed in case of true
$result['key1'] = $variable1;
$result['key2'] = $variable2;
$result['key3'] = $variable3;
} else {
// code to be executed in case of false
}
} else {
$result['error'] = 'Something went wrong!!!';
}
echo json_encode($result); // line 121
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 8 years ago.
I was trying to print the value of a variable from a function, but it is showing undefined variable value. Please help me and tell me how to solve this problem; I'm pretty new
to PHP coding. I tried the function as public, but it shows some other error.
<?php
if(isset($_POST['btn_submit']))
{
$num1 = $_POST['cards1'];
$num2 = $_POST['cards2'];
$num1=substr($num1,0,(strlen($num1)-1));
$num2=substr($num2,0,(strlen($num2)-1));
}
function get_value() {
switch ($num2) {
case 'A' :
$value = 11;
break;
case 'J':
case 'Q':
case 'K':
$value = 10;
break;
default:
$value = $num2;
break;
}
//return $value;
}
echo '<script type="text/javascript">alert("Out Put is'.$value.' ");</script>';
//$total_value = $num1+$num2;
?>
$num2 is local to your function. You need to pass it in as a parameter:
function get_value($num2) {
*** YOUR CODE
}
file capitais.php
<?php
/*Capitais*/
ini_set('display_errors',1);
ini_set('display_startup_erros',1);
error_reporting(E_ALL);
require_once('dados.php');
$dados = new Dados;
$estado = $dados->_estado;
$capitais = $dados->_capitais;
for($i=1;$i<count($estado);$i++){
echo $estado["$i"].' : '.array_search($capitais["$i"],$dados->cidades["$i"]).'<br />';
}
?>
file dados.php
<?php
class Dados{
public $_estado = array("Selecione","AC","AL","AM","AP","BA");
public $_capitais = array("Selecione Primeiro o Estado","RIO BRANCO","MACEIO","MANAUS","MACAPA","SALVADOR");
public function cidades($estado){
switch($estado){
case "1":
return array("Selecione","RIO BRANCO","RODRIGUES ALVES","SANTA ROSA","SENA MADUREIRA","SENADOR GUIOMARD","TARAUACA","XAPURI");
break;
case "2":
return array("Selecione","MACEIO","MAJOR ISIDORO","MAR VERMELHO","MARAGOGI","MARAVILHA","MARECHAL DEODORO","MARIBONDO");
break;
case "3":
return array("Selecione","MANAQUIRI","MANAUS","MANICORE","MARAA","MAUES","NHAMUNDA","NOVA OLINDA DO NORTE","NOVO AIRAO","NOVO ARIPUANA","PARINTINS","PAUINI","URUCURITUBA");
break;
case "4":
return array("Selecione","AMAPA","CALCOENE","MACAPA");
break;
case "5":
return array("Selecione","RUY BARBOSA","SALINAS DA MARGARIDA","SALVADOR","SANTA BARBARA");
break;
}
}
}
?>
Notice: Undefined property: Dados::$cidades in capitais.php on line 14
Warning: array_search() [function.array-search]: Wrong datatype for second argument in
$dados->cidades["$i"]
should be
$dados->cidades("$i")
cicades isn't a property of the Dados class, it's a method, so it needs to be called as
$dados->cidades();
passing in the appropriate $estado argument
echo $estado["$i"].' : '.array_search($capitais["$i"],$dados->cidades["$i"]).'<br />';
should be
echo $estado["$i"].' : '.array_search($capitais["$i"],$dados->cidades("$i")).'<br />';
as cidades is a function, not an array property.
$dados->cidades["$i"]) should be $dados->cidades($i))
I keep getting an "Notice: Undefined index: did" error with this query, and I'm not understanding why... I'm much more used to mysql, so, maybe the syntax is wrong?
This is the php query code:
function get_demos() {
global $session;
$demo = array();
$result = pg_query("SELECT DISTINCT(did,vid,iid,value) FROM dv");
if(pg_num_rows($result) > 0) {
while($r = pg_fetch_array($result)) {
switch($r['did']) {
case 1:
$demo['a'][$r['vid']] = $r['value'];
break;
case 2:
$demo['b'][$r['vid']] = $r['value'];
break;
case 3:
$demo['c'][$r['vid']] = $r['value'];
break;
}
}
} else {
$session->session_setMessage(2);
}
return $demo;
}
When I run that query at the pg prompt, I get results:
"(1,1,1,"A")"
"(1,2,2,"B")"
"(1,3,3,"C")"
"(1,4,4,"D")"
"(1,5,5,"E")"
"(1,6,6,"F")"
"(1,7,7,"G")"
"(1,8,8,"H")"
"(1,9,9,"I")"
"(1,10,A,"J")"
"(1,11,B,"K")"
"(1,12,C,"L")"
"(1,13,D,"M")"
"(2,14,1,"A")"
"(2,15,2,"B")"
"(2,16,0,"C")"
"(3,17,1,"A")"
"(3,18,2,"B")"
"(3,19,3,"C")"
"(3,20,4,"D")"
"(3,21,5,"E")"
"(3,22,6,"F")"
"(3,23,7,"G")"
You must use pg_fetch_assoc() function if you want to get associative array in $r.