Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Aug 2012
    Posts
    142
    Thanks
    39
    Thanked 3 Times in 3 Posts

    Simplified ballot results

    I have set up a form that records votes on 10 candidates in five positions and stores them in a table called ballot. I've come up with a simple query for returning the results of each column and it's working just fine but I'd like to simplify my coding for future ballots so that I can return the column name as well as the results.

    The ballot table is very simple.
    |id|position1|position2|position3|position4|position5|

    My simplified solution to record the votes for the each candidate is as follows:
    PHP Code:
    <?php 
    $sql 
    mysql_query(
    "SELECT *,
    COUNT(position1) TotalCount
    FROM ballot
    GROUP BY position1
    HAVING COUNT(position1)>=1;"
    );
     while(
    $row mysql_fetch_assoc($sql)) {            
        
    $array[] = $row;
    }
    echo 
    '<p>Position 1</p>';
    foreach(
    $array as $row) {
        echo 
    $row['position1']." - ".$row['TotalCount']."<br>";
    }
    ?>
    It would be great I could run a query that would return both the column names and the results until I ran out of columns. There may be voting on 200 or 300 items in the near future and I would hate to have to duplicate this query for each of the 200 positions. I think it's the Group By argument that fouls things up.

    Any Ideas?

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,026
    Thanks
    23
    Thanked 588 Times in 587 Posts
    This is mine:

    PHP Code:
    <?php
    // open using sqli
    require ('./inc/DB_connectsqli.php');

    $TABLENAME "YOUR TABLE NAME HERE";
    $query "SELECT * FROM $TABLENAME";
    $result mysqli_query($mysqli$query);
    $colCount mysqli_field_count($mysqli);
    echo 
    "<br /><table id='myTable' border='1'>$TABLENAME<br />";
    echo 
    "<tr>";
    for(
    $colNumber 0$colNumber $colCount$colNumber++)
    {
        
    $fieldName mysqli_fetch_field($result); // FETCHES THE COLUMN NAMES
        
    echo "<th>$fieldName->name</th>";
    }
    echo 
    "</tr>";

    while(
    $row mysqli_fetch_row($result)){  // PUTS VALUES OF THE ROWS INTO THE CORRECT COLUMNS
        
    echo "<tr align=left VALIGN=TOP>";
        for(
    $colNumber 0$colNumber $colCount$colNumber++){
            echo 
    "<td>$row[$colNumber]</td>";
            }
            echo 
    "</tr>";
    }
    echo 
    "</table><br />";
    ?>
    Evolution - The non-random survival of random variants.

  • Users who have thanked sunfighter for this post:

    rgEffects (11-07-2013)

  • #3
    Regular Coder
    Join Date
    Aug 2012
    Posts
    142
    Thanks
    39
    Thanked 3 Times in 3 Posts
    That might be what I'm looking for. I'll give it a shot tonight.

    Thanks


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •