1

I've got, in this case, 4 radio buttons in a form. In a previous step a user gets to choose between options (like A, B, C, D) that include different radios being selected. Eg: Choose A and radio 1 is selected, and so on. This choice is saved in mysql as

// If B:

Customer Prod Group Chosen
12345    1    1     0
12345    2    1     1
12345    3    1     0
12345    4    1     0

For later use in some other weird IT system, I need to save the non selected as 0. Later the user can visit this page again. I then check the database to mark the correct radio as checked.


Problem

The user must not make the initial choice.

Since the user must not make a choice (A, B...) I'm having trouble with the sql query when it's returning an empty result (no choices found). I save the sql result to $p in this case and then check if it's 1 or 0/empty.

<input type="button" <?= empty($p[0])?'':'checked';?> />
<input type="button" <?= empty($p[1])?'':'checked';?> />
<input type="button" <?= empty($p[2])?'':'checked';?> />
<input type="button" <?= empty($p[3])?'':'checked';?> />

This results in the first 3 radios being checked.

Any idea how to tackle this? I somehow need to check if the sql entry is 1 (chosen), 0 (not chosen)or not present at all.

The SQL is just a simple query:

$sql = "
SELECT chosen 
FROM choices 
WHERE group = 1 
AND customer = 12345";

Edit:

var_dump($result) says bool(false). Is this normal?

Xavio
  • 437
  • 1
  • 6
  • 21

3 Answers3

0

How about

<input type="button" <?=(empty($p[0]) || $empty === '0')?'':'checked';?> />

(check if it's empty or if it's value is 0 - not checked.)

or if it's only checked if it's a 1 :

EDIT

if($p[0]) {
 ?>
   <input type="button" <?=($p[0] === '1') ? 'checked':'';?> />
 <?
 }else {
   <input type="button" />
 }
?>

(check if it's value is 1 - checked)

JohnnyFaldo
  • 4,121
  • 4
  • 19
  • 29
  • I used the second one of those before, but that triggered offset errors since there sometimes wasn't any $p[x] due to empty sql result. – Xavio Jul 25 '13 at 08:23
  • check the array exists before outputting the input button then see edit – JohnnyFaldo Jul 25 '13 at 08:25
  • According to the [documentation](http://php.net/manual/en/function.empty.php) `0` is considered to be `empty` – harryg Jul 25 '13 at 08:52
  • So is there a way to know the difference between a "0" in the db and an empty query result? Like if I check whether a product is 1, 0 or not in the table at all? – Xavio Jul 25 '13 at 10:44
  • @harryg that's a good reason to avoid using empty() in this case then. This is easily done by checking strlen(). – JohnnyFaldo Jul 25 '13 at 10:46
0

At first, simple query in sql:

$group = 1;
$customer = 12345;

// @de_nuit we have the same idea :-D
$sql = "SELECT `chosen` FROM `choices` WHERE `group` = '".$group."' AND `customer` = '".$customer."'";
$result = mysql_query($sql);

$p = array();
while($row = mysqli_fetch_array($result))
{
   $p[$row['prod']] = true;
}

Maybe simply is create a small function?

function emptyCheck($p, $trick){
   // Your syntax to check
   if( isset($p[$trick]) ){
      if( $p[$trick] == true ){ echo "checked"; }
   }
}

Then use:

<input type="button" <?=emptyCheck($p,0)?> />

Now, it's old method, read this for more informations: Why shouldn't I use mysql_* functions in PHP?

Community
  • 1
  • 1
Maciej A. Czyzewski
  • 1,539
  • 1
  • 13
  • 24
  • If no choice has been made at all (empty sql result), wouldn't that trigger an offset error? – Xavio Jul 25 '13 at 08:24
  • Ah, this eventually did the trick. Just had to add true/false to the function variables in order to check if it's the `yes` or `no` button that is checked. – Xavio Jul 26 '13 at 05:59
0
$sql = "SELECT `chosen` FROM `choices` WHERE `group` = '1' AND `customer` = 12345"; 

You have to put a string in quotes.

If you are using Variables:

$sql = "SELECT `chosen` FROM `choices` WHERE `group` = '".$group."' AND `customer` = ".$customer; 

Edit:

You might have a mysql_query(); somewhere. Add a die method. You can the see if your requests threw any errors. if so post them to your answer.

$query = mysql_query($sql) or die(mysql_error());
de_nuit
  • 650
  • 7
  • 13
  • I've tried the query that doesn't return anything in an external db manager (Sequel Pro) and that just says 0 results, no errors. I'd guess that should clear any case of errors. – Xavio Jul 25 '13 at 10:40