co.ador Posted July 27, 2009 Share Posted July 27, 2009 hey guys on line 484 is an undefined variable and I commented the line on the script so you will be able to see which one is line 484. error appearance Notice: Undefined variable: num_1_star in C:\wamp\www\shoes\stores\itemdetails2.php on line 484 <?php $sql="SELECT rating, COUNT(rating) FROM rating WHERE (item_name='$platename') GROUP BY rating"; $result=mysql_query($sql); if (mysql_num_rows($result) > 0) { while ($row= mysql_fetch_assoc($result)) { $star= $row['rating']; switch($star) { case '1': $num_1_star= $row['COUNT(rating)']; break 1; case '2': $num_2_star= $row['COUNT(rating)']; break 1; case '3': $num_3_star= $row['COUNT(rating)']; break 1; case '4': $num_4_star= $row['COUNT(rating)']; break 1; case '5': $num_5_star= $row['COUNT(rating)']; break 1; } echo '<h3>1 Star Active</h3> <ul class="rating onestar"> <li class="one"><a href="#" title="1 Star">1</a></li> <li class="two"><a href="#" title="2 Stars">2</a></li> <li class="three"><a href="#" title="3 Stars">3</a></li> <li class="four"><a href="#" title="4 Stars">4</a></li> <li class="five"><a href="#" title="5 Stars">5</a></li> <li class="total">['. $num_1_star['rating'].'</li> // line 484 </ul> <h3>2 Stars Active</h3> <ul class="rating twostar"> <li class="one"><a href="#" title="1 Star">1</a></li> <li class="two"><a href="#" title="2 Stars">2</a></li> <li class="three"><a href="#" title="3 Stars">3</a></li> <li class="four"><a href="#" title="4 Stars">4</a></li> <li class="five"><a href="#" title="5 Stars">5</a></li> <li class="total">['. $row['rating'].'</li> // When I put it $row['rating'] then it prints the index number location which is 2, the rating field is in the index number 2 </ul> <h3>3 Stars Active</h3> <ul class="rating threestar"> <li class="one"><a href="#" title="1 Star">1</a></li> <li class="two"><a href="#" title="2 Stars">2</a></li> <li class="three"><a href="#" title="3 Stars">3</a></li> <li class="four"><a href="#" title="4 Stars">4</a></li> <li class="five"><a href="#" title="5 Stars">5</a></li> <li class="total">[60]</li> </ul> <h3>4 Stars Active</h3> <ul class="rating fourstar"> <li class="one"><a href="#" title="1 Star">1</a></li> <li class="two"><a href="#" title="2 Stars">2</a></li> <li class="three"><a href="#" title="3 Stars">3</a></li> <li class="four"><a href="#" title="4 Stars">4</a></li> <li class="five"><a href="#" title="5 Stars">5</a></li> <li class="total">[80]</li> </ul> <h3>5 Stars Active</h3> <ul class="rating fivestar"> <li class="one"><a href="#" title="1 Star">1</a></li> <li class="two"><a href="#" title="2 Stars">2</a></li> <li class="three"><a href="#" title="3 Stars">3</a></li> <li class="four"><a href="#" title="4 Stars">4</a></li> <li class="five"><a href="#" title="5 Stars">5</a></li> <li class="total">[100]</li> </ul>'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/167558-solved-hi-guys-i-am-getting-this-undefined-variable-error/ Share on other sites More sharing options...
almightyegg Posted July 27, 2009 Share Posted July 27, 2009 Should it not just be $num_1_star without the rating thing, or am I missing soemthing Quote Link to comment https://forums.phpfreaks.com/topic/167558-solved-hi-guys-i-am-getting-this-undefined-variable-error/#findComment-883549 Share on other sites More sharing options...
vineld Posted July 27, 2009 Share Posted July 27, 2009 You have no variable called $row['COUNT(rating)'], change that first of all. Quote Link to comment https://forums.phpfreaks.com/topic/167558-solved-hi-guys-i-am-getting-this-undefined-variable-error/#findComment-883550 Share on other sites More sharing options...
co.ador Posted July 27, 2009 Author Share Posted July 27, 2009 This is going to count the times that users has rated item=x as 1 star then users has rated item=x as 2 stars etc until you get to five stars. So i think it need to have a variable that indentify what variable is counting 1 star, 2 stars , three stars of item='x' to place it in the html code. Quote Link to comment https://forums.phpfreaks.com/topic/167558-solved-hi-guys-i-am-getting-this-undefined-variable-error/#findComment-883554 Share on other sites More sharing options...
co.ador Posted July 27, 2009 Author Share Posted July 27, 2009 it is coming from while ($row= mysql_fetch_assoc($result)) i guess $row is needed there to specific where the index is coming from I guess? if you have any other suggestion . Quote Link to comment https://forums.phpfreaks.com/topic/167558-solved-hi-guys-i-am-getting-this-undefined-variable-error/#findComment-883557 Share on other sites More sharing options...
almightyegg Posted July 27, 2009 Share Posted July 27, 2009 <li class="total">['. $num_1_star['rating'].'</li> makes no sense try: <li class="total">$num_1_star</li> Quote Link to comment https://forums.phpfreaks.com/topic/167558-solved-hi-guys-i-am-getting-this-undefined-variable-error/#findComment-883560 Share on other sites More sharing options...
co.ador Posted July 27, 2009 Author Share Posted July 27, 2009 i tried taking the $row variable out then it produces a parse error Parse error: parse error in C:\wamp\www\shoes\stores\itemdetails2.php on line 460 <?php $sql="SELECT rating, COUNT(rating) FROM rating WHERE (item_name='$shoename') GROUP BY rating"; $result=mysql_query($sql); if (mysql_num_rows($result) > 0) { while ($row= mysql_fetch_assoc($result)) { $star= $row['rating']; switch($star) { case '1': $num_1_star=['COUNT(rating)']; // line 460 break 1; case '2': $num_2_star=['COUNT(rating)']; break 1; case '3': $num_3_star=['COUNT(rating)']; break 1; case '4': $num_4_star=['COUNT(rating)']; break 1; case '5': $num_5_star=['COUNT(rating)']; break 1; } echo '<h3>1 Star Active</h3> <ul class="rating onestar"> <li class="one"><a href="#" title="1 Star">1</a></li> <li class="two"><a href="#" title="2 Stars">2</a></li> <li class="three"><a href="#" title="3 Stars">3</a></li> <li class="four"><a href="#" title="4 Stars">4</a></li> <li class="five"><a href="#" title="5 Stars">5</a></li> <li class="total">['. $row['rating'].']</li> </ul> <h3>2 Stars Active</h3> <ul class="rating twostar"> <li class="one"><a href="#" title="1 Star">1</a></li> <li class="two"><a href="#" title="2 Stars">2</a></li> <li class="three"><a href="#" title="3 Stars">3</a></li> <li class="four"><a href="#" title="4 Stars">4</a></li> <li class="five"><a href="#" title="5 Stars">5</a></li> <li class="total">['. $row['rating'].']</li> </ul> <h3>3 Stars Active</h3> <ul class="rating threestar"> <li class="one"><a href="#" title="1 Star">1</a></li> <li class="two"><a href="#" title="2 Stars">2</a></li> <li class="three"><a href="#" title="3 Stars">3</a></li> <li class="four"><a href="#" title="4 Stars">4</a></li> <li class="five"><a href="#" title="5 Stars">5</a></li> <li class="total">[60]</li> </ul> <h3>4 Stars Active</h3> <ul class="rating fourstar"> <li class="one"><a href="#" title="1 Star">1</a></li> <li class="two"><a href="#" title="2 Stars">2</a></li> <li class="three"><a href="#" title="3 Stars">3</a></li> <li class="four"><a href="#" title="4 Stars">4</a></li> <li class="five"><a href="#" title="5 Stars">5</a></li> <li class="total">[80]</li> </ul> <h3>5 Stars Active</h3> <ul class="rating fivestar"> <li class="one"><a href="#" title="1 Star">1</a></li> <li class="two"><a href="#" title="2 Stars">2</a></li> <li class="three"><a href="#" title="3 Stars">3</a></li> <li class="four"><a href="#" title="4 Stars">4</a></li> <li class="five"><a href="#" title="5 Stars">5</a></li> <li class="total">[100]</li> </ul>'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/167558-solved-hi-guys-i-am-getting-this-undefined-variable-error/#findComment-883561 Share on other sites More sharing options...
MadTechie Posted July 27, 2009 Share Posted July 27, 2009 I have posted in your other thread with the exact same question but this is my last post.. we have rules for a reason.. ! Quote Link to comment https://forums.phpfreaks.com/topic/167558-solved-hi-guys-i-am-getting-this-undefined-variable-error/#findComment-883563 Share on other sites More sharing options...
co.ador Posted July 27, 2009 Author Share Posted July 27, 2009 I didn't know you have posted in the other thread i did another thread because the code I have posted in the other thread was so different from the one I have opened in here, but yes I have asked you for you opinion madtechie in the other thread for this matters Excuse me my purpose is not creating trouble is to evolve. Sorry if I have done something wrong here. Quote Link to comment https://forums.phpfreaks.com/topic/167558-solved-hi-guys-i-am-getting-this-undefined-variable-error/#findComment-883565 Share on other sites More sharing options...
vineld Posted July 27, 2009 Share Posted July 27, 2009 <li class="total">['. $num_1_star['rating'].'</li> makes no sense try: <li class="total">$num_1_star</li> No, you're wrong, it is always an array (at those times when it's set that is...). Quote Link to comment https://forums.phpfreaks.com/topic/167558-solved-hi-guys-i-am-getting-this-undefined-variable-error/#findComment-883571 Share on other sites More sharing options...
MadTechie Posted July 27, 2009 Share Posted July 27, 2009 I'm not sure what output your expecting but something like this would probably make more sense <?php $sql="SELECT rating, COUNT(rating) as total FROM rating WHERE (item_name='$shoename') GROUP BY rating"; $result=mysql_query($sql); $number = array("one","two","three","four","five"); if (mysql_num_rows($result) > 0) { while ($row= mysql_fetch_assoc($result)) { $star= $row['rating']; $total= $row['total']; echo '<h3>'.$star.' Star Active</h3> <ul class="rating '.$number[$star].'star"> <li class="one"><a href="#" title="1 Star">1</a></li> <li class="two"><a href="#" title="2 Stars">2</a></li> <li class="three"><a href="#" title="3 Stars">3</a></li> <li class="four"><a href="#" title="4 Stars">4</a></li> <li class="five"><a href="#" title="5 Stars">5</a></li> <li class="total">['.$total.']</li> </ul>'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/167558-solved-hi-guys-i-am-getting-this-undefined-variable-error/#findComment-883574 Share on other sites More sharing options...
co.ador Posted July 27, 2009 Author Share Posted July 27, 2009 I haven't totally mastered the last code you posted madtechie but i get a taste of it. i will repeat the <ul> as long as it find a number from one to 5 in the rating table. The only thing is that i would like to print all of the star even though it has been rated or not and then set to [0] the ones that hasn't been rated, Nice code though it will only need some modification let me keep analyzing it. Quote Link to comment https://forums.phpfreaks.com/topic/167558-solved-hi-guys-i-am-getting-this-undefined-variable-error/#findComment-883581 Share on other sites More sharing options...
co.ador Posted July 27, 2009 Author Share Posted July 27, 2009 i added and string to the $number variable and it will be "no" <?php $sql="SELECT rating, COUNT(rating) as total FROM rating WHERE (item_name='$platename') GROUP BY rating"; $result=mysql_query($sql); $number = array("no" "one","two","three","four","five"); if (mysql_num_rows($result) > 0) { while ($row= mysql_fetch_assoc($result)) { $star= $row['rating']; $total= $row['total']; echo '<h3>'.$star.' Star Active</h3> <ul class="rating '.$number[$star].'star"> <li class="one"><a href="#" title="1 Star">1</a></li> <li class="two"><a href="#" title="2 Stars">2</a></li> <li class="three"><a href="#" title="3 Stars">3</a></li> <li class="four"><a href="#" title="4 Stars">4</a></li> <li class="five"><a href="#" title="5 Stars">5</a></li> <li class="total">['.$total.']</li> </ul>'; } } ?> and what this does is that picks up a row of value in the css stylesheet called nostar .nostar {background-position:0 0} .onestar {background-position:0 -7px;} .twostar {background-position:0 -14px;} .threestar {background-position:0 -21px;} .fourstar {background-position:0 -28px;} .fivestar {background-position:0 -35px;} /* totals */ Quote Link to comment https://forums.phpfreaks.com/topic/167558-solved-hi-guys-i-am-getting-this-undefined-variable-error/#findComment-883586 Share on other sites More sharing options...
co.ador Posted July 27, 2009 Author Share Posted July 27, 2009 I have also taken the <a></a> tags because in this print i don't want to hover over effect that is actived in the css stylesheet if i leave the <a></a> tagas <?php $sql="SELECT rating, COUNT(rating) as total FROM rating WHERE (item_name='$platename') GROUP BY rating"; $result=mysql_query($sql); $number = array("no", "one","two","three","four","five"); if (mysql_num_rows($result) > 0) { while ($row= mysql_fetch_assoc($result)) { $star= $row['rating']; $total= $row['total']; echo '<h3>'.$star.' Star Active</h3> <ul class="rating '.$number[$star].'star"> <li class="one">1</li> <li class="two">2</li> <li class="three">3</li> <li class="four">4</li> <li class="five">5</li> <li class="total">['.$total.']</li> </ul>'; } } ?> the only thing is that the other stars won't print because they have not value and i would like to print them even though they haven't been set and equal it to [0] Quote Link to comment https://forums.phpfreaks.com/topic/167558-solved-hi-guys-i-am-getting-this-undefined-variable-error/#findComment-883590 Share on other sites More sharing options...
MadTechie Posted July 27, 2009 Share Posted July 27, 2009 try this (these are untested, I'm kinda lazy) <?php $sql="SELECT rating, COUNT(rating) as total FROM rating WHERE (item_name='$shoename') GROUP BY rating"; $result=mysql_query($sql); $number = array("no","one","two","three","four","five"); $total = array_fill(0, 6, 0); if (mysql_num_rows($result) > 0) { while ($row= mysql_fetch_assoc($result)) { $total[$star]= $row['total']; } foreach($number as $K =>$num) { echo '<h3>'.($K).' Star Active</h3> <ul class="rating '.$num.'star"> <li class="one"><a href="#" title="1 Star">1</a></li> <li class="two"><a href="#" title="2 Stars">2</a></li> <li class="three"><a href="#" title="3 Stars">3</a></li> <li class="four"><a href="#" title="4 Stars">4</a></li> <li class="five"><a href="#" title="5 Stars">5</a></li> <li class="total">['.$total[$K].']</li> </ul>'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/167558-solved-hi-guys-i-am-getting-this-undefined-variable-error/#findComment-883597 Share on other sites More sharing options...
co.ador Posted July 27, 2009 Author Share Posted July 27, 2009 The only thing was that the variable $star was not defined $star= $row['rating']; I defined before $total[$star]= $row['total']; because it was passing empty... <?php $sql="SELECT rating, COUNT(rating) as total FROM rating WHERE (item_name='$platename') GROUP BY rating"; $result=mysql_query($sql); $number = array("one","two","three","four","five"); $total = array_fill(0, 6, 0); if (mysql_num_rows($result) > 0) { while ($row= mysql_fetch_assoc($result)) { $star= $row['rating']; // place where $star needed to be defined so it can pass a value to next line. $total[$star]= $row['total']; } foreach($number as $K =>$num) { echo '<h3>'.($K).' Star Active</h3> <ul class="rating '.$num.'star"> <li class="one">1</li> <li class="two">2</li> <li class="three">3</li> <li class="four">4</li> <li class="five">5</li> <li class="total">['.$total[$K].']</li> </ul>'; } } ?> Thank you guys in special to madtechie for all your feedback without you guys I was not going to be able to achieve this and fix all this issues Quote Link to comment https://forums.phpfreaks.com/topic/167558-solved-hi-guys-i-am-getting-this-undefined-variable-error/#findComment-883622 Share on other sites More sharing options...
MadTechie Posted July 27, 2009 Share Posted July 27, 2009 Yep that's a correct fix.. shocked i missed it lol EDIT: your welcome, if this is solved can to click topic solved (bottom left) Quote Link to comment https://forums.phpfreaks.com/topic/167558-solved-hi-guys-i-am-getting-this-undefined-variable-error/#findComment-883624 Share on other sites More sharing options...
co.ador Posted July 27, 2009 Author Share Posted July 27, 2009 ups one more thing <?php $sql="SELECT rating, COUNT(rating) as total FROM rating WHERE (item_name='$shoename') GROUP BY rating"; $result=mysql_query($sql); $number = array("one","two","three","four","five"); $total = array_fill(0, 5, 0); if (mysql_num_rows($result) > 0) { while ($row= mysql_fetch_assoc($result)) { $star= $row['rating']; $total[$star]= $row['total']; } foreach($number as $K =>$num) { echo '<h3>'.($K).' Star Active</h3> <ul class="rating '.$num.'star"> <li class="one">1</li> <li class="two">2</li> <li class="three">3</li> <li class="four">4</li> <li class="five">5</li> <li class="total">['.$total[$K].']</li> </ul>'; } } ?> I took the "no" because there is nosense on putting a nonestar row in there. so I took it out plus I fixed the array_fill to (0, 5, 0). I have observe that if the item has not been rated then it wont display the <ul> on the browser. For instance the variable $shoename is coming from the url, so when it come to this page and the script we have work on realize that this shoename has not been rated then it won't display the star rows but if it has been rated then it will display even with one or anything then the rows will display. Quote Link to comment https://forums.phpfreaks.com/topic/167558-solved-hi-guys-i-am-getting-this-undefined-variable-error/#findComment-883632 Share on other sites More sharing options...
MadTechie Posted July 27, 2009 Share Posted July 27, 2009 i think you mean this <?php $sql="SELECT rating, COUNT(rating) as total FROM rating WHERE (item_name='$shoename') GROUP BY rating"; $result=mysql_query($sql); $number = array("no","one","two","three","four","five"); $total = array_fill(0, 6, 0); if (mysql_num_rows($result) > 0) { while ($row= mysql_fetch_assoc($result)) { $total[$row['rating']]= $row['total']; } } foreach($number as $K =>$num) { echo '<h3>'.($K).' Star Active</h3> <ul class="rating '.$num.'star"> <li class="one"><a href="#" title="1 Star">1</a></li> <li class="two"><a href="#" title="2 Stars">2</a></li> <li class="three"><a href="#" title="3 Stars">3</a></li> <li class="four"><a href="#" title="4 Stars">4</a></li> <li class="five"><a href="#" title="5 Stars">5</a></li> <li class="total">['.$total[$K].']</li> </ul>'; } ?> (its 3:30am) i'm going to bed EDIT option2 (I think) <?php $sql="SELECT rating, COUNT(rating) as total FROM rating WHERE (item_name='$shoename') GROUP BY rating"; $result=mysql_query($sql); $number = array("one","two","three","four","five"); $total = array_fill(1, 5, 0); if (mysql_num_rows($result) > 0) { while ($row= mysql_fetch_assoc($result)) { $total[$row['rating']]= $row['total']; } } foreach($number as $K =>$num) { echo '<h3>'.($K+1).' Star Active</h3> <ul class="rating '.$num.'star"> <li class="one"><a href="#" title="1 Star">1</a></li> <li class="two"><a href="#" title="2 Stars">2</a></li> <li class="three"><a href="#" title="3 Stars">3</a></li> <li class="four"><a href="#" title="4 Stars">4</a></li> <li class="five"><a href="#" title="5 Stars">5</a></li> <li class="total">['.$total[$K+1].']</li> </ul>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/167558-solved-hi-guys-i-am-getting-this-undefined-variable-error/#findComment-883635 Share on other sites More sharing options...
co.ador Posted July 27, 2009 Author Share Posted July 27, 2009 Wait I didn't see option two let me see. Hey option two solved the problem (1, 5 , 0) I did that before in the array_fill but never cross my mind to put a +1 in the $K variable That absolutely solved the problem even when the item hasn't been rated I will study the code Issue solved Quote Link to comment https://forums.phpfreaks.com/topic/167558-solved-hi-guys-i-am-getting-this-undefined-variable-error/#findComment-883637 Share on other sites More sharing options...
co.ador Posted July 27, 2009 Author Share Posted July 27, 2009 i don't know we can still post when i clicked solved when i thought it was solved. Well the script is perfect the only thing is that if i click in a item that hasn't been rated yet it won't show the 5 star rows set to [0]. It will only display if the rating has been rated and then will display the rows that hasn't been displayed also but when the item is nostar and i click in the link that takes me to the page then the ul of list won't show even to show the five star row empty with [0] I made a mistake i thought i have seen displaying the row of star even when the item hasn't been rated for the first time. Quote Link to comment https://forums.phpfreaks.com/topic/167558-solved-hi-guys-i-am-getting-this-undefined-variable-error/#findComment-883645 Share on other sites More sharing options...
co.ador Posted July 27, 2009 Author Share Posted July 27, 2009 Problem solved I was putting the = symbol before the greater symbol > if (mysql_num_rows($result) >=0) { I thought that the problem was there and tried the = symbol but i miss placed it before the > Thank you bro Quote Link to comment https://forums.phpfreaks.com/topic/167558-solved-hi-guys-i-am-getting-this-undefined-variable-error/#findComment-883651 Share on other sites More sharing options...
co.ador Posted July 27, 2009 Author Share Posted July 27, 2009 I forgot to say good night, time for me to go to sleep. Now I have to deal with the html placement of the code which it will be some another time for more fun.... Quote Link to comment https://forums.phpfreaks.com/topic/167558-solved-hi-guys-i-am-getting-this-undefined-variable-error/#findComment-883684 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.