php full message show - php

After few word i put a read more button when i click read more it's show full message, it's Ok, but It's show all my post's full message. I need individual post's full message, Can you please tell me what is the wrong in my code bellow:
This is a function i used in my code in different file, name: "func.php"
<?php
function truncate($mytext,$link,$var,$id) {
//Number of characters to show
$chars = 200;
$mytext = substr($mytext,0,$chars);
$mytext = substr($mytext,0,strrpos($mytext,' '));
$mytext = $mytext." <a href='$link?$var=$id'>read more...</a>";
return $mytext;
}
?>
This is index.php page code:
<?php
include "db/db.php";
$upload_path = "secure/content/blogpostimg";
$sql= mysql_query("SELECT * FROM blog_post ORDER BY post_id DESC");
while ($rel = mysql_fetch_assoc($sql))
{
$id = $rel['post_id'];
$sub = $rel['subject'];
$imgname = $rel['img_name'];
$img = $rel ['image'];
$msg = $rel['message'];
$date = $rel['date'];
$poster = $rel['poster'];
$cat_name = $rel['cat_name'];
echo "<h1>". "$sub" ."</h1>". "<br/>";
echo '<img src="' . $upload_path . '/' . $imgname . '" width="200" /> ';
include_once("func.php");
echo truncate($rel['message'],"index.php","post_id",$rel['post_id']);
}
?>

You need to do a check on index.php to see if the $_GET['post_id'] is set, and if so, display a different MySQL select
if (isset($_GET['post_id']) && $_GET['post_id'] != '') {
$p_id = (int) $_GET['post_id'];
$sql= mysql_query("SELECT * FROM blog_post WHERE post_id = '{$p_id}' ORDER BY post_id DESC");
} else {
$sql= mysql_query("SELECT * FROM blog_post ORDER BY post_id DESC");
}
// Your original code. Only at the end, if $p_id is set, display a full post view instead of the truncate() function

Related

Return data from mysql where value equals URI

I'm trying to return a row from my database however, when I replace the page='url' with $filePath = $_SERVER["REQUEST_URI"]; page='.$filePath.' it returns nothing.
I'm assuming the answer is simple however, I can't see to find the solution.
Full Code
$filePath = $_SERVER["REQUEST_URI"];
$query = "SELECT * FROM Meta WHERE page=' . $filePath . '";
$result = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($result)) { ?>
<title><?php echo $row["title"]; ?></title>
<?php } ?>
try the query like this
$query = "SELECT * FROM Meta WHERE page = '" . $filePath . "' ";

How do i change the URL id?

Hi how do i change the URL id if the id is above max id in the db?
If i get the id from a database and use it in a read more button to make people read the data in a new page with the id as different url query?
--The real question
How do i make sure that if there only are 4 news in the db that if you write etc
newsTest.php?id=5 in the browser the browser will not execute or go back to max page?
//read more button
Læs mere
<?php
include_once 'includes/db.php';
$sql1 = "SELECT COUNT(id) AS total FROM rock_news ";
$result1 = $dbCon->query($sql1);
$row1 = $result1->fetch_assoc();
$total_pages = $row1["total"];
$thisVar = $_GET['id'];
if($total_pages >= $thisVar){
echo "alt er godt";
if(isset($_GET['id']) && !empty($_GET['id'])){
$id = $_GET['id'];
// mod sqlinjection
$id = $dbCon->real_escape_string($id);
$sql = " SELECT id, heading, subheading, description, created, author FROM rock_news WHERE id = " . $id ;
$result = $dbCon->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_object();
$id = $row->id;
$heading = utf8_encode($row->heading);
$subheading = utf8_encode($row->subheading);
$description = utf8_encode($row->description);
$created = $row->created;
$author = utf8_encode($row->author);
$output .= $id . "<br>" . $heading . "<br>" . $description;
};
// udskriv output til bruger
echo $output;
};
} else {
$_GET['id'] = $total_pages ;
echo "nothing";
}
?>
I am very new to php
Check if total_pages is less than id then show show total_pages id.
<?php
include_once 'includes/db.php';
$sql1 = "SELECT COUNT(id) AS total FROM rock_news ";
$result1 = $dbCon->query($sql1);
$row1 = $result1->fetch_assoc();
if(isset($_GET['id']) && !empty($_GET['id'])){
$total_pages = $row1["total"];
$thisVar = $_GET['id'];
if($total_pages < $thisVar){
$thisVar=$total_pages
$id = $thisVar;
// mod sqlinjection
$id = $dbCon->real_escape_string($id);
$sql = " SELECT id, heading, subheading, description, created, author FROM rock_news WHERE id = " . $id ;

I want to show all the images name in the browser . how will that be possible?

Database Screenshot
Query:
$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
$qry = "SELECT `image` FROM product_images WHERE product_id = '".$id."';";
$res = mysqli_query($conn,$qry);
$row = mysqli_fetch_assoc($res);
Question:
I want to show all the images name in the browser . how will that be possible?
Try this:
<?php
$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
$qry = 'SELECT image FROM product_images WHERE product_id = \'' . $conn->real_escape_string($id) . '\'';
$res = mysqli_query($qry, $conn);
while($row = mysqli_fetch_assoc($res))
echo $row . '<br />' . "\n";
}
<?php
// absolute or relative path
$base_path = 'http://domain.com/uploads/';
$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
$qry = "SELECT image FROM product_images WHERE product_id = '".$id."'";
$res = mysqli_query($conn,$qry);
if (mysqli_num_rows($res) > 0)
{
while ($row = mysqli_fetch_assoc($res))
{
$image_name = $row['image'];
$image_path = $base_path.$image_name;
// To display image name
echo 'Image name = '.$image_name.'<br />';
//To display image
echo '<img src="'.$image_path.'" width="" height="" alt="" title="" />';
}
}
?>

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!

Dynamic pages using php

I am trying to create dynamic pages on my website, but it fails.
When I try the code on Xampp it works perfect. There is another thing that I don't understand.
It will catch the id, but not the title or anything with characters from the database.
When I try $title = $_GET['title']; it won't work. It works only with $_GET['id'];
Any help?
Here is the code:
index.php
<?php
include('inc/code.inc.php');
$fetch = mysql_query("SELECT * FROM `star` ORDER BY `title`");
while ($output = mysql_fetch_assoc($fetch))
{
echo ''. $output['title'] .'<br />';
}
?>
run.inc.php
<?php
include_once('inc/code.inc.php');
$newID = $_GET['id'];
$fetch = mysql_query("SELECT * FROM `star` WHERE `id` = $newID");
while ($output = mysql_fetch_assoc($fetch))
{
echo $output['title'] . '<br />' . $output['explain'];
}
?>
Here is the code that won't work:
index.php
<?php
include('inc/code.inc.php');
$fetch = mysql_query("SELECT * FROM `star` ORDER BY `title`");
while ($output = mysql_fetch_assoc($fetch))
{
echo ''. $output['title'] .'<br />';
}
?>
run.inc.php
<?php
include_once('inc/code.inc.php');
$newID = $_GET['title'];
$fetch = mysql_query("SELECT * FROM `star` WHERE `title` = $newID");
while ($output = mysql_fetch_assoc($fetch))
{
echo $output['title'] . '<br />' . $output['explain'];
}
?>
Add title to your URL GET parameter please..Like you have added ID
<?php
include('inc/code.inc.php');
$fetch = mysql_query("SELECT * FROM `star` ORDER BY `title`");
while ($output = mysql_fetch_assoc($fetch))
{
echo '<a href = "run.inc.php?id='. $output['id'] .'"'.'?title='. $output['title'] .'</a><br />';
}
?>
Modify the url according to your needs please.
If you are comparing a string like $_GET['title'] to the column, you need to escape it (as opposed to a numeric ID, which does not need to be escaped).
Try this for run.inc.php
$newID = $_GET['title'];
$fetch = mysql_query("SELECT * FROM `star` WHERE `title` = '$newID'");
while ($output = mysql_fetch_assoc($fetch))
{
echo $output['title'] . '<br />' . $output['explain'];
}
?>

Categories