简体   繁体   中英

How to display a chunk of code from mysql rows using PHP?

I am trying to get my head around displaying a chunk of code pulled from MySQL database records but cannot seem to get it working. Any help would be much appreciated.

I have this gallery script which requires a chunk of code to display an image within the gallery:

<ul data-category-name="ALL">

<li data-url="link: htpp://www.here.com" data-target="_blank">
<img src="content/media/thumbnails/framephoto1.jpg" alt="custom alt"/>
    <div data-thumbnail-content1="">
    <p class="centerDark">Photo Name</p>
        <p class="centerNormalDark">Photo Desc</p>
</div>
</li>
<ul>

I thought I would try and connect it up so to a MySQL database so I could simply add the details of each photo to rows in a database and then this script will just pull them and place them in the order that I want without me having to manually add the code snippet everytime I add a new photo.

So far I have this code:

<?php
echo "<table style='border: solid 1px black;'>";
echo "<tr><th>ID</th><th>Photo Name</th><th>Value</th></tr>";

class TableRows extends RecursiveIteratorIterator { 
function __construct($it) { 
    parent::__construct($it, self::LEAVES_ONLY); 
}

function current() {
return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>";
}

function beginChildren() { 
    echo "<tr>"; 
} 

function endChildren() { 
    echo "</tr>" . "\n";
} 
} 


$servername = "localhost";
$username = "dbusername";
$password = "dbpassword";
$dbname = "dbname";

 try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT id, name, value FROM photos ORDER BY value DESC"); 
$stmt->execute();

// set the resulting array to associative
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC); 
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) { 
    echo $v;
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
echo "</table>";
?> 

The problem with this display is it is creating a table with rows and columns to display the text so I was thinking of adding the above snippet to a cell within the table (code_snippet) and then simply calling that to be displayed for each row without a table or any other code and have them ordered in value?

Is anyone able to help me work out how to strip out this table and just display the one cell of code?

Or if not then how to add in the relevant cells into the code snippet for each cell?

Thank you in advance.

UPDATED:

Hiya. Sorry but that has totally thrown me. This is what I have at the moment for the connection:

try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT * FROM photos ORDER BY value DESC"); 
$stmt->execute($all);
} catch (PDOException $e) {
echo 'Error '. $e->getMessage();
}


try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT * FROM photos WHERE type=1 ORDER BY value DESC"); 
$stmt->execute($people);
} catch (PDOException $e) {
echo 'Error '. $e->getMessage();
}

That is now in the dbconnect file. Then I have tried adding these two snippets below into their relevant locations into the main document where I would like them both to work. :>/ The call is still made for all records which it shows but doesn't do the separate call to the database.

For all photos to be displayed:

<?php foreach($stmt->fetchAll($all) as $p): ?>
<li data-url="link: <?= $p['link'] ?>" data-target="_blank">
    <img src="<?= $p['image'] ?>" alt="custom alt"/>
    <div data-thumbnail-content1="">
        <p class="centerDark"><?= $p['name'] ?></p>
        <p class="centerNormalDark"><?= $p['desc'] ?></p>
    </div>
</li>

<?php endforeach; ?>

For category 1 only to be displayed:

<?php foreach($stmt->fetchAll($people) as $p): ?>
<li data-url="link: <?= $p['link'] ?>" data-target="_blank">
    <img src="<?= $p['image'] ?>" alt="custom alt"/>
    <div data-thumbnail-content1="">
        <p class="centerDark"><?= $p['name'] ?></p>
        <p class="centerNormalDark"><?= $p['desc'] ?></p>
    </div>
</li>
<?php endforeach; ?>

Sorry to be a pain but this is really throwing me. Thank you.

Your code is pretty messy and way too sophisticated for what you really want to achieve. Here's your code totally rewrited to avoid using complex pattern.

<?php
$servername = "localhost";
$username = "dbusername";
$password = "dbpassword";
$dbname = "dbname";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare("SELECT id, name, value FROM photos ORDER BY value DESC"); 
    $stmt->execute();
} catch (PDOException $e) {
    echo 'Error '. $e->getMessage();
}

?>

<ul data-category-name="ALL">
<?php foreach($stmt->fetchAll() as $p): ?>
    <li data-url="link: htpp://www.here.com" data-target="_blank">
        <img src="content/media/thumbnails/framephoto1.jpg" alt="custom alt"/>
        <div data-thumbnail-content1="">
            <p class="centerDark"><?= $p['name'] ?></p>
            <p class="centerNormalDark"><?= $p['value'] ?></p>
        </div>
    </li>
<?php endforeach; ?>
<ul>

I probably missed some of the field you want to display, but you can add them with <?= $d['nameOfTheData'] ?>

And if you want to retrieve the pictures from a defined category, just change your request by something like this:

$stmt = $conn->prepare("SELECT id, name, value FROM photos WHERE type = ? ORDER BY value DESC"); 
$stmt->execute([$id']);

$id is the id of your category, you can retrieve him from the URL (with $_GET['id'] ) or define it yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM