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!
Related
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
I'm trying to make a navigation menu. It's displaying perfectly, but I don't know how to attach a link to it and how to set it active when particular page is opened.
<?php
$query = "SELECT * FROM menu";
$select_all_categories_query = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($select_all_categories_query)) {
$cat_id = $row['cat_id'];
$cat_title = $row['cat_title'];
echo "<li><a href='#'>$cat_title</a></li>";
}
?>
for the link you can create a new column in your table for links and call it in your link href
echo "<li><a href='<?php echo $row['yourlink'];?>'>$cat_title</a></li>";
for the active page it needs javascript, but I use another trick with css,I create a unique css style only for the child I want in each page
Well this was what exactly I've been looking for but I was finally able to do it myself.
<?php
$query = "SELECT * FROM menu";
$select_all_categories_query = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($select_all_categories_query)){
$cat_id = $row['cat_id'];
$cat_title = $row['cat_title'];
$cat_active = $row['cat_active'];
if(isset($_GET['category']) == $cat_id){
$cat_id == $_GET['category'] ? $cat_active = 'active' : '';
}
echo "<li class='{$cat_active}'><a href='{$cat_title}.php?category={$cat_id}'>$cat_title</a></li>";
}
?>
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
I have a page where I list items ( in this case businesses) that are pulled from a mysql database with php.
<?
include('config.php');
echo "<h3>Saved Businesses</h3>";
echo "<ul style='list-style-type:none;'>";
$result = mysql_query("SELECT * FROM `saved_biz` WHERE user_id = '$id'") or trigger_error(mysql_error());
while($row = mysql_fetch_array($result)){
foreach($row AS $key => $value) { $row[$key] = stripslashes($value); }
$business_id = $row['business_id'];
$result = mysql_query("SELECT * FROM `company` WHERE id = '$business_id'") or trigger_error(mysql_error());
while($row = mysql_fetch_array($result)){
$business_name = $row['name'];
}
echo "<li>" . nl2br( $business_name);
echo "<a href=deletesavedbiz.php?id={$row['id']}>Delete</a></li>";
echo "</tr>";
}
echo "</ul>";
?>
If the user decides to delete one of the businesses ( by clicking the "Delete" link ) he/she is then taken to deletesavedbiz.php and then, if successful, presented with a link to get back to the profile.php page that he/she was just on.
<?
include('config.php');
$id = (int) $_GET['id'];
mysql_query("DELETE FROM `saved_biz` WHERE `id` = '$id' ") ;
echo (mysql_affected_rows()) ? "Row deleted.<br /> " : "Nothing deleted.<br /> ";
?>
<a href='profile.php'>Back To Listing</a>
Now, what I want to do is have the php delete and then do like a php header redirect to profile.php without ever making the user click a link back. How can I accomplish this? Also, I'm fine with having the answer being javascript if it's not possible or not very clean in PHP.
Thanks for all help!
Do redirect in case your query returned TRUE.
function redirect($page = 'profile.php'){
header("Location: $page");
exit;
}
function your_query(){
include('config.php');
$id = (int) $_GET['id'];
//returns TRUE on success, FALSE otherwise
return mysql_query("DELETE FROM `saved_biz` WHERE `id` = '$id' ") ;
}
if ( your_query() ){
redirerct();
} else {
redirect('some_error_page.php');
}
header("Location: profile.php");
Put that at the bottom of your script that deletes the record.
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.