I'm horrible at arrays. And I'm now using a scrip that has a lot of neat predefined functions and things that I adore and would never have time to come up with myself.

I'm facing a problem with sorting an array in it though. Can't figure out where to really go in and... determine how to get the info displayed in correct order.

the $tpl->assign_block_vars basically creates a loop out of variables to output using a html template. Now... I want all this sorted by the variable called 'CURRENT'. Can't do ORDER BY in the sql statements since it's nested within a other sql statement call etc. Any help would be apprecciated. Here's the code:

		$getitem = mysql_fetch_assoc(mysql_query("SELECT * FROM witem WHERE itemname='".addslashes($item_name)."'"));
		$itemdesca= $getitem['itemdesc'];
		$itemdescb= str_replace("\n", "<br>", $itemdesca);
		$mob= $getitem['mob'];
		$itemloc=$getitem['itemloc'];
		$iid=$getitem['iid'];
		$itemlocup= mysql_fetch_assoc(mysql_query("SELECT * FROM wlocation WHERE lid='$itemloc'"));
		$itemmobup= mysql_fetch_assoc(mysql_query("SELECT * FROM wmob WHERE mid='$mob'"));
		$curmli= $user->data['user_id'];
		$curmemberloggedin= mysql_fetch_assoc(mysql_query("SELECT * FROM " . MEMBER_USER_TABLE . " WHERE user_id='$curmli'"));
		$curnamememli= $curmemberloggedin['member_id'];
		$findcurmemlogin= mysql_fetch_assoc(mysql_query("SELECT * FROM " . MEMBERS_TABLE . " WHERE member_id='$curnamememli'"));
		$curnamememli2= $findcurmemlogin['member_name'];
		$findcurmemlogin2= mysql_fetch_assoc(mysql_query("SELECT * FROM " . W_PLAYER_TABLE . " WHERE pname='$curnamememli2'"));
		$pid2= $findcurmemlogin2['pid'];


	$reqres= mysql_query("SELECT * FROM " . W_REQUEST_TABLE . " WHERE iid='$iid'");
	$bnum = mysql_num_rows($reqres);
	$a =0;
	while ($a < $bnum){
	$rid=mysql_result($reqres,$a,"rid");
	$iid=mysql_result($reqres,$a,"iid");
	$pid=mysql_result($reqres,$a,"pid");

	$findplayer= mysql_fetch_assoc(mysql_query("SELECT * FROM " . W_PLAYER_TABLE . " WHERE pid='$pid'"));
	$pname= $findplayer['pname'];
	$findmember= mysql_fetch_assoc(mysql_query("SELECT * FROM " . MEMBERS_TABLE . " WHERE member_name='$pname'"));
	$member_earned= $findmember['member_earned'];
	$member_spent= $findmember['member_spent'];
	$member_adjustment= $findmember['member_adjustment'];
	$member_current= $member_earned-$member_spent+$member_adjustment;


	$tpl->assign_block_vars('wish_row', array(
        'ROW_CLASS' => $eqdkp->switch_row_class(),
        'MEMBER_WISH' => $pname,
		'U_VIEW_MEMBER' => 'viewmember.php'.$SID.'&amp;' . URI_NAME . '='.$findmember['member_name'],
		'EARNED'        => $member_earned,
        'SPENT'         => $member_spent,
       	'ADJUSTMENT'    => $member_adjustment,
        'CURRENT'       => $member_current,
        'LASTRAID'      => ( !empty($findmember['member_lastraid']) ) ? date($user->style['date_notime_short'], $findmember['member_lastraid']) : '&nbsp;',
        'C_ADJUSTMENT'  => color_item($findmember['member_adjustment']),
        'C_CURRENT'     => color_item($findmember['member_current']),
        'C_LASTRAID'    => 'neutral')

    );

	++$a;
	}

    You could use the [man]usort/man function, something like:

    function mysort($a, $b)
    {
       return($a['CURRENT'] - $b['CURRENT']);
    }
    usort($arrayName, 'mysort');
    

    The above assumes the 'CURRENT' element is an integer. If it is a string, you'd use [man]strcmp/man in the mysort function, and if it's a float then the function would need to be tweaked so that it returns a negative integer, 0, or a positive integer.

      NogDog wrote:

      You could use the [man]usort/man function, something like:

      function mysort($a, $b)
      {
         return($a['CURRENT'] - $b['CURRENT']);
      }
      usort($arrayName, 'mysort');
      

      The above assumes the 'CURRENT' element is an integer. If it is a string, you'd use [man]strcmp/man in the mysort function, and if it's a float then the function would need to be tweaked so that it returns a negative integer, 0, or a positive integer.

      What do I call as $arrayName though? this is where I'm lost. Anything I try using usort on gives a fatal error of some sort <.<

      Thanks for response so far 🙂

        $arrayName should be changed to whatever array it is that you want to sort.

          If the array was assigned one variable this would be easy. I don't know how to calll that particular array or sort it though. If anyone can actually understand the script from the code I've written enough to know where and how to sort it, I'd aprecciate it.

            It sounds like you don't actually have an array to sort, yet. I certainly can't find it.

            Some of the database interaction is eye-twisting, too.

            $findplayer= mysql_fetch_assoc(mysql_query("SELECT * FROM " . W_PLAYER_TABLE . " WHERE pid='$pid'"));
                    $pname= $findplayer['pname'];
                    $findmember= mysql_fetch_assoc(mysql_query("SELECT * FROM " . MEMBERS_TABLE . " WHERE member_name='$pname'")); 

            Why not

            Select
            member_earned, member_spent, member_current, member_adjustment, member_lastraid
            From
            MEMBERS_TABLE, W_PLAYER_TABLE
            Where
            MEMBERS_TABLE.member_name=W_PLAYER_TABLE.pname
            and pid=$pid

              12 days later

              Last response got me into researching various joins to actually query multiple databases at once. Still failing. I've also posted to different forums and getting sorta sad that there doesn't seem to really be any solution to it. I've tried for over a week and still get bugs.

              Here's the rough of it.

              Using and reading up on joins and various ways of query multiple tables at same time to get an array of rows, I haven't been able to find anything that works for me.

              The query roughly, that I've experimented is something like:

              $sql = "SELECT m.member_name, (m.member_earned-m.member_spent+m.member_adjustment) AS m.member_current, m.member_earned, m.member_spent, m.member_adjustment, m.member_lastraid,
              		p.pid, p.pname, w.rid, w.pid, w.iid
              		FROM " . MEMBERS_TABLE . " m, " . W_PLAYER_TABLE . " p, " . W_REQUEST_TABLE . " w
              		WHERE  w.iid='$iid' AND (m.member_name = p.pname) AND (w.pid=p.pid)
              		ORDER BY m.member_current";

              The purpose is to get a array to loop through, with the columns I've selected.

              Best result I have gotten was
              Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

              Looping only through table 3 at first, I can get all the data output I want. By using mysql fetch assoc instead.
              But that loses the ability to order output by m.member_current since all data from MEMBERS_TABLE is gotten after the loops has already started.

              I thought I had it when I finally came up with:

              $reqres= mysql_query("SELECT m.member_name, m.member_earned, m.member_spent, m.member_adjustment
              	(m.member_earned-m.member_spent+m.member_adjustment) AS m.member_current
              	m.member_lastraid, p.pid
              FROM " . MEMBERS_TABLE . " m, " . W_REQUEST_TABLE . " w
              RIGHT JOIN " . W_PLAYER_TABLE . " p
              ON m.member_name = p.pname
              WHERE (w.iid = '$iid') AND (w.pid = p.pid)");
              
              
              	while ($row = mysql_fetch_array($reqres))
              	{ (insert PHP formatting of output here)
              }		

              but again, I get the Warning.

              I've been trying my best, debugging this to bits. What I really want to know is, can someone come up with a query to actually do what I want this to do:

              Have a preset $iid that I want to look up who requested p.pid=w.pid and then loop through the member data of said player by looking up p.pname and looping through p.pname's records by setting it = to m.member_name. Then to finish it all off order this by a value I set in sql m.member_current.

              Thanks for the comments so far.

                Finally did find solution. Removing the m. infront of when I was trying to calculate member_current did the trick.

                  Write a Reply...