0

This plugin function I'm working on parses a CSV file, and outputs it as an HTML table; in addition to that, it also queries a database and gets values for one of the columns.

Every other part of the script seems to run fine, and does what I need it to, except for the database query portion:

$newdb = new wpdb( 'user', 'password', 'db', 'localhost' );

$results = $newdb->get_results("SELECT ItemID, Price FROM $table WHERE ItemID = $quanid GROUP BY Price ORDER BY COUNT(*) DESC LIMIT 1");


echo('<td>');
echo($results);
echo('</td>');

$results is displayed as "Array" in the HTML output.


function create_table_form() {
$handle = fopen( plugin_dir_path( __FILE__ )  . 'example.csv' , "r" );  
$data = fgetcsv($handle, 1000, ",");
echo('<input type="text" class="search" id="search" placeholder="Search">');
echo('<br>');
echo('<table id="table">    <tr class="hidden">
    <th><b>
        Name</b>
    </th>
    <th><b>
        Fudge</b>
    </th>
    <th><b>
        Price</b>
    </th>
    <th><b>Vote</b>
    </th>
</tr>
<tbody>');
$a = 1;
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {

    $name = $data[0];
    $quanid = $data[2];
    $table = $data[3];
    unset($data[2]);
    unset($data[3]);
    //generate HTML
    echo('<tr>');
    foreach ($data as $index=>$val) {
        echo('<td>');
        echo htmlentities($val, ENT_QUOTES);
        echo('</td>');


    }
    $newdb = new wpdb( 'user', 'password', 'db', 'localhost' );

    $results = $newdb->get_results("SELECT ItemID, Price FROM $table WHERE ItemID = $quanid GROUP BY Price ORDER BY COUNT(*) DESC LIMIT 1");


    echo('<td>');
    echo($results);
    echo('</td>');

    echo('<td>
        <button class="toggler" data-prod-cat="' . $cssId . '">Vote</button>
    </td>');
    echo('</tr>');
    echo('<tr class="cat' . $cssId . ' hidden" style="display:none">');
    echo('<td colspan="4" style="white-space: nowrap">Enter ' . $name . ' Price:
    <input type="text" maxlength="4" name="' . $quanid . '" value="" class="input" />&nbsp;
    <button class="submit" type="submit" value="Submit">Submit</button>
    </td>
    </tr>');

    $cssId = 'row-'.$a;

$a++;
}

echo("</table>");
fclose($handle);

}
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Kreation
  • 327
  • 3
  • 10
  • Does this answer your question? [How can I echo or print an array in PHP?](https://stackoverflow.com/questions/9816889/how-can-i-echo-or-print-an-array-in-php) – mickmackusa Feb 08 '23 at 12:20

1 Answers1

1

Change echo($results) to:

print_r( $results );
wbdlc
  • 1,070
  • 1
  • 12
  • 34
  • Thanks!, it's now displaying the values from the database, but the output is a little strange `Array ( [0] => stdClass Object ( [ItemID] => 1 [Price] => 40 ) ) ` How can I display just the value from Price? – Kreation Mar 20 '15 at 21:29
  • print_r returns your arrays keys and elements. It's a good way to see what is within the array. To display just the value of price you could probably just use `echo $results['Price'];` – wbdlc Mar 21 '15 at 12:58