Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Aug 2010
    Posts
    408
    Thanks
    17
    Thanked 2 Times in 2 Posts

    comparing two arrays for a conditional

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

    PHP Code:
    $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.

    Last edited by Inigoesdr; 11-13-2011 at 03:14 AM.

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,197
    Thanks
    23
    Thanked 605 Times in 604 Posts
    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.

  • #3
    Regular Coder
    Join Date
    Aug 2010
    Posts
    408
    Thanks
    17
    Thanked 2 Times in 2 Posts
    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:
    PHP Code:
    $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...

    PHP Code:
    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..

    PHP Code:
    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>";} 
    Last edited by turpentyne; 11-12-2011 at 05:50 PM.

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,197
    Thanks
    23
    Thanked 605 Times in 604 Posts
    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 "

  • #5
    Regular Coder
    Join Date
    Aug 2010
    Posts
    408
    Thanks
    17
    Thanked 2 Times in 2 Posts
    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':

    PHP Code:
    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';};
    Last edited by turpentyne; 11-12-2011 at 07:23 PM.

  • #6
    Regular Coder
    Join Date
    Aug 2010
    Posts
    408
    Thanks
    17
    Thanked 2 Times in 2 Posts
    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 = '';};


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •