I am trying to set a dynamic page title for my one of my pages but it seems to not be working. I use phpMyAdmin and I have the same code that is dynamic for another page that grabs a server name. I cannot figure out what I am doing wrong.
Here is my my current code: URL looks like this http://test.com/index.php?category=test
switch ($pageTitle) {
case 'index.php':
if(isset($_GET['id']) && !empty($_GET['id'])){
$id = (INT)$_GET['id'];
$query = $database->query("SELECT `name` FROM `categories` WHERE `category_id` = '$id'");
$fetch = $query->fetch_assoc();
$pageTitle = $fetch['name'];
} else {
$pageTitle = "test title";
$description = "test desc";
}
break;
Here is another code snippet that works and uses a server name: URL looks like this http://test.com/server.php?id=#
case 'server.php':
if(isset($_GET['id']) && !empty($_GET['id'])){
$id = (INT)$_GET['id'];
$query = $database->query("SELECT `name` FROM `servers` WHERE `id` = '$id'");
$fetch = $query->fetch_assoc();
$pageTitle = $fetch['name'];
} else {
$pageTitle = "Server Details";
}
break;
Anyone know why I cannot get the name of the category?
Related
I'm building a custom CMS and I'm trying to create multilingual content by using MySQL with column approach. I'm not using any framework, it's pure PHP.
Here's my code:
<?php
$query = "SELECT * FROM posts";
$result = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($result)) {
$title = $row['title'];
}
echo $title;
?>
I'm successfully printing the title from the database.
Now if I change the table posts by adding a new fields "title_en" and "title_de" and add a session
$_SESSION['current_language'] = 'en';
$_SESSION['current_language'] = 'de';
How can I create a language switcher and print the title depends what language is activated?
EDIT:
Here's the code that I'm trying
<?php
session_start();
if(isset($_GET['lang']) && in_array($_GET['lang'],['en','de'])) {
$lang = $_GET['lang'];
$_SESSION['current_language'] = $lang;
print_r($_SESSION['current_language']);
} else {
$lang = isset($_SESSION['current_language']) ? $_SESSION['current_language'] : 'en';
}
$query = "SELECT * FROM posts";
$result = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($result)) {
$title = $row['title_'.$lang];
echo $title;
}
?>
but on www.mywebsite.com/lang=de it still shows the English title.
EDIT2
I've just tried www.mywebsite.com/?lang=de and it's displaying properly. Big thanks to Павел Иванов!
Just use saved language as key. get attribute should have more priority than session saved ( for example when user want to switch language ).
if(isset($_GET['lang']) && in_array($_GET['lang'],['en','de'])) {
$lang = $_GET['lang'];
$_SESSION['current_language'] = $lang;
} else {
$lang = isset($_SESSION['current_language']) ? $_SESSION['current_language'] : 'en';
}
$query = "SELECT * FROM posts";
$result = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($result)) {
$title = $row['title_'.$lang];
echo $title;
}
Edit:
language switcher is simple ul>li list in url with lang="Your lang"
Edit: I've changed the query to this version but I'm still not getting any
results even when I should be.
if (isset($_POST['schbttn'])) {
$breed1 = $_POST['schbreed1'];
$breed2 = $_POST['schbreed2'];
$sex = $_POST['schsex'];
$colour = $_POST['schcolour'];
$age = $_POST['schage'];
include ('inc/dbconn.php');
// If breed2 NULL, search with this query
if ($breed2 == "NULL") {
$search = mysqli_query($dbconn, "SELECT * FROM `lstfnd` WHERE `doglf_stat` = 'Lost' AND `doglf_breed1` = '$breed1' AND `doglf_breed2` IS NULL AND `doglf_sex` = '$sex' AND `doglf_colour` = '$colour' AND `doglf_age` = '$age'");
// Else search with this query
} else {
$search = mysqli_query($dbconn, "SELECT * FROM `lstfnd` WHERE `doglf_stat` = 'Lost' AND `doglf_breed1` = '$breed1' AND `doglf_breed2` = '$breed2' AND `doglf_sex` = '$sex' AND `doglf_colour` = '$colour' AND `doglf_age` = '$age'");
}
$schrow = mysqli_fetch_assoc($search);
}
I'm trying to create a simple search function where a user can search by multiple fields.
I've taken the entries of each field
$breed1 = $_POST['breed1'];
$breed2 = $_POST['breed2'];
$sex = $_POST['sex'];
$colour = $_POST['colour'];
$age = $_POST['age'];
and built the query through if loops
$query = "SELECT * FROM `table` WHERE `stat` = 'Lost'";
// If breed1 is not ALL, add to search
if ($breed1 != "ALL") {
$query = $query." AND `breed1` = '$breed1'";
}
// If breed2 is not ALL, add to search
if ($breed2 != "ALL") {
if ($breed2 == "NULL") {
$query = $query." AND `breed2` IS NULL";
} else {
$query = $query." AND `breed2` = '$breed2'";
}
}
// If sex is not ALL, add to search
if ($sex != "ALL") {
$query = $query." AND `sex` = '$sex'";
}
// If colour is not ALL, add to search
if ($colour != "ALL") {
$query = $query." AND `colour` = '$colour'";
}
// If age is not ALL, add to search
if ($age != "ALL") {
$query = $query." AND `age` = '$age'";
}
$query = $query.";";
and placed the query in a PHP variable to use when running the query.
include ('inc/dbconn.php');
$search = mysqli_query($dbconn, "'.$query.'");
$schrow = mysqli_fetch_assoc($search);
However, when I try to display the results of the search, I get an error code.
mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given...
So is what I am attempting possible to accomplish using this method? And if not, any suggestions for alternative methods?
change this line
$search = mysqli_query($dbconn, "'.$query.'");
to
$search = mysqli_query($dbconn, $query);
$query is variable, do not use that as string.
I'm using smarty pagination My pagination URL look like this
categories.php?id=1&next=3
Now {paginate_prev} & {paginate_next} is working properly
but {paginate_middle} is stripping ID parameter it is generating url like this
categories.php?next=3
My pagination code is here
function get_db_results() {
global $conn;
$id = htmlspecialchars((int)$_GET['id']);
if ($id){
$_query = sprintf('SELECT SQL_CALC_FOUND_ROWS * FROM newser where idblog = '.$conn->qstr($id).' and (main = '.$conn->qstr('0').' or main = '.$conn->qstr('2').') ORDER BY blogid DESC LIMIT %d,%d',
SmartyPaginate::getCurrentIndex(),SmartyPaginate::getLimit());
}
else
{
$_query = sprintf('SELECT SQL_CALC_FOUND_ROWS * FROM newser where (main = '.$conn->qstr('0').' or main = '.$conn->qstr('2').') ORDER BY blogid DESC LIMIT %d,%d',
SmartyPaginate::getCurrentIndex(),SmartyPaginate::getLimit());
}
$brecordSet = $conn->Execute($_query);
if(!$brecordSet)
print $conn->ErrorMsg();
else
while(!$brecordSet->EOF) {
$_data[] = $brecordSet->GetRowAssoc(false);
$brecordSet->MoveNext();
}
$_query = "SELECT FOUND_ROWS() as total";
$crecordSet = $conn->Execute($_query);
if(!$crecordSet)
print $conn->ErrorMsg();
else
$_row = $crecordSet->GetRowAssoc();
$total = $crecordSet->fields['total'];
SmartyPaginate::setTotal($total);
return #$_data;
$brecordSet->Close();
$crecordSet->Close();
}
require ('libs/SmartyPaginate.class.php');
SmartyPaginate::connect();
SmartyPaginate::setLimit(4);
SmartyPaginate::setUrl('categories.php');
$smarty->caching = $caching;
$smarty->assign('results',get_db_results());
SmartyPaginate::assign($smarty);
$id = htmlspecialchars((int)$_REQUEST['id']);
#$next = htmlspecialchars((int)$_REQUEST['next']);
$cid = $id.$next;
$smarty->display('categories.php',$cid);
Please help me understand if there is any problem with my code or it a bug with smarty {paginate_middle} which is not handeling multiple parameters
I don't use SmartyPaginate at all but it seems you should change at least this line:
SmartyPaginate::setUrl('categories.php');
into
SmartyPaginate::setUrl('categories.php?id=1');
I am trying to build a CMS system where an admin inserts data in a template. There can be as many pages created with that template as the admin wants. The pages should then be accessed in a menu with links and be displayed with the respective data according the link you just clicked.
Everything is working as expected except the system is not picking up the data from the id that the link had. Instead the system is picking up the data from the last id on the database.
Example: If I click on the link Page#2, I will have the URL http://www.website/page.php?pid=2 but the content that is loaded is the content from the last id on the database and not the 2.
Code to make and display the links:
if (!$_GET['pid']) {
$pageid = '1';
} else {
$pageid = preg_replace('/[^0-9]/', "", $_GET['pid']); // filter everything but numbers for security
}
$sqlCommand = "SELECT id, linklabel FROM pages WHERE showing='1' ORDER BY id ASC";
$query = mysqli_query($myConnection, $sqlCommand) or die('Error: ' . mysqli_error($myConnection));
$listadeavatars = '';
while ($row = mysqli_fetch_array($query)) {
$pid = $row["id"];
$avatar = html_entity_decode($row["linklabel"]);
}
$sqlCommand = "SELECT id, linklabel FROM pages WHERE showing='1' ORDER BY id ASC";
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error());
$menuDisplay = '';
while ($row = mysqli_fetch_array($query)) {
$pid = $row["id"];
$linklabel = $row["linklabel"];
$avatar = html_entity_decode($row["linklabel"]);
$menuDisplay .= '<li>' . $avatar . '</li>';
}
mysqli_free_result($query);
Code to display the data on page.php according to the ID
if (!$_GET['pid']) {
$pageid = '1';
} else {
$pageid = ereg_replace("[^0-9]", "", $_GET['pid']); // filter everything but numbers for security
}
$sqlCommand = "SELECT pagetitle, linklabel, evenemang, presentation, producent, pagebody, mapa FROM pages WHERE id='$pid' LIMIT 1";
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error());
while ($row = mysqli_fetch_array($query)) {
$pagetitle = $row["pagetitle"];
$linklabel = $row["linklabel"];
$evenemang = $row["evenemang"];
$presentation = $row["presentation"];
$producent = $row["producent"];
$pagebody = $row["pagebody"];
$mapa = $row["mapa"];
$avatar = html_entity_decode($row["linklabel"]);
}
mysqli_free_result($query);
Please do let me know if I need to explain better! Any suggestions to solve this issue? Cheers!
On page.php, change WHERE id='$pid'
$sqlCommand = "SELECT pagetitle, linklabel, evenemang, presentation, producent, pagebody, mapa FROM pages WHERE id='$pid' LIMIT 1";
to WHERE id='$pageid'
$sqlCommand = "SELECT pagetitle, linklabel, evenemang, presentation, producent, pagebody, mapa FROM pages WHERE id='$pageid' LIMIT 1";
I'm currently working on making a few amendments to a website.
The way the website currently works is that the page calls for a require once for the config.inc.php page. The config page also has the SQL call for the what is required to show from the database. While this works fine, I need to create a new page that shows the same table, but the SQL has changed to show different data.
The trouble I’m having is that the config.inc.php page has the SQL data on it that pulls in what is wanted. I want to show a different result on a different page, but the website requires the config.inc.php to show everything. I changed the SQL query to test if what I wanted to show will work and it works a charm, but how do I get this to show without having to mess around with the original SQL command.
Here's the original code;
$config = require_once 'link to admin section';
$db_username = $config['components']['db']['username'];
$db_password = $config['components']['db']['password'];
$db_link = mysql_connect('nosey', $db_username, $db_password) or die(mysql_error());
mysql_select_db('nosey', $db_link);
if ( $_SERVER['REQUEST_METHOD'] == 'GET' ) {
if ( isset($_GET['id']) && $_GET['id'] ) {
$selectSqlJSONString = 'SELECT * FROM vacancies WHERE id = '.$_GET['id'].' ORDER BY created ASC LIMIT 1';
$jsonResult = mysql_query($selectSqlJSONString, $db_link);
$jsonRow = mysql_fetch_object($jsonResult);
if ( !isset($_SERVER['HTTP_X_FANCYBOX']) )
include 'details_ajax_header.php';
include 'filename.php';
if ( !isset($_SERVER['HTTP_X_FANCYBOX']) )
include 'details_ajax_footer.php';
exit;
}
}
$region = '1';
if ( isset($_GET['region']) && $_GET['region'] ) {
$region .= ' AND ' . sprintf('county LIKE "%%%s%%"', mysql_real_escape_string(str_replace('-', ' ',$_GET['region']), $db_link ));
}
$selectSqlString = 'SELECT * FROM vacancies WHERE '.$region.' ORDER BY created DESC';
$result = mysql_query($selectSqlString, $db_link) or die(mysql_error());
$vacancies = array();
while ($row = mysql_fetch_object($result)) {
$vacancies[] = $row;
}
and here's what I would to show only on another page to show a different set of results.
$config = require_once 'link to admin section';
$db_username = $config['components']['db']['username'];
$db_password = $config['components']['db']['password'];
$db_link = mysql_connect('nosey', $db_username, $db_password) or die(mysql_error());
mysql_select_db('nosey', $db_link);
if ( $_SERVER['REQUEST_METHOD'] == 'GET' ) {
if ( isset($_GET['id']) && $_GET['id'] ) {
$selectSqlJSONString = 'SELECT * FROM vacancies WHERE id = '.$_GET['id'].' ORDER BY created ASC LIMIT 1';
$jsonResult = mysql_query($selectSqlJSONString, $db_link);
$jsonRow = mysql_fetch_object($jsonResult);
if ( !isset($_SERVER['HTTP_X_FANCYBOX']) )
include 'details_ajax_header.php';
include 'filename.php';
if ( !isset($_SERVER['HTTP_X_FANCYBOX']) )
include 'details_ajax_footer.php';
exit;
}
}
$region = '1';
if ( isset($_GET['region']) && $_GET['region'] ) {
$region .= ' AND ' . sprintf('county LIKE "%%%s%%"', mysql_real_escape_string(str_replace('-', ' ',$_GET['region']), $db_link ));
}
$selectSqlString = 'SELECT * FROM vacancies WHERE featured = "1" ORDER BY created DESC';
$result = mysql_query($selectSqlString, $db_link) or die(mysql_error());
$vacancies = array();
while ($row = mysql_fetch_object($result)) {
$vacancies[] = $row;
}
Notice how all I want to change is the SELECT * FROM vacancies WHERE featured = "1".
This is all I need! But I want to include this without having to alter the config.inc.php page! (which is where this information is coming from) How do I do it?