Jump to content

[SOLVED] hi guys I am getting this undefined variable error


co.ador

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 */

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.