I am trying to retrieve data from a wordpress table using a shortcode function I found. It works and retrieves the data for all the columns. When I try to specify the columns I want it still give me everything. Ultimately what I am trying to do is retrieve the data for the columns I want and style that data using css (not in a table format). I am new to php so I was wondering if anyone could let me know if this is even possible using shortcodes. If it is possible any help would be appreciated. Thank you in advance, Randy
Here is the code I am using:
if ( !defined( 'WP_FD_ABSPATH' ) )
define( 'WP_FD_ABSPATH', plugin_dir_path( __FILE__ ) );
function wp_first_shortcode($atts){
global $wpdb;
$a = shortcode_atts( array(
'table_name' => ",
'columns' => ",
), $atts );
$showcolumns=array();
if($a['columns']!=""){
$column=$a['columns'];
$showcolumns=explode(',',$a['columns']);
}
else{
$column='*';
$get_columns=$wpdb->get_results("describe ".$a['table_name']."");
foreach($get_columns as $fields) $showcolumns[]=$fields->Field;
}
$columncount=count( $showcolumns);
$result_query=$wpdb->get_results( "select $column from ".$a['table_name']."");
echo '<table><tr>';
for($i=0;$i<$columncount;$i++) echo '<th>'.$showcolumns[$i].'</th>';
echo '</tr>';
foreach($result_query as $result_row){
echo '<tr>';
for($i=0;$i<$columncount;$i++) echo '<td>'.$result_row->$showcolumns[$i].'</td>';
echo '</tr>';
}
echo '</table>';
}
add_shortcode('fetchdata', 'wp_first_shortcode');
function table_value_function() {
query_value(array('orderby' => 'date', 'order' => 'DESC' , 'showvalues' => ""));
wp_reset_query();
return $return_string;
}
?>
Here is the shortcode I am using:
[fetchdata table_name='my_wp_table' columns ='Display_Name' 'First_Name' 'Last_Name' 'Address_1' 'Address_2' 'City' 'State' 'Phone']
Ok, I think you just want to get rid of the table as you want to style it with CSS. Please follow the code below:
echo '<div class="something">';
for($i=0;$i<$columncount;$i++) echo '<h1>'.$showcolumns[$i].'</h1>';
foreach($result_query as $result_row){
echo '<div class="something_2">';
for($i=0;$i<$columncount;$i++) echo '<p>'.$result_row->$showcolumns[$i].'</p>';
echo '</div>';
}
echo '</div>';
}
Hope, it helps.
Related
So i am trying to make a website for collecting data from user and classify the data as 'suspicious' or 'not suspicious'. After i collect and classify the data, i want to show and send to server ONLY 'suspicious' data.
My table has one column called 'note', where the value of the 'note' is 'suspicious' or 'not suspicious'
is it possible to show the part of table that only have 'not suspicious' value in 'note' ? if possible, can you guys give me any idea how ?
Here is my code for showing the data:
Controller:
public function success() {
$dataz['resultss'] = $this->welcome_model->dataz();
$this->load->view('success',$dataz);
}
Model
public function dataz() {
$query = $this->db->get('info');
$results = $query->result();
return $resultss;
}
View
<?php
foreach($resultss as $row) {
echo '<tr>';
echo '<td>'.$row->name.'</td>';
echo '<td>'.$row->note.'</td>';
?>
<td>
</td>
<?php
echo '</tr>';
}
?>
Any help would be appreciated!
You can achieve your objective in many ways.
For 'not suspicious' records, use this,
$query = $this->db->get_where('info', array('note' => 'not suspicious'));
For 'suspicious' records, use this,
$query = $this->db->get_where('info', array('note' => 'suspicious'));
The method #Naga suggested is better but if you want to stick to your way and do it in view;
<?php
foreach($resultss as $row) {
if($row->note == 'not suspicious') {
echo '<tr>';
echo '<td>'.$row->name.'</td>';
echo '<td>'.$row->note.'</td>';
echo '<td></td>'; // why this extra td?
echo '</tr>';
}
}
?>
Im trying few hours repair my code but its always fail.
I have array in file:
$liquidyLista7 = array(
'Arbuz' => array(
'Wyraźny arbuz, soczysty arbuz, naprawde chyba najlepszy z oferty',
'Delikatny arbuz.',
'odświeżajacy arbuz',
'Bardzo delikatny, mniej wyrażny arbuz'
),
// and much more...
... i want to echo all values from each arrays but my "bad epic" code not work :/
function pokazOpisyVolish($liquidyLista7)
{
$i = 0;
$select = '<div id="volishPage'.$i.'" class="tab-pane fade"><ul class="comment-list">';
foreach($liquidyLista7 as $key => $record) {
$i++;
if (is_array($record)) {
$select .= '<li><p class="desc">'.$key.'</p>';
$select .= pokazOpisyVolish($record);
$select .= '</li>';
} else {
$select .= '<li><p class="desc">'.$record.'</p></li>';
}
}
$select .= '</ul></div>';
return $select;
echo pokazOpisyVolish($liquidyLista7);
Pls visit my test website and check how it works in "Mr Jack" (simple HTML - not PHP) but in "Volish" is my PHP code... :(
may be you are looking for some thing like this. the link you gave
uses plugin and css to display the result onto the webpage,
i can show you how to do it, but after that you have use your own css to display the result, i am thinking you might have add some div
name
just run this code in separate file, and you are good, understand what's happening and than add the div
<?php
$liquidyLista7 = array(
'Arbuz' => array(
'Wyraźny arbuz, soczysty arbuz, naprawde chyba najlepszy z oferty',
'Delikatny arbuz.',
'odświeżajacy arbuz',
'Bardzo delikatny, mniej wyrażny arbuz'
),
'newArtist' => array(
'Wyraźny arbuz, soczysty arbuz, naprawde chyba najlepszy z oferty',
'Delikatny arbuz.',
'odświeżajacy arbuz',
'Bardzo delikatny, mniej wyrażny arbuz'
)
);
//get the link from url
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
foreach ($liquidyLista7 as $key => $record) {
//ad artist name to the current url
echo "<a href=$actual_link?artistName=$key>$key</a><br>";
if(isset($_GET['artistName'])){
if(is_array($record)){
echo "<ul>";
foreach($liquidyLista7[$key] as $value)
if($_GET['artistName']==$key)
echo "<li>$value</li>";
}
echo "</ul>";
}
}
?>
I am writing helper functions that allow me to build tables quickly. I have a function called responsive_table that accepts two parameters: headings, and rows. The rows are dynamic and are pulled from a database. I build an array of rows with a foreach loop because some of the db table rows don't match the contents or order of the html table... some of the cells in the html table contain combinations of db columns, etc. and can't be displayed directly from the db. None of the rows in my HTML table array are being displayed. Nothing from inside of the function that's being declared in the function parameter is being displayed. Here's my code:
<?php
$output = responsive_table(array('<input type="checkbox" name="all">', 'Thumbnail', 'Title', 'Location', 'Author', 'Date Submitted', 'Actions'), function(){
foreach($listing_results as $listing){
$listings[] = array(
'<input type="checkbox" name="delete[]" value="'.$listing['listing_id'].'">',
'<img src="http://placehold.it/50x50" alt="Thumbnail">',
$listing['title'],
$listing['location'],
anchor('members/modify/'.$listing['member_id'], $listing['display_name']),
date($setting['date_format'], $listing['date_submitted']),
array(
array(
'title' => 'Modify Listing',
'color' => 'primary',
'href' => 'listings/modify/'.$listing['listing_id'],
'text' => '<i class="fa fa-pencil"></i>'
),
array(
'title' => 'Delete Listing',
'color' => 'danger',
'href' => 'listings/delete/'.$listing['listing_id'],
'text' => '<i class="fa fa-eraser"></i>',
'confirm' => true
)
)
);
}
return $listings;
});
echo $output;
function responsive_table($headings = array(), $rows = array(), $sortable = false){
$output = '
<div class="table-responsive">
<table data-sortable class="table">
<thead>
<tr>
';
if(count($headings) > 0){
foreach($headings as $heading){
$output .= '
<th>'.$heading.'</th>';
}
}
$output .= '
</tr>
</thead>
<tbody>';
if(count($rows) > 0){
foreach($rows as $row){
$output .= '
<tr>
<td>'.$row.'</td>
</tr>';
}
}
$output .= '
</table>
</div>';
return $output;
}
Yes, what you are looking for is a http://php.net/manual/en/class.closure.php
Any valid PHP code may appear inside a function, even other functions and class definitions. What it looks like your trying to do is work with anonymous functions, also known as closures. Here is a link that might help you out with this PHP anonymous functions.
I have a view in my site that has two divs. one houses a search box and the other is where the result of the search is shown. by default, nothing is loaded on the result div. only when there is a search via post request, the result div is populated. However, this is causing problem with the pagination functionality as the second time the page loads, it doesnt have the data to go around with listing the rest of the results. How can i modify my controller/view to meet this need?
My action in the controller:
public function search(){
if($this->request->is('post')){
if($this->request->data['User']['search']!=null){
$search_name=$this->request->data['User']['search'];
$this->Paginator->settings = array(
'conditions'=>array('User.name LIKE'=>'%'.$search_name.'%'),
'order' => array('User.datetime' => 'desc'),
'limit' => 10
);
$feed = $this->Paginator->paginate('User');
$this->set('search_result',$feed);
}
else{
$this->Session->setFlash(__("Cant make an empty search"));
return $this->redirect(array('action'=>'search'));
}
}
}
My View:
<?php
include 'header.ctp';
echo '<div id="feed">';
echo $this->Form->create(null, array('url' => array('controller' => 'users', 'action' => 'search')));
echo $this->Form->input('search');
echo $this->Form->end('Search');
echo 'search by name';
echo '</div>';
if(isset($search_result)){
echo '<div id="tweet_records">';
if(!empty($search_result)){
foreach ($search_result as $user) :
$formatted_text;
$latest_tweet_time;
$formatted_time;
if(!empty($user['Tweet'])){
$formatted_text=$this->Text->autoLinkUrls($user['Tweet']['0']['tweet']);
$latest_tweet_time=$user['Tweet']['0']['datetime'];
$formatted_time;
if(strlen($latest_tweet_time)!=0){
$formatted_time='Tweeted at '.$latest_tweet_time;
}
else{
$formatted_time='No tweets yet';
}
}
else if(empty($user['Tweet'])){
$formatted_text='No tweets yet';
$formatted_time='';
}
echo '<table id="t01">';
echo'<tr>';
echo '<td>'.
$user['User']['name'].'#'.$this->HTML->link($user['User']['username'], array('controller'=>'tweets','action'=>'profile',$user['User']['id'])).'<br>'.$formatted_text.' '.'<br>'.'<font color="blue">'.$formatted_time.'</font>';
echo '<div id="delete">';
$selector='true';
foreach ($user['Follower'] as $follower) :
if($follower['follower_user_id']==AuthComponent::user('id')){
$selector='false';
echo $this->Form->postlink('Unfollow',array('controller'=>'followers','action'=>'unfollow',$user['User']['id']),array('confirm'=>'Do you really want to unfollow this person?'));
}
endforeach;
if($selector=='true' & $user['User']['id']!=AuthComponent::user('id')){
echo $this->Form->postlink('Follow',array('controller'=>'followers','action'=>'follow',$user['User']['id']));
}
echo '</div>';
echo '</td>';
echo '</tr>';
endforeach;
echo'</table>';
echo 'Pages: ';
echo $this->Paginator->prev(
' < ',
array(),
null,
array('class' => 'prev disabled')
);
echo $this->Paginator->numbers();
echo $this->Paginator->next(
' > ' ,
array(),
null,
array('class' => 'next disabled')
);
}
else{
echo '<font color="red"> No results found </font>';
}
echo '</div>';
}
You are searching for results only on post request. But when you press the next button you are making a get request. So the first if statement doen't return true so you are not passing any data to your view.
Even if you remove the first if statement for post you'll still won't see any data because the next button doesn't post any data to make true the second statement.
PS:
On CakePHP include is not a preferable thing to do. Also you haven't mentioned the version you are using.
I am trying to make an auto complete function in CakePHP but did not succeed. I tried the following code.
public function find() {
if ($this->request->is('ajax')) {
$this->autoRender = false;
$country_name = $this->request->data['Country']['name'];
$results = $this->Country->find('all', array(
'conditions' => array('Country.name LIKE ' => '%' . $country_name . '%'),
'recursive' => -1
));
foreach($results as $result) {
echo $result['Country']['name'] . "\n";
}
echo json_encode($results);
}
}
// Form and jquery
<?php
echo $this->Form->create('Country', array('action' => 'find'));
echo $this->Form->input('name',array('id' => 'Autocomplete'));
echo $this->Form->submit();
echo $this->Form->end();
?>
<script type="text/javascript">
$(document).ready(function($){
$('#Autocomplete').autocomplete({
source:'/countries/find',
minLength:2
});
});
</script>
foreach($results as $result) {
echo $result['Country']['name'] . "\n";
}
Breaks your JSON structure.
Keep in mind that autocomplete by default expects "label" and value keys in your JSON table, so all the script should do after fetching DB records is:
$resultArr = array();
foreach($results as $result) {
$resultArr[] = array('label' =>$result['Country']['name'] , 'value' => $result['Country']['name'] );
}
echo json_encode($resultArr);
exit(); // may not be necessary, just make sure the view is not rendered
Also, I would create the URL to your datasource in the jQuery setup by
source:'<?=$this->Html->url(array("controller" => "countries","action"=> "find")); ?>',
And try to comment-out (just to make sure if the condition is not met by the request when autocomplete makes its call)
if ($this->request->is('ajax')) {
condition