Jump to content

display database content


KevHopwood

Recommended Posts

Hi guys.

 

I am creating a cms for my own and am rather new to php and databases.

 

what i want is to simply display each item on my mysql database in a list and display each image with the title next to it.

 

I currentiy have this code on my index page:

<?php
 
include_once('include/connection.php');
include_once('include/article.php');

$app = new article;
$apps = $app->fetch_all();

?>
 
<html>

<head>
<title>App Donate</title>
<link rel="stylesheet" href="style.css" />
</head>

<body>

<ol>
    <?php foreach ($apps as $app) { ?>


         <li><a href="article.php?id=<?php echo $apps['app_id']; ?>">
          
             <img src="<?php echo $row_getdetails['app_img']; ?>" alt="Image from DB" />
             <?php echo $apps['app_title']; ?>

          </a>


    <?php } ?>
</li></ol>

</div>
</body>

</html>

include/article.php


<?php

class article {
public function fetch_all(){
    global $pdo;
      $query = $pdo->prepare("SELECT * FROM `apps`");
      $query->execute();
return $query->fetchAll();
              }

public function fetch_data($app_id) {
   global $pdo;

 $query = $pdo->prepare("SELECT * FROM apps WHERE app_id = ?");
  $query->bindValue(1, $app_id);
   $query->execute();

return $query->fetch();

}

}

?>

my index page can be found HERE

 

as you can see it has seen that there is 2 posts in my databace but it just is not displaying the content.

 

please can someone help?

 

THANK YOU.

Link to comment
https://forums.phpfreaks.com/topic/279856-display-database-content/
Share on other sites

Just a recommendation, but if you are new to databases and php then why are you starting out using Object-Oriented Programming Style? I would find a current book that shows procedural style programming that uses mysqli or PDO or a video tutorial.

<?php foreach ($apps as $app) { ?>

You're iterating over $apps creating an $app variable, so why are you using the array variable $apps instead of $app in your loop?

    <?php foreach ($apps as $app) { ?>


         <li><a href="article.php?id=<?php echo $apps['app_id']; ?>">
          
             <img src="<?php echo $row_getdetails['app_img']; ?>" alt="Image from DB" />
             <?php echo $apps['app_title']; ?>

          </a>


    <?php } ?>

And $row_getdetails doesn't seem to be a valid variable either. You need to learn to turn on error reporting and check your error logs, because I'm sure these things are producing at last warnings.

Plus $app is a new instance of a class it doesn't matter what you do it is going to create an error. I still say get the procedural style down pat so you can get a grasp of php and databases. Though turning on error reporting will help you out in the long run. :shrug: 

 

P.S. -> I know you can fetch a class and the following is a snippet of my website

    // Check that rows were returned:
    if ($result && $result->rowCount() > 0) {
        // Set the fetch mode:
        $result->setFetchMode(PDO::FETCH_CLASS, 'Page');
        // Records will be fetched in the view:
        include('views/index.html');
    } else { // Problem!
        throw new Exception('No content is available to be viewed at this time');
    }

;D

Hi all..

 

 

I did use a video tutorial.

 

I used this one: http://www.youtube.com/watch?v=vM6_OCAoiFg

 

i watched all 7 and it worked perfectly.

 

What I then did is added a new table to my mysql called "apps" and I replaced all the column names which where column_??? to app_???. I just assumbed that as I have changed the little details then it must work the same.

 

so can no one tell me where I am going wrong?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.