I built a poll system. In this system, the values selected by the user are saved as 0,1,2. Now I want each user's choices to be counted.
For example:
Option one: 10 votes
Option two: 5 votes
Option three: 12 votes
...
foreach ($choices as $choice) {
echo $wpdb->get_var("SELECT COUNT(*) FROM mytable WHERE (id = {$_GET['id']} AND user_choice = {$choice}))");
}
try this:
user_choice: Column containing user votes
$sql=mysqli_query($con,"SELECT user_choice FROM mytable WHERE (id = {$_GET['id']} AND user_choice = {$choice}) ");
$arr = array();
while ($all = mysqli_fetch_array($sql, MYSQLI_NUM)) {
foreach($all as $val) {
$arr[] = $val;
}
}
$vote_choice = array_count_values($arr);
foreach($vote_choice as $name=>$qty) {
echo "Who Choice(".$name.'):->'.$qty.'<br />';
}
Related
This is my table with name and unique numberenter image description here
This is my PHP code
$id = explode(",", $params["txtID"]);
for ($i = 0; $i <count($id); $i++) {
$sql = "SELECT 'auto_increment' as LastID FROM
INFORMATION_SCHEMA.TABLES WHERE table_name = 'esi_master' ";
$result = $this->con->query($sql);
if (intval($result->rowCount($result)) > 0) {
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
$lclId = intval($row["LastID"]) - 1 + intval('1');
$lclDcno = '00'.$lclId ;
echo $row["LastID"];
}
} else {
$lclId = intval($row["LastID"]) + intval('1');
$lclDcno = '00'.$lclId ;
echo $row["LastID"];
}
$sql = $this->con->prepare("UPDATE esi_master SET esi_status = 1,
esi_dcno = '$lclDcno[$i]' Where esi_id = '$id[$i]'");
echo $result = $sql->execute();
}
Here First I insert Names to the table using Insert Statements and next for some operation I update the Unique number unique number should start updating from the empty row, update done with unique id, The txtID contains ID`s like 1, 2, 3, 4 so on as I select table row in front end, in that basis that updates.Thanks in Advance.
You need another field to save, with type varchar(10).
data will not be able to save into integer(auto_increment)
I realy need help to fix this code. Actualy I want get gcm_id from table where the user selects several category from table category. An example of what I want main point is gcm_id data
"gcm_id will show if user select category A,B,C" etc.. How do I do this in php mysql. I try FIND_IN_SET with no luck
uid | email | gcm_id | app_type | categories
1 demo#gmail.com xzxzxzxz A 2,5,6
How get gcm_id if user have been select categories id 2,5, and 6?
the code :
if (!empty($cat)) {
foreach ($cat as $key => $value) {
$wc.="FIND_IN_SET('$value',categories) > 0 OR ";
}
}
$pos = strrpos($wc, "OR");
if ($pos !== false) {
$wc = substr_replace($wc, "AND", $pos, strlen("OR"));
}
$wc.=" is_active=1";
$q = "select * from datanotif where app_type='$type' AND $wc ";
$r = mysqli_query($mysqli,$q);
$users = array();
while ($row1 = mysqli_fetch_assoc($r)) {
$users[] = $row1;
}
$ids = array();
foreach ($users as $key => $value) {
$ids[] = $value['gcm_id'];
}
In mysql table table data, one column has multiple value like this,
code name
1,2,3 a
4 b
My desired output will be
code name
1 a
2 a
3 a
4 b
Here is my code:
$sql="SELECT * FROM hsc_sub group by sub_name order by sub_code";
$res = app_db_query($app,$sql);
while($row = mysqli_fetch_assoc($res)){
//enter code here
$subj[] = $row;
$subj[] = explode(",",$row['sub_code']);
}
Within the ForEach Loop, we can achieve your requirement.
$subCode[];
$name[];
echo("Code");
echo("\t");
echo("Name");
echo("\n");
$sql="SELECT * FROM hsc_sub group by sub_name order by sub_code";
$res = app_db_query($app,$sql);
while($row = mysqli_fetch_assoc($res)){
$nm = $row['sub_name']
$sub[] = explode(",",$row['sub_code']);
foreach($sub as $item)
{
$name.push($nm);
$subCode.push($item);
echo($item);
echo("\t");
echo($nm);
echo("\n");
}
}
i have a table with staff_id and subjects, i want to display all staffs according to their subjects.
my table
result i want
Physics
-001
-004
-006
Chemistry
-002
-009
Biology
-003
-008
Mathematics
-005
My code
$q = mysql_query("Select staff_id from my_table");
while($row = mysql_fetch_array($q)){
echo $subject .'</br>';
echo $staff_id.'</br>';
}
but this doesn't give the result i want.
any help?
What you need is ORDER BY.
Change your query to:
SELECT STAFF_ID, SUBJECT FROM my_table ORDER BY SUBJECT, STAFF_ID
So you get the records in the right order to work with them.
Something like this?
$q = mysql_query("SELECT `staff_id`, `subject` FROM `my_table`;");
$data = array();
while($row = mysql_fetch_array($q)){
$data[$row['subject']][] = '-'.$row['staff_id'];
}
print_r($data);
Or to echo out the rows
foreach($data as $heading => $rows){
echo $heading.'<br>';
foreach($rows as $row){
echo $row.'<br>';
}
}
You can write your code like this below:
$q = mysql_query("SELECT * FROM my_table ORDER BY SUBJECT, STAFF_ID");
while($row = mysql_fetch_array($q)){
//Do staff
}
The following code should help. You should split each subject into a separate array within your query. Once your query is complete, you should iterate through the subject array, and then within each staff id.
$subjects = array();
$q = mysql_query("Select staff_id from my_table");
while($row = mysql_fetch_array($q)){
if ($subjects[$row['SUBJECT']] == nil) {
$subjects[$row['SUBJECT']] = array();
}
array_push($subjects[$row['SUBJECT']], $row['STAFF_ID']);
}
foreach ($subjects as $key=>$value) {
echo $key . '<br>;
foreach ($vaue as &$staff) {
echo $staff . '<br>';
}
}
$result=mysql_query("SELECT * from table GROUP BY subject");
while($ext=mysql_fetch_object($result)) {
$query=mysql_query(" SELECT * from table WHERE subject='".$ext->subject."'");
echo $ext->subject;
while($res=mysql_fetch_object($query)) {
echo $res->staff_id;
}
}
I am wanting to order my array by how many times a value comes up in that array and also then print only the highest 6 results.
This is what I have at the moment:
$delimiter = " ";
$tags = array();
$sql = mysql_query("SELECT tags FROM share WHERE site_id = $id");
while($result = mysql_fetch_assoc($sql)) {
$tags_new = explode($delimiter, $result['tags']);
$tags = array_merge($tags , $tags_new);
}
Hum... You can do that:
while($result = mysql_fetch_assoc($sql)) {
$tags_new = explode($delimiter, $result['tags']);
foreach($tags_new as $tag){
$tags[$tag]++;
}
}
After, you need to sort by value, using function sort or rsort (desc).
rsort($tags);
Last, you need slice and get only 6 first:
$high_tags = array_slice($tags, 0, 6, true);
Edit: showing key and value:
foreach($high_tags as $key => $value){
echo "{$key}: {$value}";
}
Or just do:
$high_keys = array_keys($high_tags);
var_dump($high_keys);
Bye :)
You can do this in MySQL with the following query:
SELECT tags, COUNT(tags) AS tag_count
FROM share WHERE site_id = $id
GROUP BY tags
ORDER BY tag_count DESC
LIMIT 6;
This will select the top 6 tags with the highest count. You can then loop over them similar to the PHP code you already have.