Jump to content

array problem....I think...though its probably just me :)


Noctagon

Recommended Posts

Hi all, I must be still missing something, when I run this code I end up with 'array'

I thought the while fixes this.

Total noob so go easy on me ;)

Thanks in advance for your help

BTW, My aim is to sum the values in the votes column of table vote_table where the user_id value =1

Then if the value is within a certain value range then run the appropriate if line.


[code] $query = 'SELECT votes FROM vote_table where user_id="1"';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
while ($tvotes = mysql_fetch_array($result)) {

if ($tvotes['vote_table'] < 1000) {
$votecolour = '<b><font color=black>Black</font></b>';
} else {
if ($tvotes['vote_table'] < 5000 and $tvotes['vote_table'] >= 1000) {
$votecolour = '<b><font color=blue>Blue</font></b>';
} else {
$votecolour = '<b><font color=red>Red</font></b>';[/code]
Give this a try:
[code]
$query = 'SELECT votes FROM vote_table where user_id="1"';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
while ($tvotes = mysql_fetch_array($result)) {
  $votes = $tvotes['votes'];
  if ($votes < 1000) {
    $votecolour = '<b><font color=black>Black</font></b>';
  }
  else if (($votes < 5000) && ($votes >= 1000)) {
    $votecolour = '<b><font color=blue>Blue</font></b>';
  }
  else {
    $votecolour = '<b><font color=red>Red</font></b>';
  }
}
[/code]
That looks like the same code to me :)

I actually omited something any way...this is the code I was using:

[code]$query = 'SELECT SUM(votes) FROM vote_table where user_id="1"';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
while ($tvotes = mysql_fetch_array($result)) {

if ($tvotes['vote_table'] < 1000) {
$votecolour = '<b><font color=black>Black</font></b>';
} else {
if ($tvotes['vote_table'] < 5000 and $tvotes['vote_table'] >= 1000) {
$votecolour = '<b><font color=blue>Blue</font></b>';
} else {
$votecolour = '<b><font color=red>Red</font></b>';[/code]
Why are you fetching "vote_table"? That is the table that holds the field "votes", which contains the votes correct? If so, you need:
[code]
$tvotes['votes']
[/code]

Try:
[code]
$query = "SELECT SUM(votes) AS vote_count FROM vote_table WHERE user_id='1'";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
while ($tvotes = mysql_fetch_array($result)) {
  $vote_count = $tvotes['vote_count'];
  if ($vote_count < 1000) {
    $votecolour = '<b><font color=black>Black</font></b>';
  }
  else if (($vote_count < 5000) && ($vote_count >= 1000)) {
    $votecolour = '<b><font color=blue>Blue</font></b>';
  }
  else {
    $votecolour = '<b><font color=red>Red</font></b>';
  }
}
[/code]

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.