Chicken&Egg situation in printing rows while searching in them
Please consider the following code:

    // index.php?q=Something
    // Note: the following could have been done through SQL,
    // but let's consider it wasn't (I'm well aware of the consequences).
    $search=$_REQUEST['q'];
    foreach($rows as $i=>$row){
        $found=false;
        foreach((array)$row as $k=>$v){
            if(stripos($v,$search)!==false){
                $found=true;
                break; // <- optimization
            }
        if(!$found)unset($rows[$i]);
    }

    // And somewhere later on...
    echo '<table>';
    foreach($rows as $row){
        echo '<tr>';
        foreach((array)$row as $k=>$v){
            echo '<td>';
            echo formatter($row,$k,$v); // <- in truth, I'm using call_user_func(),
                                   // but for the sake of argument...
            echo '</td>';
        }
        echo '</tr>';
    }
    echo '</table>';
    
    // And maybe somewhere earlier, perhaps passed as a function argument...
    function formatter($row,$column,$cell){
        switch($column){
            case 'uid':
                $cell=getUser($cell)->name().' '.getUser($cell)->surname();
        }
        return htmlspecialchars(''.$cell,ENT_QUOTES);
    }


I know it's bit jumbled up. Hopefully it's not too difficult to understand. In short, here what it does:

 - we first filter out the database results ($rows) according a search term (search system is very basic, *do not suggest alternative search engines, that's not the point*).

 - next we print a table of the search results using a formatting function...
 - which we might have defined somewhere earlier*
 - which using caching techniques, we print out mixed db data without any further queries (thus the reason I didn't `SELECT * FROM xyz, users WHERE xyz.uid=users.id`
 - **it is important to note that as a side effect, I'm using getUser()**

The problem? I can't search for user "John Doe" since at the time the search is done, only user IDs are know (formatting as done later on) [as well as the fact that user John Doe doesn't exist **=P**].

In this specific case, we could fix it easily with some specific coding. But sometimes, the formatting routine is quite complex, more than just resolving an ID to a name (such as parsing WikiMarkup or same advanced computations).

The logical solution would be to do the search at the end, ie after rendering the rows.
With output buffering(needed since some stuff in formatter() is written directly) and `strip_tags`, this would be as easy as:

    ob_start();
    foreach($rows as $row){
        foreach((array)$row as $k=>$v){
            echo '<td>'.formatter($row,$k,$v).'</td>';
            stripos(strip_tags(ob_get_contents()),$search)!==false
                ? ob_flush() : ob_clean();
        }
    }
    ob_end_clean();

Basically, we flush the buffer if search query matches, otherwise, we clear the buffer.

Although I believe it ought to work, and might even require less CPU cycles, it sounds like a lot of content manipulation.

Oh, and come to think of it, scratch off output buffering - it makes absolutely no sense if I had to introduce pagination (which I have the very intention to do).

**See where is the problem?**

It needs to manipulate/search the generated data (search) to actually generate the right one. Sounds awfully inefficient in several counts.
Chicken&Egg situation in printing rows while searching in them
===

Please consider the following code:

    // index.php?q=Something
    // Note: the following could have been done through SQL,
    // but let's consider it wasn't (I'm well aware of the consequences).
    $search=$_REQUEST['q'];
    foreach($rows as $i=>$row){
        $found=false;
        foreach((array)$row as $k=>$v){
            if(stripos($v,$search)!==false){
                $found=true;
                break; // <- optimization
            }
        if(!$found)unset($rows[$i]);
    }

    // And somewhere later on...
    echo '<table>';
    foreach($rows as $row){
        echo '<tr>';
        foreach((array)$row as $k=>$v){
            echo '<td>';
            echo formatter($row,$k,$v); // <- in truth, I'm using call_user_func(),
                                   // but for the sake of argument...
            echo '</td>';
        }
        echo '</tr>';
    }
    echo '</table>';
    
    // And maybe somewhere earlier, perhaps passed as a function argument...
    function formatter($row,$column,$cell){
        switch($column){
            case 'uid':
                $cell=getUser($cell)->name().' '.getUser($cell)->surname();
        }
        return htmlspecialchars(''.$cell,ENT_QUOTES);
    }


I know it's bit jumbled up. Hopefully it's not too difficult to understand. In short, here what it does:

 - we first filter out the database results ($rows) according a search term (search system is very basic, *do not suggest alternative search engines, that's not the point*).

 - next we print a table of the search results using a formatting function...
 - which we might have defined somewhere earlier*
 - which using caching techniques, we print out mixed db data without any further queries (thus the reason I didn't `SELECT * FROM xyz, users WHERE xyz.uid=users.id`
 - **it is important to note that as a side effect, I'm using getUser()**

The problem? I can't search for user "John Doe" since at the time the search is done, only user IDs are know (formatting as done later on) [as well as the fact that user John Doe doesn't exist **=P**].

In this specific case, we could fix it easily with some specific coding. But sometimes, the formatting routine is quite complex, more than just resolving an ID to a name (such as parsing WikiMarkup or same advanced computations).

The logical solution would be to do the search at the end, ie after rendering the rows.
With output buffering(needed since some stuff in formatter() is written directly) and `strip_tags`, this would be as easy as:

    ob_start();
    foreach($rows as $row){
        foreach((array)$row as $k=>$v){
            echo '<td>'.formatter($row,$k,$v).'</td>';
            stripos(strip_tags(ob_get_contents()),$search)!==false
                ? ob_flush() : ob_clean();
        }
    }
    ob_end_clean();

Basically, we flush the buffer if search query matches, otherwise, we clear the buffer.

Although I believe it ought to work, and might even require less CPU cycles, it sounds like a lot of content manipulation.

Oh, and come to think of it, scratch off output buffering - it makes absolutely no sense if I had to introduce pagination (which I have the very intention to do).

**See where is the problem?**

It needs to manipulate/search the generated data (search) to actually generate the right one. Sounds awfully inefficient in several counts.