Jump to content

How to have a unique, featured image from a mysql database.


guttyguppy

Recommended Posts

have another column in your table called `featured` with a bool value then you can set the featured image by setting the bool to true(1) and all other to false(0) then the form need only pull the image with its `featured` value = 1
[code]
<?php
function setFeatured($idvalue)
{
$query = "UPDATE table SET featured = 0";
mysql_query($query)or die ('Error in query: $query. ' . mysql_error()); //sets all featured values in the table to false
$query1 = "UPDATE table SET featured = 1 WHERE idtable = '$idvalue'";
mysql_query($query1)or die ('Error in query: $query1. ' . mysql_error()); //sets the new one featured to true
}

[/code]

so when you want to change the featured image, just run the function with the correct table names etc, passing it the idvalue of the image you wish to be featured for that day such as
[code]
<?php
setFeatured(12); //would set image id 12 featured to true
?>
[/code]
then to get the image for your page

[code]
<?php
$query = "SELECT image FROM table WHERE featured = 1";
$result = mysql_query($query)or die ('Error in query: $query. ' . mysql_error());
$row = mysql_fetch_assoc($result);
$featured_image = $row['image']; //from then on just use the variable $featured_image
?>
[/code]

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.