By default the second query $content is executed, which is working fine. This content should be replaced with data from the query $menukaart when one of the links is clicked but it is not it is just giving me the empty div kaart. Does anyone see what is wrong?
Thank you!
The databse table is there and has data in it as you will see in this example page, because the navigation on the left is coming from the same table. But I don’t get any results. I even tried to set the varable $menu_id manually at the top of my PHP but still no result
One of the possible causes of ending up with blank data when the page is displayed in your browser is making sure the field you are specifying in the $row[‘field’] is included from your query. As it turns out, what you specify in $row IS case sensitive. I think the following line in your code may be causing the issue:
Edit:
I think it has to do with the way I am using PDO! Because for some other websites with a similar functionality I used mysqli and they are all working fine
Was the PHP code in your first post from the file modules/site/menu_items.php? If so, is that the entire file, because you’re not creating a DB connection anywhere?
Your JS is making a new request to menu_items.php, which means that the script in that file doesn’t have access to any variables or data from the current request. So yes, you would need to include your DB connection file within menu_items.php.
If you’re also including menu_items.php from fanskitchen-menukaart.php, make sure you use [fphp]include_once[/fphp] when including the DB connection script to avoid PHP trying to include the file twice.
A common way of dealing with this issue is to use the Front Controller pattern. This is where you set up your server to direct all page requests through a single script (often index.php) which loads all common resources (e.g. a DB connection) and then loads and displays the correct template based on the URL.
Most PHP frameworks use this pattern, and there are several good, light-weight frameworks out there that you can start with to ‘dip your toe in the water’ if you’ve not used a framework before, such as Slim.
A (very basic) example of using Slim might look like this:
$app = new \\Slim\\Slim;
$app->db = new PDO ('mysql:host=localhost;dbname=mydb', 'myuser', 'mypass');
// www.mydomain.com
$app->get('/', function(){
echo "Home Page";
});
// www.mydomain.com/testPage
$app->get('/testPage', function() use ($app) {
$conn = $app->db; // Get DB connection
$app->render('testpage.php');
});
$app->run();
Notice that the PDO connection is set up and available to all routes (pages) on the site.
You’d include the framework files into your index.php file, and then set up your routes like in the example above.
If you downloaded the zip file from the website, it includes a basic example ready to go. It’s probably easier to look through that, and have a read of the documentation on the site and see if you think you want to go down that route. You can always start another thread if you have questions, and I or someone else can help you out.
It can be a bit of a learning curve if you’ve not used a framework before, but for anything above the level of simple scripts it really does save you time and make things easier.
Hi fretburner. Apparently I have the same problem with:
$menu_kaart_type = $row["menu_kaart_type_$lang"];
where variable $lang represent the language i want the listing to been shown. Unlike the the database connection I can not delare that one in the file (modules/site/menu_items.php) itself since I want to use just one file to serve several languages.
Hi fretburner. No I am not using sessions. The mail language, in ths case ducth is in the root and the other languages have their own directory e.a. en, el, es etc. But I already found a sollution I sticked the variable $lang in the url