mercredi 20 mai 2015

A non-executing PHP if/else statement, with two choices for input submit button state

The closest I could find to the physical description of my current dilemma is in the post HTML/PHP if-else Statement where the main issue similarity resides in the button state changes. As I'm not attempting anything in twitter-bootstrap, my issue is divergent from that point of reference. I'm dealing with overcoming (originally) terrible coding for a content publishing and retraction system.

This code;

echo '<input type="submit" name="action" value="Edit" />'; 
if ($row['is_published'] || $row['date_published']) {
    if($action == 'Retract') {
echo '<input type="submit" name="action" value="Retract"/>';
            } else {
            if($action == 'Publish') {
echo '<input type="submit" name="action" value="Publish"/>';
            }
       }   
  }      

if($row['access_level'] > 1 || $_SESSION['access_level'] > 1){
echo '<input type="submit" name="action" value="$action" />';
    }
echo'<input type="submit" name="action" value="Delete"/>';
}}  

returns the result of the submit inputs as being;

 _____________ ____________ ____________
|             |            |            |
|    EDIT     |  $ACTION   |   DELETE   |
|_____________|____________|____________|

I KNOW I'm doing something simple wrong because the state of the $ACTION input is supposed to change in relationship to whether the $action variable is influenced by database state change (embodied by $row['is_published'] || $row['date_published']). I've declared the $action variable previously

$action = '';
    if (isset($_GET['action'])) $action = $_GET['action'];  

and I'm pretty sure my solution lies in the fact that I either mangled the nested if/else inside the original if statement or the echo of the $action input isn't helping to trip the (conditional) input state. I'm not having any problem whatsoever with the ability to publish/retract content - just getting the input to reflect the correct state depending on whether content is published (use 'Retract' input label value) or retracted (use 'Publish' input label value). I even toyed with the idea of using switch casing, but that was a bit of a train wreck - but no more so than the ORIGINAL VERSION I adapted;

if ($row[‘is_published’]) {
$buttonType = “Retract”;
} else {
$buttonType = “Publish”;
}
echo “<input type=’submit’ class=’submit’ “ .
“name=’action’ value=’Edit’ /> “;
if (($row[‘access_lvl’] > 1) or ($_SESSION[‘access_lvl’] > 1)) {
echo “<input type=’submit’ class=’submit’ “ .
“name=’action’ value=’$buttonType’ /> “;
     }  

(what a stinkin' mess, lol!)

I'm disconsolately vexed that neither repeated perusal of Manual control structures nor consultation of original script control flow has provided a resolution of the matter. It could also be that I just don't see the forest for the trees. I'm afraid I've just read myself silly...but I'm pretty sure it's Human Error since I can never get the $ACTION input state to change according to content state.

Aucun commentaire:

Enregistrer un commentaire