2

I'm returning an array from my DB with something like 0,0,1,0 which would be chosen products in this case. With that I'm checking if I should mark an element as selected.

$p gets the array with the product group. Then for a series of elements I use something like this.

<input <?= $p[0]=='1'?'checked':'';?> value="Product 1" />
<input <?= $p[1]=='1'?'checked':'';?> value="Product 2" />
<input <?= $p[2]=='1'?'checked':'';?> value="Product 3" />
<input <?= $p[3]=='1'?'checked':'';?> value="Product 4" />

But sometimes no choice has been made at all, which would return an empty array, which in turn triggers php errors like Uninitialized string offset: 3

What would be a good way to handle empty arrays while keeping the markup rather tidy? Separate functions or so?


UPDATE

Updated solution, which with 4 radios results in the first three of them checked.

<?php $p=check(1); ?>

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

> And the sql query result might not even be an empty array. I guess if the query doesn't result in anything, not even an empty array is set as a result?

Xavio
  • 437
  • 1
  • 6
  • 21
  • you really should stop using the = syntax. http://stackoverflow.com/questions/200640/are-php-short-tags-acceptable-to-use `` – Rufinus Jul 24 '13 at 14:04
  • 4
    @Rufinus bad advice [Since PHP 5.4.0, = is always available.](http://php.net/manual/en/ini.core.php). Note also that php 5.3 is already end of life, so 5.4 is the current version of php - it's appropriate to use unless legacy support is a concern. – AD7six Jul 24 '13 at 14:06
  • 2
    @Rufinus I would not say he should stop using them. He should learn about the possible disadvantages and decide. For example, I use them most of the time. – kapa Jul 24 '13 at 14:06
  • 1
    ok ok ... = is fine... and according to docs, = is not considered a short tag :D – Rufinus Jul 24 '13 at 14:10
  • @Rufinus, PHP has flip-flopped on their stance on *short tags* through versions. However, there is a difference between short output tags and short open tags. Generally [I avoid both](http://jason.pureconcepts.net/2012/08/better-php-developer/).. But, `=` is the lesser of two evils :) – Jason McCreary Jul 24 '13 at 14:14
  • @JasonMcCreary agreed – Rufinus Jul 24 '13 at 14:15
  • I generally use ` – Xavio Jul 24 '13 at 14:20

1 Answers1

8

Use empty():

<input <?= !empty($p[0]) ? 'checked' : ''; ?> value="Product 1" />

From the docs:

No warning is generated if the variable does not exist. That means empty() is essentially the concise equivalent to !isset($var) || $var == false.

Since 0 is a false value, this works in your case.

Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
  • 6
    Would be slightly clearer logic to invert the inline if in that case `= empty($foo) ? '' : 'checked'` - i.e. avoid double negative logic (if it's not-not-empty echo "checked"). +1 – AD7six Jul 24 '13 at 14:04
  • @AD7six, agreed. I would personally do that, but wanted to keep the OP's original logic. – Jason McCreary Jul 24 '13 at 14:05
  • That solution just gives me 3 checked radios. `$p` gets the raw db result, if that makes any difference. – Xavio Jul 24 '13 at 14:23
  • What I provided is just an example to get your started. If you are still having trouble, update your post with the updated code. – Jason McCreary Jul 24 '13 at 14:25
  • You will need to invert your logic if you are using `empty()` vs. `!empty()`. See **AD7six**'s comment. – Jason McCreary Jul 24 '13 at 14:46
  • Tried `= empty($p[0])?'':'checked';?>` but that just gave me the same first three checked radios. As I mentioned, maybe the sql result isn't even an empty array if the sql query returns zero results. – Xavio Jul 24 '13 at 14:58
  • 1
    You should post another question regarding your SQL results. Your original question has been answered. – Jason McCreary Jul 24 '13 at 14:59