SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    hi guys I am getting this undefined variable error

    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 Code:
    <?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 (
    $rowmysql_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>'
    ;
    }

    ?>

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Try and give the column a name, stay away from COUNT as a name as its a reserved word, try 'CNT' - then reference it as shown.

    PHP Code:
    $sql="SELECT rating, COUNT(rating) AS CNT FROM rating WHERE (item_name='$platename') GROUP BY rating";

    $result=mysql_query($sql);

    if (
    mysql_num_rows($result)  > 0) {

        while (
    $rowmysql_fetch_assoc($result)) {

            
    $star$row['rating'];

            switch(
    $star) {

                case 
    '1':

                    
    $num_1_star$row['CNT'];

    ... 
    *Untested and a guess.*

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The only place in your script that defines that variable, is in the switch statement. case '1': needs to be true for the code which follows to be executed. If that case is never true, the variable never gets defined.

    You might consider defining the variable outside the switch, to make sure it always gets defined.

  4. #4
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok let me try that one.

  5. #5
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you cups problem solved.

    i have another question is this correct?

    if (mysql_num_rows($result) =>0) {
    I want the result to display when it is equal and greater than 0 as well.

  6. #6
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Problem solved I was putting the = symbol before the greater symbol
    if (mysql_num_rows($result) >=0) {


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •