vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   Retrieving checkbox values from MYSQL (https://vborg.vbsupport.ru/showthread.php?t=284262)

m002.p 06-10-2012 02:47 PM

Retrieving checkbox values from MYSQL
 
Hello everyone.

I am tearing my hair out with this one and I sort of know the reason why it wont work, being a loop issue but I cant see where I am going wrong.

I have checkboxes as seen in the $a_offenses array. I want the script to check the MYSQL table row 'offenses' which contains an array of those selected checkboxes in the format 'VALUE 1, VALUE 2, VALUE 4, VALUE 6' for example.

The problem comes where the first value eg. VALUE 1 is ticked in the list of checkboxes but the others are not and left unchecked.

Any ideas?

PS - The $c counter is used to ensure only 3 checkboxes are displayed per row ie. formatting only nothing else

Thanks in advance.

Matt

PHP Code:

// Query DB
$query mysql_query("SELECT offenses FROM reports WHERE id='$id'");
// Get ROW
$row mysql_fetch_array($query);

// Store all checkboxes as array
$a_offenses = array("Team Killing""Team Tazing""Team Nading""Team Peppering""Team Injuring""Insulting Players""Insulting Admins""Insulting Clan Members"
"Tag Stealing""Cheating / Hacking""Disallowed Name""Inappropiate Language""Spamming""Advertising / Recruiting""Rambo Gameplay""Random Shooting" "ROE Violations"
"Use of Non-English""Dropping as VIP""Spawn Killing""Camping""Injuring / Killing VIP""Injuring / Killing Civilians");

// Explode stored checkboxe values from DB selected
    
$offenses explode(',',$row['offenses']);

 
$c 0;
  foreach (
$a_offenses as $offense) {
 
$c++;
 
        if(
in_array($offense,$offenses)) 
    {
        if(
$c == 0)
        {
                  echo 
"<tr><td><input name=\"offenses[$c]\" type=\"checkbox\" value=\"$offense\" checked> $offense</td>";
        }
        else
        {
            echo 
"<td><input name=\"offenses[$c]\" type=\"checkbox\" value=\"$offense\" checked> $offense</td>";
        }
        } 
    else
        {
              if(
$c == 0)
        {
                  echo 
"<tr><td><input name=\"offenses[$c]\" type=\"checkbox\" value=\"$offense\"> $offense</td>";
        }
        else
        {
            echo 
"<td><input name=\"offenses[$c]\" type=\"checkbox\" value=\"$offense\"> $offense</td>";
        }
        }
              } 


kh99 06-10-2012 03:22 PM

Quote:

Originally Posted by m002.p (Post 2338468)

The problem comes where the first value eg. VALUE 1 is ticked in the list of checkboxes but the others are not and left unchecked.


And what happens when that's the case?

m002.p 06-10-2012 03:43 PM

Hi, thanks for your response.

I dont follow what you mean.

Heres an example.

Row 'offenses' for ID 4; Team Killing, Spamming, ROE Violations

Out of all the checkbox possibilities as displayed with array $a_offenses, only the first value "Team Killing" is checked and not the other 2 in the example using the code posted.

Hope that answers your query?

Matt

kh99 06-10-2012 03:54 PM

Oh, I get it now. I thought you meant some problem was happening if you checked only one box.

Anyway, I think your problem is that explode(',', array) splits by comma but doesn't take away the extra spaces. Try this:

PHP Code:

$offenses array_map('trim'explode(','$row['offenses'])); 


m002.p 06-10-2012 04:22 PM

Genius, works like a charm. Thank you, something so simple but so simply overlooked.

Matt

kh99 06-10-2012 04:31 PM

Yeah, sometimes it just takes a fresh pair of eyes.

m002.p 07-29-2012 04:46 PM

Hi Kh99,

I know its been a while since I had this issue, but I have since been trying using another method.

Instead of using the old method:

Code:

// Store all checkboxes as array
$a_offenses = array("Team Killing", "Team Tazing", "Team Nading", "Team Peppering", "Team Injuring", "Insulting Players", "Insulting Admins", "Insulting Clan Members",
"Tag Stealing", "Cheating / Hacking", "Disallowed Name", "Inappropiate Language", "Spamming", "Advertising / Recruiting", "Rambo Gameplay", "Random Shooting" , "ROE Violations",
"Use of Non-English", "Dropping as VIP", "Spawn Killing", "Camping", "Injuring / Killing VIP", "Injuring / Killing Civilians");

I am now retrieving this array from a MYSQL table.

Example:

$a_offenses = $db_offenses['list'];

Value stored in MYSQL column 'list' is the array eg. "Team Killing, Team Tazing, Team Nading" etc.

Now the problem is back again. I removed your first solution to simply:

Code:

$offenses = explode(',',$row['offenses']);
and it partially works. When I say partially, multiple boxes are ticked but for some elements of the array the box remains unticked.

Eg.

Offenses listed in DB; Team Killing, Team Tazing, Rambo Gameplay

Checkboxes ticked: Team Killing, Rambo Gameplay

So some are missed from the stored values. I looked at how both arrays are stored in the DB and cant see any extra spaces.

Can you help?

--------------- Added [DATE]1343584787[/DATE] at [TIME]1343584787[/TIME] ---------------

Dont worry, forgot to pass both variables through array_map.

Thanks once again!


All times are GMT. The time now is 12:08 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01201 seconds
  • Memory Usage 1,747KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (2)bbcode_code_printable
  • (2)bbcode_php_printable
  • (1)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (7)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete