0

i am having database table in Microsoft access which has field as follows: partno,description,quantity

when i am retriving data and trying to show it in html table it shows error

Illegal string offset 'partno'

my php code is as follows:

<?php
 foreach($items as $row){
    $partno=$row['partno'];
    $desc=$row['description'];
    $qty=$row['quantity'];
  ?>
  <tr>
  <td><?=$partno?></td>
  <td><?=$desc?></td>
  <td><?=$qty?></td>
  </tr>

  <?php  
    }
    }
    if(!($items)){
  ?>
  <h3 style="color: red;">Item not found!</h3>
  <?php
    }
  ?>

i tried see data through

print_r($items);

data of array $items is :

Array ( 
    [partno] => BRZU 
    [0] => BRZU 
    [description] => CONTROL UNIT 
    [1] => CONTROL UNIT 
    [quantity] => 3 
    [2] => 3 
) 
1
  • Add the code with which you retrive data to get more help Commented Jun 2, 2017 at 18:10

3 Answers 3

3

It seems that $items actually contains just a single row. Using a foreach loop over it will cause $row to assume values BZRU, BZRU, CONTROL UNIT, 2, 3.

I think that you were expecting an array of arrays like this, over which you can iterate with your foreach loop.

Sign up to request clarification or add additional context in comments.

Comments

1

Verify first that the array items is not empty before proceeding with the loop.

<?php
     if(is_array($items) && count($items) > 0){//check that we have something before proceeding with the loop
       foreach($items as $row){
        $partno=$row['partno'];
        $desc=$row['description'];
        $qty=$row['quantity'];
      ?>
      <tr>
      <td><?=$partno?></td>
      <td><?=$desc?></td>
      <td><?=$qty?></td>
      </tr>

      <?php  
        }
      } else {
          ?>
      <h3 style="color: red;">Item not found!</h3>
      <?php
        }
      ?>      

Comments

0

Your logic is incorrect. You need to check if $items is empty before attempting to iterating through it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.