Since global usage is frowned upon, I'm trying to create a class that has variables which are defined by data obtained from an fgetcsv function. I'm doing this so I can pass these variables to various other functions in this plugin that need access to them.
What I have posted below breaks the function, and doesn't display any of the output that previously functioned.
What do I want my class to contain, the function that is defining the variables? Where do I define them?
class My_Class{
var $name;
var $quanid;
var $table;
function open_csv_file() {
ob_start();
$handle = fopen( plugin_dir_path( __FILE__ ) . 'test.csv' , "r" );
$data = fgetcsv($handle, 1000, ",");
echo('<br>');
echo('<table id="table"> <tr class="hidden">
<th><b>
Name</b>
</th>
<th><b>
Cheese</b>
</th>
<th><b>
Price</b>
</th>
<th><b>Vote</b>
</th>
</tr>
<tbody>');
$a = 1;
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$this->name = $data[0];
$this->quanid = $data[2];
$this->table = $data[3];
unset($data[2]);
unset($data[3]);
$cssId = 'row-'.$a;
$a++;
//generate HTML
echo('<tr>');
foreach ($data as $index=>$val) {
echo('<td>');
echo htmlentities($val, ENT_QUOTES);
echo('</td>');
}
global $wpdb;
$sql = "SELECT ItemID, Price
FROM {$this->$table}
WHERE ItemID = %d
GROUP BY Price
ORDER BY COUNT(*) DESC LIMIT 1";
$results = $wpdb->get_var( $wpdb->prepare( $sql, $this->quanid));
echo('<td>');
print_r($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 ' . $this->name . ' Price:
<form action="" name="form' . $this->quanid . '" method="post"><input type="text" id="' . $this->quanid . '" maxlength="4" name="' . $this->quanid . '" value="price_input" class="input" />
<button id="submit" name="submit" class="submit" type="submit" value="Submit">Submit</button></form>
<?php
?>
</td>
</tr>');
wp_nonce_field('price_input');
}
echo("</table>");
return ob_get_clean();
fclose($handle);
}
}
Aucun commentaire:
Enregistrer un commentaire