1

I'm writing a delete function in codeigniter. Users have two selection. Delete all or delete one row. I don't want to repeat same codes and I use a function like that. But there is an error ' Parse error: syntax error, unexpected T_ELSE'. How can I solve that problem? Only way to repeat codes?

public function _delete_port()
{
    if ($_GET['delete_all']): 
        foreach ($_GET['delete_id'] as $delete_id):         
    else:
        $delete_id = $_GET['delete_id'];
    endif;
        // Some controls
        // ...

        $this->db->delete('ma_port', array('id' => $delete_id));

    if ($_GET['delete_all']) :
        endforeach;
    endif;

    redirect($url);
}
6
  • 4
    You have no body following the foreach():. The alternate flow control syntax (using the if/endif foreach/endforeach is not recommended outside of HTML templating. Use proper bracketed control structures and this stuff becomes easy to spot. if () {...} else {...} and foreach() {... } Commented Oct 25, 2013 at 13:10
  • Oh wait a minute, what are you trying to do here? It almost looks like you are attempting to procedurally construct the PHP code by conditionally starting and ending loops. Commented Oct 25, 2013 at 13:14
  • @MichaelBerkowski I must repeat the codes? Is it only solution? Commented Oct 25, 2013 at 13:16
  • nested and dirty... So CHEARS Commented Oct 25, 2013 at 13:17
  • @revo I must repeat the codes? Is it only solution? Commented Oct 25, 2013 at 13:18

2 Answers 2

1

The main issue facing your code seems to be a misunderstanding of how flow control works. The approach you have taken is to attempt to use the if / else / endif to construct how you want your code to look instead of what you want your code to do.

Your procedure is to use a loop if $_GET['delete_all'] is present as an array, and to otherwise use $_GET['delete_id']. So first check the contents of the variables and then take the appropriate action. The whole thing can be simplified by placing $_GET['delete_id'] into an array if $_GET['delete_all'] isn't there.

// Empty array we'll fill later
$delete_ids = array();

// If the delete_all array is available, use it as the array to delete from
if (isset($_GET['delete_all']) && is_array($_GET['delete_all'])) {
  $delete_ids = $_GET['delete_all'];
}
// Next if delete_id is present
else if (isset($_GET['delete_id'])) {
  // Place it into the array to use later
  $delete_ids[] = $_GET['delete_id'];
}
else {
  // Neither was set, you have an error state, so handle it however you need to
  // Redirect, display an error, whatever.
}

// Now loop over your array and perform the action.
// The array either contains the original array from $_GET['delete_all'] or the single element $_GET['delete_id']
// Or it may be empty entirely, and therefore won't do anything
foreach ($delete_ids as $delete_id) {
  $this->db_delete('ma_port', array('id' => $delete_id));
}

// And finish with your redirect
redirect($url);
Sign up to request clarification or add additional context in comments.

Comments

0

That was so dirty, I should be a better translator thoguh:

public function _delete_port()
{
    if ($_GET['delete_all']): 
        foreach ($_GET['delete_id'] as $delete_id):         
            // Some controls
            // ...
            $this->db->delete('ma_port', array('id' => $delete_id));
        endforeach;
    else:
        $delete_id = $_GET['delete_id'];
        $this->db->delete('ma_port', array('id' => $delete_id));
    endif;
    redirect($url);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.