CakePHP pagination not working beyond 1st page - php

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.

Related

CodeIgniter using if to show some table

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>';
}
}
?>

PHP echo last Array values

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>";
}
}
?>

Retrieve Data from Database using Shortcode

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.

How to make auto complete form in cakephp?

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

Cakephp generated form not submitting all data

I have a view called Prices.ctp with the following code. It creates a form which echo's all the data from a variable called $products which contains all the data from a table called Products.
<?php echo $this->Form->create('Product', array('action' => 'changePrice')); ?>
<fieldset>
<h3>Products</h3>
<?php
foreach($products as $k=>$v){
echo $this->Form->hidden('id', array('value'=> $v["Product"]['id']));
echo $this->Form->input('name', array('value' => $v["Product"]["name"] ));
echo $this->Form->hidden('slug', array('value'=>$v["Product"]['slug']));
echo $this->Form->hidden('description', array('value'=>$v["Product"]['description']));
echo $this->Form->hidden('cateID', array('value'=>$v["Product"]['cateID']));
echo $this->Form->input('price', array('value' => $v["Product"]['price']));
echo $this->Form->hidden('photo', array('value'=>$v["Product"]['photo']));
echo $this->Form->hidden('photo_dir', array('value'=>$v["Product"]['photo_dir']));
echo $this->Form->hidden('active', array('value'=>$v["Product"]['active']));
echo $this->Form->hidden('views', array('value'=>$v["Product"]['views']));
echo $this->Form->hidden('created', array('value'=>$v["Product"]['created']));
echo $this->Form->hidden('modified', array('value'=>$v["Product"]['modified']));
}?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
It shoots to this controller method, called changePrice
public function changePrice(){
$this->Product->saveMany($this->request->data['Product']);
$this->Session->setFlash( "Prices Saved.");
$this->redirect ( "/admin/products/" );
return;
}
However when I used debug() to check the contents of of $this->request->data it shows that only the final iteration of the foreach loop in the view is being sent.
To re-word, if the original $products variable (passed into the view prices.ctp) has 4 products: product1, product2, product3, and product4, all with their own data from the Product table (id, name, etc), when the submit button is pressed on the page, only product4's variables will be passed into $this->request->data.
Why is this happening?
Cheers
you can do like this to get the data of all products
<?php
foreach($products as $k=>$v){
echo $this->Form->hidden("Product.{$k}.id", array('value'=> $v["Product"]['id']));
echo $this->Form->input("Product.{$k}.name", array('value' => $v["Product"]["name"] ));
echo $this->Form->hidden("Product.{$k}.slug", array('value'=>$v["Product"]['slug']));
echo $this->Form->hidden("Product.{$k}.description", array('value'=>$v["Product"]['description']));
echo $this->Form->hidden("Product.{$k}.cateID", array('value'=>$v["Product"]['cateID']));
echo $this->Form->input("Product.{$k}.price", array('value' => $v["Product"]['price']));
echo $this->Form->hidden("Product.{$k}.photo", array('value'=>$v["Product"]['photo']));
echo $this->Form->hidden("Product.{$k}.photo_dir", array('value'=>$v["Product"]['photo_dir']));
echo $this->Form->hidden("Product.{$k}.active", array('value'=>$v["Product"]['active']));
echo $this->Form->hidden("Product.{$k}.views", array('value'=>$v["Product"]['views']));
echo $this->Form->hidden("Product.{$k}.created", array('value'=>$v["Product"]['created']));
echo $this->Form->hidden("Product.{$k}.modified", array('value'=>$v["Product"]['modified']));
}
?>

Categories