Currently I'm making a blog out of scratch.
I already have a login page and a "create post page". But when I create a post I just store the information inside the database.
How can I make it that I create a new page for the blog post?
Something like: [sitename].com/post.php?id=1
Thanks for helping :)
Interesting question. Here I have an example on how you could do that (PDO). $_GET["id"] will take the ID from the URL. As example domain.com/post.php?id=[getid]
<?php
$id = $_GET["id"];
$sql = "SELECT * FROM `posts` WHERE `id` = $id";
$result = $conn->query($sql);
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
echo $row["title"].' '.$row["content"];
}
?>
And if you want to link them back to the homepage when the ID is not given, you could use this;
if (empty($_GET["id"])) {
header("Location: index.php");
}
For linking from the index to the posts you can use;
$sql = "SELECT * FROM `posts` ORDER BY `id` DESC";
$result = $conn->query($sql);
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
echo '<a href="posts.php?id='.$row["id"].'">';
}
}
In your post.php file, you can load the content from a specific post using the id provided in your url.
<?php
$postid = $_REQUEST['id'];
$post = someSqlFunction($postid); // <-- Replace with database code
echo '<h1>' . $post['title'] . '</h1>';
So if you wanted to load the post with id 15, you simply go to [sitename].com/post.php?id=15
You can create a page for each and every blog post you create. When you create a blog post there must a submit or post, after submitting it goes to a php file where you write the code to store the contents of the blog post. In that very file you write some code to create html file (file programing) for blog post having particular id. And you add all the code of css,html,also contents and write to that file and save it as .html format(eg. BlogPost_1.html etc). You can store file names in database for later use.
I hope thats what you needn
Related
www.mydomain.com/filename/ instead of www.mydomain.com/file.php?id=1
currently im developing a Auto index site script
From index.php i had put a hyper link to download page Download page and on file.php page i had put
$id=$_REQUEST['id'];
$query = "update mydatabase SET views = views+1 where id ='".$id."'";
$update = mysqli_query($con,$query) or die(mysql_error());
$query = "SELECT * from mydatabase where id='".$id."'";
$result = mysqli_query($con,$query) or die(mysql_error());
$row = mysqli_fetch_assoc($result);
<?php echo $row['id'];?>
<?php echo $row['name'];?>
<?php echo $row['size'];?>
etc..
when i click on Download page it redirect me to www.mydomain.com/file.php?id=1 page and show the related file details.
But i really need to change the above url as www.mydomain.com/song/file-name
please can anybody advise or help me on this ??
Maybe you can try this on your download page.
history.replaceState(null, null, '/song/file-name')
The code below stores array of post id's into session and using foreach I iterate over post id and call data(title,body) related to that post id. It works fine.
index.php
$result = $query->fetchAll(PDO::FETCH_COLUMN, 0);
$_SESSION['postid']=$result;
foreach($_SESSION['postid'] as $key=>$postid){
$sql = "SELECT * FROM posts WHERE postid=:postid";
$query = $db->prepare($sql);
$query->execute(array(':postid'=>$postid));
$row = $query->fetch(PDO::FETCH_ASSOC);
<?php echo $row['title']; ?>
<p><?php echo row['body'] ?></p>
}
Now I want to get directed to post page by clicking on title link from index page, and call that specific post id from session array whose title was clicked. Using $postid = $_SESSION['postid']; on post page gives the whole array of post id's . Is there any way by which I can start session on post page for only that specific post id whose title was clicked on index page.
In index.php page, modify the anchor tag like this :
<?php echo $row['title']; ?>
then on post page use this code to set postid in session
<?php
$_SESSION['pid'] = isset($_GET['pistid'])$_GET['pistid']:null?;
?>
I am new to php and js and I am working on a simply on one webpage but i get stuck. What I am trying to do is next:
I am logging on the web
I am using the user id to retrieve data associated with that id from table in db. I am retrieving just the "name" column and using it as a hyperlink.
Here is my code:
$query = "SELECT * FROM cases WHERE id='".$_SESSION['id']."'";
$result=mysql_query($query) or die("Query Failed : ".mysql_error());
while($rows=mysql_fetch_array($result))
{
echo '<tr>
<td><ul><li><a href="casedetails.php">'.$rows['Name'].'</li></ul></td>
</tr>';
}
It is retrieving data that is in the row named "name" in the table, and I want when i click on the hyperlink to save the name of the hyperlink (for ex. Murder(link to cases.php) ). because i want to use it in a query on the page of the hyperlink. I will appreciate if someone can help me!
If you want to click the link and it takes you a different page while remembering what ID (or name) was clicked, you would write your code like this:
$query = "SELECT * FROM cases WHERE id='".$_SESSION['id']."'";
$result=mysql_query($query) or die("Query Failed : ".mysql_error());
while($rows=mysql_fetch_array($result))
{
$name - $rows['Name'];
echo '<tr>
<td><ul><li><a href="casedetails.php?name='.$name.'">'.$name.'</li></ul></td>
</tr>';
}
So, whenever you go to casedetails.php, the variable $name will be in a GET that you can retrieve and use for queries. For example, on casedetails.php you would have $newName = $_GET['name']; and that would retrieve the name.
I have a blog page "plugin" in my own little cms and it's displayed with return() on the index.php.
Part of the index.php:
$id = (isset($_GET['m_id'])) ? $_GET['m_id'] : 1; //menu id
$sql = "SELECT m_cim, m_tartalom, m_plugin
FROM menu
WHERE m_s_id = (SELECT s_id FROM statusz WHERE s_nev='aktiv')
AND m_id = ".$id;
$eredmeny = mysql_query($sql);
if (#mysql_num_rows($eredmeny) == 0) {
$tartalom = "<h1>404!</h1>\n";
}
else {
$sor = mysql_fetch_assoc($eredmeny);
$tartalom = "<h2>{$sor['m_cim']}</h2>
<span class=\"tart\">{$sor['m_tartalom']}</span>\n";
if(!empty($sor['m_plugin'])){
$tartalom.=include("./modul/{$sor['m_plugin']}");
}
}
<section id="content">
<?php print $tartalom; ?>
</section>
The posts come from a database and the news.php is included in the index.php.
This is the "plugin" news.php
$aktiv="(SELECT s_id FROM statusz WHERE s_nev='aktiv')";
$sql = "SELECT hir_id, hir_cim, hir_tartalom, hir_ido
FROM hirek
WHERE hir_s_id=".$aktiv."
ORDER BY hir_id DESC";
$eredmeny = mysql_query($sql);
$kimenet = "";
while ($sor = mysql_fetch_assoc($eredmeny)) {
$kimenet.= "<article class=\"hirek\">
<h3>{$sor['hir_cim']}</h3>
<span class=\"hido\">{$sor['hir_ido']}</span>
<p class=\"htart\">".substr(strip_tags($sor['hir_tartalom']),0,200)."</p>
Tovább...
</article>\n";
}
return $kimenet;
If I use a php pagination, I only saw, that the page number is posted via the ‘GET’ method, but in that case, I use the GET method for the menu id, and when I want to post something else with get, the result will be the menu item with the actual id. Is it possible to use $_GET method for this?
I think this technique can solve my problem, but I don't know if it's outdated/not-so-good/do-not-use-it or not.
When I find somewhere a solution for my actual problem, somebody always says that "do not use it, because..." "it's not the best idea, because..." "this is not the best solution,because.."
Can I trust in this?
I've scoured the web for a tutorial about this simple task, but to no avail. And so I turn to you helpful comrades. Here's what I need to do:
I have a MySQL database with an Events table. I need to create a PHP web page with a list of the Event titles, and each title must be a link to the full details of the Event. But I want to avoid having to create a static page for each event, primarily because I don't want the data entry volunteer to have to create these new pages. (Yes, I realize that static pages are more SEO friendly, but I need to forego that in this case for the sake of efficiency.)
I've seen PHP url syntax with something like this:
pagename.php?id=20
but I don't know how to make it work.
Any and all help greatly appreciated.
Thanks!
Kip
This is basic php. You would simply query the DB for the event details before the page headers are written and write the html accordingly.
The first thing I would ask you is if you know how to connect to your database. From there, you query based on the $_GET['id'] value and use the results to populate your html.
Not to be rude, but the question itself suggests you're new to PHP, right? So in order to provide a solution that works we might want to know just how far you got.
Also, you can rewrite your dynamic urls to appear like static ones using apache's mod_rewrite. It's probably a novice level thing if you're interested in "pretty" url's.
MODIFIED ANSWER:
In your loop you would use the id from the query result (assuming your primary key is id)...
while($field = mysql_fetch_array($result)) {
echo "<p class='date'>";
echo $field['month']." ".$field['day'].", ".$field['year'];
echo "</p>";
echo "<h3>";
echo ''.$field['event_name'].'';
echo "</h3>";
}
Then on somepage.php you would use the get var id to pull the relevant info...
$result = mysql_query("SELECT * FROM `calendar` WHERE `id` = '".mysql_real_escape_string($_GET['id'])."');
don't forget to look into mysql_real_escape_string() for cleaning entries.
It's wise to take extra care when you are using $_GETvariables, because them can be easily altered by a malicious user.
Following with the example, you could do:
$foo = (int)$_GET['id'];
So we are forcing here the cast of the variable to a integer so we are sure about the nature of the data, this is commonly used to avoid SQL injections.
lets say you have the php file test.php
<?php
$conn = mysql_connect("localhost", "root", "");
mysql_select_db("db", $conn);
$id = $_GET['id'];
$sql = "select * from table where id = $id";
$result = mysql_query($sql, $conn);
if ($result){
$row = mysql_fetch_row($result);
$title = $row[0];
$content = $row[1];
}
?>
<html>
<head>
<title><?php echo $title ?></title>
</head>
<body>
<h1><?php echo $title ?></h1>
<p><?php echo $content ?></p>
</body>
</html>
a dynamic page would be something like that..
Here is the pertinent code for extracting a list of events in November from a table named calendar, with each event having a link to a page called event.php and with the event's id field appended to the end of the url:
$result = mysql_query("SELECT * FROM calendar WHERE sort_month='11'");
while($row = mysql_fetch_array($result))
{echo
"<a href='event.php?id=".$row['id']."'>".$row['event_name']."</a>"
;}
And here is the pertinent code on the event.php page. Note the row numbers in brackets depends on the placement of such in your table, remembering that the first row (field) would have the number 0 inside the brackets:
$id = $_GET['id'];
$sql = "select * from calendar where id = $id";
$result = mysql_query($sql, $con);
if ($result){
$row = mysql_fetch_row($result);
$title = $row[12];
$content = $row[7];
}
?>
<html>
<head>
<title><?php echo $title ?></title>
</head>
<body>
<h1><?php echo $title ?></h1>
<p><?php echo $content ?></p>
</body>
</html>
This works for me, thanks to the help from those above.
$foo=$_GET['id'];
in your example $foo would = 20