php forum
php mysql forum
php mysql smarty
 
Topic Options
#284298 - 03/15/05 09:04 AM Help with a SELECT Query that is beyond me
Medar Offline
Junior Member

Registered: 03/19/00
Posts: 571
So these things get intricate and my eyes start crossing. Hey, still not bad for a Project Manager that has no coding training!<br /><br />So I am coding sort of a "challenge ladder", and everything works fine, but I am to a point where I want to display all past records. Here is the layout of my records table:<br /><br />ladder_records<br />R_Id = autonum<br />R_Ladder = INT (this is a digit that represents what ladder this ties to)<br />R_Win = INT (this is the USER NUMBER of the winner)<br />R_Loss = INT (this is the USER NUMBER of the loser)<br />R_Type = CHAR (either 'W' or 'F' for win or forfeit)<br />R_Date = datestamp field.<br /><br />So thing of the R_Win and R_Loss field as the same exact User numbers we have with Threads (ie U_Number).<br /><br />ladder_members<br />M_Id = autonum<br />M_Name = User name input by them<br />.....rest should not matter<br /><br />So......I am trying to write a query that will pull all Wins AND Losses of a given member, order it by DATE, but display the User NAMES instead of their ID's. I am getting expectedly goofy results.<br /><br />In the below example, $mid is my MEMBER ID variable called prior to this select query.<br /><br />
Code:
$result = mysql_query("SELECT R_Id,R_Win,R_Loss,R_Type,R_Date,M_Name <br />                             FROM dl_Members,dl_Records <br />                             WHERE R_Win=$mid OR R_Loss=$mid AND M_Id=$mid <br />                             ORDER BY R_Date");<br />    if ($myrow = mysql_fetch_array($result)) {<br />    <br />      do {<br />      <br />        $date = date("m/d/Y", $myrow["R_Date"]);        <br />        if ($myrow["R_Win"] == $mid) {<br />           printf("&lt;td&gt;%s - %s - Win over %s (%s)&lt;/td&gt;&lt;/tr&gt;\n", $myrow["R_Id"], $date, $myrow["M_Name"], $myrow["R_Type"]);<br />        }<br />        elseif ($myrow["R_Loss"] == $mid) {<br />           printf("&lt;td&gt;%s - %s - Lost to %s (%s)&lt;/td&gt;&lt;/tr&gt;\n", $myrow["R_Id"], $date, $myrow["M_Name"], $myrow["R_Type"]);<br />        }<br />        else {}<br />      }<br />      <br />      while ($myrow = mysql_fetch_array($result));<br />}
<br /><br />This obviously does not work, and sadly I have tried about 10 different variations based off stuff I am trying to glean off the internet. Anyone know off the top of their head the PROPER way to write this select query?<br /><br />Thanks.

Top
#284299 - 03/15/05 09:22 AM Re: Help with a SELECT Query that is beyond me [Re: Intel -*RW*-]
scroungr Offline
Old Hand

Registered: 10/17/03
Posts: 2409
Loc: Richmond, VA
SELECT t1.U_Username, t2.R_Id,t2.R_Win,t2.R_Loss,t2.R_Type,t2.R_Date<br />FROM w3t_Users AS t1<br />LEFT JOIN dl_Records AS t2 ON t1.U_Number = t2.R_Win<br />LEFT JOIN dl_Records AS t3 ON t1.U_Number = t2.R_Loss<br />WHERE t1.U_Username='$mid'<br />ORDER BY t2.R_Date<br /><br />providing <br />R_Id,R_Win,R_Loss,R_Type,R_Date are all in dl_Records<br /><br />should work but without working with an actual database mileage may vary
_________________________
Couchtomatoe - www.couch-tomatoe.cc
My abilities are for hire for installs, upgrades, custom themes and custom modifications.

Top
#284300 - 03/15/05 10:54 AM Re: Help with a SELECT Query that is beyond me [Re: 234234]
scroungr Offline
Old Hand

Registered: 10/17/03
Posts: 2409
Loc: Richmond, VA
now that would list every member in the user table say you wanna refine it and just show users with a win or loss..<br /><br />SELECT t1.U_Username, t2.R_Id,t2.R_Win,t2.R_Loss,t2.R_Type,t2.R_Date<br />FROM w3t_Users AS t1<br />LEFT JOIN dl_Records AS t2 ON t1.U_Number = t2.R_Win<br />LEFT JOIN dl_Records AS t3 ON t1.U_Number = t2.R_Loss<br />WHERE t1.U_Username='$mid' && (t2.R_Win != '' || t2.R_Loss != '')<br />ORDER BY t2.R_Date<br /><br />that would just display those users with something in win or loss...
_________________________
Couchtomatoe - www.couch-tomatoe.cc
My abilities are for hire for installs, upgrades, custom themes and custom modifications.

Top
#284301 - 03/15/05 10:59 AM Re: Help with a SELECT Query that is beyond me [Re: 234234]
Medar Offline
Junior Member

Registered: 03/19/00
Posts: 571
Thanks scroungr! I am getting much closer, and using your arcade code as a textbook, LOL.<br /><br />
Code:
$query = "<br />     SELECT t1.U_Username, t2.R_Id,t2.R_Win,t2.R_Loss,t2.R_Type,t2.R_Date<br />     FROM w3t_Users AS t1<br />     LEFT JOIN dl_Records AS t2 ON t1.M_Id = t2.R_Win<br />     LEFT JOIN dl_Records AS t3 ON t1.M_Id = t2.R_Loss<br />     WHERE t1.U_Number='$mid'<br />     ORDER BY t2.R_Date<br />     ";<br />     $sth = $dbh -&gt; do_query($query,__LINE__,__FILE__);<br />     while (list($name,$rid, $rwin, $rloss, $rtype, $rdate) = $dbh -&gt; fetch_array($sth)) {<br />     $date = date("m/d/Y", $rdate);<br />          echo "&lt;tr&gt;&lt;td&gt;$rid - $date - $rwin/$rloss - $charname ($rtype)&lt;/td&gt;&lt;/tr&gt;";<br />     }
<br /><br />This outputs:<br /><br />Statistics <br />ID - DATE - - - UID / UID - LOSER - TYPE<br />17 - 03/14/2005 - 1/8 - Win over Medar (W) <br />20 - 03/14/2005 - 1/8 - Win over Medar (W) <br />22 - 03/14/2005 - 1/3 - Win over Medar (W) <br />24 - 03/14/2005 - 1/7 - Win over Medar (W)<br /><br />My first problem is that it is pulling MY username instead of my opponent (I am user 1, my opponents are listed above as users 8, 3, and 7).<br /><br />There should also be 8 lines total, the ones above are all of my WINS...so the losses are not being pulled in by my statement. I have 4 losses that should display in the same format.<br /><br />Still messing with it...but in theory it should show up as this:<br /><br />Statistics <br />ID - DATE - - - UID / UID - OTHER USER (TYPE)<br />17 - 03/14/2005 - 1/8 - Win over Joe (W) <br />19 - 03/14/2005 - 8/1 - Loss to Joe (W) <br />20 - 03/14/2005 - 1/8 - Win over Joe (W) <br />22 - 03/14/2005 - 1/3 - Win over Bob (W) <br />23 - 03/14/2005 - 3/1 - Loss to Bob (W) <br />24 - 03/14/2005 - 1/7 - Win over Fred (W)<br />25 - 03/14/2005 - 2/1 - Loss to Sue (W) <br />26 - 03/14/2005 - 9/1 - Loss to Kate (W)

Top
#284302 - 03/15/05 11:22 AM Re: Help with a SELECT Query that is beyond me [Re: Intel -*RW*-]
Medar Offline
Junior Member

Registered: 03/19/00
Posts: 571
Not that anyone cares to 'see' this per se...but here is the VERY simple'd up view of it (I have not prettied it up yet).<br /><br />http://www.bladekeep.com/ladder/<br /><br />Basically it is a challenge ladder I can use in any games we play that allows people to receive a challenge notification in a PM, accept it within 48 hours or forfeit, and then post results within 48 hours of accepting.<br /><br />If the lower-ranked challenger wins, he swaps places with the higher-ranked winner. A challenger can only challenge up to 3 ranks above him. But if he loses, he drops one notch.<br /><br />The part I am working on above is the 'myladder' page that will show your record against all opponents, it is pretty much the last piece I need to figure out!

Top
#284303 - 03/15/05 03:03 PM Re: Help with a SELECT Query that is beyond me [Re: Intel -*RW*-]
scroungr Offline
Old Hand

Registered: 10/17/03
Posts: 2409
Loc: Richmond, VA
cause I am guessing $mid is your current number if you want all then leave that WHERE t1.U_Number='$mid' out
_________________________
Couchtomatoe - www.couch-tomatoe.cc
My abilities are for hire for installs, upgrades, custom themes and custom modifications.

Top
#284304 - 03/15/05 03:39 PM Re: Help with a SELECT Query that is beyond me [Re: 234234]
Medar Offline
Junior Member

Registered: 03/19/00
Posts: 571
Tried that, and yep, got all records for everyone. The $mid stands for Member_Id...which I am trying to have that query show all statistics for me (or whoever is looking).<br /><br />Sort of a character sheet with past matches listed...<br /><br />I just wiped all data on the ladder, going to have some of my members start banging it and get some better test data in there. Using that I can hopefully get this displaying correctly.<br /><br />If anyone is bored...Threadsdev / threadsdev

Top
#284305 - 03/19/05 07:59 PM Re: Help with a SELECT Query that is beyond me [Re: Intel -*RW*-]
DrChaos Offline
Coder

Registered: 09/12/03
Posts: 816
Loc: Hollywood Florida.
Hey Medar, Is this something you are going to release? (or want to release)<br /><br />I was tring to get something like this started with threads but never found anyone interested that had the know how. I had someone here (forgot who it was. its been awhile) but never heard anything.<br /><br />This is a great addition to a gaming forum. Is there an option to add other game types like halo and starcraft?
_________________________
DrChaos
LeetGamers

Top
#284306 - 03/20/05 04:57 PM Re: Help with a SELECT Query that is beyond me [Re: Duck]
scroungr Offline
Old Hand

Registered: 10/17/03
Posts: 2409
Loc: Richmond, VA
yeah I had about 99.9% completed but never had a chance to fix the brackets
_________________________
Couchtomatoe - www.couch-tomatoe.cc
My abilities are for hire for installs, upgrades, custom themes and custom modifications.

Top
#284307 - 03/20/05 08:41 PM Re: Help with a SELECT Query that is beyond me [Re: 234234]
DrChaos Offline
Coder

Registered: 09/12/03
Posts: 816
Loc: Hollywood Florida.
snif, snif. I still lub you <img src="http://www.ubbdev.com/forum/images/graemlins/smile.gif" alt="" /><br /><br />lol
_________________________
DrChaos
LeetGamers

Top
#284308 - 03/21/05 09:03 AM Re: Help with a SELECT Query that is beyond me [Re: Duck]
Medar Offline
Junior Member

Registered: 03/19/00
Posts: 571
I can guarantee that Scroungr's is a hell of a lot more polished than mine...as I basically can hack out a few things here and there, but have no conventional PHP training.<br /><br />Simply put, sometimes my stuff works, but it is never optimized and certainly not pretty on the inside.<br /><br />But sure...I can drop the code out here, there are a ton of people that could make it better. BUT - there are no brackets with this, it is a classic ladder-style setup that we used to use in an old game called Neverwinter Nights back in the early 90's.

Top
#284309 - 03/21/05 02:10 PM Re: Help with a SELECT Query that is beyond me [Re: Intel -*RW*-]
DrChaos Offline
Coder

Registered: 09/12/03
Posts: 816
Loc: Hollywood Florida.
Do you know what a fully intergrated Tournament or player vs player mod would g for? I will prolly never be able to afford photopost and if there was an intergrated tournament mod for threads, IM positive i could not afford what it would be worth.
_________________________
DrChaos
LeetGamers

Top



Latest Posts
[7.2.1] - Naked shoutbox
by bellaonline
05/05/12 05:00 PM
[7.x] Stop Forum Spam Integration v0.4
by bellaonline
05/05/12 03:53 PM
Shout Box

(Views)Popular Topics
Known public proxy servers 1689885
Integrated Index Page (IIP) 5.3.1 555705
Finished-[6.5.2] Games Arcade Deluxe v1.9 501236
Integrated Index Page (IIP) 5.1.1 415112
TLD Bv2.1 Released - Threads Links Directory 396822
[6.0x] Who's Online 4.0.0 [Finished] 389412
Finished-[6.5.1] Integrated Index Page (IIP) 6.5 330423
Q & A 298663
Slash UBB 266936
[6.3.x] [beta] Hit Hack 2.0 227970
Forum Stats
13621 Members
59 Forums
37191 Topics
295716 Posts

Max Online: 686 @ 06/28/07 07:04 AM

 

 

 
fusionbb message board php hacks