View Full Version : Resolved comparing two arrays for a conditional

11-12-2011, 02:28 AM
I'm trying to figure out how to take two queries, cross-reference them. Then I want to echo all of the results from query1 with an added bit of text for those that have a match in the second query. Here's as far as I've gotten...

$quer1=mysql_query("SELECT `ID`, `usage` FROM `edible_uses` order by `usage`");

$quer2=mysql_query("SELECT `plant_id`, `edible_id` FROM `plant_edible_link` WHERE `plant_id` = $id ");

$q1 = mysql_fetch_array($quer1);
$q2 = mysql_fetch_array($quer2);

echo explode(string ',' $q2); // need to figure how to explode.

while($q2 = mysql_fetch_array($quer2)) {

$ch2 = array_intersect($q2, $q2);

// array intersect only gives the matches. I still need to echo the unmached ones from the first quer.

11-12-2011, 09:34 AM
I think you need to use a JOIN here.
Is the ID from query 1 the same as $id in WHERE `plant_id` = $id?

Tell us what you are getting from the querys and what the match between then is.

11-12-2011, 06:44 PM
aha! Yeah! I was completely overthinking this! So this query gets what I wanted, and now I'm trying to figure out how to echo 'checked' or not for each row that isn't NULL on the checked column.
The working query:

$quer2=mysql_query("SELECT `ID`, `Name`,`uses_id` AS `checks` FROM `plant_uses` LEFT OUTER JOIN plant_uses_link ON plant_uses.id = plant_uses_link.uses_id AND plant_id = $id ORDER BY `Name`

now, to echo that info into what I'm printing, how do I write the ternary? Kinda new to those. I've got this right now, but I don't think it's quite right...

while($row = mysql_fetch_array($quer2)) {
if($row[id]==@$cat){echo "<input type='checkbox' name='option1'". $row[checks] ? 'checked' : '' ."value='$row[ID]'>$row[Name]<BR>";}
else{echo "<input type='checkbox' name='option1' value='$row[id]'>$row[usage]<br>";}

UPDATE: I changed it to this, and now every single box is checked. Not what I wanted..

while($row = mysql_fetch_array($quer2)) {
if($row[id]==@$cat){echo "<input type='checkbox' name='option2' checked='$row[checks3] ? 'checked' : NULL' value='$row[id]'>$row[Name]<BR>";}
else{echo "<input type='checkbox' name='option1' value='$row[id]'>$row[Name]<br>";}

11-12-2011, 07:34 PM
Your while statements do not have a closing bracket.
this line:
if ($row[id] == @$cat) ---- Remove the '@'.

And this line:
echo "<input type='checkbox' name='option1' value='$row[id]'>$row[usage]<br>";

the $row[id] has to be removed from the echo with ".$row[id]." and don't forget to escape the "

11-12-2011, 08:02 PM
hmmm... I cleaned it up, but still no go.

depending on how I write it, I can get none or all of the checkboxes to show checked, but not selected ones, like I need. I've turned the statement into a variable instead of inline with the echo. I've gotten to this right now. Still nothin':

while($row = mysql_fetch_array($quer2)) {

$ch2 = (is_null($row[checks2])) ? 'no' : 'yes';

if($row[id]==$cat){echo "<input type='checkbox' name='option2' checked=".$ch2." value='".$row[id]."'>".$row[usage].$row[checks2]."<BR>";}
else{echo "<input type='checkbox' name='option1' checked=".$ch2." value='".$row[id]."'>".$row[usage]. $row[checks2]." <br>";}
// i added that last row[checks2] just so I could make sure it's pulling data and it does. a number appears next to records that aren't null.

I also tried this to no avail: if (0!=strlen($row[checks2]) && is_numeric($row[checks2])) { $ch2 = 'checked=no';} else {$ch2 = 'checked=yes';};

11-12-2011, 08:29 PM
Neeever mind!!

It was simple html semantics on that checkbox

Here's what worked!

if (0!=strlen($test) && is_numeric($test)) { $ch2 = 'checked=yes';} else {$ch2 = '';};