Page 1 of 1

Could someone suggest a rearrangement of my code

Posted: Mon Oct 23, 2006 9:28 am
by impulse()
I've been looking through old code and updating it as best as possible. One script I have come across is a voting system script but I've noticed that when you place a vote it never updates untill you refresh the page, or place a 2nd vote, but even then you 2nd vote doesn't update untill a 3rd vote is place or the screen is refreshed.

Code: Select all

include_once("/var/www/html/php/db/logon.php");

mysql_connect($host, $user, $pass);
mysql_select_db("poll");
$qu = "SELECT * FROM votes";

$results = mysql_query($qu);
$i = 0;

$vote = $_REQUEST["proglan"];



if (isset($vote)) {

  switch($vote) {
    case "C++":
      $cplus = mysql_result($results, $i, "cplus");
      $newCplus = $cplus + 1;
      $q = mysql_query("UPDATE votes SET cplus='$newCplus'");
      break;
    case "Java":
      $java = mysql_result($results, $i, "java");
      $newJava = $java + 1;
      $q = mysql_query("UPDATE votes SET java='$newJava'");
      break;
    case "PHP":
      $php = mysql_result($results, $i, "php");
      $newPhp = $php + 1;
      $q = mysql_query("UPDATE votes SET php='$newPhp'");
      break;
    default:
      echo "How the hell?";
      break;
  }
}



$totalCplus = mysql_result($results, $i, "cplus");
$totalJava = mysql_result($results, $i, "java");
$totalPhp = mysql_result($results, $i, "php");

$foo = $totalCplus + $totalJava + $totalPhp;
$percCplus = $totalCplus / $foo * 100 * 2;
$percJava = $totalJava / $foo * 100 * 2;
$percPhp = $totalPhp / $foo * 100 * 2;
$percCplusOutput = $percCplus / 2;
$percJavaOutput = $percJava / 2;
$percPhpOutput = $percPhp / 2;
There is some more code that's HTML but you don't need to see that, I don't think.

I have suspicion it's where the mysql_query statements are placed.

Stephen,

Posted: Mon Oct 23, 2006 9:31 am
by waradmin
Well my hacked answer for code like that is to just throw in a header redirect back to the script. So after the query where it throws the data into the db, redirect back to the original URL. However I realize its a lazy way of doing it, but works when all else fails.

Posted: Mon Oct 23, 2006 9:37 am
by impulse()
I like it :)

Posted: Mon Oct 23, 2006 9:40 am
by JayBird
This should do it

Code: Select all

include_once("/var/www/html/php/db/logon.php");

if (isset($vote)) {

  switch($vote) {
    case "C++":
      $cplus = mysql_result($results, $i, "cplus");
      $newCplus = $cplus + 1;
      $q = mysql_query("UPDATE votes SET cplus='$newCplus'");
      break;
    case "Java":
      $java = mysql_result($results, $i, "java");
      $newJava = $java + 1;
      $q = mysql_query("UPDATE votes SET java='$newJava'");
      break;
    case "PHP":
      $php = mysql_result($results, $i, "php");
      $newPhp = $php + 1;
      $q = mysql_query("UPDATE votes SET php='$newPhp'");
      break;
    default:
      echo "How the hell?";
      break;
  }
}


mysql_connect($host, $user, $pass);
mysql_select_db("poll");
$qu = "SELECT * FROM votes";

$results = mysql_query($qu);
$i = 0;

$vote = $_REQUEST["proglan"];


$totalCplus = mysql_result($results, $i, "cplus");
$totalJava = mysql_result($results, $i, "java");
$totalPhp = mysql_result($results, $i, "php");

$foo = $totalCplus + $totalJava + $totalPhp;
$percCplus = $totalCplus / $foo * 100 * 2;
$percJava = $totalJava / $foo * 100 * 2;
$percPhp = $totalPhp / $foo * 100 * 2;
$percCplusOutput = $percCplus / 2;
$percJavaOutput = $percJava / 2;
$percPhpOutput = $percPhp / 2;