Jump to content

UGGHHH. Array is not getting defined.


Alex1646

Recommended Posts

In my website I want to have a rating system of stoires. I also want to avarage out the ratings (there on a scale of 1-10). I am putting all of my ratings in a array but it doesnt seem to be working. EVerytime I call the array I get a undefined error thingy.


$query3 = "
SELECT * FROM story_commets
WHERE story = '$id_get'
ORDER BY date_added
";
$select3 = mysql_query($query3) or die(mysql_error());
$x=1;

while($rows3 = mysql_fetch_assoc($select3))
{


$commetdb = $rows3['commet'];
$user_com_db = $rows3['user'];
$datedb = $rows3['date_added'];
$stardb = $rows3['star'];
//get profile picture
$query4 = "
SELECT * FROM story_commet
WHERE user = '$user_com_db'
";	
$select4 = mysqli_query($dbc, $query4);
$rows4 = mysqli_fetch_array($select4);
	$profile_pic = $rows4['profile_pic'];
	$user_id = $rows4['id'];
echo "
<div class='com_info'>
<img src='$profile_pic' width='600' height='568' /> <br />
<a href='http://scribofabula.com/soc?p=profile&id=$user_id'> $user_com_id </div> <br />
$datedb <br />

</div>
Rating: $stardb/10 <br />
$commetdb
  ";

$ratav[$x]=$stardb; 
$x++;
  
  
}

Link to comment
https://forums.phpfreaks.com/topic/229710-ugghhh-array-is-not-getting-defined/
Share on other sites

$id_get is defined at the begining of the page. There is a lot more code to display the story. But the id_get is the id of the story. As the var name tells it is a get variable.

First one:

Notice: Undefined variable: ratav in C:\wamp\www\Login\inc\page.php on line 115

Second One:

Warning: array_sum() expects parameter 1 to be array, null given in C:\wamp\www\Login\inc\page.php on line 116

Third One:

Notice: Undefined variable: ratav in C:\wamp\www\Login\inc\page.php on line 117

 

Here is the rest of the code if needed:

<?php 
$id_get = $_GET['id'];
//grab data from db
mysqlConnect();
//query*****************************************************
$query1 = "
SELECT * FROM story_info
WHERE story_id = '$id_get'
";

$select = mysql_query($query1) or die(mysql_error());

$rows1 = mysql_fetch_assoc($select);
//put data into vars
$titledb = $rows1['title'];
$sumdb = $rows1['sum'];
$notesdb = $rows1['notes'];
$storydb = $rows1['story'];
$otherdb = $rows1['other'];
$userdb = $rows1['user'];
$catdb = $rows1['cat'];
$ratingdb = $rows1['rating'];
//take data from user_info table
//query*****************************************************

$query2 = "
SELECT * FROM login_info
WHERE user='$userdb'
";
$select2 = mysql_query($query2) or die(mysql_error()); 
$rows2 = mysql_fetch_assoc($select2);
//take data from table
$id_db = $rows2['id'];
$profile_db = $rows2['profile_picture'];
?>
<div class='story'>
  <h2> <?php echo $titledb; ?> </h2>
  <?php echo $storydb?>
</div>
<div class='story_info'>
  <img src="<?php echo $profile_db;?>" width="172" height="166" /> <br />
  <a href="http://scribofabula.com/soc?p=profile&pid=<?php echo $profile_db; ?>"> <?php echo $userdb; ?> </a> ><br />
  <h3> Summary </h3>
  <?php echo $sumdb; ?>
  <h3>Catagory/Genre:</h3>
  <a href='http://scribofabula.com/soc?p=cat_view&gen=<?php echo $catdb ?>'> <?php echo $catdb; ?> </a> </p>
  <h3>Rating:</h3>
  <?php echo $ratingdb; ?>
<?php if (isset($notesdb))
		{
		echo "<h3> Author's Notes: </h3>";
		echo $notesdb;
	}
  ?>

  <?php 
  echo "<h3> Other Stories</h3>";
  echo $otherdb; ?>
</div> <br/>
<div class='com'>
<h2> Reviews: </h2>
<?php 
$query3 = "
SELECT * FROM story_commets
WHERE story = '$id_get'
ORDER BY date_added
";
$select3 = mysql_query($query3) or die(mysql_error());
$x=1;

while($rows3 = mysql_fetch_assoc($select3))
{


$commetdb = $rows3['commet'];
$user_com_db = $rows3['user'];
$datedb = $rows3['date_added'];
$stardb = $rows3['star'];
//get profile picture
$query4 = "
SELECT * FROM story_commet
WHERE user = '$user_com_db'
";	
$select4 = mysqli_query($dbc, $query4);
$rows4 = mysqli_fetch_array($select4);
	$profile_pic = $rows4['profile_pic'];
	$user_id = $rows4['id'];
echo "
<div class='com_info'>
<img src='$profile_pic' width='600' height='568' /> <br />
<a href='http://scribofabula.com/soc?p=profile&id=$user_id'> $user_com_id </div> <br />
$datedb <br />

</div>
Rating: $stardb/10 <br />
$commetdb
  ";

$ratav[$x]=$stardb; 
$x++;
  
  
}

$a=1;
/*
while($a>$array_num)
{
$ratav_final = array ();
$a++;
$ratav_final[$a] = $ratav[$a];

}
*/
print_r($ratav);
$sum = array_sum($ratav);
$count = count($ratav);
$av = $sum * $count;
echo"<div id='message'> Rating Avarage: $av /10 </div>";

if (isset($_SESSION['user']))
{
echo'
<p> Did you like this story? Did you hate it? Give it a rating and let the author know!</p>
<form action="commet_res" method="post" enctype="multipart/form-data" target="_self"> 

<label> Your rating is on a scale of 1-10 </label>
<select name="rat">
	<option> 1 </option>
    <option> 2 </option>
    <option> 3 </option>
    <option> 4 </option>
    <option> 5 </option>
    <option> 6 </option>
    <option> 7  </option>
    <option> 8 </option>
    <option> 9 </option>
    <option> 10 </option>
</select>
<label> Commets: </label>
<textarea name="commet" cols="70" rows="9"></textarea>
</form>
';
}
else 
{
echo "<div id='message'> Sign in to post a review! </div>";
}

?>

Let's try this, then. See the comments in the code; there are four changes to try so we can try to figure out what's going on. Two changes at the beginning, and two at the end.

 

// $x=1; COMMENT OUT THIS LINE
$ratav = array(); // INITIALIZE AN EMPTY ARRAY
while($rows3 = mysql_fetch_assoc($select3))
{


$commetdb = $rows3['commet'];
$user_com_db = $rows3['user'];
$datedb = $rows3['date_added'];
$stardb = $rows3['star'];
//get profile picture
$query4 = "
SELECT * FROM story_commet
WHERE user = '$user_com_db'
";	
$select4 = mysqli_query($dbc, $query4);
$rows4 = mysqli_fetch_array($select4);
	$profile_pic = $rows4['profile_pic'];
	$user_id = $rows4['id'];
echo "
<div class='com_info'>
<img src='$profile_pic' width='600' height='568' /> <br />
<a href='http://scribofabula.com/soc?p=profile&id=$user_id'> $user_com_id </div> <br />
$datedb <br />

</div>
Rating: $stardb/10 <br />
$commetdb
  ";

$ratav[]=$stardb; // CHANGED FROM $ratav[$x] 
//	$x++; COMMENT OUT THIS LINE
}	  
  

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.