Jump to content

insert data from mysql rows into an array


pro_se

Recommended Posts

heres one of the content of the site i gave you

<?php

$con = mysql_connect("localhost","peter","abc123");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

 

mysql_select_db("my_db", $con);

 

$result = mysql_query("SELECT * FROM person");

 

while($row = mysql_fetch_array($result))//see this $row here in array from the db ????

  {

  echo $row['FirstName'] . " " . $row['LastName'];

  echo "<br />";

  }

 

mysql_close($con);

?>

<?php
$pre_toid = explode("?id=", $_SERVER['HTTP_REFERER']);
$toid = $pre_toid[1];
$result = mysql_query("SELECT * FROM `hub` where to_id='".$toid."'");
$nvotes=mysql_num_rows($result);
while($row=mysql_fetch_array($result)) {
$dbratings = $row["rating"];
$votes_array = array("votes" => $dbratings);
}
$added_votes = array_sum($votes_array);
$divided_votes = $added_votes/$nvotes;
$total_rating = $divided_votes; 
?>

 

Ok, I got that far, but there is something wrong with the while loop. When I try and print or add the array it only prints or gets the sum of one vote from the array, and I know for a fact that there are at least 30 entries in the table.

You need to make sure you don't error out on 0 because you ill get an error right now try this to help

 

<?php
/*
$pre_toid = explode("?id=", $_SERVER['HTTP_REFERER']);
$toid = $pre_toid[1];
*/
//try this to save you time since id is a get var
$toid = $_GET['id'];
$result = mysql_query("SELECT * FROM `hub` where to_id='".$toid."'");
$nvotes=mysql_num_rows($result);
if ($nvotes >0){
while($row=mysql_fetch_array($result)) {
//dbratings = $row["rating"] Pointless line just use below since you never use dbratings 
$votes_array = array("votes" => $row['rating']);
}
}
$added_votes = array_sum($votes_array);
$divided_votes = $added_votes/$nvotes;
$total_rating = $divided_votes; 
?>

<?php

/*

$pre_toid = explode("?id=", $_SERVER['HTTP_REFERER']);

$toid = $pre_toid[1];

*/

//try this to save you time since id is a get var

$toid = $_GET['id'];

$result = mysql_query("SELECT * FROM `hub` where to_id='".$toid."'");

$nvotes=mysql_num_rows($result);

if ($nvotes >0){

while($row=mysql_fetch_array($result)) {

//dbratings = $row["rating"] Pointless line just use below since you never use dbratings

$votes_array['votes'][] = $row['ratings'];  //appends to a multi level array $votes_array['votes']

}

}

echo $nvotes."<br/><br/>"; //To see how many results

print_r($votes_array['votes'][]; //Try and see what this gives you

 

$added_votes = array_sum($votes_array['votes']);

$divided_votes = $added_votes/$nvotes;

$total_rating = $divided_votes;

?>

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.