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
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
any short code example for counting like that?
i didn't get you ,sorry :(
tahnx
Dormilich 8,658
Recognized Expert Moderator Expert
something like: - for ($i=0; $i<$arr_length; $i++) {
-
// compare this and the next value
-
// if true increase count
-
if ($arr[$i] == $arr[$i+1]) $count++;
-
else {
-
// if false reset count to 1 and save value along with current count
-
save_in_storage($arr[$i], $count);
-
$count = 1;
-
}
-
}
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
thanx allot ,im going to try that :)
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 :)
i didnt figure this yet ,please help!!
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
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!! - $query = mysql_query("SELECT * FROM `bidding_details` where bid_id='$bid_id' and username='$username' limit 50") or die(mysql_error());
-
$last = '';
-
$count = 0;
-
-
while($line = mysql_fetch_assoc($query)) {
-
if($last == $line['username']) $count++;
-
else {
-
$last = $line['username'];
-
$count = 1;
-
}
-
-
if($count == 4) {
-
header("location:product_detailframe.php?msg=11&&bid_id=$bid_id");
-
exit;
-
}
-
}
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
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: -
SELECT username, count(*) FROM biddingdetails group by username having count(*) >=1 ;
-
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
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!! :(
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
Markus 6,050
Recognized Expert Expert @chelvan
Care to explain a little more, even I don't understand.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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
|
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....
|
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...
|
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...
| |
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...
|
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 =...
|
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...
|
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...
|
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...
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |