array comparison and the drop-down box again

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jrhitokiri
    New Member
    • Apr 2008
    • 35

    array comparison and the drop-down box again

    i have this form that edits current documents which are stored in a mysql db. these documents can have single or multiple document owners, which are just users specifically picked.

    now, i want to dump the whole list of users into the drop-down box, and when i get to the names which were selected in the past, they would be highlighted. my problem is that the names keep on repeating themselves the same number of times there are selected names.

    here's my code:
    Code:
    <?
    $doc_owners = get_result("SELECT doc_owners.auth_uname FROM doc_owners WHERE ctrl_no =  '{$ctrl_no}' ");
    
    while($row = mysql_fetch_row($doc_owners))
    	$curr_owners[] = $row[0];
    
    $res = get_result("SELECT users.username,users.full_name FROM users,org_roles	WHERE users.org_role_id = org_roles.org_role_id ORDER BY org_roles.org_role_id");
    												    while($names = mysql_fetch_row($res))
    {
    	foreach($curr_owners as $i)
            {
                     if ($names[0] != $i)
    		     echo"<option value=".$names[0]." >".$names[1]."</option>";
    		else{
    		     echo"<option value=".$names[0]." selected='selected'>".$names[1]."</option>";
    		     unset($i);
    		}
    	}
    }
    echo"</select>";
    thanks! you guys never failed to help me..^^,

    cheers!
    Last edited by jrhitokiri; Apr 8 '08, 09:46 AM. Reason: solved
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    Originally posted by jrhitokiri
    i have this form that edits current documents which are stored in a mysql db. these documents can have single or multiple document owners, which are just users specifically picked.

    now, i want to dump the whole list of users into the drop-down box, and when i get to the names which were selected in the past, they would be highlighted. my problem is that the names keep on repeating themselves the same number of times there are selected names.

    here's my code:
    Code:
    
    
    <?
    $doc_owners = get_result("SELECT doc_owners.auth_uname FROM doc_owners WHERE ctrl_no =  '{$ctrl_no}' ");
    
    while($row = mysql_fetch_row($doc_owners))
    	$curr_owners[] = $row[0];
    
    $res = get_result("SELECT users.username,users.full_name FROM users,org_roles	WHERE users.org_role_id = org_roles.org_role_id ORDER BY org_roles.org_role_id");
    												    while($names = mysql_fetch_row($res))
    {
    	foreach($curr_owners as $i)
            {
                     if ($names[0] != $i)
    		     echo"<option value=".$names[0]." >".$names[1]."</option>";
    		else{
    		     echo"<option value=".$names[0]." selected='selected'>".$names[1]."</option>";
    		     unset($i);
    		}
    	}
    }
    echo"</select>";
    thanks! you guys never failed to help me..^^,

    cheers!

    Please put this code just before your second while loop and post your output, i want to see what the data looks like.
    [php]

    print_r($curr_o wners);
    print_r($names) ;
    die("END");

    [/PHP]

    Comment

    • jrhitokiri
      New Member
      • Apr 2008
      • 35

      #3
      Originally posted by dlite922
      Please put this code just before your second while loop and post your output, i want to see what the data looks like.
      [php]

      print_r($curr_o wners);
      print_r($names) ;
      die("END");

      [/PHP]
      nevermind, i solved this one already.. ^_^ thanks anyway...

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Originally posted by jrhitokiri
        nevermind, i solved this one already.. ^_^ thanks anyway...
        How did you solve said problem?

        Comment

        • jrhitokiri
          New Member
          • Apr 2008
          • 35

          #5
          Originally posted by markusn00b
          How did you solve said problem?
          what i did wrong was i queried twice and tried to compare 2 arrays of names, which means that there was a tendency to have duplicate entries. that was the hard way. but there was an easier way.

          what i did was i queried the general list of names, then for each name in the general list, i would query the project document owners. if there was an entry with the same project id with their name, then i would add the 'select' option, else dont.

          here's the code.

          Code:
          <select name="doc_owners[]" multiple="multiple">
           
          <?
          $res = get_result("SELECT users.username, users.full_name
              FROM users,org_roles
              WHERE users.org_role_id = org_roles.org_role_id
              ORDER BY full_name");
          												    while($names = mysql_fetch_row($res))
          {
              $doc_owners = get_result("SELECT doc_owners.*
              FROM doc_owners
              WHERE ctrl_no =  '{$ctrl_no}' && auth_uname = '{$names[0]}'");
              
              $num = mysql_num_rows($doc_owners);
              if ($num)
                  echo"<option value=".$names[0]." 
                        selected='selected'>".$names[1]."</option>";
              else
                 echo"<option value=".$names[0]." >".$names[1]."</option>";
          }
          												echo"</select>";

          Comment

          Working...