Short if statement is not working properly

I have two if statements in my input fields for two radio buttons.  The deal is to have the on radio button checked if the viewable field reads on and the off radio button is checked if the viewable field reads off.  I hope that makes since.  Anyway, this is my code.


<input " . ($item['viewable'] == 'on') ? ('checked=checked') : ('') . " type='radio' name='viewable' value='on'> On <br/><input " . ($item['viewable'] == 'off') ? ('checked=checked') : ('') . " type='radio' name='viewable' value='off'> Off

Right now all I get is the following output:

checked=checkedchecked=checkedchecked=checked
LVL 1
pingeyegAsked:
Who is Participating?
 
Michael701Connect With a Mentor Commented:
OH DUH, smacks self in head.

ALL the items have the SAME radio name

you need the append the name with the item id

name='viewable".$item['id']."'
0
 
Michael701Commented:
this is not LISP (ok, that's a joke for a few coders)

You went () crazy, try this version
echo "<input " . ($item['viewable']=='on' ? 'checked=checked' : '') . " type='radio' name='viewable' value='on'> On <br /><input " . ($item['viewable']=='off' ? 'checked=checked' : '') . " type='radio' name='viewable' value='off'> Off<br />\n";

Open in new window

0
 
Ionut A. TudorPHP ProgrammerCommented:
Hi,
you can't work with if/else structures inside echo or print functions, but the other way around. Below is a way to do it.

<?php
$item['viewable'] = "off";
 
$checked_on = "";
$checked_off = "";
 
$item['viewable'] == 'on' ? $checked_on = 'checked=checked' : $checked_off = 'checked=checked';
 
echo "<input " . $checked_on  . " type='radio' name='viewable' value='on'> On <br/><input " . $checked_off  . " type='radio' name='viewable' value='off'> Off";
?>

Open in new window

0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
Michael701Commented:
al3cs12, actually you can use that shortcut in echo statements. it's the one place i agree with the shortcut. my posting works just fine, give it a try
0
 
pingeyegAuthor Commented:
Well, the list came back to me, but the only set that is checked are the last ones.  I have 3 listings in the db and they all are marked as off, but the only set that is checked as off are the last ones.

<input " . ($item['viewable'] == 'on' ? 'checked=checked' : '') . " type='radio' name='viewable' value='on'> On <br/><input " . ($item['viewable'] == 'off' ? 'checked=checked' : '') . " type='radio' name='viewable' value='off'> Off
0
 
Michael701Commented:
Could you please post more code?

maybe echo $item['viewable'] just to double check that it's 'off'
0
 
pingeyegAuthor Commented:
I echoed all the results and they all say off, but only the last listing is checked off.


$viewable = 'off';
                                                            
                                                            echo "<table width='757' border='0' cellspacing='2' cellpadding='5' align='center' bgcolor='white'>";
                                                                  
                                                                  $viewable = (string) 'off';
                                                                  
                                                                  $getItems = sprintf("SELECT category_items.id, category_items.gID, category_items.cID, category_items.organizationID, category_items.item_name, category_items.catID, category_items.image, category_items.price, category_items.listing, category_items.image, category_items.viewable, subcat.name, subcat.catID AS subcatID
                                                                        FROM category_items
                                                                        LEFT JOIN subcat ON category_items.catID = subcat.catID
                                                                        LEFT JOIN organization ON category_items.organizationID = organization.id
                                                                        WHERE viewable='%s'", $viewable);
                                                                  $itemsGot = mysql_query($getItems) or die("Items did not grab because: " . mysql_error());
                                                                        if(mysql_num_rows($itemsGot) > 0);
                                                                              $i=0;
                                                                              while($item = mysql_fetch_array($itemsGot)) {
                                                                                    if($i==0);
                                                                                          echo "<tr>";
                                                                                          echo "<td>
                                                                                                            <div align=center'>
                                                                                                                  <h2><font size='2' face='Trebuchet MS, Geneva, Arial, Helvetica, SunSans-Regular, sans-serif'>{$item['name']}</font></h2>
                                                                                                            </div>
                                                                                                      </td>
                                                                                                      <td width='125'>
                                                                                                            <div align='center'>
                                                                                                                  <a href='/listing-image/{$item['image']}' target='_blank'><img src='/listing-image/{$item['image']}' alt='' width='125' height='188' border='0' /></a><br />
<font size='1' face='Trebuchet MS, Geneva, Arial, Helvetica, SunSans-Regular, sans-serif'>(Click to enlarge)</font>
                                                                                                            </div>
                                                                                                      </td>
                                                                                                      <td width='388'>
                                                                                                            <div align='left'>
                                                                                                                  <h2><font size='2' face='Trebuchet MS, Geneva, Arial, Helvetica, SunSans-Regular, sans-serif'>{$item['item_name']}<br />{$item['listing']}<br /><a href='mailto:{$item['email']}'>Email</a> for more info.</font></h2>
                                                                                                            </div>
                                                                                                      </td>
                                                                                                      <td width='51'>
                                                                                                            <div align='center'>
                                                                                                                  <h2><font size='2' face='Trebuchet MS, Geneva, Arial, Helvetica, SunSans-Regular, sans-serif'>#{$item['id']}</font></h2>
                                                                                                            </div>
                                                                                                      </td>
                                                                                                      <td width='58'>
                                                                                                            <div align='center'>
                                                                                                                  $<font size='2' face='Trebuchet MS, Geneva, Arial, Helvetica, SunSans-Regular, sans-serif'>{$item['price']}</font>
                                                                                                            </div>
                                                                                                      </td>
                                                                                                      <td width='58'>
                                                                                                            <div align='center'>
                                                                                                                  <input " . ($item['viewable'] == 'on' ? 'checked=checked' : '') . " type='radio' name='viewable' value='on'> On <br/><input " . ($item['viewable'] == 'off' ? 'checked=checked' : '') . " type='radio' name='viewable' value='off'> Off
                                                                                                                  {$item['viewable']}
                                                                                                            </div>
                                                                                                      </td>";
                                                                                                $i++;
                              
                                                                                    if($i==1) {
                                                                                          echo "</tr>";
                                                                                          $i=0;
                                                                                    }
                                                                              }
0
 
Michael701Commented:
how about this
while($item = mysql_fetch_array($itemsGot))
{
   if($i==0) // take out the ;
       echo "<tr>";
   echo "<td>
     <div align='center'> // add a ' before the 'center'
0
 
pingeyegAuthor Commented:
Man that would have been nice if that would have worked.  Kind of embarrassing none the less.
0
 
Michael701Commented:
ok, is there a url where i can view the html source code that gets generated?
0
 
pingeyegAuthor Commented:
cornerlist.com/admin

UN/ pingeyeg
PW/ t@rh33l

Select the Filter Items button.
0
 
Michael701Commented:
the html kinda looks ok, the checked='checked' is on the 'off' section
Let's try something odd.

reorder the html tags
from
<input checked="checked" name="viewable" value="off" type="radio">
to
<input type="radio" name="viewable" value="off" checked="checked">

In theory this shouldn't matter, but that never applies to browsers and standards.
0
 
pingeyegAuthor Commented:
Well, I tried that to no avail.
0
 
pingeyegAuthor Commented:
Man, how could I've missed that.  Thanks!
0
 
pingeyegAuthor Commented:
I'm a little confused here for some reason, but I can't seem to figure out how to update a listing using the name='viewable" . $item['id'] . "'.

Would I do:

$viewable = $_POST['viewable[id]'];
0
 
Michael701Commented:
I setup  the variable as an array
name='viewable[".$item['id']."]' value=

here's my test code
if (isset($_POST['viewable']))
  foreach ($_POST['viewable'] as $id=>$value)
    echo "Item number: ".$id." is set as ".$value. "<br />\n";
 
echo "<form method='post'>\n";
$item['viewable']='on';
for ($id=1; $id<=3; $id++)
echo "<input " . ($item['viewable']=='on' ? 'checked=checked' : '') . " type='radio' name='viewable[$id]' value='on'> On <br /><input " . ($item['viewable']=='off' ? 'checked=checked' : '') . " type='radio' name='viewable[$id]' value='off'> Off<br />\n";
echo "<input type='submit' name='submit' value='submit' />\n";
echo "</form>\n";

Open in new window

0
 
pingeyegAuthor Commented:
I'm getting an error when trying the foreach statement:

Invalid argument supplied for foreach()

foreach($_POST['viewable'] AS $id=>$value) {
                  echo "Item number: ".$id." is set as ".$value. "<br />\n";
            }
            exit();
0
 
Michael701Commented:
do you have the
if (isset($_POST['viewable']))
if not you;ll get the error the first time you view the page
0
 
pingeyegAuthor Commented:
Ok?... now I just get a blank page.  The following is my if statement:

if($_POST['updateView']) {
            if(isset($_POST['viewable'])) {
            foreach($_POST['viewable'] AS $id=>$value) {
              echo "Item number: ".$id." is set as ".$value. "<br />\n";
        }}
        exit();
            $viewable = (string) $_POST['viewable'];
            $iID = (int) $_POST['id'];
            
            $updateView = sprintf("UPDATE category_items
                  SET viewable='%s'
                  WHERE id=%d", $viewable, $iID);
                  echo $updateView;
            $viewUpdated = mysql_query($updateView) or die("View change did not work because: " . mysql_error());
                  if(mysql_affected_rows() > 0) {
                        $changed = "The view has now been changed.";
                  } else {
                        $changed = "There appears to have been an issue completing your request.  Please try again or send us an email to <a href=mailto:email>email</a> so that we can rectify the issue.";
                  }
      }
0
 
Michael701Commented:
is this set
if($_POST['updateView'])
if not it won't execute the foreach

i'm sure it's stopoing here
  exit();

--

the foreach and echo was to give you an idea of the variable names to use in your mysql update loop.
            $updateView = sprintf("UPDATE category_items
                  SET viewable='%s'
                  WHERE id=%d", $value, $id);

Open in new window

0
 
pingeyegAuthor Commented:
Yes, the updateView is set as a hidden field in the form.  I took out the exit(), but the foreach statement doesn't seem to be recognized.  I just get my normal page.
0
 
Michael701Commented:
it's cause you have to change the origianal form to make viewitem an array
you have
<input name="viewable26"

it needs to be

<input name="viewable[26]"
0
 
pingeyegAuthor Commented:
I gotcha, that makes sense.  I have now changed it to name='viewable[" . $item['id'] . "]'

So would something like this work?

if($_POST['updateView']) {
            foreach($_POST['viewable'] AS $id=>$value);
            
            $updateView = sprintf("UPDATE category_items
                  SET viewable='%s'
                  WHERE id=%d", $value, $id);
                  echo $updateView;
            $viewUpdated = mysql_query($updateView) or die("View change did not work because: " . mysql_error());
                  if(mysql_affected_rows() > 0) {
                        $changed = "The view has now been changed.";
                  } else {
                        $changed = "There appears to have been an issue completing your request.  Please try again or send us an email to <a href=mailto:support@goodboyweb.com>support@goodboyweb.com</a> so that we can rectify the issue.";
                  }
      }
0
 
pingeyegAuthor Commented:
I'm sure you can tell this is my first time messing with the foreach function.
0
 
pingeyegAuthor Commented:
Also, what does the => mean?
0
 
Michael701Commented:
It's just to take the array['key']='value' and assign them to variables

I keep thinking it's -> but then have to remember it's =>

so, it's just PHP syntax
0
 
pingeyegAuthor Commented:
Did you see my first comment about the code?  I'm not sure I'm doing this correctly.
0
 
pingeyegAuthor Commented:
Well, nevermind.  I figured it out.  Is it a smart idea to use the foreach statement on update sql statements?  Just curious for when best to use it.  Also, is there any documentation on the => symbol?
0
 
Michael701Commented:
guess not, here's my comment

foreach($_POST['viewable'] AS $id=>$value);

the ; at the end is a big problem, remove it

then add { } around the statements you want in the foreach loop.
0
 
Michael701Commented:
Also, is there any documentation on the => symbol?

not that i've gone looking for. sometimes when the programmers who write compilers need a symbol they just make it up.
0
 
pingeyegAuthor Commented:
lol, ok...  Can you give me an example of how to use it with this?

$something = $_POST['something'];
0
 
pingeyegAuthor Commented:
Michael701: Man I thought this was working, but it's not.  Even though the numbers are being dynamically generated for each name, only ever third item has it's off radio button selected.  The others don't have anything selected.
0
 
pingeyegAuthor Commented:
What's even weirder is the fact that it is showing two extra items per item even though they're not in the database.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.