473,503 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

counting rows

155 New Member
i got for example this result from data base:

user1
user1
user1
user2
user2
user1
user1

how can i count in php like that:

user 1 have 3 in a row ,then user2 have 2 in a row ,then user1 have 2 in a row?

thanx
Nov 14 '08 #1
13 1751
Dormilich
8,658 Recognized Expert Moderator Expert
compare the adjacent values, count up if they are the same and store the count somewhere if the test fails. you have to make up a suited storage system/variable though.

regards
Nov 14 '08 #2
canabatz
155 New Member
any short code example for counting like that?

i didn't get you ,sorry :(

tahnx
Nov 14 '08 #3
Dormilich
8,658 Recognized Expert Moderator Expert
something like:
Expand|Select|Wrap|Line Numbers
  1. for ($i=0; $i<$arr_length; $i++) {
  2. // compare this and the next value
  3. // if true increase count
  4.   if ($arr[$i] == $arr[$i+1]) $count++;
  5.   else {
  6. // if false reset count to 1 and save value along with current count
  7.     save_in_storage($arr[$i], $count);
  8.     $count = 1;
  9.   }
  10. }
note that you have to define save_in_storage() and $arr_length yourself. this is not a working example, it is meant to show the principle, you can use it as starting point.

regards
Nov 14 '08 #4
canabatz
155 New Member
thanx allot ,im going to try that :)
Nov 14 '08 #5
pbmods
5,821 Recognized Expert Expert
Heya, canabatz.

Try adding a DISTINCT to your SELECT query (http://www.w3schools.com/SQL/sql_distinct.asp).
Nov 14 '08 #6
canabatz
155 New Member
it is not distinct my problem :) , i need to count the results sent by users ,i need the code to start counting from top of the results and find me results from the same user ,i want to be able to limit results by user ,i want to limit the user to maximum 4 rows in a run if it reaches 5 rows the result number 5 will be deleted or something !

like this situation is good:

user1
user1
user1
user1
user2 <===== there is one result between user1 so its ok
user1

and this way is not good:

user1
user1
user1
user1
user1 <===== there is 5 result from user1 so result No. 5 will be deleted
user2

thanx :)
Nov 15 '08 #7
canabatz
155 New Member
i didnt figure this yet ,please help!!
Nov 25 '08 #8
nathj
938 Recognized Expert Contributor
Hi,

Yo could do one of two things.

1. Run a second query using a count() and group by clause to get just the count out.

2. Use the query you have got and the loop through an array keeping count, when the value changes store the count to an associative array and rest the count.

Either of those options should get you going in the right direction.

nathj
Nov 25 '08 #9
canabatz
155 New Member
i try so many combinations ,but not results :(

this is the code i'm using right now for testing ,but its limiting the user to maximu 4 results from the total of 50!!

Expand|Select|Wrap|Line Numbers
  1. $query = mysql_query("SELECT * FROM `bidding_details` where bid_id='$bid_id' and username='$username' limit 50") or die(mysql_error());
  2. $last = '';
  3. $count = 0;
  4.  
  5. while($line = mysql_fetch_assoc($query)) {
  6.   if($last == $line['username']) $count++;
  7.   else {
  8.     $last = $line['username'];
  9.     $count = 1;
  10.   }
  11.  
  12.   if($count == 4) {
  13.     header("location:product_detailframe.php?msg=11&&bid_id=$bid_id");
  14.     exit;
  15.   }
  16. }
and here is what i need it to do:




green is good it's not over the limit ,red is bad its over the limit ,the yellow row is the last inserted row ,so username canabatz got is bid in 5 rows in a row ,and it will be deleted or just display a massage!!

thanx
Nov 25 '08 #10
nathj
938 Recognized Expert Contributor
Ok, I think I get it now, that picture helped me see what you are after.

Perhaps we need to change approach here. Counting rows is all well and good and perhaps a SQL COUNT() and group by query would work maybe something like:
Expand|Select|Wrap|Line Numbers
  1. SELECT username, count(*) FROM biddingdetails group by username having count(*) >=1 ;
  2.  
That should get you a list of each username that has bid at least once and how many bids they have made.

Alternatively it may be that you generate a new table that permanently stores the count and that figure is incremented with every bid. This would give you the bid count and the full bid history.

Have a think around those two options.
nathj
Nov 25 '08 #11
canabatz
155 New Member
there is no way to do it like that:

query started

if found user1! start counting
if no more user1 in the list stop counting user1
start counting user2 if finished with this user2 and there is new user
start counting the new user!!

im stuck!! :(
Nov 25 '08 #12
chelvan
90 New Member
define an array. its length limit by your database values (distinct). then increase the specific array element.then finally you able to get the result.

regards
chel-1
Nov 26 '08 #13
Markus
6,050 Recognized Expert Expert
@chelvan
Care to explain a little more, even I don't understand.
Nov 26 '08 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

2
3824
by: Reply via newsgroup | last post by:
Folks, When performing an update in mysql (using PHP), can I find out how many records were matched? mysql_affected_rows() won't work... and I have the following problem that I thought I...
16
1696
by: walexand | last post by:
I use the database mysql v.4. My problem is... I have a select like: select * from user where language = "de"; the result are then: id name =================== 1 max
3
3031
by: Viswanatha Thalakola | last post by:
Hello, Can someone point me to getting the total number of inserts and updates on a table over a period of time? I just want to measure the insert and update activity on the tables. Thanks....
2
1741
by: Adam | last post by:
In my MYISAM table I have an index (Index_A) on 2 fields (Field_A, Field_B). There are millions of rows in the table. The cardinality of Index_A is 53. I think a query to count the number of rows...
3
2047
by: Peter | last post by:
Hi, This post is about counting rows and finding the max count in a group of rows. I'm trying to find the player who scored the most goals in a game. I know how to find out how many goals the...
0
1217
by: Chia Lee Lee | last post by:
Hello… I have problem when counting the number of records, which is based on the start date and end date. I have tried to use message box to prompt the result, but the result given is...
1
3037
by: Newmanbt | last post by:
I've got the following code testlabel.Text = allquestions.Select().Count.ToString(); GridView1.DataSource = allquestions.Select(); GridView1.DataBind(); testlabel.Text =...
1
1626
by: jasone | last post by:
hey, this is what ive got so far: ("SELECT (Select count(*) from tbl_flight_details) + (select count(*) FROM tbl_flight_departures) as grandtotal") i need to count all the records in the...
2
1598
by: akadeco | last post by:
Hi I need to write a script that will allow me to check the value of any link in a table cell. The desired result would be something like this: row.cell.innerHTML, allowing me to call the contents...
7
6373
by: crochunter | last post by:
Hi I was trying to count rows and columns in a tab delimited flat file. Like here in example below i should be able to detect automatically the no of rows and columns. So I should get 5 columns...
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7353
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7011
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4689
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3170
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
747
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.