HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

Populating a database with golf scores and stroke calculations

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
golfwithpopulatingdatabasecalculationsscoresandstroke

Problem

This script updates a MySQL database with golf scores and calculates how many strokes under the player is in real time and is working. I'm just looking for a better way to make it cleaner and compact.

```
0) {
$s01 = $h01 - $n01;
}else{
$s01 = 0;
}
if( $h02 > 0) {
$s02 = $h02 - $n02;
}else{
$s02 = 0;
}
if( $h03 > 0) {
$s03 = $h03 - $n03;
}else{
$s03 = 0;
}
if( $h04 > 0) {
$s04 = $h04 - $n04;
}else{
$s04 = 0;
}
if( $h05 > 0) {
$s05 = $h05 - $n05;
}else{
$s05 = 0;
}
if( $h06 > 0) {
$s06 = $h06 - $n06;
}else{
$s06 = 0;
}
if( $h07 > 0) {
$s07 = $h07 - $n07;
}else{
$s07 = 0;
}
if( $h08 > 0) {
$s08 = $h08 - $n08;
}else{
$s08 = 0;
}
if( $h09 > 0) {
$s09 = $h09 - $n09;
}else{
$s09 = 0;
}
if( $h10 > 0) {
$s10 = $h10 - $n10;
}else{
$s10 = 0;
}
if( $h11 > 0) {
$s11 = $h11 - $n11;
}else{
$s11 = 0;
}
if( $h12 > 0) {
$s12 = $h12 - $n12;
}else{
$s12 = 0;
}
if( $h13 > 0) {
$s13 = $h13 - $n13;
}else{
$s13 = 0;
}
if( $h14 > 0) {
$s14 = $h14 - $n14;
}else{
$s14 = 0;
}
if( $h15 > 0) {
$s15 = $h15 - $n15;
}else{
$s15 = 0;
}
if( $h16 > 0) {
$s16 = $h16 - $n16;
}else{
$s16 = 0;
}
if( $h17 > 0) {
$s17 = $h17 - $n17;
}else{
$s17 = 0;
}
if( $h18 > 0) {
$s18 = $h18 - $n18;
}else{
$s18 = 0;
}
}
else
{
if( $h01 > 0) {
$s01 = $h01 - $d01;
}else{
$s01 = 0;
}
if( $h02 > 0) {
$s02 = $h02 - $d02;
}else{
$s02 = 0;
}
if( $h03 > 0) {
$s03 = $h03 - $d03;
}else{
$s03 = 0;

Solution

There are a number of ways to improve this code:

  • You really need to use arrays for this.



  • $d and $n are identical so I don't see any need for the if ($course = 'Augusta') condition.



  • Use msqli methods instead of mysql, as they've been deprecated.



  • Use prepared statements to guard against sql injection.



Try this:

function _getName($i) { return 'h' . str_pad($i, '0', 2, STR_PAD_LEFT); }
function _getPost($n) { return $_POST[$n]; }
function _getSqlParam($n) { return $n.'=?'; }
$hNames =  array_map(_getName, range(1, 18));
$h = array_map(_getPost, $hNames);
$played=$_POST['played'];
$n = array(4, 5, 4, 3, 5, 3, 4, 4, 4, 4, 4, 4, 4, 3, 5, 3, 4, 5);
$s = $n; // copy array

for($i = 0; $i  0) {
        $s[$i] = $h[$i] - $n[$i];
    } else {
        $s[$i] = 0;
    }
}

$sqlSetClause = implode(',', array_map(_getSqlParam, $hNames));
$sql="UPDATE $tbl_name 
    SET name=?,course=?,played=?,score=?,$sqlSetClause
    WHERE uid=?";
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
if ($stmt = $mysqli->prepare($sql)) {
    $allParams = array_merge(array($name, $course, $played, $score), $h, array($uid));
    $stmt->bind_param(str_repeat('s', count($allParams)), $allParams);
    $result = $stmt->execute();

    if($result) {
        // echo "window.location = 'index.php'";
        echo "UPDATED - Back to Admin Page";
    }
}

Code Snippets

function _getName($i) { return 'h' . str_pad($i, '0', 2, STR_PAD_LEFT); }
function _getPost($n) { return $_POST[$n]; }
function _getSqlParam($n) { return $n.'=?'; }
$hNames =  array_map(_getName, range(1, 18));
$h = array_map(_getPost, $hNames);
$played=$_POST['played'];
$n = array(4, 5, 4, 3, 5, 3, 4, 4, 4, 4, 4, 4, 4, 3, 5, 3, 4, 5);
$s = $n; // copy array

for($i = 0; $i < count($h); ++$i) {
    if( $h[$i] > 0) {
        $s[$i] = $h[$i] - $n[$i];
    } else {
        $s[$i] = 0;
    }
}

$sqlSetClause = implode(',', array_map(_getSqlParam, $hNames));
$sql="UPDATE $tbl_name 
    SET name=?,course=?,played=?,score=?,$sqlSetClause
    WHERE uid=?";
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
if ($stmt = $mysqli->prepare($sql)) {
    $allParams = array_merge(array($name, $course, $played, $score), $h, array($uid));
    $stmt->bind_param(str_repeat('s', count($allParams)), $allParams);
    $result = $stmt->execute();

    if($result) {
        // echo "<script>window.location = 'index.php'</script>";
        echo "<h1>UPDATED - <a href='index.php'>Back to Admin Page</a></h1>";
    }
}

Context

StackExchange Code Review Q#25045, answer score: 5

Revisions (0)

No revisions yet.