Jump to content


Photo

Loops not working?


  • Please log in to reply
18 replies to this topic

#1 jwer78

jwer78
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 27 February 2006 - 05:39 PM

Well not sure what Im doing wrong here, so here goes. Im trying to make a dynamic bid module for my league website. People will place bids on players so players can have multiple bids. I am trying to pull the team whos total bid(fbid) is the highest on a given player, and then loop it to the next player; but somewhere I screwed up lol. Any help would be greatly appreciated.
$mysql_query = "SELECT Player from nuke_fa_bids group by Player";
$res=mysql_query($mysql_query);
while($result = mysql_fetch_array($res)){
$current_player=$result['Player'];
$current_id=$result['id'];

$mysql_query = "SELECT * from nuke_fa_bids where Player=$current_player order by Fbid DESC LIMIT 1";
$res=mysql_query($mysql_query,$db);
$num_rows = mysql_num_rows($res);
for($count=0;$count<$num_rows;$count++){
$team[$count]=mysql_result($res,$count,"Team");
$player_name[$count]=mysql_result($res,$count,"Player");
$player_pos[$count]=mysql_result($res,$count,"Position");
$fbid[$count]=mysql_result($res,$count,"Fbid");
}
}
$tdbgcolor = array("D6CFCE","0000FF");
    for($count=0;$count<$num_rows;$count++){
        echo "<tr>";
        echo "<td bgcolor=" . $tdbgcolor[$count%2] . " align=left class=boldblacktext><img src=images/smalllogos/". $team .".gif></td>";
        echo "<td bgcolor=" . $tdbgcolor[$count%2] . " align=center class=boldblacktext>$player</td>";
        echo "<td bgcolor=" . $tdbgcolor[$count%2] . " align=center class=boldblacktext>$player_pos</td></tr>";

}


#2 earl_dc10

earl_dc10
  • Members
  • PipPipPip
  • Advanced Member
  • 71 posts

Posted 27 February 2006 - 11:19 PM

well, I couldn't decifer what you were trying to accomplish with this script (sorry), but this may be the problem, change
where Player=$current_player

to
WHERE Player='$current_player'

Im pretty sure the ' ' are necessary

if that's not the problem, please post the error message you get

hope that helps
got a problem? Google helps many of those in need

#3 jwer78

jwer78
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 28 February 2006 - 12:13 AM

Well that fixed a little something, thank you. Now Im getting "Array" to display. What I need this script to do is go into a table. Find all unique player names then find the highest bidder for each player and display that team, player and position. Right now its just showing the result is still an array so I am still stuck...but atleast its showing something lol. Any additional help would be appreciated, thanks.

#4 jwer78

jwer78
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 28 February 2006 - 12:25 AM

Doh..dumb me forgot [$count]. Now its working but its not lopping to get each player. There is 2 test entries on 2 different players each. but its only grabbing 1 of them. Here is the updated code. Again any help would be greatly appreciated. Here is the [a href=\"http://bhfl.maddenfootballleagues.com/modules.php?name=Free_Agent_Leading_Bids\" target=\"_blank\"]URL[/a] if it helps.

$mysql_query = "SELECT Player from nuke_fa_bids group by Player";
$res=mysql_query($mysql_query);
while($result = mysql_fetch_array($res)){
$current_player=$result['Player'];
$current_id=$result['id'];

$mysql_query = "SELECT * from nuke_fa_bids where Player='$current_player' order by Fbid DESC LIMIT 1";
$res=mysql_query($mysql_query);
$num_rows = mysql_num_rows($res);
for($count=0;$count<$num_rows;$count++){
$team[$count]=mysql_result($res,$count,"Team");
$player_name[$count]=mysql_result($res,$count,"Player");
$player_pos[$count]=mysql_result($res,$count,"Position");
$fbid[$count]=mysql_result($res,$count,"Fbid");
}
}
$tdbgcolor = array("D6CFCE","0000FF");
    for($count=0;$count<$num_rows;$count++){
        echo "<tr>";
        echo "<td bgcolor=" . $tdbgcolor[$count%2] . " align=left class=boldblacktext><img src=images/smalllogos/". strtolower($team[$count]) .".gif></td>";
        echo "<td bgcolor=" . $tdbgcolor[$count%2] . " align=center class=boldblacktext>$player_name[$count]</td>";
        echo "<td bgcolor=" . $tdbgcolor[$count%2] . " align=center class=boldblacktext>$player_pos[$count]</td></tr>";

}


#5 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 28 February 2006 - 12:34 AM

Try this, it eliminates all of your repetitive querys...there is only one now.

$mysql_query = "SELECT * FROM nuke_fa_bids ORDER BY Player ASC, Fbid DESC";
while ($row = mysql_fetch_array($mysql_query, MYSQL_ASSOC)) {
    $player = $row['Player'];
    if (!$result[$player] || $result[$player]['bid'] < $row['bid']) {
        $result[$player]['bid'] = $row['Fbid'];
        $result[$player]['team'] = $row['Team'];
        $result[$player]['position'] = $row['Position'];
    }
}

$tdbgcolor = array("D6CFCE","0000FF");
$count = 0;

echo '
    <table border="1" cellpadding="3" cellspacing="0">
        <tr>
            <th>Team</th>
            <th>Player Name</th>
            <th>Position</th>
            <th>Winning Bid</th>
        </tr>';
        
foreach ($result as $key => $value) {
    echo '
        <tr style="background-color:' . $tdbgcolor[$count%2] . ';">
            <td align="left"><img src="images/smalllogos/'. $value[team] .'.gif"></td>
            <td align="center" class="boldblacktext">' . $value[player] . '</td>
            <td align="center" class="boldblacktext">' . $value[position] . '</td>
            <td align="center" class="boldblacktext">' . $key . '</td>
        </tr>';
        $count++;
}
echo '</table>';


#6 jwer78

jwer78
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 28 February 2006 - 12:39 AM

It doesnt seem to pull any data. I just get the first table row(column headers) and nothing else. Thanks for the suggestion.

#7 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 28 February 2006 - 12:41 AM

Apparently I removed too much of your script. Change:

$mysql_query = "SELECT * FROM nuke_fa_bids ORDER BY Player ASC, Fbid DESC";
while ($row = mysql_fetch_array($mysql_query, MYSQL_ASSOC)) {
    $player = $row['Player'];
    if (!$result[$player] || $result[$player]['bid'] < $row['bid']) {
        $result[$player]['bid'] = $row['Fbid'];
        $result[$player]['team'] = $row['Team'];
        $result[$player]['position'] = $row['Position'];
    }
}

to:

$query = "SELECT * FROM nuke_fa_bids ORDER BY Player ASC, Fbid DESC";
$r = mysql_query($query) or die("Could not query: " . mysql_error());
while ($row = mysql_fetch_array($r, MYSQL_ASSOC)) {
    $player = $row['Player'];
    if (!$result[$player] || $result[$player]['bid'] < $row['bid']) {
        $result[$player]['bid'] = $row['Fbid'];
        $result[$player]['team'] = $row['Team'];
        $result[$player]['position'] = $row['Position'];
    }
}

That should fix it.

#8 jwer78

jwer78
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 28 February 2006 - 12:48 AM

Still the same results and it didnt spit out the "Could not query" error.

#9 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 28 February 2006 - 12:58 AM

Hmmm, changed how the row colors are done, and changed bid to Fbid when it's building the array:

$query = "SELECT * FROM nuke_fa_bids";
$r = mysql_query($query) or die("Could not query: " . mysql_error());
while ($row = mysql_fetch_array($r, MYSQL_ASSOC)) {
    $player = $row['Player'];
    if (!$result[$player] || $result[$player]['bid'] < $row['Fbid']) {
        $result[$player]['bid'] = $row['Fbid'];
        $result[$player]['team'] = $row['Team'];
        $result[$player]['position'] = $row['Position'];
    }
}

$count = 0;

echo '
    <table border="1" cellpadding="3" cellspacing="0">
        <tr>
            <th>Team</th>
            <th>Player Name</th>
            <th>Position</th>
            <th>Winning Bid</th>
        </tr>';
        
foreach ($result as $key => $value) {
    if (($count % 2) == "0") {
        $color = "D6CFCE";
    } else {
        $color = "0000FF";
    }
    
    echo '
        <tr style="background-color:' . $color . ';">
            <td align="left"><img src="images/smalllogos/'. $value[team] .'.gif"></td>
            <td align="center" class="boldblacktext">' . $key . '</td>
            <td align="center" class="boldblacktext">' . $value[position] . '</td>
            <td align="center" class="boldblacktext">' . $value[bid] . '</td>
        </tr>';
        $count++;
}
echo '</table>';


#10 jwer78

jwer78
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 28 February 2006 - 01:03 AM

Nope, still nothing and still no error.

#11 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 28 February 2006 - 01:05 AM

Are you sure your query is returning rows?

#12 jwer78

jwer78
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 28 February 2006 - 01:12 AM

Meaning is there actually data present? Yes there is, I was finally getting the data how I originally coded it but it wasnt looping to get the next player, it was only returning the first player.

#13 jwer78

jwer78
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 28 February 2006 - 01:45 AM

Ok I echoed the $player here is the results
Resource id #291
Chris Redman
Chris Redman
Chris Redman
Jonathan Goodwin
Jonathan Goodwin
Shows there is data there...

#14 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 28 February 2006 - 01:55 AM

Make sure error reporting is on too:

ini_set("error_reporting", "1");
ini_set("display_errors", "E_ALL");


#15 jwer78

jwer78
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 28 February 2006 - 02:01 AM

Done. No errors reported.

#16 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 28 February 2006 - 02:08 AM

Hmm, I haven't a clue...try this:

echo "<pre>";
$query = "SELECT * FROM nuke_fa_bids";
$r = mysql_query($query) or die("Could not query: " . mysql_error());
while ($row = mysql_fetch_array($r, MYSQL_ASSOC)) {
  print_r($row);
}

Just execute that code. Make sure that the arrays are full of data. If it's possible, can you post a couple of the sub arrays?

#17 jwer78

jwer78
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 28 February 2006 - 02:18 AM

This will be a bit long but here you go. Hopefully it helps.
Array
(
    [id] => 1
    [Team] => Cowboys
    [Player] => Chris Redman
    [Position] => QB
    [Years] => 2
    [Thousand] => 0
    [Million] => 1.2
    [Bthousand] => .100
    [Bmillion] => 0
    [Fbid] => 0.7
    [ip] => xx.xx.xx.xx
    [user_name] => thecommish
)
Array
(
    [id] => 2
    [Team] => Lions
    [Player] => Chris Redman
    [Position] => QB
    [Years] => 2
    [Thousand] => 0
    [Million] => 2.0
    [Bthousand] => .500
    [Bmillion] => 0
    [Fbid] => 1.5
    [ip] => xx.xx.xx.xx
    [user_name] => thecommish
)
There others on another player for test purposes but I believe this will give you what you need.

#18 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 28 February 2006 - 02:32 AM

Using this:

<?php

//$query = "SELECT * FROM nuke_fa_bids";
//$r = mysql_query($query) or die("Could not query: " . mysql_error());
//while ($row = mysql_fetch_array($r, MYSQL_ASSOC)) {
$temp = array(
            Array('id' => '1','Team' => 'Cowboys','Player' => 'Chris Redman','Position' => 'QB','Years' => '2','Thousand' => '0','Million' => '1.2','Bthousand' => '.100','Bmillion' => '0','Fbid' => '0.7','ip' => 'xx.xx.xx.xx','user_name' => 'thecommish'),
            Array('id' => '2','Team' => 'Lions','Player' => 'Chris Redman','Position' => 'QB','Years' => '2','Thousand' => '0','Million' => '2.0','Bthousand' => '.500','Bmillion' => '0','Fbid' => '1.5','ip' => 'xx.xx.xx.xx','user_name' => 'thecommish'),
            
            Array('id' => '3','Team' => 'alpha','Player' => 'Ch Redman','Position' => 'QB','Years' => '2','Thousand' => '0','Million' => '2.0','Bthousand' => '.500','Bmillion' => '0','Fbid' => '1.0','ip' => 'xx.xx.xx.xx','user_name' => 'thecommish'),
            Array('id' => '4','Team' => 'bravo','Player' => 'Ch Redman','Position' => 'QB','Years' => '2','Thousand' => '0','Million' => '2.0','Bthousand' => '.500','Bmillion' => '0','Fbid' => '1.5','ip' => 'xx.xx.xx.xx','user_name' => 'thecommish'),
            Array('id' => '5','Team' => 'charlie','Player' => 'Ch Redman','Position' => 'QB','Years' => '2','Thousand' => '0','Million' => '2.0','Bthousand' => '.500','Bmillion' => '0','Fbid' => '2.5','ip' => 'xx.xx.xx.xx','user_name' => 'thecommish'),
            
            Array('id' => '6','Team' => 'delta','Player' => 'Chris Red','Position' => 'QB','Years' => '2','Thousand' => '0','Million' => '2.0','Bthousand' => '.500','Bmillion' => '0','Fbid' => '.5','ip' => 'xx.xx.xx.xx','user_name' => 'thecommish'),
            Array('id' => '7','Team' => 'echo','Player' => 'Chris Red','Position' => 'QB','Years' => '2','Thousand' => '0','Million' => '2.0','Bthousand' => '.500','Bmillion' => '0','Fbid' => '10.5','ip' => 'xx.xx.xx.xx','user_name' => 'thecommish')
);

foreach ($temp as $row) {
    $player = $row['Player'];
    if (!$result[$player] || $result[$player]['bid'] < $row['Fbid']) {
        $result[$player]['bid'] = $row['Fbid'];
        $result[$player]['team'] = $row['Team'];
        $result[$player]['position'] = $row['Position'];
    }
}

$count = 0;

echo '
    <table border="1" cellpadding="3" cellspacing="0">
        <tr>
            <th>Team</th>
            <th>Player Name</th>
            <th>Position</th>
            <th>Winning Bid</th>
        </tr>';
        
foreach ($result as $key => $value) {
    if (($count % 2) == "0") {
        $color = "D6CFCE";
    } else {
        $color = "0000FF";
    }
    
    echo '
        <tr style="background-color:' . $color . ';">
            <td align="left"><img src="images/smalllogos/'. $value[team] .'.gif"></td>
            <td align="center" class="boldblacktext">' . $key . '</td>
            <td align="center" class="boldblacktext">' . $value[position] . '</td>
            <td align="center" class="boldblacktext">' . $value[bid] . '</td>
        </tr>';
        $count++;
}
echo '</table>';

It works fine for me, so I'm at a loss.

#19 jwer78

jwer78
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 28 February 2006 - 03:31 AM

Hmm. Maybe its something with the versions of php or something. $Player will echo but if I echo the team, position, or bid nothing will show. May have to go back to square one I guess. Thanks for taking the time to help though, very much appreciate it.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users