The problem is that get_row
returns a row object (or array), not an array of row objects.
You want $wpdb->get_results
instead. This always returns an array of row objects, event if there is only one result.
You may also wish to add an
if($satrent){
//Foreach on $satrent
}
so that if no results are returned, you simply don’t display the table (or display something else) rather than trying to loopi through a null object which will cause an error.
Also as Chip as pointed out in his answer: You’re using $satrent
inside your foreach loop, instead of $sr
.
Try the following code:
<?php
global $wpdb;
$rows= $wpdb->get_results("SELECT id, name FROM phones WHERE phonetype="satellite" AND rent="1"", ARRAY_A);
?>
And then loop through each rows
<?php if($rows): ?>
<?php foreach($rows as $row): ?>
<tr>
<td> </td>
<td><?php echo $row['name']; ?></td>
<td><a href="#view">View</a></td>
<td><a href="#edit">Edit</a></td>
<td><a href="#delete">Delete</a></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
Edit: See Chips answer on why you are seeing duplication. But you are seeing two rows because you are looping through each returned column: ‘name’ and ‘id’. After making Chip’s corrections you’ll find the table displays a row for each column in the database – see my solution to address this.