Help to filter results by user_id

Hello everyone I’m beginner on PHP

on this code i need help to filter results by user_id = the logged-in user on session

<?php 
                        if($orders!= null){
                            foreach ($orders as $o) {
                                echo '<tr>';
                                echo '<td>'.$o->idOs.'</td>';
                                echo '<td>'.date('d/m/Y' ,strtotime($o->dataInicial)).'</td>';
                                echo '<td>'.date('d/m/Y' ,strtotime($o->dataFinal)).'</td>';
                                echo '<td>'.$o->name.'</td>';
				echo '<td>'.$o->user_id.'</td>';
                                echo '<td>';
                                if($this->permission->checkPermission($this->session->userdata('permisson'),'vOs')){
                                   echo '<a href="'.base_url().'index.php/os/view/'.$o->idOs.'" class="btn"> <i class="icon-eye-open" ></i> </a> '; 
                                }					
                                echo '</td>';
                                echo '</tr>';
                            }
                        }
                        else{
                            echo '<tr><td colspan="3">None OS open</td></tr>';
                        }    

?>

anyone can help ?

Do you use $orders (for all users) elsewhere on the page? If not then I’d change the query so that the db returns the data set you want instead of filtering in PHP.

You can also consider this code, not echoing HTML will usually make your life a bit easier.

<?php 
if($orders!= null):
    foreach ($orders as $o): ?>
        <tr>
            <td><?= $o->idOs ?></td>
            <td><?= date('d/m/Y' ,strtotime($o->dataInicial)) ?></td>
            <td><?= date('d/m/Y' ,strtotime($o->dataFinal)) ?></td>
            <td><?= $o->name ?></td>
            <td><?= $o->user_id ?></td>
            <td>
                <?php if($this->permission->checkPermission($this->session->userdata('permisson'),'vOs')): ?>
                   <a href="<?= base_url().'index.php/os/view/'.$o->idOs?>" class="btn">
                       <i class="icon-eye-open"></i>
                   </a>  
                <?php endif; ?>
            </td>
        </tr>
    <?php
    endforeach;
else: ?>
    <tr>
        <td colspan="3">None OS open</td>
    </tr>
<?php endif ?>

Thanks for the answer. I will try to follow your advice not to put echo HTML, as I am still learning PHP about 3 months.

Regarding the page is accessed by all users, I am creating a simple dashboard with a small summary in table just wanted to see the respective orders for the user who was logged in.
for the administrator could check everything,

so my question to use some filter in this table through user_id

Then change the query that you get $orders from so it selects orders where user_id = logged in user id. This code is not included in the code you pasted here so it’s impossible to suggest a fix

Ok, thanks.

I will try on select orders. only one question

it is possible check 2 conditions on clause where ?

Example:$this->db->where(‘order.status’ , ‘value1’, ‘value2’);

this code is correct?

Ok thanks for your super help.

It’s already works:

In where i solved with:

$this->db->where(‘order.status’ , ‘value1);
$this->db->or_where(‘order.status’ , ‘value2);

Sponsor our Newsletter | Privacy Policy | Terms of Service