Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I want make raffle always after 50 donates.
I try this (because all information about donates I have in DB)
<?php
$percent = round(min(100*($sum/$goal),100));
$test = mysql_query("SELECT * FROM dc_comments WHERE message='GTA' ORDER BY RAND() LIMIT 1");
if ($percent > "50") {
echo $test;
}
?>
$percent - how much percent is acquired
goal e.g 50$
After beyound the goal I would like to automatically chose one random person from a database which is inscribed the message e.g GTA
It would be nice if it was repetitive. eg removed the data from the last draw and chose from among the new persons.
How can I do it?
The best way to do it would be to store the raffle winners in a separate table. That way you can still use your query, but adding a NOT IN condition for the previous raffle winners.
So what do you need to change to the current code?
- Create a new table raffle_winners
- Change your query to the following:
$test = mysql_query("SELECT * FROM dc_comments WHERE message='GTA' AND user_id NOT IN (SELECT user_id FROM raffle_winners) ORDER BY RAND() LIMIT 1");
After each raffle, insert the raffle winner int the raffle_winners table.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
two columns
user_id purchase_item
1 watch
1 TV
2 watch
2 fridge
2 Toys
when i use distinct comand in sql
same as it is show data but i want to following
user_id:1
purchase_item:watch
tv
user_id:2
purchase_item:watch
fridge
Toys
means i want user id print only one time so how it is possible in php
plz help me and tell coding please
If you want to do it completely in Mysql you can use aggregation to get items as comma separated list per user
select user_id, group_concat(purchase_item) purchase_item
from table
group by user_id
this will give you results like
user_id purchase_item
1 watch,TV
2 watch,fridge,Toys
Also note using group_concat the result is truncated to the maximum length that is given by the group_concat_max_len system variable, which has a default value of 1024. The value can be set higher, although the effective maximum length of the return value is constrained by the value of max_allowed_packet
But its better if you do this in php just get the ordered results and apply your logic to show user_id only once for multiple purchase_item related to that user
select * from table order by user_id asc
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Currently trying to implement a next button that would take you to the next topic in a forum but I have no idea where to start.
Assuming the forum is constantly updated in terms of the time of the last post.
If they have an autoincrementing primary key in the database, you could get the current ID, bump it and create a link pointing to the next one (provided you do a quick check to make sure its valid, e.g., the next post wasn't deleted previously).
EDIT: If the forums are sorted by last update (and you want the "next" post to mean the next most recently updated, then you'll need to do a little querying.
$sql = "SELECT postID, postName FROM forum_posts WHERE last_updated < '$thisPostsLastUpdate' ORDER BY last_updated DESC LIMIT 1";
$result = mysqli_query( $dbconn , $sql );
$fetch = $result->fetch_array();
$fetch should contain the array of values you want (the ID for generating a link to the next post and postName as the link's text).
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have been trying to print out data from a database table in custom order like for example..
I have a table and that have alot rows and have one column as listing_type which have values like Gold,Premium,Silver,Free etc for each row..! so how I would be able to print the data from fetched array in order like at first it should echo all Gold and then Premium and then Silver and then Free etc like..!
Any help would be appreciated..Thanks waiting for your reply.!
Use an ORDER BY clause in your MySQL query like this:
SELECT *
FROM `table`
ORDER BY FIELD(`listing_type`, 'Gold', 'Premium', 'Silver', 'Free')
The MySQL function FIELD() returns the position of str in the given strings. With this, you can create a custom order to sort your results on.
In Mysql query you can easily order results by using field() function,so returned results will be ordered in way where listing_type is gold first then premium then silver results then free
select *
from your_table
order by field(listing_type,'Gold','premium','Silver','Free' )
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
$randWord = mysql_query("SELECT * FROM words ORDER BY RAND() LIMIT 1");
After I call a random row from my table (which includes a word, its definition, and an id column) I want to store that word and definition so I can edit it if another button is pressed. From what I've seen online the common solution is to store it by its id. How do I store a random SQL query by its id?
Here's an example of where the pro tip
Never use SELECT * in software, instead give the names of the
columns you want.
would have helped you. If your words table has an id column, store that value.
RAND() and fetching by * is strongly not recommended because of it's bad performance.
Suggested example:
$total = mysql_result(mysql_query('SELECT COUNT(*) FROM words'), 0);
$randWord = mysql_fetch_array(mysql_query('SELECT wid, word, definition FROM words LIMIT '.rand(0, $total - 1).', 1'), 0);
Where mysql_result obtains the total number of records, mysql_fetch_array turns the result into an array. rand(0, $total - 1) generates a random number between available records, and then satisfy the [LIMIT start, limit] statement in the query.
You may build up your own DB class to reduce the code redundancy.
For your question, you should create a field as a primary key (or unique identifier). Like the 'wid' field I suggested above. Then you can output it as the value of a button and so on.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am having a problem in solving this quiz program. I am currently stuck at inserting a row. I'll explain it in detail: Let's say I have two tables, named "Table 1" and "Table 2". Table 1 has all the questions and correct answers which were inputted by the teacher. Table 2 contains answers given by students. How do I "compare" data from the 2 tables then insert the "result" of the student's answers, whether they're "correct" or "wrong", to Table 2? The image below is the kind of table I am trying to achieve.
I've been at it for almost a day and I've come to the conclusion that I am stuck.
May you provide me some ideas, concepts, or even sample codes?
tl;dr: Compare table1.correct_answer to table2.student_answer then provide data for table2.result
Considering that the two tables have id columns and the questions and answers are in the same order (with same id). You could do something like this:
First put all the corrects answers into an array.
$result1 = mysql_query("Select * From table1");
while ($correct_row = mysql_fetch_array($result1) ){
$correct[ $correct_row[id] ] = $correct_row[correct_answer];
}
Then compare them with the students answers and update the table of the result while going thru every answer of the student.
$result2 = mysql_query("Select * From table2");
while ($student_row = mysql_fetch_array($result2) ){
if ($student_row[student_answer] == $correct[ $student_row[id] ] ){
mysql_query("UPDATE table2 SET result=correct WHERE id=$student_row[id]");
} else {
mysql_query("UPDATE table2 SET result=wrong WHERE id=$student_row[id]");
}
}
Hope that helps.
Table1 should have some kind of ID. Table2 needs a column with references Table1s ID as a foreign key.