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);

?>

Link to comment
Share on other sites

So, what I am doing is adding integers from an array (the one I pulled out of the mysql db) together, how can I add the multiple rows I am pulling out... I was thinking of using array_sum, but I did not know how to go about using it..

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

actually, wait.....

 

I have like 30 entries in the table right, and its not making sense because when I do <?php print_r($votes_array); ?> its only printing out one of the rows. I want to get the sum of all the rows, not just one of them...

Link to comment
Share on other sites

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

?>

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.