Hi and I'm terribly sorry for asking such question.
I'm quite catching up on Laravel, maybe 30% smarter than before but I'm stuck on this kind of scenario.
How do I call a blade with all parameters passed to its controller without refreshing the page.
Here's my folder structure (I will not place all just controller, pages)
Folrder Structure
Now, I'm in let's say dashboard.blade.php
In the Side bar of my dashboard I want to go to a link
<li class="nav-item">
<a href="{{url('/get-greeting/{id}/{user}')}}" class="nav-link get-greeting">
<i class="nav-icon fas fa-table"></i>
<p>
Requested Son
<span class="right badge badge-danger side-span requested-span"></span>
</p>
</a>
</li>
Route will be
Route::any('/get-greeting/{id}/{user}', 'DefaultController#pullGreeting')->name('getgreeting');
Controller will be
public function pullGreeting($id, $user){
$userName = User::activeUser($user);
$defaultarray = array( 'greetingLogs' => recordLogs::pullgreetings($user) );
return view('pages/department1/greetings',$defaultarray);
}
When I click it, it refreshes the page. Anyone, can you help me and pinpoint where am I doing wrong so I can make it not refreshing the page but instead changing the blade dynamically?
Does this matter?
return view('pages/department1/greetings',$defaultarray);
or
return view('pages.department1.greetings',$defaultarray);
PHP is a server-side programming language therefore any changes to the view will require a full page refresh.
Without the use of Javascript, achieving a part page refresh would require the use of an old school technique called Frames.
You could redesign your application to render HTML via different endpoints and have a Javascript powered frontend that calls to those endpoints.
You could easily achieve through the use of JS Libraries such as React or JQuery.
Related
I'm building a website while also trying to keep changing code an easy task.
For that, I include the navbar with <?php require_once('./includes/navbar.php'); ?> so that I only need to change the navbar.php to see changes site-wide.
I also use Bootstrap 5 and let it take care of styling the currently active page by using <a class="active"> (and also using the recommended aria-current="page" attribute).
Now my problem is that I need to somehow dynamically apply the above mentioned class and tag to only the active page. I have no idea how to do that in an elegant way but thought that this might be possible in PHP?
Does somebody know how to do it?
Click here to see code of the navbar
I did some research and managed to fix my problem with a little bit of javascript code which you can just add with <script></script> tags anywhere in the html code:
Give every link in the navbar html a unique id (e.g. id="pricing").
Add class="active" to active page and Bootstrap.css will style that element accordingly.
Remove class="active" from the other pages/links which are not active.
Set aria-current="page" to active page and set aria-current="none" to inactive pages.
in navbar.php:
<a id="home" class="nav-link active" href="index.php">Home</a> <!-- 1. -->
<a id="pricing" class="nav-link" href="pricing.php">Pricing</a> <!-- 1. -->
in pricing.php:
<script>
var pricing = document.getElementById("pricing");
pricing.classList.add('active'); //2.
pricing.setAttribute("aria-current", "page"); //4.
var home = document.getElementById("home");
home.classList.remove('active'); //3.
home.setAttribute("aria-current","none"); //4.
</script>
Hope this may help someone else too.
I have a very specific problem with the layout
Iam using the PHP, MVC CodeIgniter framework
I have a home page layout where there is left navigation withe following links with fade-in and fade-out effect.
Iam loading all the data from a home controller for these menus. All the data is from a single table
except for current_affairs which Iam loading it as a separate partial or html
Followin is the code snippet I have.
<div class="parrent pull-left">
<ul class="nav nav-tabs nav-stacked">
<li class="active"><a href="#home" data-toggle="tab"class="analistic><a></li> </ul>
</div>
<div class="tab-pane fade" id="home1">
<p><?php echo $result -> home1?></p>
<p><?php =current_affairs ?></p>
</div>
Now Iam adding another new page which is the "local_news" page which will be from another method in the same controller
I need the same left navigation in here as well. But the body content is different here. In that case if have a separate view
for local_news and load both of them they are loading one after the other. which is not correct.
Is there a way to display local news just like home,about ie, within the nav
Any help on this would be highly appreciated. Thanks in advance.
I think you have already discovered that you are loading the same thing twice. In local news just load the content instead of the whole page. Moreover, Its not clear what you are doing in the controller. But "YES" there are many ways to solve the problem. The way to follow is depended on the way you are organizing your things.
I'm working on a scrollable one-page website, in which each div represents a different section. For example, url is dynaone.com/index.php, and when I click on the menu's different buttons, it changes to /index.php#s1, #s2, #s3, and so on. In section nº 2 I added another menu that works with php GET, in a way that when you click on one of the buttons, it redirects you to index.php?id=1 and so on. Of course when I first tried it, it scrolled me back to the top of the page, 'home', as I wasn't specifying the section. But when I did, assigning "index.php?id=1#s2" to the first element in the inner menu, it redirected me to section 1. It doesn't matter which number I write after "#s", it will keep putting section nº 1 on top of the page.
I'm using SMINT's demo as a base, adjusting it to my website's needs, but I couldn't find anything related to GET method issues on the plugin's page.
I would really appreciate some help with this, as it's very annoying having to go back everytime I click to correctly view section nº 2.
This is part of my inner menu:
<div id="botonera_productos"><ul>
<li> Quemadores </li>
<li> BCAAs</li> </ul> </div>
This is my php section:
if(#$_GET['id']==1) {
?>
<figure><img src="imagenes/quemadores/1.jpg"><figcaption> ULTIMATE L-CARNITINA 500 60 TABS - <b>$330</b> </figcaption></figure>
<?php } ?>
And this is the site's main menu:
<nav class="subMenu" >
<div class="inner">
Home
Nosotros
Productos
Local
Envíos
Consultas
Facebook
</div>
</nav>
Thank you.
The question isn't very clear but basically:
The anchor is an HTML construct. So once the page is already loaded, you can use #whatever to take you to wherever on that page that the anchor has been defined; ala <a id="whatever">
The PHP arguments, like ?id=1 - a new page will be loaded depending on what you use here.
You can use anchors in conjunction with a PHP page (ie. a PHP page may have multiple anchors) but you cannot load a new page with anchors alone.
Maybe Javascript will help you achieve what you want to do. With AJAX calls and the like then there are far less restrictions on how you load data.
Currently for my projects I create the navigation for each page manually and it looks something similar to:
<nav>
<ul id="mainMenu"><!--Main Menu-->
<li><a class="active" href="index.php">Home</a></li>
<li>Contact</li>
</ul>
</nav>
This works fine, however for projects that has many many pages it is not a really good practice or even efficient to do it manually. So I was wondering if there is anyone who can direct me to the right path and advice me on how to make my navigation dynamic? I know about PHP include and the .inc files - they are good. BUT I want to add class .active to the <a> of the page that is currently open. How can I do that?
BTW: I don't know if this s the right place to post this sort of questions here, but the moderator told me to post it here.
Use include to add a central php file, that contains a function which can take the current page as a parameter:
nav.inc:
function renderNavigation($current_page) {
//render you navigation
}
main.php:
require_once("nav.inc");
renderNavigation("Subpage 1")
i am new to codeigniter framework. i am having problem with href link. in my home page i have some menu, that goes to different page. for example in normal php if i want to go Sell Books page then i just put sellBook.php in href link. now in codeigniter how can i do this. do i need write something in controller ?
<li >Home </li>
<li>Buy Books </li>
<li>Sell Books </li>
<li>Books on Demand </li>
<li>Request a Book </li>
<li>About Us </li>
as per MVC pattern each of your url is a controller so:
if you, for example, want to link to
http://www.site.com/users
so the controller will look like:
class Users extends CI_Controller{
function index(){
//do somenthing here
}
function list(){
//list your users
}
}
then in your views linking to that controller is simple:
this will link to your users controller and index() method
this will link to your users controller and list() method
the site_url(); method will helps you finding the right link both if
you are using index.php or not in your urls
<a href="<?php echo base_url() ?>controller_name/function_name">
If you are using mod_rewrite to remove the index.php from the URL, you can write your URL as href="/sellbook" otherwise you will have to include it, such as href="/index.php/sellbook".
This assumes, of course, that you have a properly configured route name sellbook. See http://ellislab.com/codeigniter/user-guide/general/routing.html for details on how to achieve this.
If the links are to other pages on your site, then I think your best bet will be to use the url helper, assuming you're using the standard seo friendly format and not query strings. This way, you don't have to take into consideration whether you'll be using .htaccess or not. For example:
echo anchor('your_controller_name/your_function_name/param_1/param_2', 'Sell Books', 'title="Sell Books"');
// or for your home page
echo anchor('/', 'Home', "title='Home'");
If it's an external link, you can use the same function or just a plain html tag:
echo anchor('http://google.com', 'Google Me', 'title="Google Me"');
// OR
<a href="http://google.com" title="Google Me" >Google Me</a>
Note: make sure you load your url_helper in your controller if you'll be using the anchor function:
$this->load->helper('url')
// or add to autoload.php
You can also do it the way #sbaaaang suggested.
Account Details
You can use like this
<li>Home</li>
<li>Buy Books </li>
<li>Sell Books </li>
<li>Books on Demand </li>
<li>Request a Book </li>
<li>About Us </li>
//use
site_url in link with controller name and method
and
$this->load->view('page_name');