How to upload articles with PHP

Hi everyone,

I was wondering how to upload articles with PHP when the articles have different structures. I mean, if the structure were the same, for example, a photo for the article, an extract and then the article…the code I would be something like this:

    try {
        $conexion = new PDO('mysql:host=localhost;dbname=Articles', 'root', '');
    } catch (PDOException $e) {
        header ('Location: error.php');
        echo "ERROR: ". $e->getMessage();
        die();
    }

//this is just to load all the articles:

    $statement = $conexion->prepare('SELECT  * FROM article');
    $statement->execute();
    $articles = $statement->fetchAll();

//then in HTML:

    <?php foreach ($articles as $article): ?>

        <h1> <?php echo $article['title'] ?> </h1>
        <img src="images/<?php echo $article['thumb'] ?>" alt="">
        <p> <?php echo $article['article'] ?> </p>
            
    <?php endforeach; ?>

The question is…what if inside the article I want to introduce some images? The amount of images will vary between the articles…so I don´t know how to deal with this problem. The same for the quotes, how do I say when and where has to be load in the article? I don´t know if I am explaining properly. But just think in an online newspaper. The articles have different structures, some have 3 images and 4 quotes, another has 2 images and no quotes…

Thanks in advice!

Using an html template like you have above with <h1><img><p> is fine only if all articles follow that very same structure. But if different articles vary in number of images, paragraphs, ect… that is never going to work.
What I do for some pages is to have a template with only the common elements, like title, author info, publishing date, which is pulled from the database. But then the actual body of the articles is an include file of html with all that article’s unique content.

Thanks for your response,

So you have to create an individual HTML file per article, I´m right? This is good for the performance of the website?

This is the method I use when the articles have very unique and varied structures. As always in php, there is more than one way to skin a cat. :scream_cat:
But I find this to be easy and efficient.
If possibly could be done with some complex programming and databases, with extra tables for images, then look up tables to match those images with articles, then possibly more to match images with individual sections or paragraphs of the articles, that would mean more tables to contain data about the sections/paragraphs. You can see how it could get complex doing it all programmatically.
But it would be simple if all the article follow the same structure.

I would think it is more efficient than doing it with more complex code and database structures.
The down side is the manual work in formatting the articles into html.

1 Like

If you have only a limited number of possible article formats I think having a template for each format could work.

eg.
“title-img-p-img-p.tpl.php”
“title-img-p-p-img-p.tpl.php”
“title-img-p-img-img-p.tpl.php”
etc.

1 Like

[quote=“SamA74”]This is the method I use when the articles have very unique and varied structures. As always in php, there is more than one way to skin a cat. :scream_cat:
But I find this to be easy and efficient.
If possibly could be done with some complex programming and databases, with extra tables for images, then look up tables to match those images with articles, then possibly more to match images with individual sections or paragraphs of the articles, that would mean more tables to contain data about the sections/paragraphs. You can see how it could get complex doing it all programmatically.
But it would be simple if all the article follow the same structure.[/quote]

It´s not a bad idea, the only thing is the number or html files you will have. But I´m thinking a little bit of this possibility, and I suppose that you have to introduce at least the name of the html file so that you can load the article dynamically. Something like:

include "<?php echo $article['file_name'] ?>.php";

[quote=“Mittineague”]If you have only a limited number of possible article formats I think having a template for each format could work.

eg.
“title-img-p-img-p.tpl.php”
“title-img-p-p-img-p.tpl.php”
“title-img-p-img-img-p.tpl.php”
etc.[/quote]

It´s not a bad Idea, could also work. Thanks!

You could store the filename in the database, but another option is to name your files in a way that it can be identified from the data. Possibly using the article’s ID, title or both.
If the file was called:-
12-article.php
call it with:-
include $article['id'].'-article.php' ;

or if you name the file:-
All-About-Fish.php
call it with:-
include str_replace(' ', '-', $article['title']).'.php' ;

So you don’t necessarily need another column in your table.

Interesting, I will try, I suppose is better to include all the article files in another folder.

Thanks!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.