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))
Related
Here is my code:
$order_newest = $order_votes = $order_featured = $order_frequent = '';
switch ($_GET['o']) {
case 'newest':
$order_newest = 'order_active';
break;
case 'votes':
$order_votes = 'order_active';
break;
case 'featured':
$order_featured = 'order_active';
break;
case 'frequent':
$order_frequent = 'order_active';
break;
default:
$order_newest = 'order_active';
break;
}
It throws following error when o argument isn't exist in the URL:
Notice: Undefined index: o in C:\xampp\htdocs... on line 71
I can fix the problem by adding this condition:
$order_newest = $order_votes = $order_featured = $order_frequent = '';
if ( isset($_GET['o']) ) {
switch ($_GET['o']) {
.
.
.
}
} else {
$order_newest = 'order_active';
}
But I guess this isn't the right way. Because in this case I have to add lots of conditions contain isset() function.
Anyway, is there any better approach to handle that?
Use filter_input(). As example:
$order = filter_input(INPUT_GET, 'o');
switch ($order) {
// ...
In a webpage in top i use this code:
<?php
switch ($urlcomecatid) {
case "95":
$target_cat1='96';
$target_cat1_name = "A";
break;
case "96":
$target_cat1='95';
$target_cat1_name = "B";
break;
?>
and in another down part the page i use these variable
<?php
echo "<p class='pagefooterlifestyle' align='center'>
<a href='../lifestyle/lifestylesub.php? catid=$target_cat1'>$target_cat1_name</a></p>";
?>
BUT i get error undefined variable for
$target_cat1
$target_cat1_name
please let me know that what is the problem?
Initialize your variables:
switch ($urlcomecatid) {
case "95":
$target_cat1='96';
$target_cat1_name = "A";
break;
case "96":
$target_cat1='95';
$target_cat1_name = "B";
break;
default:
$target_cat1 = '';
$target_cat1_name = '';
}
You must be prepared for different values for $urlcomecatid as well.
If your $urlcomecatid variable contains neither 95 nor 96, PHP will ignore both cases, so the $target_cat1 and $target_cat1_name variables won't be initialized.
You can use default to tell PHP what to do when all cases are ignored.
http://php.net/manual/fr/control-structures.switch.php
here's some pseudo-code (it's not written correctly, the point of my ? is the variable, not the switch):
switch ($action) {
case "1":
//this is a function
case "2":
//this is a function
//etc.
}
How should this be written:
$variable = result of function in case 1.
Your switch statement is wrong . It requires a break keyword between every case
$action = 1;
$result = "Success";
switch ($action) {
case 1:
$variable = $result;
echo $variable;//prints Success
//this is a function
break; // like this
case 2:
//this is a function
break;//
//etc.
}
Just run the function(s) (you can pass args in too) as part of the code within the case / break blocks like this :
$action = 1;
switch ($action) {
case 1:
$variable = someFunctionOne();
break;
case 2:
$variable = someOtherFunctionTwo();
break;
//etc.
}
how to variable the result of php switch.
ex:
<?php
//variables of cases
$var_1 = 1;
$var_2 = 2;
$var_3 = 3;
$var_0 = 0;
//end variables of cases
//action variable
$action = 10;
//end action variable
//start switch
switch ($action) {
case "1":
echo "$var_1;";
break;
case "2":
echo "$var_2;";
break;
case "3":
echo "$var_3;";
break;
default:
echo "$var_0;";
}
//receives the value of the switch.
$switch_result = get_result_case;
//in this my example I need to enter the value of the case in a variable.
?>
in this my example I need to enter the value of the case in a variable.
I created a simple sample php file for display some function outputs. here is the code..
<?php
// $printName = hello('samitha');
// $printHeader = pageHeader('main','on');
switch (key($_GET)){
case 'red':
$printHeader = pageHeader('red','on');
$printName = hello('Joel');
break;
case 'blue':
$printHeader = pageHeader('blue','off');
$printName = hello('Duck');
break;
case 'yellow':
//$printHeader = pageHeader('yellow','on');
break;
}
function hello($name){
return $name;
}
function pageHeader($header,$onoff){
if ($onoff == 'on') {
return $header."page header<br>";
}
else {return null;}
}
echo $printHeader;
echo $printName;
?>
This code is working fine without any problems.
When I call 'example.com/php/tipo34.php?red', it shows on the screen:
redpage header
Joel
And when I call 'example.com/php/tipo34.php?blue' it shows on the screen:
Duck
I tried to put the below functions inside of another php file called tipo34-req.php and received the following error:
Fatal error: Call to undefined function pageHeader() in C:\wamp\www\php\tipo34.php on line 8
The code I tried:
<?php
// $printName = hello('samitha');
// $printHeader = pageHeader('main','on');
switch (key($_GET)){
case 'red':
$printHeader = pageHeader('red','on');
$printName = hello('samitha');
break;
case 'blue':
$printHeader = pageHeader('blue','off');
$printName = hello('kalum');
break;
case 'yellow':
//$printHeader = pageHeader('yellow','on');
break;
}
include 'tipo34-req.php';
echo $printHeader;
echo $printName;
?>
tipo34-req.php code:
<?php
function hello($name){
global $name;
return $name;
}
function pageHeader($header,$onoff){ global $header, $onoff
if ($onoff == 'on') {
return $header."page header<br>";
}
else {return null;}
}
?>
How do I solve this problem? Using the function directly on the file works, but when I put the functions in another php file, it throws the error.
Thanks.
Include your file above its contents usage. PHP is unaware of the functions since they are included later in the code.
include 'tipo34-req.php';
switch (key($_GET)){
case 'red':
$printHeader = pageHeader('red','on');
$printName = hello('samitha');
break;
case 'blue':
$printHeader = pageHeader('blue','off');
$printName = hello('kalum');
break;
case 'yellow':
//$printHeader = pageHeader('yellow','on');
break;
}
echo $printHeader;
echo $printName;
?>
Have you tried including the file at the top before calling any of the functions?
When I execute this following code for example: cart.php?p=1&action=add
<?php
session_start();
if (isset($_GET['p']) && isset($_GET['action'])) {
$p_id = (int)$_GET['p'];
$action = $_GET['action'];
switch($action) {
case "add":
$_SESSION['cart'][$p_id]++;
echo $_SESSION['cart'][$p_id];//for debug
break;
case "remove":
$_SESSION['cart'][$p_id]--;
echo $_SESSION['cart'][$p_id];
if($_SESSION['cart'][$p_id] == 0) unset($_SESSION['cart'][$p_id]);
break;
case "empty":
unset($_SESSION['cart']);
break;
}
}
?>
I get the following error:
Notice: Undefined offset: 1 in cart.php on line 11
How can i configure the PHP $_SESSION corectly? I
Change this:
$_SESSION['cart'][$p_id]++
to:
$_SESSION['cart'][$p_id] = 1 + (isset($_SESSION['cart'][$p_id]) ? $_SESSION['cart'][$p_id] : 0);
So that you don't try to increment a non existent element in your array.