Trick Question; Showing 'nr of results' of MySql search - php

I have a classifieds website, and users can search ads.
The results are displayed in three tabs on top of the page. These three are "All", "Private", and "Company".
Each tab has a nr attached to it, which represents the nr of ads found in that tab.
So for example:
All Private Company
5 3 2
All is just a total of private+company!
I am using MySql as a database.
I am trying to figure out a way to find out these "numbers of ads found" for each tab.
I have one way of doing this, which is like this, but gives me a headache because it is so messy:
$query = "SELECT SQL_CACHE * FROM classified WHERE classified.classified_id=$id";
if ($adtypes=="Private"){
$query_priv_comp = "SELECT SQL_CACHE * FROM classified WHERE priv_comp='Company'";
}
else {
$query_priv_comp = "SELECT SQL_CACHE * FROM classified WHERE priv_comp='Private'";
}
switch ($adtypes){
case "Private":
$query.= " AND classified.priv_comp='Private'";
break;
case "Company":
$query.= " AND classified.priv_comp='Company'";
break;
}
$qry_result = mysql_query($query); // main query
$result_priv_comp = mysql_query($query_priv_comp); // second query
$num_priv_comp = mysql_num_rows($result_priv_comp);
if ($adtypes=="All"){
$num_total = mysql_num_rows($qry_result);
}
else if ($adtypes!="All"){
$num_total=mysql_num_rows($qry_result) + mysql_num_rows($result_priv_comp);
}
if ($adtypes=="Private"){
$num_private = $num_total - $num_priv_comp;
$num_company = $num_priv_comp;
}
else {
$num_company = $num_total - $num_priv_comp;
$num_private = $num_priv_comp;
}
Do you know of any other way which this can be done?
Thanks
BTW: I need the rows too, in order to display information to the user of the ads found!

It depends on what you need exactly. If you just need the counts it's relatively easy:
SELECT count(*) count_all
, sum(if(priv_com = 'Private', 1, 0)) count_private
, sum(if(priv_com = 'Company', 1, 0)) count_company
FROM classified
WHERE classified.classified_id=$id
If on the other hand, you need both counts as well as row data, you should either do separate queries for the data and the counts, or use a trick. Let's say your table has an id column wich is primary key, you could do:
SELECT count(*) count_all
, sum(if(priv_com = 'Private', 1, 0)) count_private
, sum(if(priv_com = 'Company', 1, 0)) count_company
, classified.*
FROM classified
WHERE classified.classified_id=$id
GROUP BY id -- group by on primary key
WITH ROLLUP
The WITH ROLLUP magic will give you an extra row with the counts for the entire query. The only snag is that you will receive this row as last row of the entire result, so if you want to report the counts before the data, you're going to have to cache the row data in an php array or so and process that later to build up the page.

After your switch variable
$query_priv_comp would be equal to:
SELECT SQL_CACHE * FROM classified WHERE priv_comp='Company'
AND classified.priv_comp='Private'
or
SELECT SQL_CACHE * FROM classified WHERE priv_comp='Private'
AND classified.priv_comp='Company'
Question: What the difference???

You can select all counts with one query:
SELECT priv_comp, COUNT(*) AS record_count FROM classified GROUP BY priv_comp
Then you can query all the records needed for the current tab.
These 2 should be separated clearly.

Related

SQL in PHP trying to list quantity column of different rows through php

I have a table of items in my inventory for my game:
Item -- Quantity
Air Orb -- 5
Fire Orb -- 10
I'm trying to find how to put these values into php variables in one SQL query instead of making a separate sql searches for each item im looking for.
I've already tried UNION for it:
$orbs= mysql_query("SELECT itemqty AS airorbqty FROM inventories WHERE username= '$username' AND itemname= 'Air Orb' UNION ALL SELECT itemqty AS fireorbqty FROM inventories WHERE username = '$username' AND itemname='Fire Orb' ");
while($row= mysql_fetch_array($orbs))
{
$airqty= $row['airorbqty '];
$fireqty= $row['fireorbqty '];
}
echo "air orbs: ", $airqty, "Fire Orbs: ", $fireqty;
but it unfortunately gives me errors. Im trying to use it later where skills only work depending on these quantities of the specific items it requires.
for example, the fire attack would use 1 air orb and 2 fire orbs and only be able to be used if those 2 items have quantities equal or higher than the required amount for them.
thanks in advance.
Easy, use SQL IN for many items:
$orbs = ['Air Orb'=>0,'Fire Orb'=>0];
$orbs_result = mysql_query("SELECT itemname, itemqty FROM inventories WHERE username = '$username' AND itemname IN('Air Orb', 'Fire Orb')");
while($row = mysql_fetch_array($orbs_result)) {
$orbs[$row['itemname']] = $row['itemqty'];
}
if ($orbs['Air Orb'] >= 5 and $orbs['Fire Orb'] >= 2) {
echo "Fire Attack Ready";
}

How can I order the results of an SQL query by the count of another table based on a shard ID?

I have an SQL query that fetches posts from a database. Everything works fine, but now I need to order the results by the number of comments each post has. The comments are on a separate table and they have a post_id column that links it to the post. I need to order the posts by the count of the comments table based on a shard ID? I have tried everything but every time I try to add something to my query it stops running completely and leaves my page blank. I need help to know where to put the other JOIN statement. This is my query:
$union = "UNION ALL
(
SELECT DISTINCT wallposts.p_id,wallposts.is_profile_notes,wallposts.times_viewed,wallposts.columnTimesShared,
wallposts.marked,wallposts.secure_id,wallposts.reshared,wallposts.group_id,
wallposts.totaluploads,wallposts.WallUploadID,wallposts.type,
wallposts.value,wallposts.media,wallposts.youtube,wallposts.post_type,
wallposts.privacy,wallposts.tagedpersons,wallposts.with_friends_tagged,wallposts.emotion_head,wallposts.selected_emotion,wallposts.title,
wallposts.url,wallposts.description,wallposts.cur_image,
wallposts.uip,wallposts.likes,wallposts.userid,
wallposts.posted_by,wallposts.post as postdata,wallusers.*,
UNIX_TIMESTAMP() - wallposts.date_created AS TimeSpent,
PosterTable.mem_pic as posterPic, PosterTable.gender as posterGender,PosterTable.oauth_uid as poster_oauth_uid, PosterTable.username as posterUsername,
PosterTable.mem_fname as posterFname,PosterTable.work as posterWork,
PosterTable.mem_lname as posterLname,walllikes_track.id as PostLikeFound,wallposts.date_created
FROM
wallusers,wallusers as PosterTable, wallposts
LEFT JOIN walllikes_track
ON wallposts.p_id = walllikes_track.post_id AND walllikes_track.member_id = ".$user_id."
WHERE
wallusers.active = 1
AND
PosterTable.active = 1
AND
wallposts.group_id IN (".$groups.")
AND
wallposts.group_id != 0
AND
PosterTable.mem_id = wallposts.posted_by
AND
wallposts.marked < ".$this->flagNumber."
AND
wallusers.mem_id = wallposts.posted_by ) ";
The comments table is called wallcomments and it has a column called post_id. I know I need to use JOIN and COUNT but I don't know where to put it within my current code.
Try this query, I didn't run but i updated it.
SELECT wallposts.p_id,wallposts.is_profile_notes,wallposts.times_viewed,wallposts.columnTimesShared,
wallposts.marked,wallposts.secure_id,wallposts.reshared,wallposts.group_id,
wallposts.totaluploads,wallposts.WallUploadID,wallposts.type,
wallposts.value,wallposts.media,wallposts.youtube,wallposts.post_type,
wallposts.privacy,wallposts.tagedpersons,wallposts.with_friends_tagged,wallposts.emotion_head,wallposts.selected_emotion,wallposts.title,
wallposts.url,wallposts.description,wallposts.cur_image,
wallposts.uip,wallposts.likes,wallposts.userid,
wallposts.posted_by,wallposts.post as postdata,wallusers.*,
UNIX_TIMESTAMP() - wallposts.date_created AS TimeSpent,
PosterTable.mem_pic as posterPic, PosterTable.gender as posterGender,PosterTable.oauth_uid as poster_oauth_uid, PosterTable.username as posterUsername,
PosterTable.mem_fname as posterFname,PosterTable.work as posterWork,
PosterTable.mem_lname as posterLname,walllikes_track.id as PostLikeFound,wallposts.date_created
FROM
wallusers,wallusers as PosterTable, wallposts
WHERE
wallusers.active = 1
AND
PosterTable.active = 1
AND
wallposts.group_id IN (".$groups.")
AND
wallposts.group_id != 0
AND
PosterTable.mem_id = wallposts.posted_by
AND
wallposts.marked < ".$this->flagNumber."
AND
wallusers.mem_id = wallposts.posted_by ) " AND wallposts.p_id = walllikes_track.post_id AND walllikes_track.member_id = ".$user_id.";
A more readable query might look like this...
At least then we'd have a chance.
SELECT DISTINCT p.p_id
, p.is_profile_notes
, p.times_viewed
, p.columnTimesShared
, p.marked
, p.secure_id
, p.media...
FROM wallposts p...

Return list of member_number count in table

Consider the following table
I need to return a list in descending order with the count of the member_nr's who is appearing the most frequent in the table where tournament = 'EPL' AND ROUND = '12'
EXAMPLE
The script should return the following results:
I thought about the problem and my logic for the problem reads like this
STEP1: GET member_nr one by one
$sql = "SELECT DISTINCT *
FROM winners
WHERE tournament='$tour' AND round='$round'";
LOOP(){ //get 1 member number
$mem_nr = ['mem_nr']; //assign mem_nr to variable
STEP2: GET the count(number of times) ^ABOVE^ member number appears in table
"$sql="SELECT *, count(member_nr) as nrWins
FROM winners
where member_nr ='$memNr' and tournament='$tournament' AND round='$round'";"
LOOP(){//get count
STEP 3: DISPLAY DATA
echo $row=['nrWins'] //Display Number Wins
echo $memNr
}//END LOOP
}//END LOOP
My Problem:
The above does not seem very efficient to me, im looking for the shortest most efficient way to return the count of member numbers in table above, any ideas / suggestions are welcomed
Try something like this:
SELECT *, COUNT(*) AS `wins`
FROM `winners`
WHERE `tournament` = '$tournament'
AND `round` = '$round'
GROUP BY `member_nr`
ORDER BY `wins` DESC
select tournament,round,member_nr,count(*)
from table
where tournament = 'EPL'
and round = 12
group by tournament,round,member_nr
order by count(*) desc

How to update top 5 enteries from the chosen top 10 enteries from db

WRT: how to insert records for top 10 enteries only
This is more refined question.
I have gathered top 10 users for specific task. Using the query given below.
mysql_query("SELECT `userid`, SUM(`points`) as `total` FROM
`tablename` GROUP BY `userid` ORDER BY total DESC LIMIT 10");
Now , I need to award bonues to the top 3 gathered users only, out of these selected top 10. I am not getting the idea , on how to write query for this purpose.
The top 3 bonuses are different for top 3
1: First user gets 1000 points
2: 2nd user gets 500 points
3: 3rd user gets 100 points
I need to update one field in tablename = user , field name = points.
To update the bonus i am using the below query(general query):
$Db1->query('UPDATE user SET points=points+'.$rate.'
WHERE userid = '.$credituser);
I hope this question is well elaborated as per the community standards.
Kindly guide.
Working on a pure MySQL version.
As you've PHP tagged, I'll incorporate that into my answer :)
(Ps: Don't use mysql_*. As OP used mysql_*, my answer will)
Fetch the results
Loop through the results
Update the results depending on their position
$objGet = mysql_query("SELECT `userid`, SUM(`points`) as `total` FROM
`tablename` GROUP BY `userid` ORDER BY total DESC LIMIT 10");
if( mysql_num_rows($objGet) ) {
$intIteration = 1;
while( $arrResults = mysql_fetch_assoc($objGet) ) {
switch($intIteration) {
case 1 :
mysql_query("UPDATE tablename SET points = points + 1000 WHERE userid = ". $arrResults['userid']);
break;
case 2 :
mysql_query("UPDATE tablename SET points = points + 500 WHERE userid = ". $arrResults['userid']);
break;
case 3 :
mysql_query("UPDATE tablename SET points = points + 100 WHERE userid = ". $arrResults['userid']);
break;
default:
//No idea.
//Not in the top 3.
break;
}
$intIteration++;
}
} else {
//No users.
}

PHP Calculate rank from database

I got a little problem, I've got a database, in that database are different names, id, and coins. I want to show people their rank, so your rank has to be 1 if you have the most coins, and 78172 as example when your number 78172 with coins.
I know I can do something like this:
SELECT `naam` , `coins`
FROM `gebruikers`
ORDER BY `coins` DESC
But how can I get the rank you are, in PHP :S ?
You can use a loop and a counter. The first row from MySql is going the first rank,I.e first in the list.
I presume you want something like:
1st - John Doe
2nd - Jane Doe
..
..
right?
See: http://www.if-not-true-then-false.com/2010/php-1st-2nd-3rd-4th-5th-6th-php-add-ordinal-number-suffix
Helped me a while ago.
You could use a new varariable
$i = "1";
pe care o poti folosi in structura ta foreach,while,for,repeat si o incrementezi mereu.
and you use it in structures like foreach,while,for,repeat and increment it
$i++;
this is the simplest way
No code samples above... so here it is in PHP
// Your SQL query above, with limits, in this case it starts from the 11th ranking (0 is the starting index) up to the 20th
$start = 10; // 0-based index
$page_size = 10;
$stmt = $pdo->query("SELECT `naam` , `coins` FROM `gebruikers` ORDER BY `coins` DESC LIMIT {$start}, {$page_size}");
$data = $stmt->fetchAll();
// In your template or whatever you use to output
foreach ($data as $rank => $row) {
// array index is 0-based, so add 1 and where you wanted to started to get rank
echo ($rank + 1 + $start) . ": {$row['naam']}<br />";
}
Note: I'm too lazy to put in a prepared statement, but please look it up and use prepared statements.
If you have a session table, you would pull the records from that, then use those values to get the coin values, and sort descending.
If we assume your Session table is sessions(session_id int not null auto_increment, user_id int not null, session_time,...) and we assume that only users who are logged in would have a session value, then your SQL would look something like this: (Note:I am assuming that you also have a user_id column on your gebruikers table)
SELECT g.*
FROM gebruikers as g, sessions as s WHERE s.user_id = g.user_id
ORDER BY g.coins DESC
You would then use a row iterator to loop through the results and display "1", "2", "3", etc. The short version of which would look like
//Connect to database using whatever method you like, I will assume mysql_connect()
$sql = "SELECT g.* FROM gebruikers as g, sessions as s WHERE s.user_id = g.user_id ORDER BY g.coins DESC";
$result = mysql_query($sql,$con); //Where $con is your mysql_connect() variable;
$i = 0;
while($row = mysql_fetch_assoc($result,$con)){
$row['rank'] = $i;
$i++;
//Whatever else you need to do;
}
EDIT
In messing around with a SQLFiddle found at http://sqlfiddle.com/#!2/8faa9/6
I came accross something that works there; I don't know if it will work when given in php, but I figured I would show it to you either way
SET #rank = 0; SELECT *,(#rank := #rank+1) as rank FROM something order by coins DESC
EDIT 2
This works in a php query from a file.
SELECT #rank:=#rank as rank,
g.*
FROM
(SELECT #rank:=0) as z,
gebruikers as g
ORDER BY coins DESC
If you want to get the rank of one specific user, you can do that in mysql directly by counting the number of users that have more coins that the user you want to rank:
SELECT COUNT(*)
FROM `gebruikers`
WHERE `coins` > (SELECT `coins` FROM `gebruikers` WHERE `naam` = :some_name)
(assuming a search by name)
Now the rank will be the count returned + 1.
Or you do SELECT COUNT(*) + 1 in mysql...

Categories