ShowMore Button for PHP-mySQL generated table - php

<?php
$nrows = mysqli_num_rows($result);
for (;$i<$nrows;)
{
echo"<tr>";
for ($j=0;$j<10&&$i<=$nrows;$j++)
{
$n = $i;
$i=$i + 1;
$k=$n%30;
$row = mysqli_fetch_assoc($result);
extract($row);
echo "<td><table width='100%' id=$Code>
<tr><td>$Code</td></tr>
<tr><td>$Name</td></tr>
</table></td>";
if ($k==0)break 2;
}
echo"</tr>";
}
?>
This is simple code for a sample table that i am working on. It being called in external file with $nrows and $i defined produces 10*3 table of 30 results. And called twice 60 results and so on.
I wish to add a showmore button to call this file. I worked around with div and ajax
I got pagination script and tried to work with it and added this button to it. and tried to get next page with ajax.
if ($currentpage != $totalpages) {
echo"<p><div id='more'><input name='ShowMore' type='button' id='$nextpage' onclick='showmore(this.id)' value='ShowMore' /></div></p>";}
The problem with this button is that it work good just First page is there while after that for every click next page appears but in place of other page.
I know problem is with div id which is constant. Is there any way to change it everytime one click on showmore.
I am also this thinking to work with mysql limit but unable to find a way to pass it to next table.
Any sort of help is welcome.
Thanx in Advance.

Could you make a javascript onClick function that alters the div's id and put it on the show more link?

well i finally devised a crude method. though I would not recommend anyone else to use it but it works perfectly. I have defined two different variable one in AJAX call and another in php script. and I increase them by one on each call. just because of simple mathematics of 1=1 and 2=2. It works perfectly. I am still looking for refined answer.

Related

Is this a jQuery or PHP issue

I have created a jsfiddle for the following problem. I have tried to recreate it but it is hard because the data is being displayed server-side with PHP from a MySQL database.
I have a table and I am trying to pass the clicked <td> content into my modal box. If it doesn't have any, the modal box should appear with the user id but the input boxes are empty.
For some reason, the user id changes fine but the start time and end time inputs don't! Also, when I inspect the element it displays 8 - 5 for all the users? Does anyone have any idea why this is happening?
php code with the click + data params
for($dow = 0; $dow <= 6; $dow++) {
?><td class="js-clickable" data-user_id="<?php echo $user_id; ?>" data-start_time="<?php echo $row['st']; ?>" data-end_time="<?php echo $row['et']; ?>"<?php
if (array_key_exists($dow, $list)) {
foreach ($list[$dow] as $row) {
if ($row['user_id'] == $user_id) {
echo date("H:i", strtotime($row['st'])) . " - " . date("H:i", strtotime($row['et']));
The problem is with your PHP code as the HTML markup shown in the fiddle clearly shows that the data-end_time and data-start_time for Michael is shown as data-end_time="2015-03-27 17:30:00" data-start_time="2015-03-27 08:30:00".
There might be some error in the values fetched from the database. Make sure that you retrieve the values from the database correctly.
The jquery that you have written works perfectly in this case.
This is a PHP issue, because it was written at the PHP.
Now why it happens like that may be at date("H:i", strtotime($row['start_time'])) and I think the expected value($row['start_time'] and $row['end_time']) don't return a expected string to the strtotime function.
Check your code at this block and check how it is returned.
if ($row['user_id'] == $user_id) {
echo date("H:i", strtotime($row['start_time'])) . " - " . date("H:i", strtotime($row['end_time']));
}
I hope I've helped ya.
I've identified the issues with your PHP code that caused the data to be incorrectly populated in the table.
The main issue was to do with bad logic in the section that writes the tags. For example, the $row variable used to write the data attributes was entirely different from the $row variable used to write the contents of the TD. Also, you were writing the data attributes of the first TD tag before even looking for the schedule for that user. I revamped the code in that section so that the full TD tag is written in two areas, either with scheduled data, or without. The code is still pretty messy, but at least now it works. Also, on line 57 of the code in the pastebin you posted had a misplaced quote between the php closing tag (?>). It was: ?"> It should be: ?>"
A couple of smaller observations:
- Bootstrap CSS was linked twice (one local, one remote). Not part of the issue, just an observation.
- The closing tag for the form and the surrounding div were in the wrong order. Close the form before the div.
Here is the working code to correctly populate the data attributes and content of the table:
// Code removed at request of OP.

Load more rows from database on click without page refresh

I am trying to have my loop stop at 5 rows by default, but when the button is clicked it would load more. Are there any simple solutions for this? I don't want the page to refresh either.
My code below:
<?php
$count = 0;
while (($row = mysql_fetch_assoc($thequery))) {
echo $row['title'];
$count+=1;
if($count%5==0){
break 1;
}
}
?>
<span onClick="loadmore_somehow">Load More</span>
An AJAX call would do that. Using Javascript (and preferably a library like jQuery) you would make a call to your script and get more rows, pass them back, and then use Javascript to draw the rows. This does not reload the page.
http://api.jquery.com/category/ajax/
I think you need to know more about Javascript Ajax, It will help you do update,retrieve the content without refreshing page.
Check out this tutorial http://www.developphp.com/view.php?tid=1350

Counting paragraphs with pagination

I’ve just started learning PHP and have attempting to build my first data driven web page to aid with my learning but I now seem to have come across a problem that's been driving me nuts.
Here is a bit of background.
What I have been attempting to do is pull content from a database, this content is already marked up with html paragraph tags. The content will be user submitted and so I have no way of knowing how many paragraphs each row will contain.
I am then looping through the content and counting the paragraphs so that I end up with 5 paragraphs inside each containing div.
Here is the code I am using to do this, I know my code is a bit basic but it seems to work fine as long as all of the content is retrieved in just a single query.
$stmt = $db->prepare('SELECT * FROM content');
$stmt->execute();
foreach($stmt as $row) {
$allParagraphs .= $row['content'];
}
$paragraphArray = explode("</p>", $allParagraphs);
$paragraphCount = 0;
echo '<div class="bookPage">';
foreach ($paragraphArray as $value) {
echo $value;
$paragraphCount = $paragraphCount + 1;
if ($paragraphCount == 5){
echo '</div><div class="bookPage">';
$paragraphCount = 0;
}
}
echo '</div>';
And now my problem.
Now I would like to include pagination with an endless scroll effect using PHP and Jquery. I feel confident enough to do this; however, I have noticed that the current code I am using (to count every 5 paragraphs) is not going to work when I introduce pagination.
As far as I can see, after working through the code what is going to happen is that the closing div is going to be inserted and then, when the 2nd query made via ajax (read more link clicked) it is going to result in a new opening div. This results in the leftover paragraphs from each query being surrounded by the div tag and not containing the 5 paragraphs I need.
Basically, in its simplest terms, what I would like to do is to still be able to wrap div tags around every 5 paragraphs but also still be able to paginate the results via ajax.
If anyone could advise me of the best way to go with this it would be very much appreciated.
My musings on this issue so far...
I am not sure if this is even possible to do which leads me to believe I have approached this the wrong way. I’m not very experienced at the moment but I can’t help think that it would be easier to store every paragraph in a separate row in the database which would allow me to then pull them out in multiples of 5. But then, something else is telling me that would not be a practical solution at all, especially as I have no way of knowing how many paragraphs each user would be writing/submitting.
I dont have enough points to post an image so here is a link to an image i uploaded in attempt to demonstrate what i mean should my attempt at describing the issue not be enough.
Diagram showing issue
Thank you in advance of any suggestions.
I guess you could remove all the HTML tags in your PHP script and just return the content of the paragraphs as JSON object, for example.
So, basically, you'd return something like:
echo json_encode($paragraphArray);
And request that with jQuery like this (which is an adoption of the first example at http://api.jquery.com/jQuery.getJSON/):
jQuery.getJSON('yourURL.php', function(data) {
var items = [];
jQuery.each(data, function(key, val) {
items.push('<p>' + val + '</p>');
if(items.length == 5) {
appendItemsToBody(items);
items = [];
}
});
appendItemsToBody(items);
});
function appendItemsToBody(items) {
if(items.length) {
jQuery('<div/>', {
'class': bookPage,
html: items.join('')
}).appendTo('body');
}
}
Depending on what should happen when clicking on "load more", you could pass the count of currently displayed items as data to the call to your PHP script via jQuery.getJSON() (see their API documentation) and then decide in your PHP script which paragraphs you should return (i.e. where to start from). You'd have to append the "rest" of the paragraphs to fill up the 5 items to the existing last div in jQuery, and put the rest into new divs.

Ajax paginate sql query

I am working on a website that needs to paginate sql database results. The idea is like this, a user can click on a submit button with each US state's code to see all of the comments left by users from that state. For example clicking NY should display all people's comments from NY and clicking TX should display all of TX and so on. Displaying the results in one big list works but I want to paginate those results now. I am currently using this code located in its own paginate.php file which needs to be re-updated each time a new state is shown:
<?php
...
$rs_result = mysql_query($sql);
$row = mysql_fetch_row($rs_result);
$total_records = $row[0];
$total_pages = ceil($total_records / 10);
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='?page=".$i."'>".$i."</a> ";
};
?>
The code itself doesn't exactly work because it makes the page refresh. Doing that loses the memory of which state was previously clicked on. How can I change this to work with Ajax so I don't need to refresh the page. If you need more information let me know.
You need to wrap the results in a DIV that has an assigned ID and change the URL's to point a javascript function (I would recommend jQuery). The function has to get the data that will be displayed in that div.
eg:
<div id="results">
</div>
<?php
for ($i=1; $i<=$total_pages; $i++) {
echo "".$i." ";
};
?>
// and script
// the script asks result.php for page and returns html result.
<script type="text/javascript">
function getPage(page){
$("#results").load("result.php", { page: page } );
}
</script>
What you would do is make a AJAX call using the same thing you have in your href attribute. After the response take the data and place it into an html element on your page. Check out jQuery's load method.
Oh ya, just to keep our little movement going. You should try to stop using mysql_query() and start working with mysqli instead. mysql is deprecated and it is no longer being supported. :/

Swap images from PHP array with Ajax and also get ID

On my website an user is able to fill in an url. When he fills in the url, he gets all the images src's from that url. I push these src's to an array in php:
array_push($goodfiles,$pic);
Now the user will be able to choose on of the pictures (with a next or prev button) and then save it to the database. The picture that's saved is based on the id of the image in the array. So $goodfiles['0'] means id = "0" and so on.
I want the swapping of the images to work with ajax, so that the pages doesn't have to refresh all the time when clicking the next or previous button. And then when I save the form, I want to know the id of the current image, so that I can save it to the database.
How do I realize this with Ajax (jquery)?
Edit:
This is how I do it right now:
$current_id = $_GET['id'];
if(empty($_GET['id']) || !empty($empty)) { $current_id = 0; }
$prev_id = $_GET['id'] - 1;
if($prev_id < 0){ $prev_id = 0;}
$next_id = $_GET['id'] + 1;
if($next_id > $_SESSION['count']-1 && $_SESSION['count'] != 'empty') { $next_id = $_SESSION['count']-1;}
This is the code for the pagination
And this is the pagination:
<div id="url_pic">
<img src="<?=$_SESSION['pictures'][$current_id]?>" class="img_load"><br>
<? if($_SESSION['count'] > 1) { ?><center><img src="img/add/left.png"> <img src="img/add/right.png"></center> <? } ?>
</div>
So right now my solution doesn't contain any javascript, but it's all php coded. And the page refreshes everytime you want to see the next picture. I want to solve this in ajax, so that you can paginate through the images without a refresh. The way I want it is like this link:
http://d-scribe.de/webtools/jquery-pagination/demo/demo_options.htm
But except for the text, I want to paginate through images.
You probably don't need to use AJAX for this. Simply return a html file containing a JavaScript array, which contains all those image URLs and do the other stuff using JavaScript.
Get back to StackOverflow in case you've a more precise question and hopefully some code, which we can help on ;)
Load a script at the bottom of your php page the user side of the PHP where all your HTML is, above the closing body tag thats something loosely similar to this
<script type="text/javascript">
var myArray = <?php echo json_encode($myPHParray); ?>
</script>
this way when your page loads out it renders with a dynamic javascript json object as a variable that you can work with client side, this removes the need for an AJAX request all together unless your doing stuff with the data your playing with. From first glance Im guessing not really per say. But yea, at the very least its one less transaction to be made when the page is loading.
edit just noticed someone said similar while I was typing out.. Lars.. so I guess this is a follow up to his answer :-D

Categories