Reputation: 71
I'm trying to make a prediction score script where users can pick a result.
I want to count the values of rows... For example, if in my DB contains 9 rows and 6 rows have the same value and other 3 different.. I want to get the value from 6 same rows and add them up together. Thanks.
Code:
<?php
include("connect.php");
?>
<html>
<head>
<title>Score Predictions</title>
</head>
<body>
<div id="id">
<?php
$query="SELECT * FROM test";
$result=mysql_query($query);
while($row=mysql_fetch_array($result)){
$id = $row['id'];
$home = $row['home'];
$away = $row['away'];
}
?>
<?php
if (isset($_POST['submit'])){
$x = $_POST["test"];
mysql_query("INSERT INTO test (home, away, score) VALUES ('$home', '$away', '$x')");
}
?>
<?php echo $home," - ",$away; ?>
<form method='post' action='http://albsocial.us/test/index.php'>
<select name='test'>
<option value="" selected='selected'></option>
<option VALUE='1'>1</option>
<option VALUE='X'>X</option>
<option VALUE='2'>2</option>
</select>
<INPUT TYPE='submit' name='submit' />
</form>
<?php
$query="SELECT * FROM test";
$result=mysql_query($query);
while($row=mysql_fetch_array($result)){
$id = $row['id'];
$score = $row['score'];
if ($score = "1"){
echo "OK";
}
}
?>
</div>
</body>
</html>
Upvotes: 3
Views: 856
Reputation: 27247
There are a few points to address:
The DB Schema
I'm assuming your table looks like this:
id INT,
home VARCHAR,
away VARCHAR,
score VARCHAR
where score
can contain 1
, 2
, or X
. It looks like you have rows of sporting event matches home vs away, where the user can submit their vote of who will win. If this isn't correct please update your question.
if in my db are 9 rows and 6 rows have the same value and other 3 different.. i only want to get the value from 6 same rows and add them up together
SELECT score, c FROM (
SELECT score, count(score) as c FROM my_table GROUP BY count(score)
) as subselect
ORDER BY c DESC
LIMIT 1
That will get you the score with the most votes. I hope you can read it without much explanation.
The php error
if ($score = "1"){
is incorrect. To do a comparison you want:
if ($score == "1"){
The other php error
$home
and $away
in the INSERT
will always be set to the values of the last row in your db table, because of how your while loop is set up. I'm not sure what you meant to do here, but this is worth pointing out.
From here, I have no idea what you actually intend to do with all of this code. This is just as far as I can tell from what you wrote.
Upvotes: 4