Jump to content

AndyJones

New Members
  • Posts

    9
  • Joined

  • Last visited

AndyJones's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Ahah - bingo - thank you so much! Thjis is the final code that works: <?php $res = $modx->query("SELECT * FROM `modx_ltda_vote` ORDER BY `option` ASC"); $data = $res->fetchAll(); $votes_cast = array_sum( array_column($data, 'total') ); foreach ($data as $r) { printf ("%s has %d votes (%0.1f %%)<br>", $r['option'], $r['total'], $r['total']*100/$votes_cast); } Thanks again _ will endevor to learn this stuff - but just needed a quick help out as it's something I need to get live for a friend. Cheers Andy
  2. Thank you. I changed the double Eqaals to a single, but it's still showing a 500 server error. I ran Barand's code, but it doesn't connect to the database table - so wondering if it was supposed to go with some of the existing code. This is what I have now: <?php $stmt = $modx->prepare("SELECT * FROM `modx_ltda_vote` ORDER BY `option` ASC"); if ($stmt->execute()) { $sum = 0; $output = array(); while($row = $stmt->fetch(PDO::FETCH__ASSOC)) { $sum += $row['total']; $output[] = $row; } foreach ($output as $line) { $pct = number_format(($line['total'] / $sum)*100, 2); echo $line['option'] . ' has ' . $line['total'] . " votes (" . $pct . "%)<br><br>"; } } else echo "Query did not execute"; I think that was a bit harsh to be fair - as I said I've picked this up to help someone out, and I don't know PHP. It would be a huge learning curve and would take many hours of learning to figure this out - when I'm just trying to help someone out - hence why I came here for advice.
  3. So I just had a play with this - but I don't really understand what to to do with your code at all - it doesn't resemble mine enough to know what to replace.
  4. So I tried your solution but it throws a 500 internal server error? I've played aroudn, but I can't get it working. Any ideas? This is what I used: $stmt = $modx->prepare("SELECT * FROM `modx_ltda_vote` ORDER BY `option` ASC"); if ($stmt->execute()) { $sum = 0; $output = array(); while($row == $stmt->fetch(PDO::FETCH__ASSOC)) { $sum += $row['total']; $output[] = $row; } foreach ($output as $line) { $pct = number_format(($line['total'] / $sum)*100, 2); echo $line['option'] . ' has ' . $line['total'] . " votes (" . $pct . "%)<br><br>"; } } else echo "Query did not execute";
  5. Ah thanks - will have a play with this too. Thanks for your input and advice!
  6. Thanks for this - I'll have a play aroudn with it in the morning! Will report back! Andy
  7. Hi there, I have inherited a site, and am not very good with PHP so please forgive me! It's a simpel voting system, where people vote for A B or C. I have the following php on a page, which looks in a table, pulls the 'total' for each row (vote) and displays then on a page so we can see how many votes were given to A, B or C. <?php $stmt = $modx->prepare("SELECT * FROM `modx_ltda_vote` ORDER BY `option` ASC"); $stmt->execute(); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); $output = []; foreach ($rows as $row) { $output[] = $row['option'] . ' has ' . $row['total'] . ' votes'; } return implode('<br><br>', $output); So the ooutut is currently: A has 10 votes B has 30 votes C has 40 votes I would like to be able to put a percentage next to each number of 'votes' - so that the user can see the percentage of all the votes so far. e.g. A has 10 votes (12.5%). B has 30 votes (37.5%). C has 40 votes (50%). I hope the above makes sense! Thanks in advance for a steer in the rirght direction - I just don't know PHP well enough to work out the syntax etc.
×
×
  • 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.