SitePoint Sponsor |
|
User Tag List
Results 1 to 19 of 19
Thread: How Can I $_GET a variable?
-
May 13, 2009, 19:03 #1
How Can I $_GET a variable?
Notice: Undefined variable: rmenu in C:\wamp\www\nyhungry\restaurants\shoe1.php on line 76
I know how to global GET an integerPHP Code:" . (int) $_GET['shoe']
How could I global get the $rmenu variable so I can use it's value.
Thank you.Last edited by co.ador; May 13, 2009 at 20:21.
-
May 13, 2009, 20:02 #2
- Join Date
- Aug 2000
- Location
- Philadephia, PA
- Posts
- 20,578
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
Please restate your question in coherent English. An example of the problem would be useful as well.
The error you have means you tried to use a variable $rmenu that was not defined before you used it. The solution is to not use variables which you haven't defined.Try Improvely, your online marketing dashboard.
→ Conversion tracking, click fraud detection, A/B testing and more
-
May 13, 2009, 20:26 #3
It was defined before but the value of $rmenu does not pass
PHP Code:while($rmenu = mysql_fetch_array($regularshoe_set)){
echo "<li";
if ($rmenu["id"] == $sel_shoe) {
echo " class=\"selected\"";
}
echo "><a href=\"shoe1.php?menu=" . urlencode($rmenu["id"]) ."\";>
{$rmenu["shoename"]}</a></li>"; }
}
if ($rmenu["id"] == $sel_menu) { // this is line 76
Sorry about the incoherent English.
-
May 13, 2009, 20:53 #4
- Join Date
- Aug 2000
- Location
- Philadephia, PA
- Posts
- 20,578
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
That variable only exists within the loop. Even if it existed outside the loop, $rmenu would simply hold false as that was the last return value of mysql_fetch_array() when there were no more rows in the result set.
Try Improvely, your online marketing dashboard.
→ Conversion tracking, click fraud detection, A/B testing and more
-
May 13, 2009, 21:02 #5
so It doesn't exist.
I will abandon this possibility then.
Thanks though!
-
May 13, 2009, 21:04 #6
- Join Date
- Aug 2000
- Location
- Philadephia, PA
- Posts
- 20,578
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
Define a variable before the loop, set its value within the loop, and after the loop is over, it will hold the value from the last row.
Try Improvely, your online marketing dashboard.
→ Conversion tracking, click fraud detection, A/B testing and more
-
May 13, 2009, 21:20 #7
ok let me try difine a variable with that name before the loop starts
PHP Code:$regularshoe_set = mysql_query($query, $connection);
if(!$regularshoe_set){
die("Database query failed:" . mysql_error());}
$rmenu = mysql_fetch_array($regularshoe_set)
// I am not sure If I should define or call $rmenu with a mysql_fetch_array?
//another thing is that before the while loop I have an If statement.
//I am not sure if I should place this variable in between the if statement and the while loop thought it was better to locate it here.
echo "<ul class=\"submenu\">";
if ($shoesubject["id"] == $sel_shoekind['id']) {
while($rmenu = mysql_fetch_array($regularshoe_set)){
echo "<li";
if ($rmenu["id"] == $sel_shoe) {
echo " class=\"selected\"";
}
echo "><a href=\"shoe1.php?menu=" . urlencode($rmenu["id"]) ."\";>
{$rmenu["shoename"]}</a></li>"; }
}
if ($rmenu["id"] == $sel_menu) { // this is line 76
-
May 13, 2009, 21:31 #8
- Join Date
- Aug 2000
- Location
- Philadephia, PA
- Posts
- 20,578
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
Doing that will throw away the first row of the result set, since you advanced the row pointer twice before getting inside your loop by calling mysql_fetch_array twice.
PHP Code:$lastrow = array();
while ($rmenu = mysql_fetch_array($regularshoe_set)) {
// other code
$lastrow = $rmenu;
}
//now you can use $lastrow
Try Improvely, your online marketing dashboard.
→ Conversion tracking, click fraud detection, A/B testing and more
-
May 13, 2009, 21:56 #9
Thank you Dan Grossman!!!
I will keep trying tomorrow morning
-
May 13, 2009, 23:20 #10
- Join Date
- Apr 2008
- Location
- Temecula, CA
- Posts
- 278
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
One thing you should know about variables is that they can be set, unset, and not set at all. Being set means that the variable has a value of some sort applied to it. Unsetting a variable makes the variable lose all value, and a variable without a value is not set.
When debugging your php applications, you will see errors like the one you saw:
Notice: Undefined variable: rmenu in C:\wamp\www\nyhungry\restaurants\shoe1.php on line 76
and this error tells you that the rmenu variable was not set. If you thought you had set it, you were wrong.
Sometimes, programatically speaking, you will not know if a variable is set or not, and in that case, you can check if it is set or not, and do what is necessary.
Tonight, after reinstalling my operating system, wamp, and all of my programs, one of my applications wasn't working, and it was hard to track down, because of the complex nature of the framework I was using. In the end, my error came down to a problem similar to yours. My script was checking for a server variable - $_SERVER['HTTP_USER_AGENT'] - and it didn't exist. I used the php function isset() to fix my problem like this:
Code:if(isset($_SERVER['HTTP_USER_AGENT'])){ $ie6 = "MSIE 6.0"; $browser = $_SERVER['HTTP_USER_AGENT']; $browser = substr("$browser", 25, 8); if($browser == $ie6){ $this->template->javascripts[] = 'js/supersleight-min.js'; } }
-
May 14, 2009, 02:20 #11
- Join Date
- Oct 2008
- Location
- London
- Posts
- 862
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
This statement is false. Loops run in the scope they are created in so all variables set in a loop do exist outside it.
True, but it's got nothing to do with PHP throwing an undefined variable error. The variable is defined and set to FALSE.
@co.ador: Please paste in more code(especially the code after problematic line).
EDIT: After formatting your code correctly:
PHP Code:while($rmenu = mysql_fetch_array($regularshoe_set)) {
echo "<li";
if ($rmenu["id"] == $sel_shoe) {
echo " class=\"selected\"";
}
echo "><a href=\"shoe1.php?menu=" . urlencode($rmenu["id"]) ."\";>{$rmenu["shoename"]}</a></li>";
}
} // what block of code is this closing?
if ($rmenu["id"] == $sel_menu) { // this is line 76
Pawel Decowski (you should follow me on Twitter)
-
May 14, 2009, 05:57 #12
Skundbad it is debuged, I debuged it for a while but still showing up the problem. I will show some more detail so you guys can see more details.
after line 76 the fallowing bit of codePHP Code:if ($rmenu["id"] == $sel_menu) {
PHP Code:echo "<ul class=\"submenu2\">";
while($rmenu = mysql_fetch_array($regularshoe_set)){
echo "<li";
if ($rmenu["id"] == $sel_shoe) {
echo " class=\"selected\"";
}
echo "><a href=\"shoe1.php?menu=" . urlencode($rmenu["id"]) ."\";>{$rmenu["shoename"]}</a></li>"; }
echo "</ul>";
decowski this is the rest of the code before the while loop and what the curly braces below closes is the If statement after the mysql_query, Thank you for the help.
PHP Code:$regularshoe_set = mysql_query($query, $connection);
if(!$regularshoe_set){
die("Database query failed:" . mysql_error());}
if ($shoesubject["id"] == $sel_shoe['id']) {
while($rmenu = mysql_fetch_array($regularshoe_set)) {
echo "<li";
if ($rmenu["id"] == $sel_shoe) {
echo " class=\"selected\"";
}
echo "><a href=\"shoe1.php?menu=" . urlencode($rmenu["id"]) ."\";>{$rmenu["shoename"]}</a></li>";
}
} //That carly brace is closing the first If then statement at the top of the code.
if ($rmenu["id"] == $sel_menu) { // this is
-
May 14, 2009, 06:09 #13
- Join Date
- Oct 2008
- Location
- London
- Posts
- 862
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You have a logical error in your code:
PHP Code:if ($shoesubject["id"] == $sel_shoe['id']) {
while($rmenu = mysql_fetch_array($regularshoe_set)) {
// [...]
}
}
if ($rmenu["id"] == $sel_menu) {
Sorry I can't help more but I can't understand the purpose of this code.Pawel Decowski (you should follow me on Twitter)
-
May 14, 2009, 06:19 #14PHP Code:
if ($rmenu["id"] == $sel_shoe)
-
May 14, 2009, 06:42 #15
- Join Date
- Oct 2008
- Location
- London
- Posts
- 862
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
This is irrelevant. I'm talking about $rmenu variable, because that's what PHP is complaining about being not set. Look again at the snippet:
PHP Code:if ($shoesubject["id"] == $sel_shoe['id']) {
while($rmenu = mysql_fetch_array($regularshoe_set)) {
// [...]
}
}
if ($rmenu["id"] == $sel_menu) {
Last edited by decowski; May 14, 2009 at 08:36.
Pawel Decowski (you should follow me on Twitter)
-
May 14, 2009, 08:20 #16
ok I will analize the code and I will print it out later so you can see in details what is happening.
-
May 14, 2009, 08:36 #17
- Join Date
- Oct 2008
- Location
- London
- Posts
- 862
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Just a guess, as I don't know what your code is supposed to do, but maybe you are trying to do this:
PHP Code:if ($shoesubject["id"] == $sel_shoe['id']) {
while($rmenu = mysql_fetch_array($regularshoe_set)) {
// [...]
}
}
if (isset($rmenu) && $rmenu["id"] == $sel_menu) {
^^^^^^^^^^^^^
Pawel Decowski (you should follow me on Twitter)
-
May 14, 2009, 11:24 #18
Paul it is something like that...
I just didn't knwo how to set it up, I am not at my house right now but once I get there I will try This.
thank you paul. I will write you back guys around five eastertime.
-
May 14, 2009, 18:51 #19Paul I built the line below
PHP Code:if (isset($rmenu) && $rmenu["id"] == $sel_menu) {
The purpose (my purpose) that I talk about is described below this quote in details.
PS: I have directed this quote to you to explain the reasons of this thread only.
Thank you for your help so far PAUL
I will explain in details what I want to achieve first, and then I will post the code explaining and clarifying bit by bit for your understanding. in other words you will be able to see what can be done about the purpose or achievements i want to do.
What I want to achieve is:
First, I have three headings and within each of the three headings you will find different subheadings. The subheadings will only appears once an user click on any of the three headings when one heading is clicked on then the subheadings display downward. for example:
Illustration 1
Sneakers
- Nike
- Adidas
- Fila
Boots
DressShoes
In this illustration there are three headings; Sneakers, Boots and DressShoes. The one clicked on is Sneakers and it shows or display a list of subheadings. In this case we have three subheadings Nike, Addidas and Fila. if you notice the others Headings stay closed containing its subheadings within because they haven't been clicked on. The set up of the code or what the code I have built so far does as fallows:
1- Enter the web address and the three headings appears
Sneakers
Boots
DressShoes
Sneakers
- Nike
- Addias
- Fila
Boots
DressShoes
Sneakers
Boots
- Timberland
- Deakers
- Western Boots
DressShoes
5-And then display the subheadings of the recent clicked on heading in this case Boots and leave it displayed. if you notice the Sneakers and DressShoes stay closed containing its subheadings inside but not visible to the users at this point.
Second part
My Purpose is:
The subheadings as I said will only display downward when you click on one of the headings which is ok. It display the subheadings and when I click on one of the subheading then it shows the content of the selected subheading in a table that I have styled to be at the right side of the page.
Illustration 2
Sneakers
- Nike
- Addias
- Fila
Boots
DressShoes
1-Air jordan
2-Air force
3-Air jordan D-Zero
4-Jordan true Flight Man
5-Lebroms Nike
As it display the content of the subheading *Nike it closes all the headings and comeback to the default state leaving the content of the *Nike display.
Illustration 3
leave the Displayed content of the *Nike subheading but .
1-Air jordan
2-Air force
3-Air jordan D-Zero
4-Jordan true Flight Man
5-Lebroms Nike
Sneakers
Boots
DressShoes
in others words, it display the content of the subheading but it doesn't leave the subheadings displayed underneath the heading selected which in this case was Sneakers.So that if a user want to select or choose another subheading other than the *Nike subheading within the heading Sneakers, to display the content of any other desired subheading in the table placed to the right of the page, the user has to open the Sneakers heading again and choose any other subheading than the *Nike for instance *Adidas to see Adidas content.
Instead I want to display the headings as default just as it is, and once an user clicked on a heading then the subheadings underneath that heading display. Onto that point that's how the actual coding I have now is set up. But after that point I want the subheadings to display the content in the table styled at the right side of the web page and at the same time I want the subheadings underneath the clicked on heading in this case *Nike *Adidas *Fila to stay displayed and visible after an user click on one of the subheadings to display its content in the table, instead avoid the headings coming back to the default state which disappear all the subheadings underneath that subheading in this case Sneakers.
default state:
Sneakers
Boots
DressShoes
Sneakers
- Nike
- Addias
- Fila
Boots
DressShoes
back to:
default state
Sneakers
Boots
DressShoes
Sneakers
- Nike
- Addias
- Fila
Boots
DressShoes
1-Air jordan
2-Air force
3-Air jordan D-Zero
4-Jordan true Flight Man
5-Lebroms Nike
Third part
I have re-explained the purpose above enough, so that you guys can understand it without leaving doubts in your head. and If you still don't understand let me know I just here to clarify I need help.
Now that the purpose is clear let me explain the code set up I have so far.
PHP Code:<?php
if(isset($_GET['shoesubject'])){
$sel_subject = $_GET['shoesubject'];
$sel_shoe = "";
} elseif(isset($_GET['shoe])){
$sel_subject = "";
$sel_shoe = $_GET['shoe'];
}else {
$sel_subject= "";
$sel_shoe = "";
}
?>
The next bit of code it's what fallows after I (isset) the variables
PHP Code:<ul class="menu">
<?php
$query = "SELECT *
FROM menusubjects "; //headings are called on this query
$menusubject_set = mysql_query($query, $connection);
if(!$menusubject_set){
die("Database query failed:" . mysql_error());
}while($menusubject = mysql_fetch_array($menusubject_set)){ // this while loop display the list of heading which are three
echo "<li";
if ($menusubject["id"] == $sel_subject) {
echo " class=\"selected\"";
}
echo "><a href=\"example1.php?subject=" . urlencode($menusubject["id"]) ."\";>{$menusubject["Subject"]}</a></li>";
$query = "SELECT *
FROM regularmenu // these are the subheadings and they display beneath the headings
WHERE menusubject_id = {$menusubject["id"]}";
$regularmenu_set = mysql_query($query, $connection);
if(!$regularmenu_set){die("Database query failed:" . mysql_error());}
echo "<ul class=\"submenu\">";
if ($menusubject["id"] == $sel_subject) { // this if statement stays that the heading will only display the subheadings withing the next while loop
while($rmenu = mysql_fetch_array($regularmenu_set)){
echo "<li";
if ($rmenu["id"] == $sel_menu) {
echo " class=\"selected\"";
}
echo "><a href=\"example1.php?menu=" . urlencode($rmenu["id"]) ."\";>{$rmenu["platename"]}</a></li>"; }
}
echo "</ul>";
}
?>
</ul> </table>
There are a final part of the entire php code i have which contain a query that calls the content of the subheadings which will be displayed in a table found within this final part of the php code set up in this file, and it goes as follows:
PHP Code:<?php $query = "SELECT shoe.shoename, shoe.price, shoe.moreinfo
FROM shoe
WHERE
shoe_kind = ". (int) $_GET['shoe']; // $_GET value I extract it from the (isset) selection at the top of the code. This condition will show the content if the shoe_kind is == to $_GET value. I don't know if the query or purpose described above need a query to be place before this query or after this query and table found on this final part of my code.
echo $query
$result = mysql_query($query, $connection);
while ($content = mysql_fetch_array($result)) {
echo "<table style=\"float:left\">
<td width=\"150\" style=\"text-align:center;\">" . $row['shoename'] . "</td>
<tr>
<td height=\"100\" width=\"100\" style=\"position:relative;\">
<img src=\"../images/shoesname.jpg\" alt=\"sd\" width=\"97\" height=\"80\" border=\"1\" style=\"border-color:#FF6600;\" />
</td></tr>
<tr>
<td width=\"5\" height=\"21\" ></td><td>" . $row['price'] . "</td>
</tr>
<td>" . $row['moreinfo'] . "</td>
</table>";
}
?>
mysql_close($connection);
?>
Help please...
Again where could I locate a query and condition
where I Click on subheading in this case the *Nike subheading it shows the content of *Nike but instead of coming back to:
default state
Sneakers
Boots
DressShoes
Instead of leaving the subheadings of the Heading Sneakers visible and still shows the content like this.
Heading and subheadings in a table to the left of the page
Sneakers
- Nike
- Addias
- Fila
Boots
DressShoes
1-Air jordan
2-Air force
3-Air jordan D-Zero
4-Jordan true Flight Man
5-Lebroms Nike
Bookmarks