MySQL PHP CMS URL creation(need to edit) - php

I apologize for the length of this question, and would like to say thanks in advance.
Below is the snippet of code in question. I have a MySQL table that stores information from a custom built PHP MySQL CMS program based off of this tutorial:cms tutorial. As you can see in the List Pages and the Display Admin functions, the URL is built with the base url and the ID as well as the title. The ID is the primary key in the MySQL table that pulls all of the necessary information to be displayed on the page. How would I rewrite the code below so that it displays the title only in the URL and not the ID, but still go off of the ID as the unique identifier for displaying information in the pages?
From functions.php:
'// Display center bottom column
function centerbottomcolumn() {
if ($_GET['id']) {
$pageID = (int) $_GET['id'];
$result = mysql_query("SELECT centerbottomcolumn FROM pages WHERE id='$pageID'");
$row = mysql_fetch_array($result);
echo $row['centerbottomcolumn'];
} else {
$result = mysql_query("SELECT value FROM settings WHERE name='homePage'");
$row = mysql_fetch_array($result);
$pageID = $row['value'];
$result = mysql_query("SELECT centerbottomcolumn FROM pages WHERE title='$pageID'");
$row = mysql_fetch_array($result);
echo $row['centerbottomcolumn'];
}
}
// List the pages
function listPages() {
// List the home page first
$result = mysql_query("SELECT value FROM settings WHERE name='homePage'");
$row = mysql_fetch_array($result);
$homeID = $row['value'];
$result = mysql_query("SELECT title FROM pages WHERE id='$homeID'");
$row = mysql_fetch_array($result);
$homeTitle = $row['title'];
echo "<li><a href='" . BASE_URL . "/index.php'>$homeTitle</a></li>";
// List the rest of the pages
$result = mysql_query("SELECT id, title FROM pages");
while ($row = mysql_fetch_array($result)) {
// Do not list the home page twice
if ($row['title'] != $homeID) {
$pageID = $row['title'];
$pageTitle = $row['title'];
echo "<li><a href='" . BASE_URL . "/?id=$id&title=$pageTitle'>$pageTitle</a></li>";
}
}
}
// Display admin table
function displayAdmin() {
// Find the home page ID
$result = mysql_query("SELECT value FROM settings WHERE name='homePage'");
$row = mysql_fetch_array($result);
$homeID = $row['value'];
// Display a table
$result = mysql_query("SELECT id, title, date FROM pages");
echo '<table width="961">';
echo '<tr height="50">
<th align="left">ID</th>
<th align="left">Title of the Page</th>
<th align="left">Date Created</th>
<th align="left">Actions</th>
</tr>';
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$title = $row['title'];
$date = date('M d, Y', $row['date']);
echo "<tr>
<td>$id</td>
<td><a href='". BASE_URL . "/id=$id&title=$title'>$title</a>";
if ($id == $homeID) {
echo ' <strong>(Home Page)</strong>';
}
echo "</td>
<td>$date</td>
<td><a href='edit.php?id=$id'>Edit</a></td><td>
<a href='confirm.php?id=$id'>Delete</a></td><td>
<a href='sethome.php?id=$id'>Set as Home</a>";
}
echo '</table>';
}
// Get array with page IDs
function getArray() {
$result = mysql_query("SELECT id FROM pages");
$IDs = array();
$i = 0;
while ($row = mysql_fetch_array($result)) {
$IDs[$i] = $row['id'];
$i++;
}
return $IDs;
}
From index.php:
(above head tag)
require_once 'functions.php';
connect();
$IDs = getArray();
(from body)
<?php if (in_array($_GET['id'], $IDs) || !$_GET): ?>
<?php centerbottomcolumn(); ?>
<?php else: ?>
<!-- Show a not found error -->
<p>Not found</p>
<?php endif; ?>'

You want your pages to show information based on a given ID but you don't want to pass the ID to the pages on the URL query string? You could POST the data to the target page instead of on the query string. You could invent a new ID that is a proxy for the real ID. But in general, you can't have a page do something based on user input (clicking a link with an ID in this case) without the input. If you want to hide the ID because it should be secret or you don't want people to be able to guess them, then you can replace your IDs with some kind of random guid that maps to the real (secret) ID in your database.

Related

how to display a single database row after clicking on row from a iterated list in php?

I have a if statement that goes through all of the title options and all of them are a link to the same page. The idea is that depending on which title option is clicked on the linked page will show the title in h3 tags.
$sql = "SELECT * FROM posts";
$result = mysqli_query($conn, $sql);
$queryResults = mysqli_num_rows($result);
if ($queryResults > 0) {
while ($row = mysqli_fetch_assoc($result)){
$item =$row['title'];
echo "<div>
<a href='p&fproject.php' name='project'>
<p>".$item."</p>
</a>
</div>";
}
}
}
if(isset($_POST['project'])){
$id = (isset($_POST['id'])) ? intval($_POST['id']): null;
echo "<br>".$id;
}
?>
The last if statement was an attempt to identify the link that was clicked on by id instead of title but it doesn't work.
Page linked to:
<?php
include 'header1.php';
$sql = "SELECT * FROM posts";
$result = mysqli_query($conn, $sql);
$queryResults = mysqli_num_rows($result);
if ($queryResults > 0) {
while ($row = mysqli_fetch_assoc($result)){
for ($i = 0; $i < $queryResults; $i++) {
$item = $row['title'];
echo "<div>
<a href='' name='project'>
<p>".$item."</p>
</a>
</div>";
}
}
}
if(isset($_POST['project'])){
$id = (isset($_POST['id'])) ? intval($_POST['id']): null;
echo "<br>".$id;
}
?>
You can easily do that passing the post id in GET. What you want to do is put in the href your link project.php followed by a question mark with the id of the post, like this: 'project.php?id='" . $row["id"] . ", and then, in the page linked to, you can access that variable by doing postId = $_GET["id"] and making an SQL query on that id

paginate the list in php

i have this php code that fetches images from the database using the userid, then displays all the images in a list form. im trying to paginate the images, in a limit of 5 items per page. but the code is showing only the first page, without a link to the other pages. here's my php code
<?php
include 'connect.php';
$Category = " ";
$query = "SELECT Img_dir, Caption, Category FROM images WHERE Category = '". $_REQUEST['Category'] ."' AND user_id = '". $_SESSION['user_id'] ."' LIMIT 0,5";
$result = mysqli_query($conn,$query);
while ($row=mysqli_fetch_array($result)){
$image = $row["Img_dir"];
$Caption= $row["Caption"];
$Category = $row["Category"];
echo "<dl>";
echo "<dd>$Category &nbsp&nbsp <img src='base64_encode($image)' />&nbsp&nbsp $Caption<dd>";
echo "</dl>";
}
//number of total pages available
$results_per_page = 10;
$number_of_results = mysqli_num_rows($result);
echo $number_of_pages = ceil($number_of_results / $results_per_page);
echo "<br>"; echo "<br>";
for($r=1;$r<=$number_of_pages;$r++)
{
?><?php echo $r." "; ?><?php
}
?>
You can try this:
Change your query (use prepare statments):
$query = "SELECT Img_dir, Category FROM images WHERE user_id = ? AND Category = ? ";
As for the structure of your data.
$results = [];
while ($row = $result->fetch_assoc()){
$key = $row['Category'];
if(!isset($results[$key])) $results[$key] = [];
$results[$key][] = $row['Img_dir ']; //['Category' => [Img_dir,Img_dir, ...]]
}
And your HTML. I would use a description list or dl as it has a nice place for the title:
foreach($results as $Category => $image){
echo "<dl>";
echo "<dt>$Category</dt>";
foreach($data as $row){
echo "<dd><img src='base64_encode($image)' /><dd>";
}
echo "</dl>";
}
Untested.
The order will probably be all wanky, so you can use ksort on it. Simply
ksort($results);
Before the foreach loops.
Cheers.

Generate title from database and show related post

I have a DB name askadoc , where people can ask about their problem. I want to show all question(or you can say the titles) together in a page. And then when user click on a question, the question will show in a different page with it's comments/details. To do this i have tried the below code and its working perfectly. but how can i do it without using button ?
<?php
$comment = "SELECT * FROM `askadoc` ";
$result = mysqli_query($conn, $comment);
$question = "";
$id="";
if(mysqli_num_rows($result)>0){
while($row = mysqli_fetch_assoc($result)) {
$id = $row["id"];
$question = $row["question"];
?>
<form action="post.php" method="post">
<?php
echo $id;
echo "<input type='hidden' name = 'id' value = ".$id.">";
echo "<button>".$question."</button>";
echo "<br>";
?>
</form>
<?php
}
}
?>
I think you don't need form this. You can user anchor like this.
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$id = $row["id"];
$question = $row["question"];
?>
<?php echo $question ?>
<?php
}
}
Now when you click this anchor, you get question id inside post.php, so easily you can display a particular question comments.
You can use anchor tag then pass the question id to post.php
if(mysqli_num_rows($result)>0){
while($row = mysqli_fetch_assoc($result)) {
$id = $row["id"];
$question = $row["question"];
?>
<?php
echo "<a href='post.php?question_id=".$id."' target='_blank'>".$question."</a>";
echo "<br>";
?>
<?php
}
}
Then to get the value of question_id use $_GET['question_id'];
$questionId = $_GET['question_id']
and its better to check if question_id does exist.
$questionID = isset($_GET['question_id'])? $_GET['question_id'] : '';
if(!empty($questionID)){
//Use $questionID here to query some data from database
}

how to pick single current page using echo with li?

i would like to retrieve single current page instant off full menu with currents
menu code :
<?php
$sql = mysql_query("SELECT * FROM pages WHERE isRoot='1' ORDER BY pageID");
while ($row = mysql_fetch_object($sql))
{
echo "<li class=\"current\">$row->pageTitle</li>";
}
?>
preview code :
?php
//if no page clicked on load home page default to it of 1
if(!isset($_GET['p'])){
$q = mysql_query("SELECT * FROM pages WHERE pageID='1'");
} else { //load requested page based on the id
$id = $_GET['p']; //get the requested id
$id = mysql_real_escape_string($id); //make it safe for database use
$q = mysql_query("SELECT * FROM pages WHERE pageID='$id'");
}
//get page data from database and create an object
$r = mysql_fetch_object($q);
//print the pages content
echo "<h1>$r->pageTitle</h2>";
echo $r->pageCont;
?>
but it gives me li pages marked with current
I think I understood what you meant... I would try something like this:
<?php
$sql = mysql_query("SELECT * FROM pages WHERE isRoot='1' ORDER BY pageID");
while ($row = mysql_fetch_object($sql))
{
$active = '';
if($_SERVER['REQUEST_URI'] == DIR . '?p=' . $row->pageID)
$active = 'class="current"';
echo "<li " . $active . ">$row->pageTitle</li>";
}
?>
This way, you will only add the 'class="current"' if the user is actually on this page.
Hope this helps!

Changing a recursive condition in a SQL/PHP website menu

I am modifying a site built in PHP with content drawn from a SQL database. The original developer had the navigation created with the following code. It creates the navigation from the CMS pages - it works fine for top level pages but I am trying to modify the way subpages are presented.
What I want it to do is when a subpage is present, it first opens a DIV containing formatting, then populates the subpagemenu by using the 'while' statement below. Finally, when the subpagemenu is finished, it closes the DIV.
I can't seem to work out which condition 'opens' the subpage menu before it goes through the loop of filling out the subpage menu.
Any and all help appreciated - thanks!
<?
$pagesrc = $_SERVER['SCRIPT_NAME'];
$getPID_sql = "SELECT * FROM tblPages WHERE parentID = 0";
$getPID_result = mysql_query($getPID_sql);
if(!$getPID_result){print mysql_error()."<br />";}else{
while ($row = mysql_fetch_array($getPID_result, MYSQL_ASSOC)) {
$parentID = $row["pageID"];
$linkName = $row["pageTitle"];
$linkID = $row["pageID"];
print "<A href = '".$pagename."?id=".$linkID."'>".$linkName."</A> | ";
if($pageID){
$subpages_sql = "SELECT * FROM tblPages WHERE parentID = $parentID";
$subpages_results = mysql_query($subpages_sql);
if(!$subpages_results){print mysql_error();}else{
$rowcount = mysql_num_rows($subpages_results);
if($rowcount > 0){
while ($row2 = mysql_fetch_array($subpages_results, MYSQL_ASSOC)) {
$sublinkName = $row2["pageTitle"];
$sublinkID = $row2["pageID"];
$sublinkParentID = $row2["parentID"];
if($sublinkParentID == $pageID || $sublinkParentID == $PID){
print "<a href='".$pagename."?id=".$sublinkID."'>".$sublinkName."</a>";
}
}
}
}
}
}
}
?>
<?
$pagesrc = $_SERVER['SCRIPT_NAME'];
$getPID_sql = "SELECT * FROM tblPages WHERE parentID = 0";
$getPID_result = mysql_query($getPID_sql);
if(!$getPID_result){print mysql_error()."<br />";}else{
while ($row = mysql_fetch_array($getPID_result, MYSQL_ASSOC)) {
$parentID = $row["pageID"];
$linkName = $row["pageTitle"];
$linkID = $row["pageID"];
print "<A href = '".$pagename."?id=".$linkID."'>".$linkName."</A> | ";
if($pageID){
$subpages_sql = "SELECT * FROM tblPages WHERE parentID = $parentID";
$subpages_results = mysql_query($subpages_sql);
if(!$subpages_results){print mysql_error();}else{
$rowcount = mysql_num_rows($subpages_results);
if($rowcount > 0){
echo "<div class='submenu'>"; //you can either give class or id whatever you want
while ($row2 = mysql_fetch_array($subpages_results, MYSQL_ASSOC)) {
$sublinkName = $row2["pageTitle"];
$sublinkID = $row2["pageID"];
$sublinkParentID = $row2["parentID"];
if($sublinkParentID == $pageID || $sublinkParentID == $PID){
print "<a href='".$pagename."?id=".$sublinkID."'>".$sublinkName."</a>";}
}
echo "</div>";
}
}
}
}
}
?>
added the tag where it is requrired

Categories