Bman900 Posted May 8, 2009 Share Posted May 8, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/157416-reports-based-on-database-info/ Share on other sites More sharing options...
forumnz Posted May 8, 2009 Share Posted May 8, 2009 It's not complicated. Start here w3schools.com/php Quote Link to comment https://forums.phpfreaks.com/topic/157416-reports-based-on-database-info/#findComment-829848 Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/157416-reports-based-on-database-info/#findComment-829849 Share on other sites More sharing options...
Bman900 Posted May 8, 2009 Author Share Posted May 8, 2009 Well what if I do how many times a keyword has redirected to a page? could that be simpler? Quote Link to comment https://forums.phpfreaks.com/topic/157416-reports-based-on-database-info/#findComment-829863 Share on other sites More sharing options...
Bman900 Posted May 8, 2009 Author Share Posted May 8, 2009 And for the questions, they are user inputed. So lets say user A asks: How was my day? and user B also asks: How was my day? I want a report saying that How was my day? has been asked twice. Quote Link to comment https://forums.phpfreaks.com/topic/157416-reports-based-on-database-info/#findComment-829865 Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 Yeah, store them in the DB under different users. :-\ Then run a query on the question you want. Quote Link to comment https://forums.phpfreaks.com/topic/157416-reports-based-on-database-info/#findComment-829887 Share on other sites More sharing options...
Bman900 Posted May 8, 2009 Author Share Posted May 8, 2009 Is there a way to run a query that checks if a table has multipes of the samething and than print those out? Quote Link to comment https://forums.phpfreaks.com/topic/157416-reports-based-on-database-info/#findComment-829898 Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 Well you can run a SQL where the WHERE clause has the question you want to search for and if it returns more than one result, then you have multiples. Quote Link to comment https://forums.phpfreaks.com/topic/157416-reports-based-on-database-info/#findComment-829900 Share on other sites More sharing options...
Bman900 Posted May 8, 2009 Author Share Posted May 8, 2009 Well the thing is I wont be looking for a specific question, am just seeing if there are repeats and how many times it is repeated. Quote Link to comment https://forums.phpfreaks.com/topic/157416-reports-based-on-database-info/#findComment-829919 Share on other sites More sharing options...
Bman900 Posted May 9, 2009 Author Share Posted May 9, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/157416-reports-based-on-database-info/#findComment-829989 Share on other sites More sharing options...
trq Posted May 9, 2009 Share Posted May 9, 2009 $double = mysql_query("SELECT COUNT(id) FROM question WHERE id IN(SELECT id FROM question GROUP BY question HAVING count(id))"); Quote Link to comment https://forums.phpfreaks.com/topic/157416-reports-based-on-database-info/#findComment-829997 Share on other sites More sharing options...
Bman900 Posted May 9, 2009 Author Share Posted May 9, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/157416-reports-based-on-database-info/#findComment-830005 Share on other sites More sharing options...
trq Posted May 9, 2009 Share Posted May 9, 2009 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']. Quote Link to comment https://forums.phpfreaks.com/topic/157416-reports-based-on-database-info/#findComment-830017 Share on other sites More sharing options...
Bman900 Posted May 9, 2009 Author Share Posted May 9, 2009 I did that and got: Question Number 10 Quote Link to comment https://forums.phpfreaks.com/topic/157416-reports-based-on-database-info/#findComment-830020 Share on other sites More sharing options...
trq Posted May 9, 2009 Share Posted May 9, 2009 Obviously you still need to select question in your query if you want the question as well. Quote Link to comment https://forums.phpfreaks.com/topic/157416-reports-based-on-database-info/#findComment-830023 Share on other sites More sharing options...
Bman900 Posted May 9, 2009 Author Share Posted May 9, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/157416-reports-based-on-database-info/#findComment-830026 Share on other sites More sharing options...
Bman900 Posted May 9, 2009 Author Share Posted May 9, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/157416-reports-based-on-database-info/#findComment-830048 Share on other sites More sharing options...
xtopolis Posted May 9, 2009 Share Posted May 9, 2009 SELECT question AND COUNT(id) AS cnt ->SELECT question, COUNT(id) AS cnt Quote Link to comment https://forums.phpfreaks.com/topic/157416-reports-based-on-database-info/#findComment-830049 Share on other sites More sharing options...
Bman900 Posted May 9, 2009 Author Share Posted May 9, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/157416-reports-based-on-database-info/#findComment-830053 Share on other sites More sharing options...
Ken2k7 Posted May 9, 2009 Share Posted May 9, 2009 HAVING count(id) ... ? something missing there. Quote Link to comment https://forums.phpfreaks.com/topic/157416-reports-based-on-database-info/#findComment-830054 Share on other sites More sharing options...
Bman900 Posted May 9, 2009 Author Share Posted May 9, 2009 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/157416-reports-based-on-database-info/#findComment-830060 Share on other sites More sharing options...
Bman900 Posted May 9, 2009 Author Share Posted May 9, 2009 Any one? Quote Link to comment https://forums.phpfreaks.com/topic/157416-reports-based-on-database-info/#findComment-830241 Share on other sites More sharing options...
fenway Posted May 11, 2009 Share Posted May 11, 2009 Why not simply: $double = mysql_query("SELECT question, COUNT(id) AS cnt FROM question GROUP BY question HAVING cnt > 1"); Quote Link to comment https://forums.phpfreaks.com/topic/157416-reports-based-on-database-info/#findComment-831548 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.