Jump to content

Recommended Posts

Hey all.

Kinda new to PHP and MySql so please be gentle :P haha

Im wanting to make a page which goes though my database and finds the most used word in a column then display it on screen. but with different search requests.

 

Heres an example

 

Database:

 

Name:                  Entry:                       Result

Tom                     A                              1
Jim                       B                              2
Tom                     A                              1
Sam                     A                              1
Jim                       B                              2
Sam                     A                              3
Sam                     B                              2 
Tom                     A                              2

 

What number comes up most for Tom A
What number comes up most for Sam B
What number comes up most for Sam A

And so on.

 

Hope it makes sense and hope someone can help me out  

with the current structure and what you need to do, it's a pita, you actually have to compare 3 columns right now, name ,entry & results.

 

I would change the db/table structure, make it:

NAME | ENTRIES | RESULT | ID

and have ID be an auto-incrementing primary key. then in the results field, you want to put "serialized array" with each result. then when a user enters a value it would unserialize that array, add the value to it, and then serialize and insert the whole thing.

 

that way your data is ready to use, you just pull the row and it will have a name & entry, run an if on each entry and then unserialize it's results field and you can use count() on the resulting array. that makes the code simpler and it's more efficient, no need to check the name on every entry if they are all tied to the name specifically.

Edited by digibucc

Most of that went right over the top of my head sorry.
I do have a auto assigned ID value in there as well (I thought that was just standard practise so I didnt mention it)

As for count() I had a look but couldnt figure out how to get it to display the most hit result rather than how many times a certain value was entered

try

SELECT name, entry, result, COUNT(*) as total
FROM pomtom
WHERE name='Tom' AND entry='A'
GROUP BY name, entry, result
ORDER BY name, entry, total DESC;

Line 3 is optional. If omitted you get counts for all, with highest occurring results first for each

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.