Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm building a custom plugin for a Wordpress project I'm building as it's quite a specific project, and there isn't an off the shelf plugin suitable for the task.

On one page I want to list objects from the database (which is also a custom database table) in a table. There is currently one item in the database, and I can echo this fairly easily to the page without any issue.

The problem occurs when I try to put this data in to a HTML table, so when there is more than one object in the database it can be presented properly. However, as I have said there is only one record in the database at this moment, but when I include this data in to a table it displays the same record twice.

Here is my code:

<?php
    global $wpdb;
    $satrent = $wpdb->get_row("SELECT id, name FROM phones WHERE phonetype = 'satellite' AND rent = '1'", ARRAY_N);
?>

And for the HTML table:

        <?php
        foreach($satrent as $sr)
        {
        ?>
        <tr>
            <td>&nbsp;</td>
            <td><?php print $satrent[1]; ?></td>
            <td><a href="#view">View</a></td>
            <td><a href="#edit">Edit</a></td>
            <td><a href="#delete">Delete</a></td>
        </tr>
    <?php
    }
    ?>

Have I missed something or done something wrong?

Many thanks!

share|improve this question
    
This is a gernaal SQL/PHP question, not a WordPress-specific question. –  Chip Bennett Jan 25 '12 at 15:22
    
Well it's using Wordpress tags and it's for a Wordpress enviroment, so that would make it a Wordpress question. –  mickburkejnr Jan 25 '12 at 15:25
    
You're querying custom data from a custom table. Just because you're doing it from within a WordPress Plugin doesn't make this question in any way germane to WordPress development. –  Chip Bennett Jan 25 '12 at 15:27
    
It's for a plugin, thats why it's tagged as plugin development. –  mickburkejnr Jan 25 '12 at 15:30
add comment

closed as off topic by Chip Bennett, Wyck, kaiser, Chris_O, Rarst Sep 22 '12 at 16:57

Questions on WordPress Development Stack Exchange are expected to relate to WordPress within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers

up vote 2 down vote accepted

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>&nbsp;</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.

share|improve this answer
    
Tried this but it still displays the two records. –  mickburkejnr Jan 25 '12 at 15:29
    
Perhaps combining the two answers? –  Chip Bennett Jan 25 '12 at 15:32
    
Makes no difference, it still displays the same duplicate result. –  mickburkejnr Jan 25 '12 at 15:33
    
@StephenHarris feel free to incorporate my answer into yours; I can then just reference yours as the complete answer. –  Chip Bennett Jan 25 '12 at 15:35
    
@ChipBennet, Thanks Chip - much clearer now. –  Stephen Harris Jan 25 '12 at 15:46
add comment

Your problem is probably here:

foreach($satrent as $sr)

...and here:

<td><?php print $satrent[1]; ?></td>

You're using $satrent inside your foreach loop, instead of $sr.

Edit

Please see @StephenHarris's answer for the complete answer, which includes this one.

share|improve this answer
    
+1 didn't spot that... –  Stephen Harris Jan 25 '12 at 15:26
    
I used $satrent at one point because $sr was just giving the first character of the item. So $sr[0] would give the first letter, $sr[1] the second letter etc. And changing it back to $sr doesn't make a difference to the number of records? –  mickburkejnr Jan 25 '12 at 15:28
    
@mickburkejnr try $sr rather than $sr[1] and my edited answer. You need to make both changes Chip and I have suggested. –  Stephen Harris Jan 25 '12 at 15:33
    
Had made the changes nothing happened. There was an issue on the dev server so the pages weren't loaded properly. Now all works with both combined answers. Thanks again! –  mickburkejnr Jan 25 '12 at 15:40
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.