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 9 of 9
  1. #1
    New Coder
    Join Date
    Sep 2005
    Location
    California
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Categorizing a blog post under MORE than one category

    I'm running a blog (runs boastMachine), and I'm trying to be able to select multiple categories for a post rather than a drop-down menu to select only one option (default).

    I've gotten as far as when the PHP processes that part of the form. Here's the block of code, as is:

    PHP Code:
    // ======= The target category

    if(isset($_POST['cat'])) {
        
    // Something wrong. The selected category couldnt be found in the current blog
        
    if(!$db->query("SELECT id FROM ".MY_PRF."cats WHERE id='{$_POST['cat']}' AND blog='{$blog}'"false)) {
        
    bmc_template('error_page'$lang['post_no_cat']); exit;
        }

    } else {
        
    // No category!
        
    bmc_template('error_page'$lang['post_no_cat']); exit;

    The problem is, I'm not exactly sure how to switch from processing a menu to checkboxes. I have the checkbox set with the same name and all as the menu did, but I don't know what to change around this part. I don't know how to give the server more than one selection. If anyone has any tips for me on this, I would very much appreciate it. Thanks.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    If I understand you correctly, you have a form kind of like so:
    <input type="checkbox" name="cat" value="first_cat" />
    <input type="checkbox" name="cat" value="second_cat" />
    am I correct?
    If this is correct, first you need to send your cat as an array. Do so by adding empty close brackets after your 'cat'. You can add a unique field to it if you prefer an associative array, but that won't be nessessary for our purposes.
    <input type="checkbox" name="cat[]" value="first_cat" />
    <input type="checkbox" name="cat[]" value="second_cat" />
    Now, for your PHP you will have to extract these variables:
    PHP Code:

     
    // ======= The target category 

    if(isset($_POST['cat'])) { 
        
    // Something wrong. The selected category couldnt be found in the current blog 
       // For my addition, I'm uncertain of what you refer to with this... 'cat' should be set if I understand you correctly.
       
    $cat_fields implode (','$_POST['cat']);
        if(!
    $db->query("SELECT id FROM ".MY_PRF."cats WHERE id IN (" mysql_real_escape_string($cat_fields) . ")  AND blog='" mysql_real_escape_string($blog) . "'"false)) { 
        
    bmc_template('error_page'$lang['post_no_cat']); exit; 
        } 

    } else { 
        
    // No category! 
        
    bmc_template('error_page'$lang['post_no_cat']); exit; 

    Another option is a multiple select field. Thats a matter of esthetics though...

  • #3
    New Coder
    Join Date
    Sep 2005
    Location
    California
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks! And yes, you are correct. I've changed the field names from cat to cat[], and I've replaced the code with what you gave me. But I don't know what to set the part you commented on to, if I'm right by thinking something needs to be changed (the ',' in $cat_fields = implode (',', $_POST['cat']);). I tried it as-is and I get
    Warning: implode(): Argument to implode must be an array. in /home/navy2/public_html/proto/blog/bmc/inc/users/post.inc.php on line 195
    You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near ') AND blog='1'' at line 1
    so I figured it did. I don't entirely know what you're saying when you said that 'cat' should be set...what do I set it to? Thanks again for your help.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    The problem is that $_POST['cat'] is not an array. Could be simply one cached file, and one thats current.

    Now, heres an option as well (I never tested this or the original code, but that shouldn't be a problem), change your isset($_POST['cat']) to !empty($_POST['cat']). To the best of my recollection, checkboxes will send NULL if they are not checked. Which means that the value is set, but there is no value to it (in which case, implode would probably fail). The implode code itself is correct, so thats not a problem. Oh, one more thing, you'll want more than just 'id' to select, as your $cat_fields is the id of several options.
    Give this a shot, see if it fixes it up. Otherwise, post your html and php for it.

  • #5
    New Coder
    Join Date
    Sep 2005
    Location
    California
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I don't know what you mean when you said that it's not an array. I only have some knowledge of arrays, but not enough to assign them by hand.

    I tried the !empty thing, but it ended up not returning any category at all (which defaulted to 0, none).

    What else should I add in for the id variable, and how?

    Here's what I have so far:

    The part in the form action:
    PHP Code:
    if(!empty($_POST['cat'])) { 
        
    // Something wrong. The selected category couldnt be found in the current blog
       // For my addition, I'm uncertain of what you refer to with this... 'cat' should be set if I understand you correctly.
        
    $cat_fields implode (','$_POST['cat']);
        if(!
    $db->query("SELECT id FROM ".MY_PRF."cats WHERE id IN (" mysql_real_escape_string($cat_fields) . ")  AND blog='" mysql_real_escape_string($blog) . "'"false)) { 
        
    bmc_template('error_page'$lang['post_no_cat']); exit; 
        } 

    } else {
        
    // No category!
        
    bmc_template('error_page'$lang['post_no_cat']); exit;

    The part in the form page:

    PHP Code:
    <?php
    // Print the category list

            
    $cats=$db->query("SELECT * FROM ".MY_PRF."cats WHERE blog='".BLOG."'");
            foreach(
    $cats as $cat) {
            echo 
    "<input type=\"checkbox\" name=\"cat\" value=\"{$cat['id']}\">{$cat['cat_name']}\n";
            }


    ?>

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Ok, your checkbox still isn't an array. Thats the problem with the implode, which later leads to the problem with the query.
    This here:
    echo "<input type=\"checkbox\" name=\"cat\" value=\"{$cat['id']}\">{$cat['cat_name']}\n";
    Needs to be:
    echo "<input type=\"checkbox\" name=\"cat[]\" value=\"{$cat['id']}\">{$cat['cat_name']}\n";

    And with the id, you can return whatever you want to return. Otherwise, you don't need to use the query at all, assuming BLOG has the same value assigned as $blog.
    Here's why:
    $cats=$db->query("SELECT * FROM ".MY_PRF."cats WHERE blog='".BLOG."'");
    This appears to only select cats containing blog equal to BLOG. If $blog has been assigned to equal BLOG, you don't need this:

    $db->query("SELECT id FROM ".MY_PRF."cats WHERE id IN (" . mysql_real_escape_string($cat_fields) . ") AND blog='" . mysql_real_escape_string($blog) . "'";
    As all ids found within $cat_fields are already from BLOG and this query is only searching for id.

    Perhaps I was jumping to a conclusion as well, I assumed that this:
    PHP Code:
     if(!empty($_POST['cat'])) {  
        
    // Something wrong. The selected category couldnt be found in the current blog 
       // For my addition, I'm uncertain of what you refer to with this... 'cat' should be set if I understand you correctly. 
        
    $cat_fields implode (','$_POST['cat']); 
        if(!
    $db->query("SELECT id FROM ".MY_PRF."cats WHERE id IN (" mysql_real_escape_string($cat_fields) . ")  AND blog='" mysql_real_escape_string($blog) . "'"false)) {  
        
    bmc_template('error_page'$lang['post_no_cat']); exit;  
        }  

    } else { 
        
    // No category! 
        
    bmc_template('error_page'$lang['post_no_cat']); exit; 

    is incomplete code. Specifically after the possibility of the non-existant query, correct? Otherwise, you will have no output as all, so long as your $_POST['cat'] exists.
    You can also check for the type of $_POST['cat'] as well, using an is_array() function, or you can even typecast it if nessessary. However, once you add [] to the end of cat in your html, your $_POST['cat'] should always exist on a submit, and should always be an array (even if its null). This shouldn't however effect your query, as it will attempt to find null -> it just won't return any results.
    Is this making sense at all?

    Edit:
    Looks like I was backwards, checkboxes are empty when unchecked, text fields are null when empty. You can use an isset() function instead of an !empty function.
    Last edited by Fou-Lu; 09-09-2005 at 08:20 AM.

  • #7
    New Coder
    Join Date
    Sep 2005
    Location
    California
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    My bad on the array for the checkboxes; I had changed it, but the file I took that code from wasn't updated on that computer.

    And no...it doesn't make much sense to me. I only started learning PHP a month or two ago, so I don't understand a lot of that. I tried taking out the line you said I didn't need:
    PHP Code:
        if(!$db->query("SELECT id FROM ".MY_PRF."cats WHERE id IN (" mysql_real_escape_string($cat_fields) . ")  AND blog='" mysql_real_escape_string($blog) . "'"false)) { 
    but it seemed to be the line that processes...well, everything, so it didn't work out too well. If you could put all that into simpler terms, I could try it all out...thanks again for all your help.

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    PHP Code:
     // ======= The target category 

    if(isset($_POST['cat'])) {  
        
    // Something wrong. The selected category couldnt be found in the current blog 
       // For my addition, I'm uncertain of what you refer to with this... 'cat' should be set if I understand you correctly. 
       
    $cat_fields implode (','$_POST['cat']); 
        if(!
    $db->query("SELECT id FROM ".MY_PRF."cats WHERE id IN (" mysql_real_escape_string($cat_fields) . ")  AND blog='" mysql_real_escape_string($blog) . "'"false)) {  
        
    bmc_template('error_page'$lang['post_no_cat']); exit;  
        }  

    } else {  
        
    // No category! 
        
    bmc_template('error_page'$lang['post_no_cat']); exit;  

    Ok, what I'm saying is that assuming that variable $blog is identical to constant BLOG, and should a category be checked, this code:
    bmc_template('error_page', $lang['post_no_cat']); exit;
    (within the isset($_POST['cat'])) will never execute, regardless of if the query is there or not. This is because your form generation page uses this as its checkbox value:
    "{$cat['id']}". Do you see what I mean?
    This code:
    PHP Code:
        if(!$db->query("SELECT id FROM ".MY_PRF."cats WHERE id IN (" mysql_real_escape_string($cat_fields) . ")  AND blog='" mysql_real_escape_string($blog) . "'"false)) {  
        
    bmc_template('error_page'$lang['post_no_cat']); exit;  
        } 
    Will always execute as false, simply because $_POST['cat'] exists, and if BLOG = $blog, and id is contained within $_POST['cat'], that we do not need to query for the just the 'id'.
    Here's a breakdown.
    Lets say you have three checkboxes that are created from '" . MY_PRF . "cats WHERE blog = '" . BLOG . "''
    These values are: 1, 5, 7 (assuming numerical, but not nessessarily).
    So long as these checkboxes are listed as name = "cat[]", the returned $_POST['cat'] will be:
    Array (
    [0] => 1,
    [1] => 5,
    [2] => 7
    )
    Now, your query seeks the id of MY_PRF.cats WHERE id IN (1,5,7) AND blog = $blog. Still stressing that BLOG = $blog, your returned array of this query using mysql_fetch_* will result in (with possible associative keys):
    Array (
    [0] => 1,
    [1] => 5,
    [2] => 7
    );
    so your query is not nessessary. Should you select more than just the id, then I can see a use for it.
    Now, should the code at the top be 100% complete (which I doubt), you will always result in a blank page if a category is checked. This is because I do not see any processing instructions or output for if the value is true. And unless the class you are using for $db sets up automatic variables, you also cannot make any use of the query beyond that point.
    Oh, I should have been more clear too, I meant you can remove this entire instruction:
    PHP Code:
        if(!$db->query("SELECT id FROM ".MY_PRF."cats WHERE id IN (" mysql_real_escape_string($cat_fields) . ")  AND blog='" mysql_real_escape_string($blog) . "'"false)) {   
        
    bmc_template('error_page'$lang['post_no_cat']); exit;   
        } 
    Not just the one line.
    Is this a little better? I'm not very good at explaining things... lol

  • #9
    New Coder
    Join Date
    Sep 2005
    Location
    California
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Okay, I think I'm getting it a little more, but still not totally. It won't hurt to leave the unneccessary code there, will it?

    Also, I don't know if we've gone over this, but how will listing the posts as being in more than one category (in the database) work? I checked out the database structure of a different blog system, and for that there's a seperate table for which categories each post is in. In one column it lists the post ID, in another the category ID, and if the post is in more than one category, there'll be another row for it. Like this:

    post_id----|---category_id
    ---1-------|-------1
    ---2-------|-------3
    ---2-------|-------4
    ---3-------|-------2
    ---4-------|-------4
    ---4-------|-------6

    So my question is, does the database have to work like this, or is it possible to just keep listing the categories that posts are in in the posts table, seperating them with commas? I don't know what's possible.


  •  

    Posting Permissions

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