Jump to content

only allow x number of items to be checked


envexlabs

Recommended Posts

Hey,

 

I have a function that renders out tag categories, and then all the tags within said category.

 

Each tag has a checkbox beside it, i am wondering if it's possible to only allow a user to check 4 tags.

 

Here is the function:

 

                $select_tags_query = mysql_query('SELECT * FROM `tag_category`');
                
                while($tag_category = mysql_fetch_row($select_tags_query))
                {
                
                    echo '<div class="tag_cat">';
                
                    //renders out the category name
                
                    echo '<h2>' . $tag_category[1] . '</h2>';
                    
                    $tags_query = mysql_query('SELECT * FROM `tags` WHERE `cat_id` = ' . $tag_category[0] . '');
                    
                    //renders out each tag in the category
                    
                    while($tags = mysql_fetch_row($tags_query))
                    {
                    
                        echo '<p><input type="checkbox" />' . $tags[1] . '</p>';
                    
                    }
                    
                    echo '</div> <!-- tag_cat div -->';
                    
                }

 

This is what is outputs:

 

whas_example.jpg

Link to comment
Share on other sites

Hey,

 

I have encountered another problem.

 

When I post the form, this is what i get when i print_r($_POST):

 

Array ( [tag20] => on [tag81] => on [change_tags] => true )

 

How can I figure out, out of 200 tags, which 2 have been chosen, because the $_POST[tagx] will change depending on which tags are chosen?!

 

Thanks,

 

envex

Link to comment
Share on other sites

the trick to this is Make creation and processing one in the same

<?php
$fields = array(
"Mens Apparel" => array(),
"Womans Apparel" => array()
);
$i  = 0;
foreach($fields as $key => $value){
echo "<b>".$key."<b/>";
foreach($value as $value2){
echo "<input type=\"checkbox\" name=\"fields[".$i."]" />";
$i++;
}
<br/>
}?>

 

then they have a counting system in the multi d array and you can pull it off that.

Link to comment
Share on other sites

Hey,

 

I've figured it out:

 

            if($_POST[submit] == "true"){
                
                $tag = $_POST['tag'];
                foreach ($tag as $tagid)
                {
                    //inserts the chosen tags into the database
                    $insert_tags_query = mysql_query('INSERT INTO `store_tags` (`store_id`, `tag_id`, `order_id`) VALUES (\'' . $au[member_id] . '\',\'' . $tagid . '\',\'0\')');
                
                }

            }else{
                //do nothing
            }

 

I am, however, back to my original problem.

 

Javascript wont work because each checkbox doesn't have a unique ID.

 

Anyone know a work around to this?

 

Thanks,

 

envex

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.