Jump to content

in_array() not working


adp1207

Recommended Posts

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" 
----------------------

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.