php - Wordpress plugin display query in linked file -
i creating wordpress plugin query db based off of user entered parameters , display results in linked html file. can display html page, results variable not passing through.
here how displaying linked html file:
//this set in location $template = 'results'; //execute sql global $wpdb; $result = $wpdb->get_results($sql); //load template $content = file_get_contents( plugins_url( 'template-files/'.$template.'.php',__file__ ) ); foreach ( $result $r ){ $contentcopy = $content; echo jww_display_php_file($contentcopy, $r); } function jww_display_php_file( $content, $r ){ $arr = (array)$r; ob_start() && extract($arr, extr_skip); eval('?>'.$content); $content = ob_get_clean(); ob_flush(); $content .= "<hr>"; return $content; }
here have in html file:
<table width="100%" border="1" cellspacing="0" cellpadding="0"> <tr> <td>name</td> <td><?php echo $name; ?></td> </tr> </table>
thanks in advance help
first of all, results.html
should results.php
, can't use php
variables or functions inside html
file, rename file results.php
, try this
$result = $wpdb->get_results($sql); $content = file_get_contents( plugins_url( 'template-files/results.php',__file__ ) ); foreach ( $result $r ){ $contentcopy = $content; echo jww_display_file($contentcopy, $r); } function jww_display_file( $content, $r ){ $arr = (array)$r; ob_start() && extract($arr, extr_skip); eval('?>'.$content); $content = ob_get_clean(); ob_flush(); $content .= "<hr>"; return $content; }
finally, inside results.php
file, change following
<td><?php echo $r->name; ?></td> <td><?php echo $r->age; ?></td> <td><?php echo $r->dob; ?></td>
to
<td><?php echo $name; ?></td> <td><?php echo $age; ?></td> <td><?php echo $dob; ?></td>
note : eval
evil it's not problem when using internal file server , know code secure, way, framework laravel
loads view
file , mix variables controller in mvc
framework.
Comments
Post a Comment