php switch menu undefined variable [duplicate] - php

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 9 years ago.
The following code has been moved to a new server and is throwing this error:
Notice: Undefined variable: menu in * on line 128
Notice: Undefined variable: menu in * on line 160
Notice: Undefined variable: menu in * on line 170
Here is the code:
Profile
Regisztráció
Kapcsolat
<?php switch($menu)
{
case "profile":
{
echo("profil");
}
case "regisztracio":
{
echo("regisztráció");
}
case "kapcsolat":
{
echo("kapcsolat");
}
default:
{
echo("Home page");
}
}
?>

I didn't understand your lang but the problrem is you are not using $_GET['menu'] to retrieve the GET parameter.
$menu = $_GET['menu'];
switch($menu) {
....
}

Profile
Regisztráció
Kapcsolat
here "menu " is not a php variable. You should pass value as $menu to switch ( $menu = $_GET['menu']; ). Not "menu" to switch.

$menu is undefined.
It is not set anywhere, e.g.
$menu = "profile";

Related

PHP select from database and return value [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
PHP undefined variable when selected data is not present in the database [duplicate]
(3 answers)
Undefined variable error when no MySQL query produces no results [closed]
(2 answers)
Closed 3 years ago.
I want to display comments on multiple pages, so I want to select the comments from the database on 1 page and include those comment on all the other pages.
I can select and display each comment, but some post's don't have comments. Now I get this error for each post without comments. Notice: Undefined variable: showComments in ...\comments.php on line 7
The page to select the comments:
class Comment {
public static function displayComments($postId) {
$comments = DB::query('SELECT comments FROM table WHERE post_id=:postid', array(':postid'=>$postId);
foreach($comments as $comment) {
$showComments[] = $comment['comment'];
}
return $showComments;//this is line 7
}
}
Other pages:
$postOutput = "postImg, postLikes, postLikeButton";
if(Comment::displayComments($post['id']) >= 1) {
$comments = Comment::displayComments($post['id']);
foreach ($comments as $comment) {
$postOutput .= $comment;
}
}
$postOutput .= "postCommentForm";
echo $postOutput;
Define empty array before you called it. And while you run foreach loop check empty condition. Right now what happens you're not getting comments from a query that's why this is happening. Try this.
class Comment {
public static function displayComments($postId) {
$showComments = array(); //this sould be defined in your code
$comments = DB::query('SELECT comments FROM table WHERE post_id=:postid', array(':postid'=>$postId);
if(!empty($comments)){ //check not empty condition.
foreach($comments as $comment) {
$showComments[] = $comment['comment'];
}
}
return $showComments;//this is line 7
}
}

Notice: Use of undefined constant for already defined variable [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 5 years ago.
I've got this code.
$rollcount=0;
$rollcounts=array(); //I define $rollcounts here
$number_of_tries = 100;
foreach(range(0,$number_of_tries-1) as $i){
do{
$roll=rand(1,6);
$rollcount++;
}while($roll!=6);
array_push($rollcounts, $rollcount);
$rollcount = 0;
}
$freqs = array();
while (!empty($rollcounts)){
$freq = count(array_filter($rollcounts,function($a) use ($rollcounts)
{return $a == $rollcounts[0];}
));
$freqs[$rollcounts[0]] = $freq;
for($i=0;$i<count($rollcounts);$i++){
if(rollcounts[$i] == $rollcounts[0]){ // THIS IS LINE 40
unset($rollcounts[$i]);
}
}
} // redo until $rollcounts is empty
That generates this error message (line 40 has been commented in the code)
Notice: Use of undefined constant rollcounts - assumed 'rollcounts'
in /Applications/XAMPP/xamppfiles/htdocs/learningphp/myfirstfile.php
on line 40
Clearly, $rollcounts has already been defined in the code. So what is the problem here?
You forgot a $
Old code
if(rollcounts[$i] == $rollcounts[0]){ // THIS IS LINE 40
unset($rollcounts[$i]);
}
New code
if($rollcounts[$i] == $rollcounts[0]){ // THIS IS LINE 40
unset($rollcounts[$i]);
}

php foreach error first line [duplicate]

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.
I always have error for first array in table.
foreach ($status_lines as $status_line) {
$xxx [] = $status_line -> status ;
}
if (count(array_unique($xxx)) == 1 && end($xxx) == 'REJECTED') { ?>
<b class="text-gray"> N / A </b>
<?php }
elseif (count(array_unique($xxx)) == 1 && end($xxx) == 'NOT APPROVED') { ?>
<b class="text-gray"> N / A </b>
<?php }
it resulting : Message: Undefined variable: xxx
but for the second line to the end in table is OK ...
Your variable $xxx has been defined within your foreach block. It is not defined anywhere else.
Define it outside the block as a global variable:
$xxx = array();
Then continue your foreach loop as follows:
foreach ($status_lines as $status_line) {
$xxx[] = $status_line -> status ;
}
...
Define it before use as
$xxx = array();
foreach ($status_lines as $status_line) {
$xxx[] = $status_line -> status ;
}
If you don't declare a new array, and the data that creates / updates the array fails for any reason, then any future code that tries to use the array will warning because the array doesn't exist.
For example, foreach() will throw an error if the array was not declared and no values were added to it. However, no errors will occur if the array is simply empty, as would be the case had you declared it.

PHP - Offset Error Encountered [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 8 years ago.
Not sure why I'm getting this error as the code is working and I can see the data I expected.
Code
$allSales = array();
foreach ($game['sales'] as $sale) {
foreach ($sale['values'] as $id => $values) {
$allSales[$id]+=$values['y'];
}
}
Error 1
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 0
Filename: player/game.php
Line Number: 81
Error 2 (Same Code)
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 1
Filename: player/game.php
Line Number: 81
The statement:
$allSales[$id] += $values['y'];
means to take the current value of $allSales[$id], add the value of $values['y'] to it, and store that result back in $allSales[$id]. But the first time that you encounter a particular $id, there is no $allSales[$id]. This results in the warning when you try to get its current value.
Change to:
if (isset($allSales[$id])) {
$allSales[$id] += $values['y'];
} else {
$allSales[$id] = $values['y'];
}
Or you could just prefix the line with # to suppress warnings (I know purists will cry over this):
#$allSales[$id] += $values['y'];

Notice: Undefined index: sid in C:\wamp\www\test\inc\template.php on line 205 [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 8 years ago.
im still new to php , and i got this error .. pls help me out here , thx .
Notice: Undefined index: sid in C:\wamp\www\test\inc\template.php on line 205
so , here is my code :
<?php
}
elseif($_SESSION['sid'] != '')
{
?>
$_SESSION is an array so you set it like so $_SESSION['sid'] = 'some sid'; if you try to use $_SESSION['sid'] without setting it then PHP throws a notice.
If you want to avoid the notice then one of these options will work:
Option 1
<?php
// check if the variable isset before using it
if(isset($_SESSION['sid']) && $_SESSION['sid'] != '')
{
// do something
}
?>
Option 2
<?php
// turn off error reporting
// NEVER DO THIS IN A DEVELOPMENT ENVIRONMENT
// error_reporting(E_ALL) should be used in development
error_reporting(0);
if($_SESSION['sid'] != '')
{
// do something
}
?>
If you want to see if a session variable is set (or any variable for that matter), use isset(). If you just want to see if it is empty, use empty() after using isset()
elseif(isset($_SESSION['sid']) && !empty($_SESSION['sid']))

Categories