PHP/MySQL - Disable radio option with count

run sq manually gets this…I tried single and double quoting Option A-C

[code][SQL] SELECT IF(COUNT(class_1) >= 1, ‘disabled’, ‘NULL’) as disabled WHERE class_1 = Option A-C

[Err] 1064 - 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 ‘WHERE class_1 = Option A-C’ at line 1[/code]

var_dump of all vars

[code]db
object(DB)#2 (1) {
[“pdoConn”:“DB”:private]=>
object(PDO)#3 (0) {
}
}
result
array(1) {
[0]=>
bool(true)
}
fields
array(4) {
[“Option A-C”]=>
NULL
[“Option A-D”]=>
NULL
[“Option B-C”]=>
NULL
[“Option B-D”]=>
NULL
}
ids
array(4) {
[0]=>
string(10) “Option A-C”
[1]=>
string(10) “Option A-D”
[2]=>
string(10) “Option B-C”
[3]=>
string(10) “Option B-D”
}
id
string(10) “Option B-D”

disabled
NULL
NULL
NULL
NULL[/code]

Well if your sql query doesn’t work then you have no data to handle. I ran the code I posted earlier on, so I know that works.

I don’t have time to look at this today, but hope you get it sorted. You need to start with figuring out why the query fails / has invalid syntax.

Coding break

You have an error here:

[php] $result = $db->query(“SELECT
IF(COUNT(class_1) >= 10, ‘disabled’, null) as disabled
FROM hot14
WHERE class_1 = ?’,
array($id)”);[/php]

It should be
[php] $result = $db->query(“SELECT
IF(COUNT(class_1) >= 10, ‘disabled’, null) as disabled
FROM hot14
WHERE class_1 = ?”,
array($id));[/php]

yea i caught that… but now im getting no errors and the options are not disabling even though there are more than 1 with each option.

i have these amounts
Option A-C (16)
Option A-D (1)
Option B-C (6)
Option B-D (10)

sql manual run:
attached

full code:
[php]<?php
ini_set(‘error_reporting’, E_ALL);
ini_set(‘display_errors’, ‘1’);

try {
require_once (’./Connections/DB.php’);
$db = new DB();

  $ids = array('Option A-C', 'Option A-D', 'Option B-C', 'Option B-D');
  $fields = array();
 
 foreach ($ids as $id) {
   $result = $db->query("SELECT
                  IF(COUNT(class_1) >= 1, 'disabled', null) as disabled
                  FROM hot14
                  WHERE class_1 = ?",
                  array($id));
  $fields[$id] = $result[0]['disabled'];
}
} catch (Exception $e) {
echo '<h3>Error:</h3>';
echo $e->getCode() . ': ' . $e->getMessage();
echo '<h3>Stack trace:</h3>';
foreach ($e->getTrace() as $trace) {
   echo $trace['file'] . ' Line #' . $trace['line'] . '<br />';
}

}

##Debugging
echo “
”;
echo “

”;
echo “db
”;
var_dump ($db);
echo “result
”;
var_dump ($result);
echo “fields
”;
var_dump ($fields);
echo “ids
”;
var_dump ($ids);
echo “id
”;
var_dump ($id);
echo ‘
’;
echo "disabled
";
foreach ($fields as $id => $disabled) {
var_dump ($disabled);
}
echo “
”;
?>

Second Annual Brothers of the Boot H.O.T. Conference


Please Fill out the form below to register for this event.

First and Last Name:
Department:
Email:
Current F.O.O.L.S. Member?:
Yes No
Member ID:

(Olny needed if yes to above)
Chapter:
Shirt Size:
Please Select Small Medium Large X-Large XX-Large XXX-Large

=========================================================
Note - Please make sure that you select a second option as you will be moved in the event the class is full
=========================================================

Training Classses - AM Block

Option A
Option B
Size Up
(Space is limited)
Mental Toughness
(Space is limited)
Presented by:
Presented by:
BOTB
Natural F.O.O.L.s
At Shreveport F.D. Training Grounds
At Shreveport F.D. Training Grounds
Classroom Training

H.O.T.
(Full PPE and SCBA Required)

Training Classses - PM Block

Option C
Option D
Leadership & Change
(Space is limited)
Vent, Enter, & Search
(Space is limited)
Presented by:
Presented by:
Chiefs Johnson & Walker
BOTB & Natural F.O.O.L.s
At Shreveport F.D. Training Grounds
At Shreveport F.D. Training Grounds
Classroom Training

H.O.T.
(Full PPE and SCBA Required)


First Class Choice:

None     <?php foreach ($fields as $id => $disabled) { ?> /> <?= $id ?>     <?php } ?>
Choose none if you will not be attending class

Second Class Choice:

None     <?php foreach ($fields as $id => $disabled) { ?> /> <?= $id ?>     <?php } ?>
Choose none if you will not be attending class

In addition to Training

I Am Attending:

Opening Cerimonies Closing Cerimonies Bash
(select all that apply)

I Agree to bring all needed equipment with me.

[/php]

Debugging:

[code]db
object(DB)#2 (1) {
[“pdoConn”:“DB”:private]=>
object(PDO)#3 (0) {
}
}
result
array(1) {
[0]=>
bool(true)
}
fields
array(4) {
[“Option A-C”]=>
NULL
[“Option A-D”]=>
NULL
[“Option B-C”]=>
NULL
[“Option B-D”]=>
NULL
}
ids
array(4) {
[0]=>
string(10) “Option A-C”
[1]=>
string(10) “Option A-D”
[2]=>
string(10) “Option B-C”
[3]=>
string(10) “Option B-D”
}
id
string(10) “Option B-D”

disabled
NULL
NULL
NULL
NULL[/code]

i even thought that it may be the short tags … so i tried changing
this
[php]<?php foreach ($fields as $id => $disabled) { ?>
<input name=“class_2” type=“radio” value="<?= $id ?>" <?= $disabled ?>/>
<?= $id ?>
   
<?php } ?>[/php]

to
[php]
None    
<?php foreach ($fields as $id => $disabled) { ?>
<input name=“class_1” type=“radio” value="<?php echo $id ?>" <?php echo $disabled ?>/>
<?php echo $id ?>
   
<?php } ?>[/php]


There’s a bug in the db script. I will answer this in a few mins :slight_smile:

As mentioned there’s a bug in the db script. This is how I got there

looked at the $result variable directly after the query was run.

It showed this with >= 1

array(1) { [0] => bool(true) } array(1) { [0] => bool(true) } array(1) { [0] => bool(true) } array(1) { [0] => bool(true) }

Note 1, it should have returned ‘disabled’ with value true/false.
Note 2, it returned this with >= 10

array(1) { [0] => bool(true) } array(1) { [0] => bool(true) } array(1) { [0] => bool(true) } array(1) { [0] => bool(true) }

So clearly something was wrong.

I knew that the DB script returns array(true) if you don’t run a select statement, so I looked here
[php]} else {
$ret = [TRUE];
}[/php]

And sure enough, that code did run.

But you ARE running a select statement, so why does it go there…?

The error is in the (sloppy) check if it is a select statement. So change out the DB->query function with this:

[php] public function query($query, $params = [], $fetch_method = ‘OBJ’, $class = ‘’) {
$stmt = $this->pdoConn->prepare($query);

  $result = $stmt->execute($params);
  
  if ($result) {
    if (strpos(strtolower(trim($query)), 'select') === 0) {
       if (strtoupper($fetch_method) === 'CLASS') {
          $ret = $stmt->fetchAll(constant('PDO::FETCH_CLASS'), $class);
       } else {
          $ret = $stmt->fetchAll(constant('PDO::FETCH_' . strtoupper($fetch_method)));
       }
    } else {
       $ret = [TRUE];
    }
  }
  return !empty($ret) ? $ret : null;

}[/php]

I will review the DB code later on and update my tutorial.

Now the page returns this (remember I still var dump out the $result array directly after the query)

[code]array(1) {
[0] =>
class stdClass#4 (1) {
public $disabled =>
string(8) “disabled”
}
}

Fatal error: Cannot use object of type stdClass as array in /srv/www/test/public/jay7981/index.php on line 19

Call Stack:
0.0000 124696 1. {main}() /srv/www/test/public/jay7981/index.php:0[/code]

Pretty straight forward, the DB class is set to return objects as default. So we should either

A: change notation from array to object
[php]$result[0][‘disabled’][/php]–>[php]$result[0]->disabled[/php]

B: change fetch method to array
[php]$result = $db->query(“SELECT
IF(COUNT(class_1) >= 10, ‘disabled’, null) as disabled
FROM hot14
WHERE class_1 = ?”,
array($id));[/php]–>[php]$result = $db->query(“SELECT
IF(COUNT(class_1) >= 10, ‘disabled’, null) as disabled
FROM hot14
WHERE class_1 = ?”,
array($id), ‘ASSOC’);[/php]

I like to work with objects, so I did method A. Now the $result dump shows this:

array(1) { [0] => class stdClass#4 (1) { public $disabled => string(8) "disabled" } } array(1) { [0] => class stdClass#5 (1) { public $disabled => NULL } } array(1) { [0] => class stdClass#3 (1) { public $disabled => NULL } } array(1) { [0] => class stdClass#4 (1) { public $disabled => string(8) "disabled" } }

And the disabling works as expected.

Sorry for messing this up for you, I haven’t encountered that bug with the DB script before :slight_smile:

;D ;D ;D ;D

You are a genius! thank you so much that was driving me absolutely nuts!!

Sponsor our Newsletter | Privacy Policy | Terms of Service