0

my last question was no quite good, so im sorry to "open" new one.

Importante data:

Table1

  • id:1
  • id:2
  • id:3

Table2

  • id:1
  • id:3

Query1 (id Results):

  • 1,2,3

Query2 (id Results):

  • 1,3

So, I have a checkbox that need to be changed to "checked" if everytime that I fetch these 2 tables (with proper query ofc) and some id from table2 exists on table1.

Im doing like this:

<?php
while($row = mysql_fetch_array($Query1))
{
while($row2 = mysql_fetch_array($Query2))
{
$checkit = '';
if($row['id'] == $row2['id'] )
$checkit = 'checked="checked"';
}
?>
<input type="checkbox" name = "checkbox-1" class="checkbox" value ="" <?php echo $checkit ?> />
<?php
}
}
?>

Problem: I just can have "checked" for the first result, when 1=1.

That While fetch only once. I need that the result should be like this (id): 11-13 | 21-23 |31-33 and checked checkbox should appear only when 11 and 33

I hope that you understand this..

4
  • need to be checked="checked" no yes Commented Aug 2, 2012 at 16:39
  • possible duplicate of how to compare two tables in mysql and php? Commented Aug 2, 2012 at 16:40
  • my last question was no quite good, so im sorry to "open" new one. Commented Aug 2, 2012 at 16:43
  • It's best to edit your previous question rather than opening a new one, that way nobody thinks you're trying to spam the site with the same one. Commented Aug 2, 2012 at 16:44

3 Answers 3

1

you have use

$checkit = 'checked="yes"';

should be

 $checkit = 'checked="checked"';

try this:--

<?php

    $arr_one = '';
    while($row = mysql_fetch_array($Query1))
    {

        $arr_one[]= $row['id'];

    }


    while($row2 = mysql_fetch_array($Query2))
    {
            $checkit = '';

            $id = $row2['id'];
            if (in_array($id,$arr_one))
            {
                $checkit = 'checked="checked"';
            }



    ?>
            <input type="checkbox" name= "checkbox-1" class="checkbox" value ="" <?php echo $checkit ?> />
    <?php
        }

    ?>
Sign up to request clarification or add additional context in comments.

3 Comments

ok but there is not the main problem. That While fetch only once. I need that the result should be like this (id): 11-13 | 21-23 | 31-33
I have update my answer. Take a look and let me know if not working.
forget my last question in the "comments" , it was working proper, thanks again!
1

You will need to reset your internal result pointer after the internal while block.

    while($row = mysql_fetch_array($Query1))
    {
        while($row2 = mysql_fetch_array($Query2))
        {
              // Your code
        }
        mysql_data_seek($Query2,0);
    }

Comments

1

I would do:

$t2data = array();
while($row2 = mysql_fetch_assoc($Query2)) {
  $t2data[] = $row2[0];
}

while($row1 = mysql_fetch_assoc($Query1)) {
  if(in_array($row1[0], $t2data)) { 
    $checked = 'checked';
  }
}

That being said, if both of the tables are in the same database, this is best solved with an OUTER JOIN query:

select t1.id, (t2.t1_id IS NOT NULL) as checked
  from t1 
  left join t2 on (t1.id = t2.t1_id)

Then loop over the results and check only the boxes where checked=true from the query.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.