0

I have mysql set up like this

MySQL

Table: gallery

+-------------+------------+
| galleryname | coverphoto |
+-------------+------------+

Table: multigalleries

+-------------+-----------+
| galleryname | galleries |
+-------------+-----------+

I am using a checkbox array from php to place galleries into multigalleries.galleries like gallery1,gallery2,gallery3. All of that works fine, the only thing i cant figure out is, when I pull the list of gallery.galleryname and reference it with multigalleries.galleries, how can i return a checked state if it exists in multigalleries.galleries?

Any help would be greatly appreciated.

EDIT// ACTUAL CODE IM USING IF IT WILL HELP

$con = mysql_connect($host,$dbusername,$dbpassword);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($databasename, $con);
$query = "SELECT galleries.gallery, galleries.coverphoto, multigalleries.multigallery,            multigalleries.galleries FROM galleries, multigalleries";
$result = mysql_query($query);

while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<div class="gallery" style="background-image:none">';
echo '<input type="checkbox" name="galleriesm[]" value="'.$line[gallery].'">'.      $line[gallery].'';
echo '</div>';
2
  • 4
    What have you tried? Commented Aug 23, 2012 at 19:51
  • I have tried isinarray as well as joining the table togethar from a select command to say if gallery.galleryname == multigalleries.gallery but its not seeming to work. most of the help I found was relating to a checked state being yes/no or 0/1 not actual values. Commented Aug 23, 2012 at 20:08

1 Answer 1

2

Try this:

$a = mysql_query("
    SELECT g.galleryname FROM gallery AS g, multigalleries AS m
    WHERE g.galleryname = 'gallery1' AND FIND_IN_SET(g.galleryname, m.galleries)
");

$b = mysql_fetch_assoc($a);

echo '<input type="checkbox" name="whatever"';

if($b['galleryname'])
{
    echo ' checked="checked"';
}

echo '>';

Edit:

$result = mysql_query("
    SELECT g.galleryname, m.galleryname AS name FROM gallery AS g 
    LEFT JOIN (SELECT galleryname, galleries FROM multigalleries) AS m ON 
    FIND_IN_SET(g.galleryname, m.galleries)
");

while($row = mysql_fetch_assoc($result))
{
    $checked = '';

    if($row['name'])
    {
        $checked = ' checked="checked"';
    }

    echo '<input type="checkbox" name="galleriesm[]" id="'.$row['galleryname'].'" value="'.$row['galleryname'].'"'.$checked.'>
    <label for="'.$row['galleryname'].'">'.$row['galleryname'].'</label>';
}
Sign up to request clarification or add additional context in comments.

6 Comments

Can get this to work either, It of course calls the correct values from the database when i echo them, but it wont check the boxes.
BUT I think this is on the right track! So I will spend the next few pots of coffee on this haha.
Could you please add your markup to your original question? Because now I'm not sure if I understood the question correctly.
I added it as per your request.
I updated my answer. :) Let me know if this is what you wanted.
|

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.