Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a set of nested while loops to compare to mysql queries to each other. Here is the code:

while($row = mysql_fetch_array($resultbat))
{
    $drafted = 0;

while($crossedRow = mysql_fetch_array($crossedAnswer))
{

    if($row['NAME'] == $crossedRow['name'])
    {
        $drafted = 1;
    }
    else
    {
        $drafted = 0;
    }
}
if ($drafted == 1)
{
    echo "<tr class='drafted' id='" . $row['NAME'] . "'>";
}
else if($n&1)
{
    echo "<tr id='" . $row['NAME'] . "'>";
}else
{
    echo "<tr class='alt' id='" . $row['NAME'] . "'>";
}

...}

In the $resultbat is a list of all players, and in the $crossedAnswer is a list of a few players that should be marked. For each player I want to see if they are in the $crossedAnswer list of players. If they are I want to mark the class of that html element to drafted.

Thanks in advance.

share|improve this question
    
Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial. –  Kermit Jan 15 '13 at 22:29
add comment

1 Answer

up vote 0 down vote accepted

I think you'll either need to:

  • open the cursor on $crossedAnswer for each row from $resultbat (open it at the beginning of each loop through, and close it at the end, which effectively runs that query for each for in resultbat), or

  • fetch all of the rows from the $crossedAnswer resultset into a structure, and look through that structure for each row from $resultbat, (to avoid running the same query multiple times against the database), or

  • ensure that both $resultbat and $crossedAnswer are ordered by key value, and do just a single loop; open both cursors, fetch a row from each, and then compare the key values to determine which resultset is "behind", and which one to fetch from (this is more efficient, but is more complicated code to write and test), or

  • if these resultsets are from the same MySQL server, rewrite this as a single query, and let MySQL do the work of joining the rows together, and process just a single resultset.


Personally, I would opt for the last. I would let MySQL do the join, get a single resultset back, and a single loop.

The query to get the resultset would be of the form:

SELECT b.*
     , IF(a.name IS NOT NULL,1,0) AS drafted
  FROM (
         query_for_$resultbat
       ) b
  LEFT
  JOIN (
         query_for_$crossedAnswer
       ) a
    ON b.name = a.name

Note that if a matching row is found in a, then the drafted column will return a 1, otherwise, it will return a 0.


The code to handle this resultset, based on the original code could be something like this:

while($row = mysql_fetch_array($result))
{
    $class = "";
    if($row['drafted'] == 1
    {
        $class = "class='drafted' ";
    }
    else if($n&1)
    {
        $class = "";
    }
    else
    {
        $class = "class='alt' ";
    }

    echo "<tr " . $class . " id='" . $row['NAME'] . "'>";
    ...
}
share|improve this answer
    
How would i fetch all the rows from $crossedAnswer into a structure? Could you give an example of this? –  rodzilla Jan 15 '13 at 22:37
    
I understand the concept of doing a join but I need to print all players from the $resultbat and just change the class on the specific players that are in the $crossedAnswer result. If I do a join to just give me players that are in both tables then I would not be able to print out all players. –  rodzilla Jan 15 '13 at 22:47
    
@user1956847: an OUTER JOIN does exactly what you need it to, it returns ALL rows from one table (or query), along with any "matching" rows (if any) from another table (or query). In your case, the resultset would need to include a column which would indicate whether the player was found in both tables or not. In general, running one query to get the resultset you need is more efficient than running two (or more) queries and rolling your own outer join operation. –  spencer7593 Jan 15 '13 at 22:49
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.