Forum Moderators: coopster

Message Too Old, No Replies

Undefined variable problem

         

tottyovi

12:38 pm on Jul 8, 2009 (gmt 0)

10+ Year Member



I'm learning PHP and MySQL after a video tutorial.
Now I have a problem and in tutorial isn't this problem.

The errors are:
Notice: Undefined variable: sel_subj in C:\wamp\www\widget_corp\content.php on line 37
Notice: Undefined variable: sel_page in C:\wamp\www\widget_corp\content.php on line 38

The source code is:

<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php
if (isset($_GET['subj'])) {
$sel_subj = $_GET['subj'];
$sel_subj = "";
} elseif (isset($_GET['page'])) {
$sel_page = $_GET['page'];
$sel_page = "";
} else {
$sel_subj = "";
$sel_page = "";
}
?>
<?php include("includes/header.php"); ?>
<table id="structure">
<tr>
<td id="navigation">
<ul class="subjects">
<?php
$subject_set = get_all_subjects();

while ($subject = mysql_fetch_array($subject_set)) {
echo "<li><a href=\"content.php?subj=" . urlencode($subject["id"]) . "\">{$subject["menu_name"]}</a> </li>";
$page_set = get_pages_for_subject($subject["id"]);
echo "<ul class=\"pages\">";
while ($page = mysql_fetch_array($page_set)) {
echo "<li><a href=\"content.php?page=" . urlencode($page["id"]) . "\"> {$page["menu_name"]}</a> </li>";
}
echo"</ul>";
}
?>
</ul>
</td>
<td id="page">
<h2>Content Area</h2>
<?php echo $sel_subj ; ?><br />
<?php echo $sel_page ; ?><br />
</td>
</tr>
</table>

<?php require("includes/footer.php"); ?>

Could somebody help me please?
Thank you

andrewsmd

2:18 pm on Jul 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You are saying $sel_subj = $GET.. and then right after that you are setting it equal to "";
The warning is saying $sel_subj is undefined because you set it to "" remove the lines
if (isset($_GET['subj'])) {
$sel_subj = $_GET['subj'];
//$sel_subj = "";
} elseif (isset($_GET['page'])) {
$sel_page = $_GET['page'];
//$sel_page = "";
} else {
$sel_subj = "";
$sel_page = "";
}
And notices don't mean that your program will fail you may want to put this at the begenning of your program
error_reporting(E_ERROR ¦ E_WARNING ¦ E_PARSE);

tottyovi

4:25 pm on Jul 8, 2009 (gmt 0)

10+ Year Member



I removed that lines and I have the same problem.
How to put this:error_reporting(E_ERROR ¦ E_WARNING ¦ E_PARSE); ? Or where? On the php file?
Thank you

tottyovi

4:27 pm on Jul 8, 2009 (gmt 0)

10+ Year Member



I did exactly how was in the video tutorial, I checked 3 times.

andrewsmd

4:43 pm on Jul 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



At the beginning of your php file.

<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php
error_reporting(E_ERROR ¦ E_WARNING ¦ E_PARSE);
if (isset($_GET['subj'])) {
$sel_subj = $_GET['subj'];
$sel_subj = "";
} elseif (isset($_GET['page'])) {
$sel_page = $_GET['page'];
$sel_page = "";
} else {
$sel_subj = "";
$sel_page = "";
}
?>
<?php include("includes/header.php"); ?>
<table id="structure">
<tr>
<td id="navigation">
<ul class="subjects">
<?php
$subject_set = get_all_subjects();

while ($subject = mysql_fetch_array($subject_set)) {
echo "<li><a href=\"content.php?subj=" . urlencode($subject["id"]) . "\">{$subject["menu_name"]}</a> </li>";
$page_set = get_pages_for_subject($subject["id"]);
echo "<ul class=\"pages\">";
while ($page = mysql_fetch_array($page_set)) {
echo "<li><a href=\"content.php?page=" . urlencode($page["id"]) . "\"> {$page["menu_name"]}</a> </li>";
}
echo"</ul>";
}
?>
</ul>
</td>
<td id="page">
<h2>Content Area</h2>
<?php echo $sel_subj ; ?><br />
<?php echo $sel_page ; ?><br />
</td>
</tr>
</table>

I don't think you understand what your problem is. You are trying to echo $_GET['subj'] and $_GET['page']; The notice is telling you that they are undefined, meaning they are nothing. Whatever url this page is at, if page and subj need to be something your url should look like
www.somesite.com/index.php?subj=yourvaluehere&page=yourothervalue
It is saying the get variables are not there. Where are they defined.

tottyovi

5:28 pm on Jul 8, 2009 (gmt 0)

10+ Year Member



So...I created a database + some php files; here you have one and this is the file where I have problem.
I insert that code(with error....) and now isn't show me the that 2 errors BUT no text from my database(must show me some numbers...like in video tutorial)when I click to the links(to the menu)
To be hones I'm a little confuse...but I hope tou can help me.
Thanks a lot.

andrewsmd

5:33 pm on Jul 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Post your database table structure and your entire html and php file.

tottyovi

5:48 pm on Jul 8, 2009 (gmt 0)

10+ Year Member



Database:
Widget_corp
pages
subjects
users

PAGES
id subject_id menu_name position visible content
1 1 History 1 1 comments
2 1 Our Mission 2 1 comments
3 2 Widget 1 1 comments

SUBJECTS
id menu_name position visible
1 About Widget Corp 1 1
2 Products 2 1
3 Services 3 1

HTML
index....is empty, so basic html file

PHP<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php
if (isset($_GET['subj'])) {
$sel_subj = $_GET['subj'];
$sel_subj = ""; //I tried without this line
} elseif (isset($_GET['page'])) {
$sel_page = $_GET['page'];
$sel_page = ""; //I tried without this line
} else {
$sel_subj = "";
$sel_page = "";
}
?>
<?php include("includes/header.php"); ?>
<table id="structure">
<tr>
<td id="navigation">
<ul class="subjects">
<?php
$subject_set = get_all_subjects();

while ($subject = mysql_fetch_array($subject_set)) {
echo "<li><a href=\"content.php?subj=" . urlencode($subject["id"]) . "\">{$subject["menu_name"]}</a> </li>";
$page_set = get_pages_for_subject($subject["id"]);
echo "<ul class=\"pages\">";
while ($page = mysql_fetch_array($page_set)) {
echo "<li><a href=\"content.php?page=" . urlencode($page["id"]) . "\"> {$page["menu_name"]}</a> </li>";
}
echo"</ul>";
}
?>
</ul>
</td>
<td id="page">
<h2>Content Area</h2>
<?php echo $sel_subj ; ?><br />
<?php echo $sel_page ; ?><br />
</td>
</tr>
</table>

<?php require("includes/footer.php"); ?>

I have also stylesheets, which I guess are not important in this context. Footer + header + constants, again not important for my errors +
FUNCTIONS

<?php
//This file is the place to store all basic functions
function confirm_query($result_set) {
if(!$result_set) {
die("Database connection failed:" . mysql_error());
}
}

function get_all_subjects() {
global $connection;
$query = "SELECT *
FROM subjects
ORDER BY position ASC";
$subject_set = mysql_query($query, $connection);
confirm_query($subject_set);
return $subject_set;
}

function get_pages_for_subject($subject_id) {
global $connection;
$query = "SELECT *
FROM pages
WHERE subject_id={$subject_id}
ORDER BY position ASC";
$page_set = mysql_query($query, $connection);
confirm_query($page_set);
return $page_set;
}
?>

andrewsmd

6:21 pm on Jul 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do me a favor and replace this

<?php
if (isset($_GET['subj'])) {
$sel_subj = $_GET['subj'];
$sel_subj = ""; //I tried without this line
} elseif (isset($_GET['page'])) {
$sel_page = $_GET['page'];
$sel_page = ""; //I tried without this line
} else {
$sel_subj = "";
$sel_page = "";
}
?>

with

<?php
if (isset($_GET['subj'])) {
$sel_subj = $_GET['subj'];
$sel_subj = ""; //I tried without this line
} elseif (isset($_GET['page'])) {
$sel_page = $_GET['page'];
$sel_page = ""; //I tried without this line
} else {
$sel_subj = "";
$sel_page = "";
var_dump($_GET);
}
?>

and post the output

tottyovi

6:31 pm on Jul 8, 2009 (gmt 0)

10+ Year Member



Notice: Undefined variable: sel_subj in C:\wamp\www\widget_corp\content.php on line 38
Notice: Undefined variable: sel_subj in C:\wamp\www\widget_corp\content.php on line 39

These appeared only when I clicked to menu. So , nothing.

andrewsmd

6:39 pm on Jul 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try this then, what I am trying to do is see what exactly is in $_GET. I don't know if you know this, but php treats $_GET as an array that's why you have to say $_GET['varName'];
<?php
var_dump($_GET);
if (isset($_GET['subj'])) {
$sel_subj = $_GET['subj'];
$sel_subj = ""; //I tried without this line
} elseif (isset($_GET['page'])) {
$sel_page = $_GET['page'];
$sel_page = ""; //I tried without this line
} else {
$sel_subj = "";
$sel_page = "";

}
?>

andrewsmd

6:45 pm on Jul 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not for sure if you understand what $_GET means. Try out this sample code. Notice the URL once you click the submit button.
<html>
<?php

//if we do a var dump here we will see all of the $_GET data
//we will not see anything on the page load because we
//haven't submitted any data yet. after we submit data this will echo
//stuff
var_dump($_GET);

?>
<form name = "myForm" method="get">

<input type = "text" name = "myText">
<input type = "text" name = "myText2">
<input type = "submit" value = "submit" name = "submit">
</form>
<?php

if(isset($_GET['submit'])){

//myText is the name of the text box
echo("text 1 = {$_GET['myText']} <br>text 2 = {$_GET['myText2']}");

}//if isset

?>
</html>

tottyovi

6:55 pm on Jul 8, 2009 (gmt 0)

10+ Year Member



Thank you very, very much for your help. I will try tomorrow morning because now I'm very tired. I hope you can help me tomorrow, too.

andrewsmd

7:01 pm on Jul 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sure thing.

tottyovi

9:27 am on Jul 9, 2009 (gmt 0)

10+ Year Member



I cleaned up the page, I inserted you code and:

array(3) { ["myText"]=> string(0) "" ["myText2"]=> string(0) "" ["submit"]=> string(6) "submit" }
text 1 =
text 2 =

andrewsmd

1:30 pm on Jul 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is what I am trying to explain to you. Type in something into those textboxes and click the submit button. It will display the input you type in. $_GET is to "get" data entered from the client. Once you understand that it will help you with your $_GET problem in your code.

tottyovi

2:56 pm on Jul 9, 2009 (gmt 0)

10+ Year Member



I'm sorry but I don't understand you.
What to do now?
i inserted your code...I typed something in textboxes...click to submit ->
array(3) { ["myText"]=> string(4) "aaaa" ["myText2"]=> string(6) "dddddd" ["submit"]=> string(6) "submit" }
text 1 = aaaa
text 2 = dddddd

What to do know?
How I can get your yahoo ID, please?
Thanks a lot

andrewsmd

3:23 pm on Jul 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am trying to show you that $_GET is data that is inputed from the user. Looking at your data you typed aaaa in the first textbox and ddddd in the second one. In your original code you were getting an error in your code because your sel_subj and sel_page variables are undefined. This code is wrong
<?php
if (isset($_GET['subj'])) {
$sel_subj = $_GET['subj'];
$sel_subj = "";
} elseif (isset($_GET['page'])) {
$sel_page = $_GET['page'];
$sel_page = "";
} else {
$sel_subj = "";
$sel_page = "";
}
?>
You are checking if $_GET['page'] and subj are set. If they are you set a variable equal to them and then set it equal to nothing again. change the previous code to this and see what happens
<?php
if (isset($_GET['subj'])) {
$sel_subj = $_GET['subj'];
$sel_page = $_GET['page'];
} elseif (isset($_GET['page'])) {
$sel_page = $_GET['page'];
$sel_subj = $_GET['subj'];
} else {
$sel_subj = "";
$sel_page = "";
}
?>