Jump to content

Recommended Posts

Hi again PhpFreakz, yet again I have some issues with my PHP part of my website.
 
A brief summary, I've created an simple CMS for my website so that I am able to add news and results faster and easier.
The CMS works great I just have some issues with the finalizing part, so that it also works and looks good with my website.
 
I have in total 3 issues, I will be stating the easiest first and follow up to the hardest. As I'm sure that many people can help me with some of them and might not be able to solve the difficult ones.
 
If you head to this page, you can see my articles I've added to my website. I have an issue with the maximum allowed articles on this page. If I were to add several more articles it would just ignore my design and keep on going. I would like to set a maximum of 4 shown articles, how can i do that?
 
Next up is another fairly easy one. I have - on same page as previous issue - an issue with the articles position. Currently the newest article created is on the bottom, I would like to diverse that so that the top has the newest news and it then goes down to the oldest.
 
My third issue is a fairly complex one (At least for me) and is also an issue on several other pages. If you notice when you click on one of the news in the article page you get redirected to a new page where you can read the full article, problem is that I would like this "new page" to be opened in the same page as where the articles are. For example if you were to change on the navigation, then you get redirected on the same page but only on the "box" that appears. I would like the same effect from when my readers click on one of my articles, they just get a box with the full article and a "go back button". How could i possibly do this?
 
Here are the codes I think are necessary to fix my issues. Let me know if you need anything else and I'll gladly add those.
 
For 1st and 2nd issue:
HTML + Some PHP part of index.php
<?php foreach ($articles as $article) { ?>
<h1 class="newsh1"><a href="article.php?id=<?php echo $article['Id'] ?>"><?php echo $article['title'] ?></a></h1>
<p class="newstext">
    <?php echo $article[ 'short'] ?>
</p>
<p class="newsby">Artikel skrevet af
    <a class="newsauthor" href="#!/page_More">
        <?php echo $article[ 'author'] ?>
    </a>| <span class="newsdate"><?php echo date('j. F o', $article['date']); ?></span>
</p>
<div class="table"></div>
<?php } ?>

For 3rd issue:

Full code of article.php

<?php include_once( 'includes/connection.php'); include_once( 'includes/articles.php'); $article=new Article; if (isset($_GET['id'])) { $id = $_GET['id']; $data = $article->fetch_data($id); ?>
<html>

<head>
    <title>CMS System</title>
    <link rel="stylesheet" href="css/cms.css" />
</head>

<body>
    <div class="container">
        <h3><?php echo $data['title'] ?></h3>
        <small class="margin-left:20px;">Skrevet af <?php echo $data['author'] ?> den <?php echo date('j. F o', $data['date']); ?></small>

        <p>
            <?php echo $data[ 'body'] ?>
        </p>

        <a href="index.php">← Back</a>
    </div>
</body>

</html>

	<?php } else { header('Location: index.php'); exit(); }	?>

HTML+PHP part on index.php

<li id="Nyheder">
    <div class="box1">
        <div class="inner">
            <a href="#" class="close" data-type="close"><span></span></a>
            <div class="news">
                <h2>Senest nyt - Side 1</h2>
                <?php foreach ($articles as $article) { ?>
                <h1 class="newsh1"><a href="article.php?id=<?php echo $article['Id'] ?>"><?php echo $article['title'] ?></a></h1>
                <p class="newstext">
                    <?php echo $article[ 'short'] ?>
                </p>
                <p class="newsby">Artikel skrevet af
                    <a class="newsauthor" href="#!/page_More">
                        <?php echo $article[ 'author'] ?>
                    </a>| <span class="newsdate"><?php echo date('j. F o', $article['date']); ?></span>
                </p>
                <div class="table"></div>
                <?php } ?>

            </div>
            <div class="oldernews"><span class="newsold"><a href="#!/Nyheder_page2"><strong>Ældre artikler ></strong></a></span></span>
            </div>
        </div>
    </div>
</li>
Edited by rec0il
Link to comment
https://forums.phpfreaks.com/topic/288683-own-cms-system-news-articles-issues/
Share on other sites

 

 

If you head to this page, you can see my articles I've added to my website. I have an issue with the maximum allowed articles on this page. If I were to add several more articles it would just ignore my design and keep on going. I would like to set a maximum of 4 articles, how can i do that?

Apply a LIMIT to your SQL query, eg

SELECT * FROM articles LIMIT 4

 

 

Next up is another fairly easy one. I have - on same page as previous issue - an issue with the articles position. Currently the newest article created is on the bottom, I would like to diverse that so that the top has the newest news and it then goes down to the oldest.

Again modify you SQL query which orders the results by the article id or publishing date in descending order, example

SELECT * FROM articles ORDER BY article_id LIMIT 4

 

Apply a LIMIT to your SQL query, eg

SELECT * FROM articles LIMIT 4

Again modify you SQL query which orders the results by the article id or publishing date in descending order, example

SELECT * FROM articles ORDER BY article_id LIMIT 4

Wow, thanks for the quick answer Ch0cu3r. Both issues are now fixed and will be uploaded on the online website soon. Thanks alot

Edited by rec0il

Well you need to alter the example queries I provided so the table/column names match the ones you're using in your actual database!

 

Also why are you using globals in a class? If your class requires the $pdo object you should pass it to the Article object on initiation and define it as a class property, this is refereed to as Dependency Injection, Example

class Article

    private $db;

    public function __construct(PDO $db)
    {
        $this->db = $db;
    }

    public function fetch_all()
    {
        $this->db->prepare(... );

        // ...etc
    }
}

$pdo = new PDO(... );

$article = new Article($pdo); // pass the pdo instance on object initialisation
$articles = $article->fetc_all();

 

Well you need to alter the example queries I provided so the table/column names match the ones you're using in your actual database!

 

Also why are you using globals in a class? If your class requires the $pdo object you should pass it to the Article object on initiation and define it as a class property, this is refereed to as Dependency Injection, Example

class Article

    private $db;

    public function __construct(PDO $db)
    {
        $this->db = $db;
    }

    public function fetch_all()
    {
        $this->db->prepare(... );

        // ...etc
    }
}

$pdo = new PDO(... );

$article = new Article($pdo); // pass the pdo instance on object initialisation
$articles = $article->fetc_all();

Yeah not sure why I didn't do that. I've updated it and everything works great now. Still got issues with the 3rd one though. Hope to see someone smart who can help me out.

Edited by rec0il
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.