I have got a feature on my website called 'View friends' that displays a hidden div containing a users friends. The only problem so far is I would like it so that it would show 7 members on each row for 3 rows so a total of 21 members on each page. I know I will have to round up NumOfMembers/21 giving me the pages needed. I just need some advice in how I should set up the pagination from when thee SQL query gets the total amount of friends. Any ideas?
The SQL-query should use the limit and offset parameters for pagination, depending on the page n you are on, like this:
SELECT .... LIMIT 21 OFFSET n*21
When handling the results, simply use the modulo operator for determining the lines and rows your current result has to be put in:
// where $i is the result number
$row = $i % 7;
$line = $i % 3;
You have 2 options:
First you can load everything from the php in one query and put all users in an array(content), and just display in pages!
content = [];
max = 21;
function handlePaginationClick(page, pagination_container) {
$('#MyContentArea').empty();
for(var i=0;i<max;i++) {
if(null!=content[(page*max)+i]) $('#MyContentArea').append(content[(page*max)+i]);
}
return false;
}
$("#News-Pagination").pagination(content.length, {
items_per_page:max,
callback:handlePaginationClick
});
you can use Jquery Pagination: https://github.com/gbirke/jquery_pagination#readme
for that.
Another approach is still using jquery pagination, but not load everything at once! then you must have same ajax call in the method 'handlePaginationClick' to pull all page information.
Related
Actually, cities are stored multiple values(with ids)
Example
(3,5)chennai,bangalore in one table.
How to get city names with the separated comma in views page.
controller code
$data['jobCityName'] =explode(',',$viewData['jobCity']);
for($i= 0; $i < sizeof($data['jobCityName']); $i++) {
$jobMultipleCity= $data['jobCityName'][$i];
$data['jobCityNames']=$this->hrm_model->getCitybyId($jobMultipleCity);
$data['jobCity']=$data['jobCityNames']['cityName'];
views Page Code
<?php echo $jobcity; ?>
present printed only one city name.
how to display cities in views page like(hyderabad,chennai,bangalore)
You can use foreach for iteration. Also you are overwriting previous value at $data['jobCity']=$data['jobCityNames']['cityName'];
like
$data['jobCityName'] =explode(',',$viewData['jobCity']);
foreach($data['jobCityName'] as $cityid) {
$data['jobCityNames']=$this->hrm_model->getCitybyId($cityid);
$data['jobCity'].=$data['jobCityNames']['cityName'];
}
Also it's recommended to use count instead of sizeof
I've got the data from nusoap webservice and put it in the table. Here is the code function:
`$filter='';
$order='';
$limit='10';
$offset='';
$result=$proxy->GetRecordset($token,$table,$filter,$order,$limit,$offset);
`
After parsing the json, here is the result:
table with total data
Now the problem is I don't know how to split the data into pagination. The total data is 251 data (country ID with name). I change offset to 10 manually from the code and it give the result from 11-20.
I stuck with this code (don't know what to do):
if(isset($_GET['page']))
{
$pageNum = $_GET['page'];
}
$total_page=ceil($total_data/$limit);
$self=$_SERVER['PHP_SELF'];
echo "<ul class='pagination'>";
for ($i=1;$i<=$total_page;$i++){
echo "<li>$i</li>";
}
echo "</ul>";
Here is the result:
Table with link pagination
But when I click page number 2,3,...etc it still the same data. How I should do when I click the page number e.g 2 it will refresh the page and change the offset to 10 and so on. Please help. Thanks.
I want to display <li> where, I am having issue with my loop, see my code below
<?php
for ($j=0;$j<$task_count;$j++)
{
$task_name = $task[$j]['summary'];
$summary_length = strlen('');
$task_id = $task[$j]['_id'];
$task_status = $task[$j]['status'];
$summary_count = strlen($task_name);
if ($task_name=='task') {
$final_task_summary = $task_id ;
}
elseif($summary_count <= 50)
{
$final_task_summary = $task_name;
}
else
{
$final_task_summary = mb_substr($task_name, 0, 50);
}
?>
Here, I want to display <li> </li> in order where first it shows <li> having status "open" then "resolved" and then "close" and only 20 <li> should take place.
If I understand correctly, you want to:
Print list items for the first 20 items.
Printing should be done ordered based on status.
If you're getting the data from a database, then it would be better to handle this in the query, If not, you can do as follows.
Change your loop to get only 20 items instead of task count
Declare three strings to hold li items for each status.
Within the loop, Check for the status and append the desired string with li items.
After the loop, print the three strings in the required order.
Let me know if anything needs clarification.
Thanks,
I have a CodeIgniter PHP application that shows two movie covers. Beside them is a "random movie" button that uses AJAX to replace the two movies with a new set of movies. You can continue to click this, over and over, and see it continue to replace the images of the movie covers. The first two covers to show are set as the defaults, and they should never show after the user has clicked the random movie button. The problem is this: When clicking the random movie button, it will some times take many clicks to finally show a new cover. That is, the same cover will be returned multiple times in a row. The two different covers being fetched are being called from slightly different URLs, so they will rarely both break at the same time. This lets me know that it is refreshing, but that the function is returning the same movie multiple times. If I access the url that is being called via AJAX directly, I never see this take place since I have used the Session class to store the last movie's and exclude it from the SQL query (i.e. WHERE id NOT IN ($default_movie, $last_movie)). Any idea why accessing the url directly would work fine, but when calling via AJAX, I'm seeing this behavior?
I know this may not have been as clear as possible, so let me know if I can clarify something that doesn't make sense. I'll add code if that helps as well. Thanks friends!
Query to get random movie:
SELECT * FROM (`movies`) WHERE `id` NOT IN (2, 10) ORDER BY RAND() LIMIT 1
Model method:
public function getRandom($count = 1, $featured = FALSE, $series = FALSE, $exclude = 0, $last = 0) {
$this->db->order_by('id', 'random');
$this->db->limit(1);
$conditions = array();
if ($exclude > 0) {
$conditions['id !='] = $exclude;
}
if ($last > 0) {
if (!empty($conditions['id !='])) {
$conditionsNotIn = "id NOT IN (" . $conditions['id !=']. ", $last)";
unset($conditions['id !=']);
$this->db->where($conditionsNotIn);
} else {
$conditions['id !='] = $last;
}
}
if ($featured) {
$conditions['featured'] = 1;
}
if ($series) {
$conditions['current_series'] = 1;
}
$movie = $this->db->get_where('movies', $conditions);
$movie = $movie->row();
if (!is_null($movie)) {
return $movie;
} else {
return FALSE;
}
}
Any idea why accessing the url directly would work fine, but when
calling via AJAX, I'm seeing this behavior?
I have an idea yes.
Browser caching.. PITA!
Try turning off caching explicitly:
$.ajaxSetup({cache: false});
Put that before your ajax request, assuming you're using jQuery.
If you're not you need to append some random variable to the url, this keep the browser from caching the requests.
I have a form with 10 fields of photo upload. What I want to do is to limit 10 photo upload. Ususally after you upload 10 photos and after that the the 10 fields will appear again. But this time I want to make it limitable. For example, there are 10 upload fields and after you upload 5 photos, there will be only 5 fields left. If you delete a photo then 6 fields will apear there. For now, I'm using select count(*) to count number of rows in MySql table and I'm using :
if ($rownumber == 4) {
"show 6 fields"
}
Are there any other method to do this? The method that I'm using now are so complicated and uses codeas a lot.
Why lots of code?
$maxuploadslots = 10;
for($i=0; $i<$maxuploadslots; $i++)
{
if($i < $rownumber)
continue;
echo "<input...."; // I believe you add them this way, right?
// that is "ADD 1 FIELD"
}
the for loop will run ten times, and everytime it checks against the uploaded images count if there i is smaller the rownumber it'll NOT add a new row, else it will.
For names you can concatenate a variable or you can take input for names as an array .