Jump to content

Reports based on Database info


Bman900

Recommended Posts

Alright so I have a script that takes in questions and stores them into a database. What I want to do is have like a Most asked question's report. Basicly I want to look trough the databse and if there are two questions that are the same I want to print it in a table with a number next to it identifying how many times it apears.

 

Next I redirect the user based on keywords found in their questions which is also stored in the database. I then want to make a report on how many times each keyword apears in all of the questions in the databse.

 

Where would I even start on this complicated journey?

Link to comment
Share on other sites

Well start with the first part. Add a DB field to keep track of how many times a question appears. Though I don't know why you would have 2 of the same question.

 

The keywords is a pain. You'll probably have to end up stripping the user's response by words and querying the DB to see how many of them match and what they are. You'll have to decide on an algorithm.

Link to comment
Share on other sites

Ok I got the following code to check for doubles:

$double = mysql_query("SELECT question FROM question GROUP BY question HAVING count(*) > 1")

or die(mysql_error());

 

Now is there a way to then display how many times those records are displayed in the database?

Link to comment
Share on other sites

Ok that looks like it might work but I need just a bit more help.

 

Here is the code I used to display the query I ran:

 

<table width="500" border="2" cellspacing="0" cellpadding="0">
  <tr>
    <th>Question Asked 2 times</th>
  </tr>
<?php
while($row2 = mysql_fetch_assoc( $double ))
{
    echo "<tr>";
    echo "<td>".$row2['question']."</td>";
    echo "</tr>";
} 
?>
  </tr>
</table>

 

Obviously I need to make some changes to that but when I added   

echo "<td>".$row2['id']."</td>";

 

But that didn't work. So how would I display this correctly?

Link to comment
Share on other sites

You need to use an alias.

 

$double = mysql_query("SELECT COUNT(id) AS cnt FROM question WHERE id IN(SELECT id FROM question GROUP BY question HAVING count(id))");

 

Now echo $row['cnt'].

Link to comment
Share on other sites

Well I am displaying my question, here is the code:

 

$double = mysql_query("SELECT COUNT(id) AS cnt FROM question WHERE id IN(SELECT id FROM question GROUP BY question HAVING count(id))");



?>
<table width="500" border="2" cellspacing="0" cellpadding="0">
  <tr>
    <th>Name</th> 
    <th>Number</th>
  </tr>
<?php
while($row2 = mysql_fetch_assoc( $double ))
{
    echo "<tr>";
    echo "<td>".$row2['question']."</td>";
    echo "<td>".$row2['cnt']."</td>";
    echo "</tr>";
} 
?>
  </tr>
</table>

 

 

Or are you talking about the selecting it in my query?

Link to comment
Share on other sites

Obviously you still need to select question in your query if you want the question as well.

 

I see what you mean now but I can't seem to do it right. So far I have:

 

$double = mysql_query("SELECT question AND COUNT(id) AS cnt FROM question WHERE id IN(SELECT id FROM question GROUP BY question HAVING count(id))");

 

What am i doing wrong?

Link to comment
Share on other sites

Ah this is frustrating, it got me a value but it is still not right.

 

I need to combine this:

 

$double = mysql_query("SELECT question FROM question GROUP BY question HAVING count(*) > 1")
or die(mysql_error()); 

 

with:

 

$double = mysql_query("SELECT COUNT(id) AS cnt FROM question WHERE id IN(SELECT id FROM question GROUP BY question HAVING count(id))");

 

Please help me, am ripping my hair out here :(

Link to comment
Share on other sites

HAVING count(id) ... ? something missing there.

 

There we go!

$double = mysql_query("SELECT question, COUNT(id) AS cnt FROM question WHERE id IN(SELECT id FROM question GROUP BY question HAVING count(id) > 1)");

 

I finally go something that makes sense but I only get one entry and I know there are more doubles and triples in my table.

 

My while statement is:

 

<?php
while($row2 = mysql_fetch_assoc( $double ))
{
    echo "<tr>";
    echo "<td>".$row2['question']."</td>";
    echo "<td>".$row2['cnt']."</td>";
    echo "</tr>";
} 
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.