Jump to content

Count number of items in a field?


djfox

Recommended Posts

I can tell php to count how many entries in a table, but I can`t figure out how to tell it to count within a field.

 

Goal: Count total number of items that are in people`s inventories.

Example:

Field name is "bought". bought holds the id numbers of virtual items that the person has bought with their virtual money. So there may be repeats. The guy who made this system for me had it set up that the items bought are stored in the field bought. SO it could appear something like: 6,7,6,19,21,20,23,36,49,59,69,69,69,

 

I want the code to be able to count how many times an id number appears in bought in all of the accounts. Is it possible? If so, how do I do it?

Link to comment
Share on other sites

So there may be repeats. The guy who made this system for me had it set up that the items bought are stored in the field bought. SO it could appear something like: 6,7,6,19,21,20,23,36,49,59,69,69,69,

 

Thats is a terrible database design. You will need to pull all those numbers out, explode them into an array and go from there. Not a pretty or efficient solution.

Link to comment
Share on other sites

SELECT COUNT(DISTINCT bought) FROM tablename WHERE id = 1

 

SELECT COUNT(DISTINCT bought) FROM tablename

 

Is it possible to specify an id number with that?

 

I do that but then the numbers are all 0 when the count is called on (shouldn`t be 0).

 

That query will not get near what you want. You will need to pull the numbers out of the db and do your calculations within php.

 

I`m not sure of what you mean.

Link to comment
Share on other sites

SELECT COUNT(DISTINCT bought) FROM tablename WHERE id = 1

That's not possible. The id will most likely never equal to 1. Just do what thorpe says.

 

Edit: djfox, if you're asking for help and someone (especially a mod) gives you the answer, don't just pass it up. Most likely, it would be the best solution. Obviously I'm not guaranteeing it 100%, but their answers are top-notch.

Link to comment
Share on other sites

Like thorpe says, it's not efficient at all.

 

I`ve heard conflicting statements about that, back when I was trying to work out favourites system for my site. I wanted to have each favourite made by it`s own entry in a table and people in another forum (don`t ask me the name, I haven`t been there in a few months) said that was inefficient and that I should add them all into one field in the person`s account information table. But here you guys say the opposite. *headscratch*

Link to comment
Share on other sites

That's because people have different views, and you'll always have more than one way to do something.  You have to decide what's best for you, not someone else.  If you don't follow what they suggest, then they just won't help you any more, but someone else with a different idea might.

 

Its how we learn.

Link to comment
Share on other sites

How about something like this:

 

<?php
$exp = explode(","$row['fieldName']);
print_r(array_count_values($exp));
?>

<?php
$exp = explode(",",$row['fieldName']);
print_r(count($exp));
?>

Forgot a comma there. And count($exp) is sufficient enough. It would be easier to query the results and apply mysql_num_rows() to find the answer rather than to split a string and then figure out what number you want to search in the array to get the answer on how many times the number is repeated.

Link to comment
Share on other sites

Truthfully, I think you should redesign the database so later on in the future you dont have to worry about it.

 

Have a table for items, users, and shopping_cart.

 

Than give them id's so items_id, users_id.

 

In the shopping_cart table you will have AT least these columns which will be users_id and items_id. These will be the items they have in the cart. Simple as that. :)

Link to comment
Share on other sites

Your current database design is what we call unnormalized; look up database normalization and our comments will make more sense.

 

What thorpe is saying is something like this:

  $column = '1,3,5,7,19';
  $column = explode(',', $column);
  echo count($column); // should print 5

Just replace the first line with one that grabs the value from the database.

 

You can count the number of items within this field using pure SQL though:

SELECT
  LENGTH(`column`) - LENGTH(REPLACE(`column`, ',', '')) AS `count`
FROM `the_table`
WHERE
  ...

 

A database redesign will be more efficient in the long run though.

Link to comment
Share on other sites

Oh no, I totally know what you guys mean by the other way of setting this up. And I agree totally that it`s easier to work with in the long run. The reason I`m trying to stick with how the guy had it was because the entire doll system he made for me depends on the items listed in bought. I`m not skilled enough to redo it myself nor can I afford to have him (or even another person) redo it.

Link to comment
Share on other sites

You did see this, right?

SELECT
  LENGTH(`column`) - LENGTH(REPLACE(`column`, ',', '')) AS `count`
FROM `the_table`
WHERE
  ...

 

I must be doing something wrong since nothing shows up:

$resb = mysql_query("SELECT
  LENGTH(`bought`) - LENGTH(REPLACE(`bought`, ',', '')) AS `count`
FROM `userdata`
WHERE bought='$c[0]'");
$try = mysql_fetch_row($resb);
mysql_free_result($resb);

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.