Hey guys hope someone can help with this.
I am using substr to give an abstract of articles. Now the problem is when you click the link to see the whole article you still see the substr version. Now this obviously happens because of the way the code is.
I need to make another query that will say if someone clicks the link then display the full article without the substr. I have no idea how to go about this can anyone help? While I am learning my PHP knowledge is fairly limited.
<?php
class MyCMS
{
function get_content($id = "")
{
if ($id != ""):
$id = mysql_real_escape_string($id);
$sql = "SELECT * FROM content WHERE blog_id = '$id'";
$return = '<p> Go Back To Content Page</p>';
else:
$sql = "SELECT blog_id, title, date, body FROM content ORDER BY blog_id DESC LIMIT 0, 3";
endif;
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) !=0):
while($row = mysql_fetch_assoc($res))
{
echo '<div id="roundedbox"><h2>' . $row['title'] . ' </h2>';
echo '<div id="date"><h5><p>' . $row['date'] . '</p></h5></div>';
echo substr('<p>' . $row['body'] . '</p>',0, 90)." .... "." read more </div>";
}
else:
echo '<p> UH OOH! THERE IS NO SUCH PAGE IT DOES\'T EXIST </p>';
echo $return;
endif;
}
}
?>
Normally you would encapsulate the article into a data class (alias "Model", see MVC architecture). This model holds all the data of the article and have a method to return the abstract or the long version of the article. To identify the version the client would like to see, pass another argument to your URL.
class Article {
protected $text;
protected $title;
public __construct ($title, $text) {
$this->title = $title;
$this->text = $text;
}
/**
* Returns the short excerpt of the article
*/
public getShortAbstract () {
// your substr() function
}
/**
* Returns the full article
*/
public getText () {
return $this->text;
}
}
Add another argument to the function to determine whether it returns the full article or an excerpt:
<?php
class MyCMS
{
function get_content($id = "", $excerpt = FALSE)
{
if ($id != ""):
$id = mysql_real_escape_string($id);
$sql = "SELECT * FROM content WHERE blog_id = '$id'";
$return = '<p> Go Back To Content Page</p>';
else:
$sql = "SELECT blog_id, title, date, body FROM content ORDER BY blog_id DESC LIMIT 0, 3";
endif;
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) !=0):
while($row = mysql_fetch_assoc($res))
{
echo '<div id="roundedbox"><h2>' . $row['title'] . ' </h2>';
echo '<div id="date"><h5><p>' . $row['date'] . '</p></h5></div>';
if ($excerpt):
echo substr('<p>' . $row['body'] . '</p>',0, 90)." .... "." read more </div>";
else:
echo '<p>' . $row['body'] . '</p>';
}
else:
echo '<p> UH OOH! THERE IS NO SUCH PAGE IT DOES\'T EXIST </p>';
echo $return;
endif;
}
}
?>
Then when you are calling the get_content() function, pass it the id and excerpt flag as follows:
get_content(123, TRUE); //for excerpt
get_content(123); //for full post
I don't think you want to do this with PHP, because when you're doing this with PHP you have to refresh the whole page.
I would advise you to use Javascript for this, like the jQuery library slideToggle.
With this method, Google will still index all the content.
Related
I was experimenting if I could use a mySQL database to store CSS settings. I set up a simple database "colors" with one table "color" that had simple structure tag and color columns. In that, one row is h1 => red.
<?php
//function to dynamically change CSS
$tag = 'h1';
$q = "SELECT * FROM `colors` WHERE `tag`='" . $tag . "'" ;
echo $q . "<br>";
$query = mysqli_query($link, $q);
if ($row = mysqli_fetch_assoc($query))
{
echo $row['color'];
} else
{
echo "error - no such tag";
}
?>
When I tried to convert to a function, the code does not work at all.
<?php
//function to dynamically change CSS
function getCSS($tag)
{
$tag = 'h1';
$q = "SELECT * FROM `colors` WHERE `tag`='" . $tag . "'" ;
echo $q . "<br>";
$query = mysqli_query($link, $q);
if ($row = mysqli_fetch_assoc($query))
{
echo $row['color'];
} else
{
echo "error - no such tag";
}
}
getCSS('h1');
?>
Help please?
My guess is that in
$query = mysqli_query($link, $q);
$link goes out of scope and is empty. You should pass it to the function as well.
For the record: using $tag without escaping could be an sql injection attack possibility.
in function, there is no $link, you shoud define it as a global variable.
At the start of your function add a reference to your global DB link:
function getCSS($tag) {
global $link;
...
This should work:
<?php
$link = mysqli_connect('server_host', 'user', 'password', 'database') OR die('Could not connect because: '.mysqli_connect_error());
//function to dynamically change CSS
function getCSS($link, $tag){
$q = 'SELECT * FROM colors WHERE tag = "' . $tag . '"' ;
$r = mysqli_query($link, $q);
if(mysqli_num_rows($r)>0){ // check if there are results
while($row = mysqli_fetch_assoc($r)){
//echo '<pre>';
//print_r($row); // print the result array for debugging
//echo '</pre>';
echo $row['color'] . '<br />';
}
return $row;
} else { // if no result is found
echo 'No such tag';
}
}
// test it:
echo '<br />if tag is h1<br />';
getCSS($link, 'h1');
echo '<br />if tag is h2<br />';
getCSS($link, 'h2');
?>
Hey guys im writting a CMS but im having problems and need a fresh pair of eyes at this. I am getting this error: Parse error: syntax error, unexpected '}' on the line just above the last else statement but there is no reason for it.
The idea is to have display a substr of the articles and then click them to get the full article. Can someone have a look at my code and tell me where im going wrong or if this will even work.
class MyCMS
{
function get_content($id = "", $excerpt = FALSE)
{
if ($id != ""):
$id = mysql_real_escape_string($id);
$sql = "SELECT * FROM content WHERE blog_id = '$id'";
$return = '<p> Go Back To Content Page</p>';
else:
$sql = "SELECT blog_id, title, date, body FROM content ORDER BY blog_id DESC LIMIT 0, 3";
endif;
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) !=0):
while($row = mysql_fetch_assoc($res))
{
echo '<div id="roundedbox"><h2>' . $row['title'] . ' </h2>';
echo '<div id="date"><h5><p>' . $row['date'] . '</p></h5></div>';
if ($excerpt):
echo substr('<p>' . $row['body'] . '</p>',0, 90)." .... "." read more </div>";
else:
echo '<p>' . $row['body'] . '</p>';
}
else:
echo '<p> UH OOH! THERE IS NO SUCH PAGE IT DOES\'T EXIST </p>';
echo $return;
endif;
}
}
Then in my main page i am calling the code as follows:
<?php
include 'cms.php';
$obj = new MyCMS();
?>
<?php
if(isset($_GET['id'])):
echo $obj->get_content($_GET['id'], TRUE);
else:
echo $obj->get_content($_GET['id']);
endif;
?
You forgot to add endif(for the if statement inside loop) and } (for closing class). Please check this code.
class MyCMS
{
function get_content($id = "", $excerpt = FALSE)
{
if ($id != ""):
$id = mysql_real_escape_string($id);
$sql = "SELECT * FROM content WHERE blog_id = '$id'";
$return = '<p> Go Back To Content Page</p>';
else:
$sql = "SELECT blog_id, title, date, body FROM content ORDER BY blog_id DESC LIMIT 0, 3";
endif;
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) !=0):
while($row = mysql_fetch_assoc($res))
{
echo '<div id="roundedbox"><h2>' . $row['title'] . ' </h2>';
echo '<div id="date"><h5><p>' . $row['date'] . '</p></h5></div>';
if ($excerpt):
echo substr('<p>' . $row['body'] . '</p>',0, 90)." .... "." read more </div>";
else:
echo '<p>' . $row['body'] . '</p>';
endif;
}
else:
echo '<p> UH OOH! THERE IS NO SUCH PAGE IT DOES\'T EXIST </p>';
echo $return;
endif;
}
}
Also please notice that second code logic is absolutely wrong..
<?php
include 'cms.php';
$obj = new MyCMS();
?>
<?php
if(isset($_GET['id'])):
echo $obj->get_content($_GET['id'], TRUE);
else:
// echo $obj->get_content($_GET['id']); // This line is executing when $_GET['id'] is undefined.So dont use $_['id'] here.
endif;
?>
I wonder if someone can help i am trying to incorporate a live search into my website, I have found a tutorial and the appearance side of things is now working, could someone please tell me how i can edit the follow PHP to pull information from only one table.
Table - movies
Fields to echo for each result - Movie, description, Image
At the moment it is pulling information from 2 tables successfully one displays the content of the search the other provides information for category dividers, What i need is to remove the category aspect and pull information from the a single table.
Apologies my PHP knowledge is very limited, hope this best describes the problem.
<p id="searchresults">
<?php
// PHP5 Implementation - uses MySQLi.
// mysqli('localhost', 'yourUsername', 'yourPassword', 'yourDatabase');
$db = new mysqli('localhost', 'yourUsername', 'yourPassword', 'yourDatabase');
if(!$db) {
// Show error if we cannot connect.
echo 'ERROR: Could not connect to the database.';
} else {
// Is there a posted query string?
if(isset($_POST['queryString'])) {
$queryString = $db->real_escape_string($_POST['queryString']);
// Is the string length greater than 0?
if(strlen($queryString) >0) {
$query = $db->query("SELECT * FROM search s INNER JOIN categories c ON s.cat_id = c.cid WHERE name LIKE '%" . $queryString . "%' ORDER BY cat_id LIMIT 8");
if($query) {
// While there are results loop through them - fetching an Object.
// Store the category id
$catid = 0;
while ($result = $query ->fetch_object()) {
if($result->cat_id != $catid) { // check if the category changed
echo '<span class="category">'.$result->cat_name.'</span>';
$catid = $result->cat_id;
}
echo '<a href="'.$result->url.'">';
echo '<img src="search_images/'.$result->img.'" alt="" />';
$name = $result->name;
if(strlen($name) > 35) {
$name = substr($name, 0, 35) . "...";
}
echo '<span class="searchheading">'.$name.'</span>';
$description = $result->desc;
if(strlen($description) > 80) {
$description = substr($description, 0, 80) . "...";
}
echo '<span>'.$description.'</span></a>';
}
echo '<span class="seperator">Nothing interesting here? Try the sitemap.</span><br class="break" />';
} else {
echo 'ERROR: There was a problem with the query.';
}
} else {
// Dont do anything.
} // There is a queryString.
} else {
echo 'There should be no direct access to this script!';
}
}
?>
</p>
Replace
SELECT * FROM search s INNER JOIN categories c ON s.cat_id = c.cid WHERE name LIKE '%" . $queryString . "%' ORDER BY cat_id LIMIT 8
with
SELECT * FROM movies WHERE movie LIKE '%" . $queryString . "%' ORDER BY movie LIMIT 8
would be a good start.
Can you carry on from there?
Aside - you wrote 'Movie' for the field name in your description above - I've used a lower case 'M' so you may need to change it.
[edit] In reply to comment #1:
Let's start off simple - replace if ($query) { ... } with
if ($query) {
while ($result = $query ->fetch_object()) { // this line loops through all the results
echo '<img src="search_images/'.$result->image.' />'; // check capital I on 'image'
echo '<strong>'.$result->movie.'</strong>';
echo $result->description.'<br />';
}
}
Basically, you just put $result->myFieldName inside of the while loop to get the data out you want.
Hopefully that should be enough get you going =]
I guess:
<p id="searchresults">
<?php
// PHP5 Implementation - uses MySQLi.
// mysqli('localhost', 'yourUsername', 'yourPassword', 'yourDatabase');
$db = new mysqli('localhost', 'yourUsername', 'yourPassword', 'yourDatabase');
if(!$db) {
// Show error if we cannot connect.
echo 'ERROR: Could not connect to the database.';
} else {
// Is there a posted query string?
if(isset($_POST['queryString'])) {
$queryString = $db->real_escape_string($_POST['queryString']);
// Is the string length greater than 0?
if(strlen($queryString) >0) {
$query = $db->query("SELECT * FROM movies WHERE Movie LIKE '%" . $queryString . "%' ORDER BY Movie LIMIT 8");
if($query) {
// While there are results loop through them - fetching an Object.
while ($result = $query ->fetch_object()) {
echo '<a href="'.$result->url.'">';
echo '<img src="search_images/'.$result->img.'" alt="" />';
$name = $result->movie;
if(strlen($name) > 35) {
$name = substr($name, 0, 35) . "...";
}
echo '<span class="searchheading">'.$name.'</span>';
$description = $result->description;
if(strlen($description) > 80) {
$description = substr($description, 0, 80) . "...";
}
echo '<span>'.$description.'</span></a>';
}
echo '<span class="seperator">Nothing interesting here? Try the sitemap.</span><br class="break" />';
} else {
echo 'ERROR: There was a problem with the query.';
}
} else {
// Dont do anything.
} // There is a queryString.
} else {
echo 'There should be no direct access to this script!';
}
}
?>
</p>
I am making a blog in which I want to show just the summary of all posts on the home page rather than whole posts.
Below is the function that I use to get the posts on my home page.
function get_content($id = '')
{
if ($id != ""):
$id = mysql_real_escape_string($id);
$sql = "SELECT * from cms_content WHERE id = '$id'";
$return = '<p>Go back to Home page</p>';
echo $return;
else:
$sql = "select * from cms_content ORDER BY id DESC";
endif;
$res = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($res) != 0):
while ($row = mysql_fetch_assoc($res)) {
echo '<h1>' . $row['title'] . '</h1>';
echo '<p>' . "by: " . $row['author'] . ", posted on: " . $row['date'] . '<p>';
echo '<p>' . $row['body'] . '</p><br />';
}
else:
echo '<p>We are really very sorry, this page does not exist!</p>';
endif;
}
And then simply I use this php code to get the posts:
<?php
if (isset($_GET['id'])) :
$obj->get_content($_GET['id']);
else :
$obj->get_content();
endif;
?>
So how to get the summary only using php function or mysql as I know that we can use mysql query to get limit the words?
function sumarize($your_string, $limit_lines){
$count = 0;
foreach(explode("\n", $your_string) as $line){
$count++;
echo $line."\n";
if ($count == $limit_lines) break;
}
}
sumarize($row['body'], 10);
will show first ten lines
1-lined:
echo '<p>'.implode("\n",array_slice(explode("\n",$row['body']),0,10)).'</p>';
instead of line with $row['body']
I am having this website http://www.finalyearondesk.com . My blogs post link are set like this.. http://www.finalyearondesk.com/index.php?id=28 . I want it to set like this ... finalyearondesk.com/2011/09/22/how-to-recover-ubuntu-after-it-is-crashed/ .
I am using the following function to get these posts...
function get_content($id = '') {
if($id != ""):
$id = mysql_real_escape_string($id);
$sql = "SELECT * from cms_content WHERE id = '$id'";
$return = '<p>Go back to Home page</p>';
echo $return;
else:
$sql = "select * from cms_content ORDER BY id DESC";
endif;
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) != 0):
while($row = mysql_fetch_assoc($res)) {
echo '<h1>' . $row['title'] . '</h1>';
echo '<p>' . "By: " . '<font color="orange">' . $row['author'] . '</font>' . ", Posted on: " . $row['date'] . '<p>';
echo '<p>' . $row['body'] . '</p><br />';
}
else:
echo '<p>We are really very sorry, this page does not exist!</p>';
endif;
}
And I am using this code to dispaly it on my index.php page...
<?php
if (isset($_GET['id'])) :
$obj -> get_content($_GET['id']);
else :
$obj -> get_content_summary();
endif;
?>
Any suggestions how to do this? And can we do this by using .htaccess?
The unfortunate thing about using mod_rewrite is that the data you are supplying in the form of a url is not the best way to query a database. But none the less you have year, month, day and title variables so you will need to rewrite your get_content function to query soomething like (depending on how you date is stored in the database.):
select * from cms_content
WHERE date='mktime(0,0,0,$month,$day,$year)'
AND title='$title'
ORDER BY id DESC
.htaccess would be something like:
RewriteRule ^(.*)/(.*)/(.*)/(.*)$ index.php?year=$1&month=$2&day=$3&title=$4