[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Short if statement is not working properly

Posted on 2009-02-11
33
Medium Priority
?
280 Views
Last Modified: 2012-05-06
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
0
Comment
Question by:pingeyeg
  • 18
  • 14
33 Comments
 
LVL 19

Expert Comment

by:Michael701
ID: 23613661
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
 
LVL 14

Expert Comment

by:Ionut A. Tudor
ID: 23613687
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
 
LVL 19

Expert Comment

by:Michael701
ID: 23613729
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:pingeyeg
ID: 23613736
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
 
LVL 19

Expert Comment

by:Michael701
ID: 23613917
Could you please post more code?

maybe echo $item['viewable'] just to double check that it's 'off'
0
 
LVL 1

Author Comment

by:pingeyeg
ID: 23614788
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
 
LVL 19

Expert Comment

by:Michael701
ID: 23614939
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
 
LVL 1

Author Comment

by:pingeyeg
ID: 23614957
Man that would have been nice if that would have worked.  Kind of embarrassing none the less.
0
 
LVL 19

Expert Comment

by:Michael701
ID: 23614990
ok, is there a url where i can view the html source code that gets generated?
0
 
LVL 1

Author Comment

by:pingeyeg
ID: 23615012
cornerlist.com/admin

UN/ pingeyeg
PW/ t@rh33l

Select the Filter Items button.
0
 
LVL 19

Expert Comment

by:Michael701
ID: 23615169
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
 
LVL 19

Accepted Solution

by:
Michael701 earned 2000 total points
ID: 23615202
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
 
LVL 1

Author Comment

by:pingeyeg
ID: 23615208
Well, I tried that to no avail.
0
 
LVL 1

Author Comment

by:pingeyeg
ID: 23615238
Man, how could I've missed that.  Thanks!
0
 
LVL 1

Author Comment

by:pingeyeg
ID: 23616168
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
 
LVL 19

Expert Comment

by:Michael701
ID: 23616325
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
 
LVL 1

Author Comment

by:pingeyeg
ID: 23616801
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
 
LVL 19

Expert Comment

by:Michael701
ID: 23616848
do you have the
if (isset($_POST['viewable']))
if not you;ll get the error the first time you view the page
0
 
LVL 1

Author Comment

by:pingeyeg
ID: 23617031
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
 
LVL 19

Expert Comment

by:Michael701
ID: 23617319
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
 
LVL 1

Author Comment

by:pingeyeg
ID: 23617365
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
 
LVL 19

Expert Comment

by:Michael701
ID: 23617544
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
 
LVL 1

Author Comment

by:pingeyeg
ID: 23617641
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
 
LVL 1

Author Comment

by:pingeyeg
ID: 23617647
I'm sure you can tell this is my first time messing with the foreach function.
0
 
LVL 1

Author Comment

by:pingeyeg
ID: 23617652
Also, what does the => mean?
0
 
LVL 19

Expert Comment

by:Michael701
ID: 23617709
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
 
LVL 1

Author Comment

by:pingeyeg
ID: 23622243
Did you see my first comment about the code?  I'm not sure I'm doing this correctly.
0
 
LVL 1

Author Comment

by:pingeyeg
ID: 23622288
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
 
LVL 19

Expert Comment

by:Michael701
ID: 23622320
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
 
LVL 19

Expert Comment

by:Michael701
ID: 23622349
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
 
LVL 1

Author Comment

by:pingeyeg
ID: 23622374
lol, ok...  Can you give me an example of how to use it with this?

$something = $_POST['something'];
0
 
LVL 1

Author Comment

by:pingeyeg
ID: 23670249
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
 
LVL 1

Author Comment

by:pingeyeg
ID: 23670354
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

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month20 days, 2 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question