...

View Full Version : Learning Array, How to Search



ptmuldoon
01-10-2008, 10:45 PM
With previous help from so many others, I've put together the below code in trying to learn and understand arrays better.

First, if there's a better easier way, please feel free to point it out, as I'm trying to learn the best I can.

Now, the code is getting all of the data as defined by the query. And then each row in the query is put into its own array. Then I want should be able to search each row/array for a pstate of 'trading'.

And if I have a match, i want to be able to back up one array to get the last player, and advance one array to get next player. I think the below is close, but it keeps giving me the same player name.



$sql2 = "SELECT * FROM players WHERE gid = 1134 AND pid > 1 AND pstate != 'dead' ";
$result2 = mysql_query($sql2);

while ($row = mysql_fetch_assoc($result2)) {
$pdata[] = $row;
}
print "<pre>";
print_r($pdata);
print "</pre>";

for ($row = 0; $row < mysql_num_rows($result2); $row++){
foreach($pdata[$row] as $key => $value){
//Get the Current Palyer
if($pdata[$row]['pstate'] == 'trading'){
$cid = $pdata[$row]['pid'];
$cplayer = $pdata[$row]['pname'];
}
// Get the Next Player Info.
if($pdata[$row]['pstate'] == 'trading'){
$next = $row + 1;
if($next = $row < mysql_num_rows($result2)){
$next = 2;
}
$nid = $pdata[$next]['pid'];
$nplayer = $pdata[$next]['pname'];
}
// Get the Last Player Info. Unsure What do use here.
}

}

Fou-Lu
01-11-2008, 07:22 AM
I'm a little confused by the requirements you are looking for.
Say I have three results:
1 -- Something
2 -- Trading
3 -- Something
The understanding I have of your question, is that you want to get the results for the 1 and the 3, that is the last player to the trading one, and the next player to the trading one. Is this the correct assumption?
If it is, you could try something like so. The $pdata[] population precedes this, though there are ways to do this in the actual fetch for the database, this is fine and probably more readable:


while ($record = current($pData))
{
if ($record['pstate'] != 'Trading')
{
continue;
}

$prev = prev($pData);
next($pData);
$next = next($pData);
// Processing stuff here
}

Its been awhile since I've controlled an array going back and forth, but if I recall correctly this should put you in the right place. Letsee, first you start at the first, then you rewind it once, store that result, forward once to the original position and step once again and store the result. The pointer should be at that next one, yeah that looks right. You will need to check your results as well, prev and next vars can both contain array out of bounds exceptions.
Is this along the idea your looking for, or something completely different?

ptmuldoon
01-14-2008, 03:55 PM
Yes, you understand 100% correctly. I hope to possibly begin working on this project again this week. I think I understand what you showed me, but I'll have to continue experimenting/learning as to how to incorportate that into what I've already started/done. I'm a little uncertain as to what code I should look to replace/modify with what you showed me.

ptmuldoon
01-16-2008, 01:39 AM
ok, I'm trying to follow what you showed me, but I'm still not getting the last player to show correctly. Am I on the right track here, or is there another way to go about it?

Again the object is to have each row set up as an array. Then to search the arrays and find out which one has the pstate of 'trading'. Then to be able to back up and the previous player/array.



$sql2 = "SELECT * FROM game_players WHERE gid = 1134 AND pid > 1 AND pstate != 'dead' ";
$result2 = mysql_query($sql2);

while ($row = mysql_fetch_assoc($result2)) {
$pdata[] = $row;
}
print "<pre>";
print_r($pdata);
print "</pre>";

while ($record = current($pdata)){
if ($record['pstate'] != 'trading'){
$cid = $record['pid'];
$cname = $record['pname'];
}

//Get the Last Player
$prev = prev($pdata);
$lid = $prev['pname'];

//Return to Current Position
next($pdata);

//Forward to Next Player
$next = next($pdata);
// Processing stuff here
}
echo "The Last Player was: " . $lid . "<BR>";
echo "The Current Player is: " . $cname;

Fou-Lu
01-16-2008, 06:44 PM
Looks like its a start on the correct track, if you give me a db structure and some test data I can compare against I can try to put it together myself as well.
What I can tell you is that if even if the current record is not trading it will still process, which is not what you want if I'm not mistaken. This means you are overwriting the value of CNAME when you probably do not mean to.


if ($record['pstate'] != 'trading')
{
continue;
}

So, if the item we are looking at is not trading, just skip the rest. Now, still in the same loop after the trading condition test:


$prev = prev($pdata);
next($pdata); // Reset it back to current
$next = next($pdata);

We now have a list for both the previous and the next values:


if ($prev != null)
{
$lid = $prev['pname'];
}
if ($next != null)
{
$nid = $next['pname'];
}
$cname = $record['pname'];

Now, you have the current name, the previous name, and the next name (if they exist) to deal with.
However, I noticed as well that your output is after your loop, not inside of it. So, this will iterate through every item and grab the data if necessary for every record, not just a specific record. If its just for wanting three results only (a specific record, the one before and the one after), we'll need to optimize the code to do just that.
Word of advice though, try to do this via hard-coded array values first, not through an SQL query. The reason for this is so that you can determine first if the functionality is correct - it may be an error in your query result set and not one in the actual code.
Let me know how it goes!

ptmuldoon
01-16-2008, 07:32 PM
Well, although I understand some of what your explaining, I must say I'm still a bit confused. But to help in trying to provide some more info, below is a simple sample table to use.


CREATE TABLE `sample_players` (
`gid` smallint(6) NOT NULL default '0',
`pid` smallint(4) NOT NULL default '0',
`pname` varchar(32) NOT NULL default '',
`pstate` varchar(25) NOT NULL default ''

) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Dumping data for table `sample_players`
--

INSERT INTO `sample_players` (`gid`, `pid`, `pname`, `pstate`) VALUES
(1134, 0, 'Player 0', 'inactive'),
(1134, 1, 'Player 1', 'inactive'),
(1134, 2, 'Player 2', 'inactive'),
(1134, 3, 'Player 3', 'trading'),
(1134, 4, 'Player 4', 'inactive'),
(1134, 5, 'Player 5', 'inactive'),
(1134, 6, 'Player 6', 'inactive'),
(1134, 7, 'Player 7', 'dead'),
(1134, 8, 'Player 8', 'inactive');

And this is the code following your lead


$sql2 = "SELECT * FROM sample_players WHERE gid = 1134 AND pid > 0 AND pstate != 'dead' ";
$result2 = mysql_query($sql2);

while ($row = mysql_fetch_assoc($result2)) {
$pdata[] = $row;
}
print "<pre>";
print_r($pdata);
print "</pre>";

while ($record = current($pdata)){
if ($record['pstate'] != 'trading'){
continue;
}

$prev = prev($pdata);
next($pdata); // Reset it back to current
$next = next($pdata);

if ($prev != null)
{
$lname = $prev['pname'];
}
if ($next != null)
{
$nname = $next['pname'];
}
$cname = $record['pname'];

}

And here's the output of the array. But attempts to echo out the correct current, last, and next player info is still incorrect.


Array
(
[0] => Array
(
[gid] => 1134
[pid] => 1
[pname] => Player 1
[pstate] => inactive
)

[1] => Array
(
[gid] => 1134
[pid] => 2
[pname] => Player 2
[pstate] => inactive
)

[2] => Array
(
[gid] => 1134
[pid] => 3
[pname] => Player 3
[pstate] => trading
)

[3] => Array
(
[gid] => 1134
[pid] => 4
[pname] => Player 4
[pstate] => inactive
)

[4] => Array
(
[gid] => 1134
[pid] => 5
[pname] => Player 5
[pstate] => inactive
)

[5] => Array
(
[gid] => 1134
[pid] => 6
[pname] => Player 6
[pstate] => inactive
)

[6] => Array
(
[gid] => 1134
[pid] => 8
[pname] => Player 8
[pstate] => inactive
)

)

Fou-Lu
01-17-2008, 02:13 AM
No problem, lets check this out.
Can you give me a couple of examples of what will be used (ie, what your query criteria is and expected results)? That would be super helpful. In the meantime, I'll work with what you got here mate.

OK.
So, I lied to you, prev/next doesn't return null, it returns === false. But, if prev === false, for some reason it will not let me reset the value, since it would step out of bounds. Just kinda loops infinitely.
So instead, lets run through a standard loop:


$prev = null;
$next = null;
$cur = null;
for ($i = 0; $i < count($pData); $i++)
{
if ($pData[$i]['pstate'] != 'trading')
{
continue;
}
$cur = $pData[$i];
if ($i - 1 >= 0)
{
$prev = $pData[$i - 1];
}
if ($i + 1 < count($pData))
{
$next = $pData[$i + 1];
}
break;
}

echo "The Last Player was: " . $prev['pname'] . "<BR>";
echo "The Current Player is: " . $cur['pname'] . '<br />';
echo "The Next player is: " . $next['pname'];

That gives me a result of:
The Last Player was: Player 2
The Current Player is: Player 3
The Next player is: Player 4

Now, the loop will exit on the first found trading player. If this is not what you want (and want to fetch all and store/do something with it), remove the 'break;' to continue within the loop.
Does that suit your purposes?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum