Forum Moderators: coopster
<?php if($page == "") {include "content/site/news.txt";} else {include ($page.".txt");}?>
And as link:
index.php?page=about
Can anyone tell me how I can do this? I now update everything myself (creating txt files for the content and making links to those files).
Thank you very much in advance for the help,
Stefan.
PS: I made a table in my database with the fields ID, Category, Pagename and Content. I know how to display the data on a page, but how can I do this dynamically in the URL?
If that's the case then you would
1. upon even trigger(if statment)
2. query the db for table and field your text is stored in ($results=@mysql_query("SELECT id, text FROM table");) or ($results=@mysql_query("SELECT text FROM table WHERE id= $id");)
3. get the text you want out of the query ($row = mysql_fetch_array($results))
4. echo data and style normaly
FIrst you need to think what is going to be the trigger for text to go where. is it going to have something to do witht he current date or user input, or will it always be from the same field and you will just change the field when you fell like it.
Also How are you going to set up your DB structure
What will be your field and table names and how are things related and organized.
Sarah
And as link:
index.php?page=about
there are I think 3 ways to do this... I just read an artical aboult it 2 days ago but don't rember where nor do I rember the 3 ways it said.... only one way and it was the way the artical said was most cumbersome(that much I do rember).
That is to have each link(I asume you are linking to these pages from other pages right?) as a mini form using the get method with a submit button.
although I guess you could just make the link url = to index.php?page=about and use $_GET to reteve the info about the catagory and page name. and then you could use that for your WHERE.
I have a layout with two columns: 1 of them is for the menu (which I generated with another script from the same table and database as the editor) and the other is for the content. I somehow want to call the content from the database and display it on a page. I want to create a menu which somehow links to a page in the database and displays it on the page. So basically, I want to create links like 'index.php?page=pagename' which then automatically calls the correct row in the database (depending on the page name in the database).
Is it possible to select a row based on the URL in PHP (something similar to your code) and create text-links for the pages? Like what I have in my previous layout (see first post).
PS: I store all the data in one row in one table. The field 'Pagename' should correspond with the given URL and should call and display everything in the field 'Content'.
Edit: Is it possible to do this with the get-method you mentioned and if there is no page name in the URL that it displays a certain file?
How can I link to content in a database and display it on the page? I used to have this on index.php:<?php if($page == "") {include "content/site/news.txt";} else {include ($page.".txt");}?>
I notice your code requires that GLOBALS be on which is not best practice. I would recommend something like
<?php if (isset($_REQUEST['page'])) {
include ($_REQUEST['page'].".txt");}
else {
include "content/site/news.txt";}
?>
This is a simple way to call a series of includes but you should probably check to make sure the values in your 'page' variable are what you expect. People may put values in that don't have a .txt written for them or worse call out to yourscript.php?page=http://www.somesite.com/somehackerscript.php to try attacking you.
As far as passing th other lines you could do something like:
yourscript.php?page=1&pageid=1<?php if (isset($_REQUEST['pageid'])) {
$sql = "SELECT * FROM `your_table` WHERE id='".$_REQUEST['pageid']."'";
<load your variables & other code>
}
?>
No validation here and simple, but a thought,
Burner
I notice your code requires that GLOBALS be on which is not best practice. I would recommend something like<?php if (isset($_REQUEST['page'])) {
include ($_REQUEST['page'].".txt");}
else {
include "content/site/news.txt";}
?>
Oops. Now I know why a site which I coded has been hacked several times... Thank you very much for the tip and code.
yourscript.php?page=1&pageid=1<?php if (isset($_REQUEST['pageid'])) {
$sql = "SELECT * FROM `your_table` WHERE id='".$_REQUEST['pageid']."'";
<load your variables & other code>
}
?>
I think that should solve the problem (I thought the code for the solution would be quite a lot bigger). I just added my own code to it, but I keep getting the error 'Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/narutouz/public_html/layout/main.php on line 85'
My code:
<?php
$dbhost = "localhost";
$dbuser = "user";
$dbpass = "pass";$connect = mysql_connect($dbhost,$dbuser,$dbpass) or die (mysql_errno().": ".mysql_error()."<BR>");
mysql_select_db("narutouz_test", $connect);
if (isset($_REQUEST['page'])) {
$sql = "SELECT * FROM content WHERE id='".$_REQUEST['page']."'";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
PRINT "{$row['Pagename']}<BR><BR>{$row['Content']}";
}
else {include ("content/site/news.php");}
?>
Can you tell me what I forgot in my code (I just started with PHP, so I keep forgetting some parts). Thanks.
Thank you very much for the help, Burner! This is exactly what I needed. I finished the rest of the script aswell. Now I don't need to FTP new pages and edit the menu manually.
Thanks again,
Stefan.
PS: I did change WHERE id by the way. I pasted the right code, but I didn't change it just then.