tags:

views:

40

answers:

3

This is my PHP script which displays a radio station's schedule:

    <div class="divider"></div>
    <div class="main" style="width:552px;">
        <img src="$image" width=115 height=60>
        <div class="time">$airtime</div>
        <div class="show"><h3><a href="$link><b>$presenter</b></a></h3>
            <p>$showdesc</p></div>
        <div class="footer"></div>
    </div>
    </div>
                <div class="footer"></div>
                <div class="bottom"></div>
            </div>

The values with the dollar sign represent the field names in my database, which is radiopresenters.

How would I get this to work as a PHP script, and display the values from the database? All values in the fields are stored in TEXT format, apart from the image field which is stored in BLOB format.

Airtime is stored in a separate database entitled as radioschedule which has all 4 fields ib, and I intend to link these together via some relational means.

What's the best way to get it to display as the above, especially the BLOB part?

A: 

Are you asking how to put those values into your file?

As for the text ones you could just use

<?=$airtime?> 

or if your server setup doesn't support short tags just use

<?php echo $airtime?>

I would just do this inline with your php.

Example:

<div class="time"><?=$airtime?></div>

As for the BLOB, I would advise not doing this and just storing the image on your server and store the image file's name in the db. BUT if you are intent on doing this I think the only way you can do this is by having a separate script that returns an image file. Something like this:

<?php 
// Do all your db stuff here, grab the blob file. Probably from a $_GET paramater
$image = $row['image'];
header("Content-type: image/jpeg"); // or gif, etc...
echo $image;
die();
?>

Then in your other file you would do something like:

<img src="imagescript.php?person_id=<?=$person_id?>" width=115 height=60>
threendib
I'm asking about how to create a script which would be used in an include file, and to use an ifelse if nothing is selected in my database. Linking the two tables together is a problem - will it cause a relational error marked by the hash sign which I've got before when doing relational DB's?
whitstone86
+2  A: 

What you want to do is set up an show_image.php script, Example

show_image.php

<?php
$id = (isset($_GET['blid']) && is_numeric($_GET['blid'])) (int)$_GET['blid'] : false;

if($id)
{
    if(false !==($res = mysql_query('SELECT blob_data FROM table WHERE blob_id = ' . $id))
    {
        $data = mysql_fetch_assoc($res);
        header("Content-type: image/jpg"); //Send the content Type here.
        print $data['blob_data'];
        exit;
    }
}
?>

Then within your html files you would do the follwing.

<div class="divider"></div>
<div class="main" style="width:552px;">

    <img src="show_image.php?id=<?php echo (int)$id?>" width=115 height=60>

    <div class="time">$airtime</div>
    <div class="show">
        <h3><a href="$link><b>$presenter</b></a></h3>
        <p>$showdesc</p></div>
        <div class="footer"></div>
    </div>
</div>
<div class="footer"></div>
<div class="bottom"></div>

That's roughly how its done, another pointer is when your selecting your data via your main page, you should not select the blob data as it will slow your application down, and show_image.php may require more work as its for example purposes only

Peace.

RobertPitt
+1  A: 

You could write the image stored as a blob in the database out to a file. You could then use a url as your image source.

Assuming $presenter doesn't have any file/url reserved characters in it, and $image is stored as an accurate binary jpg (or png or gif etc.) , you could do:

<?php
    if($fh = fopen("/webroot/images/{$presenter}.jpg", "wb")) {
        fwrite($fh, $image) ;
        fclose($fh) ;
    }
?>
<img src="/images/<? echo $presenter ; ?>.jpg" width="115" height="60">

I would suggest that you work out some way of caching the file, so it doesn't have to be written out for every page load though.

Gus
Thats not exactly caching when its being run every time, and what's to say the image file exists, your code is somewhat a little error prone.
RobertPitt
Indeed - exactly why I recommended adding caching in my answer.
Gus