PHP - How To Compare Two Columns? [closed] - php

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.

Related

How to order two tables with no relationship and different columns, by a single column that they share (date) [closed]

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 4 years ago.
Improve this question
I've got two tables in a MYSQL database. One Is a for blog posts and one is for videos. All the columns in each of them are different except for one which is the date they were created.
On the main page of my website I want to show most recent posts, whether that be a video or a blog post. So I want to order them based on their shared date column. Is this something I can do in MYSQL or would I have to pull all the data into php and then order it using my own function.
I've looked up other answers but they all seem to be cases where the tables have no relationship but share the same columns.
For table to be used in the same result set as you are suggesting, you would need a UNION; but UNIONs require all union-ed queries to have the same columns in their results. If the only field in common you have is "date"; the best you could probably do is something like.
SELECT `date` AS postDate, 'Video' AS postType
, someVideoField, null as someBlogField
FROM video_table
UNION
SELECT `date` AS postDate, 'Blog' AS postType
, null as someVideoField, someBlogField
FROM blog_table
ORDER BY postDate
;
Note: The latter aliases are not actually needed, I just tend to do that for clarity as to which field is expected to map to which. Also, the null as someBlogField portion may need tweaked to insure the result field is a type that can accept the real values from the latter half of the union; the first half determines field types.
If you end up using two queries it's easy enough to put them together in your script.
while ($row = $blogQuery->fetch()) {
$results[$row['date']]['blog'] = $row;
}
while ($row = $videoQuery->fetch()) {
$results[$row['date']]['video'] = $row;
}
If you ordered by date in your select queries, this will already be mostly in the right order, except for dates where only the video query has rows (or whichever one you fetched second). You can ksort($results) to fix the order for any of those.

Raffle after50 donates [closed]

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.

Return the number of friends in common [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
)
I'm having a problem - and I have been cracking my head against the wall for some time now!
so I hope that a new set of eyes will help me in this matter..
I'm trying to make a simple counter, that counts how many friends two users have i common, but apparently it is not that simple :-D here is my table.
table
the above table is foreign keys going to the user table, where they use their ids'.
I have tried using selects etc. and I just want to count friends in common.
eg. logged in user has id 14 and the other user has id 4. how many friends do they have in common excluding their own friendship.
thanks in advance :-D
Try with this query:
SELECT count(*)
FROM YOUR_TABLE T1
JOIN YOUR_TABLE T2
ON (T1.u_id2 = T2.u_id2)
WHERE T1.u_id1 = $first_user
AND T2.u_id2 = $first_user
AND T2.u_id1 = $second_user
AND T1.u_id2 = $second_user

PHP - Grabbing info from the database (complicated) [closed]

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
I need a little help. I need to grab all rows which contain their logged in ID from a database. Basically, I have a column in the database called "owners" and in it it has a few IDs. Lets say "1, 6, 8", If user ID 6 is logged in, I need to grab that row. If it doesn't contain 6, say "2, 5, 7" then don't grab it. My wording probably isn't good.
--------------------------------
ID |Name |Other Info |Owners|
---|--------|-----------|------|
1 |Testing |Testing 123|1,4,6 |
---|--------|-----------|------|
2 |Testing1|Catz |5,7,8 |
---|--------|=-----------------|
3 |Testing2|Woof |6,9,10|
---|--------|-----------|------|
In other words:
User ID 6 logs in.
Goes to page which needs the rows to be grabbed.
Script grabs rows 1 and 3 (because the owners list contains 6).
PHP foreach, showing the resutls in a table. (Don't need help with
this).
The reason I want this is I don't want ownerid2, ownerid3, ownerid4 taking up loads of space in separate columns in the database.
You could try this:
SELECT * FROM table_name WHERE FIND_IN_SET(?, Owners);
where ? has to be replaced with the user id, for example
SELECT * FROM table_name WHERE FIND_IN_SET(6, Owners);
as other pointed out, you might also want to read about database normalization, and here is the docs for FIND_IN_SET
A cleaner way to accomplish this is to use a separate table for the owner relationship.
Table = Owner, has ID.
Table = Test, what you are currently trying to get
Table = OwnerTestRel, has a foreign key to Owner and one to Test.
Then your query can become something like
Select test.*
FROM test
JOIN OwnerTestRel ON test.id=OwnerTestRel.testId
WHERE OwnerTestRel.ownerId = {owner's Id}
could this possibly be what you're after?
SELECT * FROM table_name WHERE Owners LIKE '%6%';
????
EDIT: doing it this way would pull up other owners like 68, 600, 6123. you should look into database normalization.
EDIT2:
just thought of this, since there's no spaces between your commas you could use the explode(); function like so:
$ownerid = //WHATEVER THE OWNERS ID IS
$sql = "SELECT * FROM table_name WHERE Owners LIKE '%$ownerid%'";
$result = mysqli_query($con, $sql);
while($data = mysqli_fetch_array($result)){
$owners = $data['Owners'];
$owners = explode(',',$data);
if(in_array($ownerid, $owners){
//SHOW ROW DATA HERE
}
}

Building a comment/post and reply system [closed]

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 9 years ago.
Improve this question
I am trying to build a comment system that has replies which can be tied to a particular comment.
Here is my sample code. I tried joining two tables together to display each reply for different comments.
function get_comments() {
$query = $this->link->query("SELECT * FROM comments, reply");
$rowCount = $query->rowCount();
if ($rowCount >= 1) {
$result = $query->fetchAll();
}
else {
$result = 0;
}
return $result;
}
That SQL query doesn't look like it's joining anything.
Since a reply is also a comment, you might be able to have a table structure like:
table comments
id
in_reply_to_id
commenter_name
comment_text
The in_reply_to_id refers to the id of the comment that this comment is in reply to.
Then you can query like:
select * from comments where in_reply_to_id = whatever_comment_id
to get all of the replies to the comment that has id equal to whatever_comment_id.
There is no way enough code there to fulfill what you require. In short you need a comments and a reply table as you have, the reply table should have a field for Comment_ID, and when you loop through each comment to display, have an inner loop that loops through each comment reply and displays it under the current comment you are iterating through.

Categories