Jump to content

Recommended Posts

Can someone please help me with the below code, I'm trying to do a batch update on listed results where the column "priority" can either be 10 or 15, Im not sure if the syntax is correct, the result ID's do pass correctly but the "Priority Values" do not pass to my update.php script, i have a feeling that my syntax is wrong somewhere.  Thanks in advance...

 

 

    <td>

          <?php print "<input type='hidden' name='ID[$i]' value='{$result_row['ID']}' />";?>

      <?php print "<select name='Priority[$i]' >"; ?>         

      <?php print "<option value=''($result_row[Priority] == Null?' selected='selected'':null)></option>"; ?>

  <?php print "<option value='10'($result_row[Priority] == '10'?' selected='selected'':null)>10</option>";?>

      <?php print "<option value='15'($result_row[Priority] == '15'?' selected='selected'':null)>15</option>";?>

          <?php print "</select>" ?>

    </td>

Link to comment
https://forums.phpfreaks.com/topic/184218-batch-processing-list-boxes/
Share on other sites

You had some major syntax issues in your code, namely your quotes.

 

         <?php print "<input type='hidden' name='ID[$i]' value='{$result_row['ID']}' />";?>
          <?php print "<select name='Priority[$i]' >"; ?>         
         <?php print "<option value=''" . (is_null($result_row['Priority']) ?" selected='selected'":null) . "></option>"; ?>
        <?php print "<option value='10'" . ($result_row['Priority'] == '10'?" selected='selected'":null) . ">10</option>";?>
         <?php print "<option value='15'" . ($result_row['Priority'] == '15'?" selected='selected'":null) . ">15</option>";?>
          <?php print "</select>"; ?>

 

Why in the world are you going in and our of PHP like that? This would be much simpler and works just the same:

 

<?php 
print "<input type='hidden' name='ID[$i]' value='{$result_row['ID']}' />
           <select name='Priority[$i]' >
           <option value=''" . (is_null($result_row['Priority']) ?" selected='selected'":null) . "></option>
           <option value='10'" . ($result_row['Priority'] == '10'?" selected='selected'":null) . ">10</option>
           <option value='15'" . ($result_row['Priority'] == '15'?" selected='selected'":null) . ">15</option>
           </select>"; 
?>

 

So I changed your quotes around on the selected, you were using a single quote after the ? and also using them around the ='selected' that was a syntax issue. Also you will notice on the first ternary I use the is_null function as that is how you have to test if a variable is null, you cannot simply do a comparison.

 

Either way that should work for you.

premiso is right.  Your really making it more difficult on yourself than it it needs to be.

 

@premiso All the times i've used ternarys ive formatted them as

 

($result_row['Priority'] == '15') ? " selected='selected'":null

 

I notices you had the parens around the whole thing. Since ive never seen them done like that I was just curious if that was also a valid way?  Just curious.

I notices you had the parens around the whole thing. Since ive never seen them done like that I was just curious if that was also a valid way?  Just curious.

 

:) Give it a shot.

 

<?php
$x = "hello";
$ternTest = ($x == 4 ? $x:"phooey");

echo $ternTest;
?>

I notices you had the parens around the whole thing. Since ive never seen them done like that I was just curious if that was also a valid way?  Just curious.

 

It's not only valid but required when it's used like that, in the middle of a larger string.

The code in the first post 'works', so best guess is either the loop that code is in (if any) is not providing expect values in $result_row or the form processing code is not processing it correctly.

 

It works, as in runs without syntax errors, but the output is a total mess and includes parts of what's supposed to be PHP code.

You had some major syntax issues in your code, namely your quotes.

 

         <?php print "<input type='hidden' name='ID[$i]' value='{$result_row['ID']}' />";?>
          <?php print "<select name='Priority[$i]' >"; ?>         
         <?php print "<option value=''" . (is_null($result_row['Priority']) ?" selected='selected'":null) . "></option>"; ?>
        <?php print "<option value='10'" . ($result_row['Priority'] == '10'?" selected='selected'":null) . ">10</option>";?>
         <?php print "<option value='15'" . ($result_row['Priority'] == '15'?" selected='selected'":null) . ">15</option>";?>
          <?php print "</select>"; ?>

 

Why in the world are you going in and our of PHP like that? This would be much simpler and works just the same:

 

<?php 
print "<input type='hidden' name='ID[$i]' value='{$result_row['ID']}' />
           <select name='Priority[$i]' >
           <option value=''" . (is_null($result_row['Priority']) ?" selected='selected'":null) . "></option>
           <option value='10'" . ($result_row['Priority'] == '10'?" selected='selected'":null) . ">10</option>
           <option value='15'" . ($result_row['Priority'] == '15'?" selected='selected'":null) . ">15</option>
           </select>"; 
?>

 

So I changed your quotes around on the selected, you were using a single quote after the ? and also using them around the ='selected' that was a syntax issue. Also you will notice on the first ternary I use the is_null function as that is how you have to test if a variable is null, you cannot simply do a comparison.

 

Either way that should work for you.

 

Thanks a bunch guys, ill try that out... I'm new to PHP which is why my code looks ugly:)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.