I'm not great with PHP, but I've got my project working pretty much as expected. My last piece is getting pagination to work correctly. By correctly, I mean retaining the submitted form values past page 1. I looked into Sessions and it seems like overkill as I'm already passing the results with GET.
Again, the pagination itself works, it just doesn't retain the form values. As I'm working with the query on the same page, I figured I could use $_SERVER['REQUEST_URI'], but for some reason when this is in the href of the pagination, it either breaks the page and nothing is displayed, or generates a URL that doesn't resolve correctly—
myurl.com/$_SERVER['REQUEST_URI']?p=2—as opposed to actually having values from GET.
Thoughts on this? I think I'm probably missing a character or brackets or something (again, PHP isn't my forte).
Thanks for any help!
function pagination($page,$num_page)
{
echo'<ul style="list-style-type:none;">';
for($i=1;$i<=$num_page;$i++)
{
if($i==$page)
{
echo'<li style="float:left;padding:5px;">'.$i.'</li>';
}
else
{
echo('<li style="float:left;padding:5px;">'.$i.'</li>');
}
}
echo'</ul>';
}
if($num_page>1)
{
pagination($page,$num_page);
}
Related
I am having an issue using isset to display content on a page.
My PHP file is called messages.php
I am directing my users with links to this URL: messages.php?inbox using if(isset($_GET['inbox']))
{ } to display the users inbox. Same principle with the other users options such as compose message is: messages.php?compose again using isset
The only problem I have is that I cannot stop people from manually typing stuff like domain.com/messages.php or domain.com/messages.php?somethingrandom.
Is there a way to direct users to messages.php?inbox when they type in the address bar something that isnt assigned to isset?
I did try to use switch but couldnt seem to get it to work properly with how ive laid out my HTML.
An example of the whole file is here http://pastebin.com/SfqN2L7g
I am fairly new to PHP and think I may have gone down the complicated route.
Any advice would be appreciated.
Thanks
The answer you added already would work, but I usually like having an array of valid options which I could maybe check against later on.
$validPages = array('inbox', 'compose');
$pageFound = false;
foreach ($validPages as $validPage) {
if (isset($_GET[$validPage])) {
$pageFound = true;
break;
}
}
if (! $pageFound) {
header('Location: /messages.php?inbox');
}
Thanks to the help of Marcos Pérez Gude, the answer is as follows:
if(isset($_GET['inbox']) || isset($_GET['compose'])){
//Then do below
}else{
header("Location: messages.php?inbox");
exit;
}
I'm trying to get my different search filters to pop up on different pages. I have an example here:
<?php
if(basename($_SERVER['REQUEST_URI']) == 'bedrijfsaanbod') {
include_once(VIEW_PATH . '/includes/filter.bedrijfsaanbod.php');
} else {
include_once(VIEW_PATH . '/includes/filter.makelaars.php');
}
?>
That is what I came up with so far. The problem however is that my URL changes as soon as the search gets conducted, which means it'll automatically show the /includes/filter.makelaars.php again. Any tips on how to tackle this problem?
I’ve been trying to figure out how to use those flashdatas.
I remember having difficulties last time, and this time again, it seems that I forget something.
So basically, I’m trying to set up a flasherror somewhere :
if(!$this->paypal_pro->APICallSuccessful($PayPalResult['ACK']))
{
$this->session->set_flashdata('flashError',
array('Errors'=>$PayPalResult['ERRORS']));
redirect('main/form');
}
And in my main/form I got :
function Form()
{
// Process validation form
if ($this->form_validation->run() == FALSE)
{
//IF the validation process hasn't been run or there are validation errors
$this->parser->parse('template/template', $data);
}
And in that view, I’m trying to get that flashError :
<?php if($this->session->flashdata('flashError')):?>
<div class='flashError'>
<?php
$flashError=$this->session->flashdata('flashError');
foreach( $flashError['Errors'] as $Error) {
echo $Error['L_SHORTMESSAGE'].' ('.$Error['L_ERRORCODE'].'):';
echo '<br/>';
echo $Error['L_LONGMESSAGE'];
}
?>
</div>
<?php endif?>
I don’t have anything in that variable, and when I try to var_dump it, It returns me false.
Can someone explain me how to use it despite the official documentation saying “will only be available for the next server request, and are then automatically cleared”
From Codeigniters documentation:
If you find that you need to preserve a flashdata variable through an
additional request, you can do so using the keep_flashdata() function.
$this->session->keep_flashdata('item');
UPDATE:
Problem seems to be here:
$this->session->set_flashdata('flashError',
array('Errors'=>$PayPalResult['ERRORS']));
Try this one:
$this->session->set_flashdata(array('Errors'=>$PayPalResult['ERRORS']));
As you are doing
if($this->session->flashdata('flashError'))
You are actually removing the flashError item, as it has been read.
What you need to do, is as you have a little further down, assign it to a variable and then do your checks.
I have a relatively simple class which deletes a post:
function delete_post($postid, $reason){
//Stuff to delete post
$this->delete_response = 'Thanks, your course has been removed.';
}
This function is called at the top of a page with a form on. If the form is submitted, the same page checks the POST[] and carries out the function, like so:
if(!empty($_POST['removecourse'])){
$courseManager->delete_post($_POST['courseid'], $_POST['cancel-reason']);
echo $courseManager->delete_response;
};
So my problem is... when I refresh the page, the message keeps displaying. I know this is because I am re-submitting the form, and because there is no such P/R/G pattern going on, but as i am new to OOP, im wondering if im doing this the right way, or if anyone could suggest a way similar to PRG or something?
Add an if that test if somthing changed, like mysql_affected_rows
function delete_post($postid, $reason)
{
//Stuff to delete post
if(mysql_affected_rows())
{
$this->delete_response = 'Thanks, your course has been removed.';
}
}
I'm working on a page where I've listed some entries from a database. Although, because the width of the page is too small to fit more on it (I'm one of those people that wants it to look good on all resolutions), I'm basically only going to be able to fit one row of text on the main page.
So, I've thought of one simple idea - which is to link these database entries to a new page which would contain the information about an entry. The problem is that I actually don't know how to go about doing this. What I can't figure out is how I use the PHP code to link to a new page without using any new documents, but rather just gets information from the database onto a new page. This is probably really basic stuff, but I really can't figure this out. And my explanation was probably a bit complicated.
Here is an example of what I basically want to accomplish:
http://vgmdb.net/db/collection.php?do=browse<r=A&field=&perpage=30
They are not using new documents for every user, they are taking it from the database. Which is exactly what I want to do. Again, this is probably a really simple process, but I'm so new to SQL and PHP coding, so go easy on me, heh.
Thanks!
<?php
// if it is a user page requested
if ($_GET['page'] == 'user') {
if (isset($_GET['id']) && is_numeric($_GET['id'])) {
// db call to display user WHERE id = $_GET['id']
$t = mysql_fetch_assoc( SELECT_QUERY );
echo '<h1>' . $t['title'] . '</h1>';
echo '<p>' . $t['text'] . '</p>';
} else {
echo "There isn't such a user".
}
}
// normal page logic goes here
else {
// list entries with links to them
while ($t = mysql_fetch_assoc( SELECT_QUERY )) {
echo '<a href="/index.php?page=user&id='. $t['id'] .'">';
echo $t['title'] . '</a><br />';
}
}
?>
And your links should look like: /index.php?page=user&id=56
Note: You can place your whole user page logic into a new file, like user.php, and include it from the index.php, if it turns out that it it a user page request.
Nisto, it sounds like you have some PHP output issues to contend with first. But the link you included had some code in addition to just a query that allows it to be sorted alphabetically, etc.
This could help you accomplish that task:
www.datatables.net
In a nutshell, you use PHP to dynamically build a table in proper table format. Then you apply datatables via Jquery which will automatically style, sort, filter, and order the table according to the instructions you give it. That's how they get so much data into the screen and page it without reloading the page.
Good luck.
Are you referring to creating pagination links? E.g.:
If so, then try Pagination - what it is and how to do it for a good walkthrough of how to paginate database table rows using PHP.