Home > other >  Unsupported operand types: string int CodeIgiter
Unsupported operand types: string int CodeIgiter

Time:04-24

I want to ask. I'm having an error creating a simple input form using CodeIgniter.

My database (tbl_user):

| id | name  |  card  | initial |
| -- | ----  |  ----  | ------- |
| 1  | Alex  |  A001  |    A    |
| 2  | Fern  |  F001  |    F    |

My Model:

public function a(){
  return $this->db->get_where('tbl_user',array('initial'=>'A'));
}

My input form:

<?php foreach ($a as $user_a){ ?>
   <input  placeholder="<?php echo $user_a->card 1;?>" readonly>
<?php } ?>

Then an error message appears: Unsupported operand types: string int. What I want to show is A002. How to do it? would you like to help me? Thank you..

CodePudding user response:

The card field is a string & cannot be added with an integer. Try this code

$user_a->card = 'A001';
/* Separate String and Number */
$letters = preg_replace('/[^a-zA-Z]/', '', $user_a->card);
$numbers = preg_replace('/[^0-9]/', '', $user_a->card);

$int = (int)$numbers 1; // Convert number part to integer
$num_padded = sprintf("d", $int);
echo $letters.$num_padded;
  • Related