adp1207 Posted October 10, 2011 Share Posted October 10, 2011 Hello: I'm trying to compare two mySQL result sets (for determining when to check a checkbox in an input form). $dataList['listname'] is my needle. (only the checkboxes I want to check) $data['listname'] is my haystack (the list of all checkboxes) while($dataList = mysql_fetch_array($runListQuery)) { $checked = ''; echo $data['listname'].'<br /><br />'; echo $dataList['listname'].' :: ' . $data['listname'] . '::' . (string)array_search ($dataList['listname'],$data['listname'],false) .'<br>'; echo in_array($dataList['listname'],$data['listname']); echo '<br>----------------------<br>'; var_dump($data['listname']); echo '<br>----------------------<br>'; var_dump ($dataList['listname']); echo '<br>----------------------<br>'; if(in_array($dataList['listname'],$data['listname'],false)) { echo 'found!!!!!!!!!!!'; $checked = 'CHECKED'; } echo '<tr>'; echo '<td>' . $dataList['listname'] . '</td>'; echo '<td><input type="checkbox" '. $checked . ' name="'.$dataList['listname'].'" />'; echo '</tr>'; } I never hit the 'found!!!!!!!!' string. My output is below: list-charter halwasiya :: list-charter:: ---------------------- string(12) "list-charter" ---------------------- string(9) "halwasiya" ---------------------- list-charter halwits :: list-charter:: ---------------------- string(12) "list-charter" ---------------------- string(7) "halwits" ---------------------- list-charter list-charter :: list-charter:: ---------------------- string(12) "list-charter" ---------------------- string(12) "list-charter" ---------------------- Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 10, 2011 Share Posted October 10, 2011 Well, you shouldn't need TWO queries to begin with. You can almost certainly get the data you need with ONE query. But, looking at the data you have above the results are correct. Not only do the values do not match - but you are using in_array() on two string values - not arrays. Quote Link to comment Share on other sites More sharing options...
adp1207 Posted October 10, 2011 Author Share Posted October 10, 2011 mjdamato: Query 1 gives me info about the particular record. Here's the code: $query = "select ls.username as email,ls.id,ls.listname as listname from users u join list_subscriber ls on ls.username = u.username where u.id = $eid LIMIT 1"; $runquery = mysql_query($query); $data = mysql_fetch_assoc($runquery); Query 2 gives me all of the possible checkboxes. Here's the code: $getListQuery = 'select distinct(listname) as listname from list_details order by listname'; $runListQuery = mysql_query($getListQuery); For each checkbox (needle, name = listname), I want to see if a user subscribes to the list (haystack, box should be checked). Quote Link to comment Share on other sites More sharing options...
jcbones Posted October 10, 2011 Share Posted October 10, 2011 So, $data['listname'] is a string, and $dataList['listname'] is a string, as both are returned in an array, but are NOT arrays themselves. Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 11, 2011 Share Posted October 11, 2011 I think your DB structure is a little out of whack. Not sure why you would need to use DISTINCT on the table that presumably stores the details of the list items. There should only be one record per list item. Also, you are storing the list name in the "list_subscriber" table. You should be storing the ID of the list item - why have an ID for your list items and not use it?. You should really reevaluate your DB design. Also, it will make your life a whole lot easier if you standardize your queries. By this I mean putting SQL functions in ALL CAPS, adding line breaks and spacing to give contextual meaning to your query. It is almost impossible for me to understand your first query without copying it into a text editor and doing that. But, give this query a try with what you have. SELECT distinct(ld.listname) as listname, IF(ld.listname=ul.listname) AS checked FROM list_details AS ld LEFT JOIN (SELECT ls.username as email, ls.id, ls.listname FROM users AS u JOIN list_subscriber AS ls ON ls.username = u.username WHERE u.id = $eid ) AS ul ORDER BY ld.listname Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.