0
<table>
<thead>
<tr>
<th>model</th>
<th>a</th>
<th>jumlah</th>
</tr>

    </thead>
    <?php  foreach((array)$query as $row):
                        if ($row->model='10') {
                            $jumlah=$row->a-10;
                        }else{
                            $jumlah=$row->a-5;
                        };?>
    <td><?php echo $row->model ?></td>
    <td><?php echo $row->a ?></td>
    <td><?php echo $jumlah ?></td>

it should result like this

enter image description here

but why is the result like this?

enter image description here

why data in coloumn model overide to 10? how to solve that?

faza
  • 249
  • 1
  • 8
  • 24

3 Answers3

2

You are using the assignment operator (=) not the equality operator (==). Change this line:

 if ($row->model = '10') {

to this:

 if ($row->model == '10') {

or this:

 if ($row->model == 10) {

In the first case, you were telling the compiler to store the value of 10 into the variable $row->model.

In the second case we are checking to see if the value of $row->model is equal to the string value of 10.

In the third case we are checking to see if the value of $row->model is equal to the numeric value of 10.

My guess is this is that you probably want to use the third case unless the numbers are stored as text values inside of your database.

kojow7
  • 10,308
  • 17
  • 80
  • 135
1

= in php is meant to assign a value to a variable while == is used as a comparison operator.

In your code you need to change this line:

if ($row->model='10') {

With:

if ($row->model == '10') {

Here you will find a complete list of PHP operators

Community
  • 1
  • 1
Florian Lemaitre
  • 5,905
  • 2
  • 21
  • 44
1

Out of good mood today:

<?php  foreach((array)$query as $row):
                    if ($row->model=='10') { # <- added double = sign
                        $jumlah=$row->a-10;
                    }else{
                        $jumlah=$row->a-5;
                    };?>
Jan
  • 42,290
  • 8
  • 54
  • 79