Jump to content

[SOLVED] having an undefined index on line 465 help please


co.ador

Recommended Posts

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'

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']]++; 

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>';
}
} 
?>

$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!

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>';
}
} 
?>

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.