nfhopmike
nfhopmike

Reputation: 65

Pass ID of row to Detail Page with PDO

I'm trying to update a master/detail setup from mysql to PDO in order to be compatible with PHP7. I have got the master page working exactly as it should - it produces a table that lists audio files and when you click on a title the idea is that you are taken to a detail page where the audio player is displayed. I am having trouble coding the detail page so that the ID from the master page is passed to the detail page and displays title and author together with the HTML5 audio player.

Here is the code for the master page:

<?php
$doc->addScript(JURI::root(true).'/templates/nfhop/js/sorttable.js' )
?>

<?php
require_once JPATH_SITE.'/includes/dbAudio.php'; ?>

<?php $query = $handler->query('SELECT * from audio ORDER BY subject ASC'); ?>

<article class="left">
<div class="formwrap">
<table class="members-audiolist table-striped sortable" border="0" align="left">
<thead>
<tr class="audio_header">
<td><p class="house-red">Subject</p></td>
<td><p class="house-red">Title</p></td>
<td><p class="house-red">Author</p></td>
</tr>
</thead>

<?php

while($r = $query->fetch(PDO::FETCH_OBJ)) { ?>
<tbody>
<tr>
<td width="25%"><?php echo $r->subject; ?></td>
<td><a href="index.php/audioplayer-pdo?recordID=<?php echo $r->id; ?>"> <?php echo $r->title; ?></a></td>
<td><?php echo $r->author; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<div class="clearfix"></div>

</div>
<div style="margin-left:18px">
<p style="margin-top: 8px; font-size: 1em">To listen: click on title</p>
<p style="font-size: 1em">To download: right click on title and select save link as...</p>
<p style="font-size: 1em">You can sort by subject, title or author, just click on heading at top of column</p>
</div>
</article>

And this is my attempt at code for the detail page:

<?php
require_once JPATH_SITE.'/includes/dbAudio.php';

$query = $handler->query('SELECT * FROM audio WHERE id = ?'); ?>

 ?>

<hr class="divider" />

<h2><?php echo $r->title; ?> </h2>

<p>Speaker: <?php echo $r->author; ?> </p>

<p><?php echo $r->date; ?> </p>

<p><span style="font-style: italic">Category: <?php echo $r->subject; ?> </p><br />
<audio controls>
<source src="http://media.nfhop.org/<?php echo $r->audio_location; ?>" type="audio/mp3">

</audio>

<p class="margin-T10"><a href="http://www.nfhop.org/<?php echo $r->notes_location; ?>"> <img src="http://www.nfhop.org/<?php echo $r->image; ?>"></a></p>

<p><a href="/index.php/resources/audio">return to audio list</a>

Would be grateful for any assistance in getting this to work!

Upvotes: 1

Views: 361

Answers (1)

u_mulder
u_mulder

Reputation: 54841

If $handler is an instance of PDO you could do it like this:

<?php
require_once JPATH_SITE.'/includes/dbAudio.php';
// Audio id is stored in `$_GET['recordID']`


$stmt = $handler->prepare('SELECT * FROM audio WHERE id = ?'); 
$stmt->bindParam(1, $_GET['recordID']);
$stmt->execute();
$r = $stmt->fetch(PDO::FETCH_OBJ);
print_r($r);?>

According to what properties are there in $r you can output them.

Upvotes: 1

Related Questions