show page in words in address bar - php

I need a little help of my brilliant friends.
Actually i m new to development so that i have no much idea how can i show my page in words like www.testsite.com/index.php?pname=**Home** except of www.testsite.com/index.php?pid=**1**
i have the following code for showing page in number
if (!$_GET['pid']) {
$pid = '1';
} else {
$pid = ereg_replace("[^0-9]", "", $_GET['pid']); }
and the sql code
$sqlCommand = "SELECT id, link FROM main_page WHERE showing='1' ORDER BY id ASC";
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error());
$menu='';
while ($row = mysqli_fetch_array($query)) {
$pid = $row["id"];
$link = $row["link"];
if ($linklabel){
$menu .=''. $link .'';
}}
i want to show and href name of page not id how can i do that.
help plz

Your example will fail if I enter *1*2*3*
you should be searching for the contents of
**(contents)**
and nothing else.
That will get you the name and the number.
Here is my example
$string = "**123naasdme456**";
preg_match("/[^\*+](?P<val>\w+)[^\*+]/",$string,$matches);
echo $matches[0];
will echo 123naasdme456
and here it is implemented into your code
function getReal($urlVar)
{
if(preg_match("/[^\*+](?P<val>\w+)[^\*+]/",$urlVar,$matches))
{
return $matches[0];
}
return false; // or default value
}
$pid = getReal($_GET['pid']);
$name = getReal($_GET['pname']);

You should add an extra field in your database (table main_page) with the name name or something similar. Then you could:
if (!$_GET['pname']) {
$pid = 'home';
} else {
$pid = mysql_real_escape_string($pid);
$sql = mysql_query("SELECT name FROM main_page WHERE name = '$pid'");
if (mysql_num_rows($sql) == 1))
{
echo "Content";
} else {
echo "404 error. Couldn't find the page you were looking for.";
}
}

URL Rewriting. http://www.addedbytes.com/for-beginners/url-rewriting-for-beginners/

Related

Check if row in table is 'equal' to other row

I have the following code to check if a row exists in MySQL:
<?php
if (!empty($_POST)) {
$code = $_POST['code'];
mysql_connect("$dbhost","$dbuser","$dbpass");
mysql_select_db("$dbname");
$result = mysql_query("SELECT 1 FROM files WHERE id='$code' LIMIT 1");
if (mysql_fetch_row($result)) {
echo 'Exists';
} else {
echo 'Does not exist';
}
}
?>
This works fine. But I need to change it a bit. I have the following fields:
id, title, url, type. When someone uses the code above ^ to check if a row exists, I need a variable to get the url from the same row, so I can redirect the user to there.
Do you have any idea how I can do that?
Thanks in advance! :)
Try this:
<?php
if (!empty($_POST)) {
$code = $_POST['code'];
mysql_connect("$dbhost","$dbuser","$dbpass");
mysql_select_db("$dbname");
$result = mysql_query("SELECT * FROM files WHERE id=" . $code . " LIMIT 1");
if (mysql_num_rows($result) > 0) {
while($rows = mysql_fetch_array($result)) {
echo 'Exists';
$url = $rows['url'];
}
} else {
echo 'Does not exist';
}
}
?>
It is quite simple. I think you don't show any effort to find the solution by yourself.
<?php
if (!empty($_POST)) {
$code = $_POST['code'];
mysql_connect("$dbhost","$dbuser","$dbpass");
mysql_select_db("$dbname");
$result = mysql_query("SELECT url FROM files WHERE id='$code' LIMIT 1");
if ($result) {
$url = mysql_fetch_row($resultado);
} else {
echo 'Does not exist';
}
}
<?php
$sql_query = "SELECT * FROM test WHERE userid ='$userid'";
$result1 =mysql_query($sql_query);
if(mysql_num_rows($result1)>0){
while($post = mysql_fetch_array($result1))
{
$url = $post['url'];
}
}
?>
If mysql_num_rows($result1)>0 it means row is existed fir the given user id

Using $_get on URL that is linked to id in database row

I'm trying to $_get part of URL in a href. What I want to happen is when you click the link you a redirected to that specific link. If that makes sense.
I have 2 functions:
First function, The list of links:
function showPosts() {
$sql = ("SELECT * FROM blog");
$query = mysql_query($sql);
while ($row = mysql_fetch_array($query)) {
$listId = $row["blog_id"];
$showTitle = $row["title"];
$showContent = $row["content"];
$showTimestamp = $row["timestamp"];
echo'
<div>
<h3>'.$showTitle.'</h3>
<div>'.$showContent.'</div>
<p>'.$showTimestamp.'</p>
</div>
';
}
}
Second function, redirect to link:
function viewPost() {
if(empty($_GET['id']) ) {
$listId = '';
} else {
$listId = $_GET['id'];
}
$sql = ("SELECT title, content, timestamp FROM blog WHERE blog_id='.$listId.'");
$query = mysql_query($sql);
while ($row = mysql_fetch_array($query)) {
$showTitle = $row["title"];
$showContent = $row["content"];
$showTimestamp = $row["timestamp"];
echo'
<div>
<h2>'.$showTitle.'</h2>
<p>'.$showTimestamp.'</p>
<div>'.$showContent.'</div>
</div>
';
}
}
As you can see i'm using $_get['id'] here and I read that $_get can be used to retrieve passed url parameters.
The way i have set up the URL is defined by a set of variables in a switch.
if(empty($_GET['task']) ) {
$task = 'show';
} else {
$task = $_GET['task'];
}
switch ($task){
case "create":
createPost();
die();
break;
case "save":
savePost();
die();
break;
case "show":
showPosts();
die();
break;
case "view":
viewPost();
die();
break;
default: echo'Something went wrong!';
}
Currently when I click a link, it redirects but all of the content related to that id is not there.
You are misusing the die() command in the switch. Die command is used to handle errors, it stops immediately the script execution and returns the error message set as argument. Try removing it.

MySQL PHP record checking

im having a simple mysql/php problem. so i am adding in Image titles for my website, and the code is displayed below. It works, but when you dont put a image, it shows up as blank. I need it to show up as 'No image title' (bc i will use this for image description to). It basically gets the image name, then takes the title from that row.
So how do i do it? :/ im still very new to PHP.
<?php
if (isset($imgtitleset))
{
$sql = "SELECT image_title FROM images WHERE image_name = '$image_main'";
$result = mysql_query ($sql);
while ($row = mysql_fetch_array($result))
{
$imgtitle= $row["image_title"];
echo "$imgtitle";
}
}
else {
echo 'no image title';
}
?>
Change the while loop like so:
while ($row = mysql_fetch_array($result)) {
$imgtitle= $row["image_title"];
if($imgtitle != '') {
echo $imgtitle;
} else {
echo 'no image title';
}
}
Also, I'm not sure what the $imgtitleset variable is for, but you can probably get rid of the if statement checking to see whether it's set.
Edit: the whole thing should probably look like this:
<?php
$sql = "SELECT image_title FROM images WHERE image_name = '$image_main'";
$result = mysql_query ($sql);
while ($row = mysql_fetch_array($result)) {
$imgtitle= $row["image_title"];
if($imgtitle != '') {
echo $imgtitle;
} else {
echo 'no image title';
}
}
?>
This all depends on what $imgtitleset is equal to. It is clearly set against something:
while ($row = mysql_fetch_array($result)) {
$imgtitle = $row["image_title"];
if (isset($imgtitle))
echo "$imgtitle";
else
echo 'no image title';
}
This would mean if nothing was found in the database then it will echo the no image title. However like I said, this could depend on what $imgtitleset is, maybe post the code for that?
If you only expect the select to return a single row, then use if rather than while and return the error on else:
<?php
if (isset($imgtitleset))
{
$sql = "SELECT image_title FROM images WHERE image_name = '$image_main'";
$result = mysql_query ($sql);
if ($row = mysql_fetch_array($result))
{
$imgtitle= $row["image_title"];
echo "$imgtitle";
}
else {
echo 'no image title';
}
}
?>

if account type = x go to link 1 else if account type = y got to link 2?

i have two types of users on my site, 'free' and 'premium'.
basically i have a messaging system that lists the message and the user who send the message.
when you click on the user's name/image it will link to 'profile.php?id=(user_id)'
what i am trying to do, is if the account type of the user who sent the message = 'free' then i want to take the user to a different link when clicked on.
i am really new to php and am not sure what to do, please can someone show me an example of how i can do this. here's my current code.
function:
function message_account_type() {
global $connection;
global $_SESSION;
global $profile_id;
global $message_id;
$query = "SELECT ptb_users.account_type, ptb_messages.from_user_id
FROM ptb_users, ptb_messages
WHERE ptb_messages.from_user_id = \"$profile_id\"
AND ptb_profiles.user_id = ptb_messages.from_user_id ";
$message_account_type = mysql_query($query, $connection);
confirm_query($query, $connection);
return $message_account_type;
}
php:
<?php
$message_account_type = message_account_type();
while ($type = mysql_fetch_array($message_account_type))
if ($type['account_type'] == 'Premium') {
echo "<?php echo "<a href=\"profile.php?id={$inbox['from_user_id']}\">{$inbox['display_name']}"; ?><? } ?>
<?php
$message_account_type = message_account_type();
while ($type = mysql_fetch_array($message_account_type))
if ($type['account_type'] == 'Free') {
echo "<?php echo "<a href=\"members.php?id={$inbox['from_user_id']}\">{$inbox['display_name']}"; ?><? } ?>
Your best bet is to serve up the relevant template file inside of profile.php. For example, on your profile.php you could do something like this:
$the_user = new User($_REQUEST['id']);
$include_file = ($the_user->account_type == 'free') ? 'free' : 'premium';
include($include_file.'-profile.php');
I seriously have NO IDEA why so many of you open and close <?php tags within a document for no reason...
well a shorter version would be:
<?php
function is_premium_user($profile_id)
{
$query = sprintf("SELECT ptb_users.account_type WHERE ptb_messages.from_user_id = '%s' AND account_type = 'Premium' LIMIT 1", $profile_id );
$result = mysql_query($query);
if (!mysql_fetch_assoc($result))
return false;
return true;
}
$message_account_type = message_account_type();
while ($type = mysql_fetch_array($message_account_type))
if (is_premium_user($profile_id))
echo ' <a href="profile.php?id='.$inbox['from_user_id'].'">'.$inbox['display_name'].'';
else
echo ' <a href="members.php?id='.$inbox['from_user_id'].'">'.$inbox['display_name'].'';
?>

Page including with mysql in php

I have a nice page including system here is the code for it
if(isset($HTTP_GET_VARS['mod']))
{
$page = $HTTP_GET_VARS['mod'];
}
else
{
$page = 'home';
}
switch($page)
{
case 'home':
require('home.php');
break;
default:
echo('Error: There is no file on this server with that name');
}
}
I am trying to get the case, require from a DB called pages there are 2 fields Name, Link i am trying to get all of the results from the table so it will display the pages
It's not particularly clear from your question, but my reading of it is that you want a way to check any value of $page against a link value in a db table (pages?), without having to write all possible values in to your switch statement,
If my understanding is correct, then the below is a quick-and-dirty function which should let you do this. In a live, heavily-trafficed environment you'd obviously need to build in caching so every page load doesn't hit the db, and strong input validation, neither of which are in the demo below, but this should at least give you an idea of where to go next.
Common library file:
/**
* Given a page name, see if we have an associated
* link in the db.
* If so, return the link value, else false
*/
function getTemplate($page)
{
// Check db to see if we have a link for this page
// On heavy-traffic sites, this should be cached out
$query = sprintf("SELECT link FROM pages WHERE name = '%s'",
mysql_real_escape_string($page));
$result = mysql_query($query, $db_cnx);
// Have we any results?
if (mysql_num_rows($result) > 0)
{
// Assumption: 'name' is unique in the db
$row = mysql_fetch_assoc($result);
return $row['link'];
}
else
{
return false;
}
}
Header.php:
include('common.lib.php');
if(isset($HTTP_GET_VARS['mod']))
{
$page = $HTTP_GET_VARS['mod'];
}
else
{
$page = 'home';
}
// Check whether our page has a link in the db
$template = get_template($page);
if($template)
{
require( $template );
}
else
{
// Got false back from get_template, no link found
echo('Error: There is no file on this server with that name');
}
$server_db = "YOUR_SERVER_DB";
$user_db = "YOUR_USER_DB";
$password_db = "YOUR_PASSWORD_DB";
$db_name = "YOUR_DB_NAME";
$table = "YOUR_TABLE_NAME";
$link = mysql_connect($server_db,$user_db,$password_db);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db($db_name);
$sql = sprintf("select Name, Link from %s",$table);
$rs = mysql_query($sql,$link);
while($row = mysql_fetch_assoc($rs)) {
echo "<a href='".$row['Link']."'>".$row['Name']."</a>";
}
mysql_free_result($rs);
mysql_close($link);

Categories