Could someone suggest a rearrangement of my code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Could someone suggest a rearrangement of my code

Post 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,
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post 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.
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

I like it :)
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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;
Post Reply