co.ador Posted July 26, 2009 Share Posted July 26, 2009 I am receiving an undefined index error what could that be thank you .. Notice: Undefined index: in C:\wamp\www\shoes\stores\itemdetails2.php on line 465 1 Star Active $query ="SELECT rating , COUNT(*) AS rating_counts FROM rating WHERE item_name = '$platename' GROUP BY rating"; $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); // see if any rows were returned if (mysql_num_rows($result) > 0) { $ratings = array_fill(1,5,0); $rowsCount = count($result); // assume $rows is the result array for($i=0;$i<$rowsCount;$i++) { $ratings[$result[$i]['rating']]++; // line 465 i don't get why it is referring to undefined index because rating is a column field in the database rating I guess it is talking about 'rating' Quote Link to comment https://forums.phpfreaks.com/topic/167546-solved-having-an-undefined-index-on-line-465-help-please/ Share on other sites More sharing options...
MadTechie Posted July 26, 2009 Share Posted July 26, 2009 Let me explain a few things 1. MySQL Fetch, read up on mysql_fetch_assoc(), as your not using it 2. Arrays $var = array("test1"); echo count($var); //Returns 1 echo $var[0]; // returns test1 $x = count($var); echo $var[$x]; // returns "" + undefined index echo $var[$x-1]; // return test1 That should help solve the problem, if it doesn't then try a quick re-write (yet i have no idea what your doing with the rating variable $query ="SELECT rating , COUNT(*) AS rating_counts FROM rating WHERE item_name = '$platename' GROUP BY rating"; $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); if (mysql_num_rows($result) > 0) { $ratings = array_fill(1,5,0); while ($row = mysql_fetch_assoc($result)) { $ratings[$row['rating']]++; Quote Link to comment https://forums.phpfreaks.com/topic/167546-solved-having-an-undefined-index-on-line-465-help-please/#findComment-883536 Share on other sites More sharing options...
co.ador Posted July 26, 2009 Author Share Posted July 26, 2009 the ratings variable is suppose to hold the values from 1 to 5 and 0 found in the rating field of the table rating So it can choose and count all the 1 for that item Quote Link to comment https://forums.phpfreaks.com/topic/167546-solved-having-an-undefined-index-on-line-465-help-please/#findComment-883540 Share on other sites More sharing options...
co.ador Posted July 27, 2009 Author Share Posted July 27, 2009 Madtechi the script has evolve to this new while loop for the count of each stars rating an item. now instead of 465 is on 484 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/167546-solved-having-an-undefined-index-on-line-465-help-please/#findComment-883544 Share on other sites More sharing options...
vineld Posted July 27, 2009 Share Posted July 27, 2009 Don't ask the same question in two different threads.... Quote Link to comment https://forums.phpfreaks.com/topic/167546-solved-having-an-undefined-index-on-line-465-help-please/#findComment-883552 Share on other sites More sharing options...
MadTechie Posted July 27, 2009 Share Posted July 27, 2009 $num_1_star['rating'] will only be set IF the rateing is 1, any other times it won't be set! I don't see the point of even have a switch statement, why not just use the $star variable ? @vineld: Nice catch, if I had noticed I would of posted the same and ignored the question posted, breaking the rules will normally and should get you less help! Quote Link to comment https://forums.phpfreaks.com/topic/167546-solved-having-an-undefined-index-on-line-465-help-please/#findComment-883559 Share on other sites More sharing options...
co.ador Posted July 27, 2009 Author Share Posted July 27, 2009 madtechie is there a way that I can set it to [0] in the html code if there hasn't been any rating for that star? Quote Link to comment https://forums.phpfreaks.com/topic/167546-solved-having-an-undefined-index-on-line-465-help-please/#findComment-883566 Share on other sites More sharing options...
vineld Posted July 27, 2009 Share Posted July 27, 2009 I think there is something wrong with your underlying logic behind this code. The switch seems unnecessary as suggested. What is it exactly this page is supposed to do? Quote Link to comment https://forums.phpfreaks.com/topic/167546-solved-having-an-undefined-index-on-line-465-help-please/#findComment-883569 Share on other sites More sharing options...
co.ador Posted July 27, 2009 Author Share Posted July 27, 2009 I really produced an counting on the html <li> but as madtechie said only if it is set.... It printed a one when this had a 1 star rated by x user <?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> </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>'; } } ?><?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> </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/167546-solved-having-an-undefined-index-on-line-465-help-please/#findComment-883570 Share on other sites More sharing options...
co.ador Posted July 27, 2009 Author Share Posted July 27, 2009 it is a page that will have a shoe item and each user will rate this shoe then after storing it in the database dump below -- Table structure for table `rating` -- CREATE TABLE IF NOT EXISTS `rating` ( `item_name` varchar(100) NOT NULL, `ip_address` varchar(15) NOT NULL, `rating` tinyint(1) NOT NULL, `date_rated` date NOT NULL, PRIMARY KEY (`item_name`,`ip_address`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; -- -- Dumping data for table `rating` then there is a script that will average the rating of all the users and will print the total average of all the rating in a star row. Now i am trying to look for a modification where i can print the number X item has been rated as 1 star then as 2 stars etc... that's the script we were discussing above. Quote Link to comment https://forums.phpfreaks.com/topic/167546-solved-having-an-undefined-index-on-line-465-help-please/#findComment-883573 Share on other sites More sharing options...
MadTechie Posted July 27, 2009 Share Posted July 27, 2009 Can we stop posting in this thread and use the other one, hi guys I am getting this undefined variable error as its getting messy Quote Link to comment https://forums.phpfreaks.com/topic/167546-solved-having-an-undefined-index-on-line-465-help-please/#findComment-883576 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.