0

I am very knew to stack overflow and PHP PDO. I have done a lot of research into this but I still can not find a solution. I have a page where you can upload a Title and description to the database. When you upload it, it records the time you uploaded it. I have multiple div's which I wish to display information from the database (HTML below). In these divs I have a TITLE and a DESCRIPTION. I want to be able to display the most recent upload in the first div, second upload in the second div and so on. Here is the div:

       <div id="box1" class="box echovshome" onclick="location.href='';">
      <h1 style="text-align: left;">Title</h1>
       <article>
          <p>Description</p>
       </article>
      </div>


      <div id="box2" class="box echovshome" onclick="location.href='';">
      <h1 style="text-align: left;">Title</h1>
       <article>
          <p>Description</p>
       </article>
      </div>

Is this possible with PHP and PDO. I have got the PHP which selects the information from the Database but I just don't know how to echo it into the divs by the most recent upload. Php code below:

$sql = "SELECT * FROM latest";
$query = mysqli_query($con, $sql); 

How can I echo the php data into different divs in order of most recent?

All help appreciated
Thank you

Qirel
  • 25,449
  • 7
  • 45
  • 62
  • 3
    It would be nice to see what it was that you tried in order to have resolved this yourself, which would have shown us that you did research this before posting the question. When you try, you also learn. If someone just gives you an answer/solution, it would have all been for nothing and I say this for the/your future. You can then edit your post to contain something that may have failed; even that shows an effort on your part, and we're always glad to help :-) – Funk Forty Niner Aug 25 '17 at 18:01
  • 1
    *"I am very knew to stack overflow"* - You've been here since July/17 actually. You [posted a question](https://stackoverflow.com/q/44957110/1415724) also where we don't know if it's solved or not. None of them were accepted, nor have you commented under any of them. – Funk Forty Niner Aug 25 '17 at 18:02
  • If that is your actual PHP you have at least 2 errors. Have you tried an `order by` with your query? – chris85 Aug 25 '17 at 18:06
  • 2
    `mysqli_query` that isn't PDO anymore. https://stackoverflow.com/revisions/45887128/3 - it's a totally different animal. – Funk Forty Niner Aug 25 '17 at 18:14
  • @Fred-ii- isn't it, oh, Please help – Tech Endling Aug 25 '17 at 18:17
  • Can you show us `$con`, also are you fetching and outputting the contents already? – chris85 Aug 25 '17 at 18:18
  • Starting with the manual's a good bet http://php.net/manual/en/pdostatement.fetch.php - just remember not to mix that `mysqli_` with PDO; those different api's don't intermix. – Funk Forty Niner Aug 25 '17 at 18:19
  • @chris85 $con is just connecting to the database and I know that works because it works on uploading it to the database. – Tech Endling Aug 25 '17 at 18:19
  • @TechEndling The question remains if you connect with PDO or MySQLi. – Qirel Aug 25 '17 at 18:22
  • @Qirel I connected with MYSQLi – Tech Endling Aug 25 '17 at 18:27

1 Answers1

0

You simply sort the query by descending order (using ORDER BY DESC) when you query for data. Your code uses mysqli_, while you wrote the question about PDO - which one you use doesn't really matter that much, but I'll show an example with mysqli_*, since you used that in the code you provided in the question.

Using htmlspecialchars(), you would ensure that the HTML won't break if the text in the database contain characters like < or >, which could also be an XSS-attack. (This function should be used on output, and not input). nl2br() will transform newlines in the text to <br /> tags, which HTML reads as newlines. The $i counter is to define the correct ID of each <div>.

timestamp should be replaced by the column-name of your timestamp-column. This assumes that $con is a valid MySQLi connection object (initialized by new mysqli(..) or mysqli_connect(..)), and that the object is accessible to this part of the code.

<?php 
$i = 0;
$sql = "SELECT * FROM latest ORDER BY timestamp DESC";
$query = mysqli_query($con, $sql); 
if ($query) {
    while ($row = mysqli_fetch_assoc($query)) {
         $i++;
         ?>
         <div id="box<?= $i; ?>" class="box echovshome" onclick="location.href='';">
             <h1 style="text-align: left;"><?= htmlspecialchars($row['title']); ?></h1>
             <article>
                 <p><?= htmlspecialchars(nl2br($row['description'])); ?></p>
             </article>
         </div>
         <?php 
    }
} else {
    // Oh noes! Something bad happened! Query failed, use
    // mysqli_error($con)
    // to find the error. Log that, and display something else (not that message) to the user
}
Qirel
  • 25,449
  • 7
  • 45
  • 62
  • One question, how do you define the different divs. I know you said "The $i counter is to define the correct ID of each
    ." but I have a div called 4 but in the code $i = 0, so how do i define it to 4. Also i have multiple divs defined to different numbers so how do i do that. Thank you for all your help.
    – Tech Endling Aug 25 '17 at 18:37
  • `$i++` inside the `while` loop increments `$i` for each iteration. – Qirel Aug 25 '17 at 18:39
  • also, in the code it says "= htmlspecialchars($row['title']); ". Won't that echo out all the titles into that div. Thanks – Tech Endling Aug 25 '17 at 18:39
  • Because `$row` is the current row in the database, that will echo the title-column of that row corresponding to the description. – Qirel Aug 25 '17 at 18:41
  • Thank you for all your help – Tech Endling Aug 25 '17 at 18:42