Pregunta

I am trying to fetch data from database and show on one page -> website name - number of orders -

$query = $this->db->query('SELECT * FROM ebay_orders WHERE order_status="Unshipped" ORDER BY webname');
$this->db->where('userid',$userid);

foreach ($query->result_array() as $row)
{
    echo '<div style="padding: 0px 10px 0 2px;">';
    echo $row['webname'];
    echo ' - ';
    echo $query->num_rows();
    echo '</div><br>';

}

Its working but repeating and showing total together like this -

ebay1 - 20
ebay1 - 20
ebay1 - 20
ebay1 - 20

ebay2 - 20
ebay2 - 20
ebay2 - 20

ebay3 - 20
ebay3 - 20
ebay3 - 20

I want simple method which show data like this -

ebay1 - 5
ebay2 - 10
ebay3 - 5

Please help!

¿Fue útil?

Solución

Use the query below

$query = $this->db->query('SELECT *, count(id) as total FROM ebay_orders 
                                              WHERE order_status="Unshipped" 
                                              GROUP BY webname 
                                              ORDER BY  webname');
$this->db->where('userid',$userid);
foreach ($query->result_array() as $row)
{
    echo '<div style="padding: 0px 10px 0 2px;">';
    echo $row['webname'];
    echo ' - ';
    echo $row['total'];
    echo '</div><br>';
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top