PHP Search Filter on different pages - php

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?

Related

At what point should code stop being separated in the MVC pattern?

This question is a bit theoretical. I'm building a web application using PHP and the MVC pattern. My question is : where should I stop separating the view and the controller ?
Let me illustrate this question with an example :
Let's say I want to build a system that displays the number of unread messages, if there are any. I will have 3 files : a view, a controller and a model (to make it simple, let's call them view.php, controller.php and model.php).
model.php is gonna get the number of unread messages from the database.
controller.php will ask and gather the information.
view.php will display this number to the user.
But if there are no unread messages, I don't want to display the number 0. So in which file should I add a condition that verifies if the number should be displayed or not ? There are two possibilities :
In view.php, add the following condition :
_
if ($unread_messages > 0) {
echo "<p>There are $unread_messages unread messages</p>";
}
In controller.php, add the following function :
_
public function countUnread() {
... // Got the data from the model
if ($unread_messages > 0) {
return "<p>There are $unread_messages unread messages</p>";
}
}
The problem with the first one is that view.php is only meant to display information, there shouldn't be any php code, except for retrieving information.
The problem with the second one is that the controller should only get the information, it shouldn't store any sentence or phrase : that's the view's job.
What should I do in this case ? What solutions do you think suit this problem ?
Thank you.
The problem with the first one is that view.php is only meant to display information, there shouldn't be any php code, except for retrieving information.
I don't believe this is correct. There is nothing wrong with putting logic in the View when it is directly related to what should be displayed. Based on your example:
if ($unread_messages > 0) {
echo "<p>There are $unread_messages unread messages</p>";
} else {
echo "<p>You have $unread_messages unread messages</p>";
}
This would be perfectly acceptable in the View, because you are determining what to display based on the information received from the Model and/or Controller. Even template engines use basic control structures like this.
Edit:
Here is a similar example using Laravel:
<?php $count = Auth::user()->newThreadsCount(); ?>
#if($count > 0)
<span class="label label-danger">{!! $count !!}</span>
#endif

Using isset to display page content

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

Paginating MySQL Results with User Values from HTML Form

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);
}

Showing a href without needing to input text(Wordpress and html issue)

I'm on wordpress atm and I'm stuck with a small issue.
To save the people receiving the site some work I hoped i'd be able to make things as easy as possible.
So what I did was add a plugin called "advanced custom fields", and I made a custom field.
What I wanted was to have 1 custom field that'd show a link(as text) in the following way:
The issue
My issue is that I want the link(example) to show without needing to fill in "The issue", because in the backend(the wordpress cms) it's a lot of trouble for the person to fill in an extra sub field called name with the exact same value.
So my question is, is it possible to show a link without needing to fill in "The Issue" or is there a different html tag for this?
Thanks in advance!
You can check if the field "The Issue" is empty and only in this case reprint in this place the "example" field.
if (!empty(the_field('The issue'))) {
<?php the_field('The issue'); ?>
} else {
<?php the_field('example'); ?>
}
If you need to ensure that a link input from the user is external (that always begins with http://) you can do something like this (of course, before printing the $link variable in the href):
if (strpos($x, "http://") == 0) {
$link = $x;
} else {
$link = "http://" . $x;
}
strpos search the position of the first occurrence of a substring on your string ($x, for example). So if the position is 0 means that the string begins with "http://", so it is external, but if this is not the case you have to add it at the beggining of the string.
One have to note that user inputs are tricky (imagine that the user writes " http: //..." with a space before the url) so it is possible that one needs more validation than this, but I think that the idea is clear.

Fetch database information on a new page without using new documents

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&ltr=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.

Categories