3

I have a MySQL DB field populated with some data. I have an option in my script which cleans that field so I can add new fresh data to it without appending to the old stuff.

However I am using a foreach loop which looks like this:

            foreach ($model->filenames as $key => $model->filename) {
            $model->get_title($model->filename);
            $model->get_showTitle($model->titles);
            $model->get_number($model->titles);
            $model->get_host($model->urls[$key]);
            $model->source_title($model->titles);
            $model->get_season();
            if ($model->show_exist_batch()) {
                $model->show_clean(); //the method in question
                $model->show_update();
            } else {
                $model->show_add();
                $model->twitter();
            }

The thing is that I want show_clean() to only run ONCE. This is how show_clean() looks:

    public function show_clean() {
    if ($this->options->clean) {
        $query = "UPDATE jos_k2_items SET extra_fields = '', extra_fields_search = '' WHERE id = " . $this->item->id . "";
        $this->db->prepare($query);
        $this->db->query();
    }
}

$this->option->clean is set by a post variable in the constructor.

Are there any clever ways of doing this or do i need to do it the long way?

3
  • Is there something wrong with how you're doing it now? Commented Mar 4, 2012 at 21:21
  • 1
    Depends what you consider the »long way«. Simply have a bool variable that you check and set it once you've called the function. Commented Mar 4, 2012 at 21:21
  • Well I am actually rewriting an old script to object oriented and i did some funky coding to get it to work. That was the "long way" Commented Mar 4, 2012 at 21:34

1 Answer 1

8

Not sure how "clever" this is, but its one way that doesn't seem to be long.

Set a variable outside of your loop indicating that you have not cleaned, and only call clean if it hasn't been set yet. Once you clean the first, time, set your flag so it will not clean again.

$cleaned = false;
foreach ($model->filenames as $key => $model->filename) {
    // ...
    if ($model->show_exist_batch()) {
        if (!$cleaned) {
            $model->show_clean();
            $cleaned = true;
        }
        $model->show_update();
    } else {
        // ...
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

OF COURSE! I actually did it slightly different. I just changed the clean flag to false after the clean was complete inside the show_clean() method itself since the php script is still running
That works too :) The art of programming are the many, many ways to solve the same problems.

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.