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.
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 6 years ago.
Improve this question
I have recently ben working on a site where users can post whatever they want. Let's pretend 100'000 article get posted every month, it would be a pain to find what you need. This is why we have search bars on sites. Here's the part I understand: I know how to create a search bar and check every item in a database to see if it a match to the user's search term. Now for the part I don't understand: I do not understand how to retrieve every single item among thousands that are related to the search term and display them in a decent way. Allow me to elaborate. On google, it shows a certain amount of results per page and then you click a button to go to the next page. How can I re-create something like this?
include_once "mysql_connect.php";
$terms = $_POST["search"];
$fetchlast = mysql_query("SELECT `id` FROM allaccounts WHERE id=(SELECT MAX(id) FROM allaccounts)");
$lastrow = mysql_fetch_row($fetchlast);
$lastid = $lastrow[0];
for ($i=1; $i <= $lastid; $i++) {
$current= mysql_query("SELECT * FROM posts WHERE id=$i");
while ($row = mysql_fetch_array($current)) {
$articlename= $row[0];
$articleid= $row[1];
$articleurl= $row[2];
}
}
Here is what I have so far. This code skims through the entire database and stores the last entry it checked in a set of variables. I am going to have a different amount of returned articles for every different search term. How can I store the details of each one?
To retrive related data for users' search from database you can use "like" in the query:
=》select * from posts where like '%$user_query%'
To show limited amount of results per page use 'pagination'. Search for pagination techniques.
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 7 years ago.
Improve this question
I'm using this query to select data:
mysql_query("select * from tenders where Heading LIKE '%{$key}%' OR Date LIKE '%{$key}%'");
now there is a field status which indicates whether tender is online or offline i.e shown on website or hidden.
I want to select only those tenders whose status is online.
Can anyone help me with this? I am unable to use multiple where clause where status=online
How can I write query for this?
You don't write multiple WHERE clauses, you write multiple conditions in the WHERE clause. (Exactly like your query already does with the two LIKE comparisons, though perhaps with some parentheses to specify the logic a little more explicitly.) Something like this:
SELECT
*
FROM
tenders
WHERE
(Heading LIKE '%{$key}%' OR Date LIKE '%{$key}%')
AND status = 'online'
Also, I think you mean that status is a column, not a row. Otherwise, this whole thing becomes much more confusing...
mysql_query("SELECT * FROM tenders WHERE Heading = '%{$key}%' OR Date = '%{$key}%'");
Using WHERE x = y OR z = a should work in your case, I am a little confused on exactly what you want but this should help!
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 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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Please help me to find next batch number corresponding to product code with using PHP and MySQL. My table is like below given image:
Table Screenshot
If I want to enter new batch of product RM-SO-070G then the PHP form must get new batch number accordingly.
Your SQL should look something like this ...
SELECT `some_tbl`.`batch_no` FROM `some_tbl` WHERE `some_tbl`.`product_code` = ? ORDER BY `some_tbl`.`batch_no` DESC LIMIT 1
And then once you get these results you will need to remove the bt from the front of the batch_no
$next_batch_no = intval(substr($result_row['batch_no'], 2))++;
Create an additional table that will pose as a sequencer - the table will have just one column holding the next batch value. Each time you add a new product to your initial table, you take the batch number from the sequencer table and update the sequencer with the next batch number you want for the next product you add.
Try this:
$product = "RM-SO-070G";
$previous = "bt001";
$query = "SELECT batch_no FROM table WHERE product_code = '".$product."'
AND batch_no = (SELECT MIN(batch_no) FROM table
WHERE batch_no > '".$previous."');";
$result = mysql_query($query);
...