skip to Main Content

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.

UPDATE
Right now, I’m at the point where the database content state and the input label state are in unison sync (with database content in PUBLISHED state);

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

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

     }         
    }
  }
echo'<input type="submit" name="action" value="Delete"/>';
  }  

The above code;

result return

 _____________ ____________ ____________
|             |            |            |
|    EDIT     |  RETRACT   |   DELETE   |
|_____________|____________|____________|

EXACTLY what I want to see. BUT, rather unfortunately, the input label state remains the same when clicked even though the database content state indeed changes – in this case – allowing for the retraction of the content. I’m so close I can taste it.

What’s really stomping my brain is the fact that I have to manually alter the $action variable and the echo statement values to have the input/content states to correctly unison sync – which is the whole point of the if/else statement in the first place (lol!).

I mean, I even went off on a little troll on comparison operators as I was
trying to evoke an inadvertant error somewhere which would lead to a resolution, but whether =, ==, or === was employed, it didn’t seem to influence the proceedings (I thought for sure = would shake something loose). I understand that else is used for an alternate block of coding if the
IF CONDITION is false
. Trying to abstain from “code-flail” at this point, but more reading seems necessary.

Thanks to all for the eyes/views as well as @Darren and @take for their splendid insights. I’ll smash this flat yet…

2ND UPDATE

Having had a “whoops” moment;

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

Just because I’m harried, no need for sloppiness (code refactor granting exact same result).

3RD UPDATE

echo '
<br>
<div class="searh_form">
<form method="POST" action="transact_blog.php">
<div>
 <input type="submit" name="action" value="Edit"/>
 <input type="submit" name="action" value="Delete"/>';

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

In between apoplectic fits of laughter and Derp-level code rages, I’m at the point of mental/physical exhaustion. Thanks to suggestion about form submission by both @Sharky and @Darren, the $action variable was given the $_POST initialization. Sadly, I find that a potential key may be that I have to allow all the inputs to exist and then do some sort of is_array explosion to create a strictly-enforced conditional functionality.

For me, the issue exists about how to flip the input label state to match whether or not the database content was published or unpublished. It really just cannot be that hard. I’m almost at the point where I’m considering some JS or jQuery that I could wrap in, but I just don’t want to go that route unless absolutely necessary (I do have “utility lines” that I use echoed out in PHP – mostly for repetitive tasks, but I’m reluctant to make large user-functionality leveraged on JS). Manually-altering this section;

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

by stripping the !empty() and swapping the $action variable values, I achieve the ability to make input label and database content publishing states to run the (desired) code execution. This aspect is what keeps me laughing rather than crying (lol!)…more reading…

IMPORTANT EDIT

I had decided to delete the line

echo '<input type="submit" name="action" value="'.$_POST['action'].'" />';  

as it wasn’t crucial to the functionality of the publishing/retraction since the process still executed as designed and implemented. For me, the splinter was (is still) that I wasn’t able to figure out how to make the $action variable grab onto either the 'Retract' / 'Publish' values. …more reading.

LAST UPDATE

About to lose it…eleven days since I’ve been completely stopped in my tracks. I have read until I’ve passed out, code-flailed repeatedly, had a number of functions I thought I’d nailed down become problems again (to be nailed flat again), and round and round in what’s fast becoming my personal Ninth Circle of Coding Hell;

if($row['access_level'] > 1 || $_SESSION['access_level'] > 1){ 
  if(empty($row['is_published']) || $action != 'Retract'){
            echo '<input type="submit" name="action" value="Publish"/>';
  }
  elseif($row['is_published'] || $action != 'Publish'){
        echo '<input type="submit" name="action" value="Retract"/>';
  } 
}   

The $action variable (which I POSTed per suggestions from @Darren and @Sharky) was isset earlier in the code;

$action = false;
if(isset($_POST['action'])){
   $action = $_POST['action'];
} 
echo $action;  

At this point, I’m in an advanced state of hermitage over this and fast approaching mental disrepair. The only positive element in this whole sordid mess is the fact that I find myself exhuming humor from the dour fact that MANUALLY swapping the places of the two echoes in the former code chunk is what changes the input label state to respond to the database content status (published or retracted) and not the if…else if statement. I just can’t believe that there hasn’t been even a crumb of a pointer under any of the rocks I’ve overturned lately. Just a whole lot of bad advice and deprecated solutions (that won’t even work, lol!). If I could just find a way to force the echoes to swap places, I’d be golden. :longsigh: Now, it’s by any means necessary…

LAST-DITCH (MERCY-KILLING NECESSARY/REQUESTED)

I just don’t know what else to do…seems I’ve exhausted all possibilities getting the input label states to “flip” from “Retract” <=> “Publish”. I’m at this point doubting that this may be a pure PHP solution. Actually got in touch with the publishers where this (horrible) code concept originated, and was fluffed off with a, “We’ll get this info to the respondible parties (lol!)”. At this point, I’m just begging for any hint of a crumb on how to go about this.

I’ve isolated it down to (what I’m thinking is) the ELSE part of the if/else is the culprit, but I don’t know how to go about force-effecting it like I successfully did in the if part;

CODE:
-----
echo '<br><div class="searh_form"><form method="post" action="transact_article.php"><div>';

echo '<p><br>';
echo '<input type="submit" name="action" value="Edit"/>';
foreach ($_POST as $key => $value) {
$submitted_array = array_keys($_POST, [0], $action);
echo ($_POST['action'][$submitted_array[0]] . " " . $submitted_array[0]);
}
if (empty($row['is_published']) || !$row['is_published'] && empty($row['date_published']) || !$row['date_published'] && $action != $_POST['action']  || $action != '') {
$action = "Publish";
}else{
if (!empty($row['is_published']) || $row['is_published'] && $row['date_published'] || $row['date_published'] && $action == $_POST['action']  || $action != $_GET['action']) {
$action = "Retract";
}}
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" />';
}}
?>
<input type="hidden" name="article_id" value="<?php echo $_GET['id']; ?>"/>
</p>
</form>

  RESULT:  
  -------
   _____________ ____________ ____________
  |             |            |            |
  |    EDIT     |  PUBLISH   |   DELETE   |
  |_____________|____________|____________|  

Re-employment of the line
echo '<input type="submit" name="action" value="$action" />';
allowed me to achieve above result – which is just how I need to have all the pending content appear before editing/posting. Please, I’m humbly inquiring after just what it is I can do to get my label to flip to “Retract” when there has been approved updating of the database content state. Thanks for ANY additional pointers I can get. I’m about at the end of my rope on this…

What in the Name of Sanity is it going to take?!?!?
Declaration of two-element array:

$buttonType = array('Publish', 'Retract');

The solution began to surface once I got onto the fact that the if/else was referring to an array structure – the print_r() being

Array ( [0] => Publish [1] => Retract )

GREAT! But, unfortunately I wasn’t able to tap the numeric indices within the if/else to force the $buttonType variable to display the Retract value as I got a fatal error as [] isn’t allowed for reading in PHP

$buttonType = array('Publish', 'Retract');
if (isset($_POST['buttonType']) && $buttonType == $_POST['buttonType']) {
foreach ($_POST as $key => $value) {
return $value;    
}
}
if ($row['is_published'] && $row['date_published'] && $buttonType != 'Publish') {
$buttonType = "Retract";
} else {
if (empty($row['is_published']) && empty($row['date_published']) && $buttonType != 'Retract') {
$buttonType = "Publish";
}
}
if (($row['access_level'] > 1) || ($_SESSION['access_level'] > 1)) {
echo '<input type="submit" name="action" value="'.$buttonType.'" />';
}   

This current rewrite produces the desired result for ‘Unpublished’;

 _____________ ____________ ____________
|             |            |            |
|    EDIT     |  PUBLISH   |   DELETE   |
|_____________|____________|____________|

However, after submitting the form, even though I get the content to become ‘Published’, the input value still remains unchanged. There has to be a simpler way to flip input value states. I’m still bogged down trying to see how I can access the numeric indices.

3

Answers


  1. Chosen as BEST ANSWER

    Terrible code is hard to overcome at times. My mistake was to try and follow pointless logic initially. Once the idea hit me to focus on the array business as the key to the input state flip that I was desperate for earlier;

    echo '<br><div class="searh_form"><form method="post" action="transact_article.php"><div><p><br>';
    echo '<input type="submit" name="action" value="Edit" />';
    foreach ($buttonType as $key=>$value){
      if ($row['is_published'] && array_key_exists([0], $buttonType, TRUE)) {
      if (($row['access_level'] > 1) or ($_SESSION['access_level'] > 1)) {
     $buttonType[] = '<input type="submit" name="action" value="Retract"/>';
       } else {
     $buttonType[] = '<input type="submit" name="action" value="Publish"/>';
     }}
     echo "<input type='submit' name='action' value="$buttonType[$key]" />";
     }
     echo '<input type="submit" name="action" value="Delete" />';
     }}
    ?>  
    <input type="hidden" name="article_id" value="<?php echo $_GET['id']; ?>"/>
    </p>
    </div>
    </form>
    <br>
    

    I had to do some physical repositioning of some of the code - moving the if/else within the foreach statement, tuning up the connection of $buttonType variable to the target inputs (having declared the $buttonType array further up in the page), while still not altering how the inputs were expected to act to fulfill the functions of publishing/retraction.

    WORKS EXACTLY AS REQUIRED...another flawless victory...


  2. Your $action isn’t recognized as variable because of the ' symbol.

    echo '<input type="submit" name="action" value="' . $action . '" />';
    
    Login or Signup to reply.
  3. If you’ve narrowed down you possible actions to those 2, you could simply run a switch/case:

    switch($action) {
        case "Retract":
        echo '<input type="submit" name="action" value="Retract"/>';
        break;
        case "Publish":
        echo '<input type="submit" name="action" value="Publish"/>';
        break;
    
    }
    

    Your Issue: is that you’re passing a string, instead of concatenating the variable. What you want to do is echo your input like this:

    echo '<input type="submit" name="action" value="' . $action . '" />';
    

    A more effective method of printing out your inputs is this:

    echo "<input type='submit' name='action' value='{$action}' />";
    

    Your issue was because you were passing the $action as a string, not having it evaluated. If you want to evaluate it in the string like that, you’ll need to use " instead of '.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search