Jump to content

[SOLVED] Image Switcher change from Microtime to show weekly pic


Recommended Posts

I have a section on my site that I want to show a particular image each week.

 

I currently have a randomize function using shuffle but this seems to change the image every time you change the page. I want it to actually change weekly if possible.

 

Any idea how I can do that?

 

Here is the code

 

<?php 
include ("inc/db.php");

$query = "SELECT * FROM main_week_pic ORDER by id "; //querying the database, this means select everything 
//from the table called boxes and order by id i.e 1,2,3,4,5,6,7,8,9



$result = mysql_query($query) or die ("Couldn't execute query.");


$pictures = array();

// Populate cells using a mysql_fetch_array script 
while ($row=mysql_fetch_array($result))
{

$id=$row["id"];
$path=$row["path"];
     $more=$row["more"];


$pictures[$id] = $path;

}

//here is the randomising function

srand ((float)microtime()*1000000);
shuffle($pictures);

?>

<h3>Picture of the week</h3>
<p class="imgcenter">


<?php // here is a loop that sorts the order of appearance and sets the size and location of images generated

for ($i = 1; $i<2; $i++)
{
	echo '<img src = "';
	echo $pictures[$i];
	echo '"width="180">';
}
?>

 

One additional question. If I was to add a link to the image using $path from the database and an author's name using $more how could I make sure that when the picture changes it also shows the corresponding path and author?

 

Here is the sql

 

CREATE TABLE `main_week_pic` (
  `id` int(11) NOT NULL auto_increment,
  `path` text NOT NULL,
  `more` text NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

-- 
-- Dumping data for table `main_week_pic`
-- 

INSERT INTO `main_week_pic` (`id`, `path`, `more`) VALUES 
(1, 'images/valcard.jpg', 'Jasper Pattison'),
(3, 'http://confluxfestival.org/media/projects/project441/441_public_web_image.jpg', 'confluxfestival.org'),
(4, 'http://graphics8.nytimes.com/images/2006/04/09/weekinreview/09bruni.600.jpg', 'Ji Lee');

Perhaps what you need to do is this.

 

Count the # of 'images of the week' that exist in the database.

 

Determine the current week number - $wk_no = date("W")

 

Using the modulus operator (%) determine which 'image of the week' to display.

 

That will show the same image to all visitors until the week number changes.

Perhaps what you need to do is this.

 

Count the # of 'images of the week' that exist in the database.

 

Determine the current week number - $wk_no = date("W")

 

Using the modulus operator (%) determine which 'image of the week' to display.

 

That will show the same image to all visitors until the week number changes.

 

Thanks

 

I have added

 

$wk_no = date("W");
echo "$wk_no";

 

the result is 21 so I assume this is the current week number

 

Not sure where to put the % do you mean in the mysql statement?

 

There are currently 3 images in the database, but the script should cycle the images that are in the database but weekly.

 

I am still a bit stuck about how to proceed. Should I still be using the array/Randomize Function/For Loop combination?

 

Just to add I have now set it up to count the rows in the table

 

// Query database
$count_sql = 'SELECT * FROM main_week_pic';
$count_result = mysql_query($count_sql);
// Get number of articles, assign value to variable
$count = mysql_num_rows($count_result);
echo "The database has this many fields: $count";

 

Here is a test page so you can see what I am trying to do

 

http://www.undesign.co.uk/undesign_of_the_week.php

I think I have nearly got it,

 

I just need to work out how to write the following in the correct syntax

 

if $wk_no increases by one the shuffle($pictures);

 

currently I have the following

 

<?php 
	include ("inc/db.php");

$wk_no = date("W"); //gives the current week
// Query database
$count_sql = 'SELECT * FROM main_week_pic';
$count_result = mysql_query($count_sql);
// Get number of articles, assign value to variable
$count = mysql_num_rows($count_result);
echo "The database has this many fields: $count <br />";
echo "We are currently in week $wk_no";


$result = mysql_query($count_sql) or die ("Couldn't execute query.");
$pictures = array();
// Populate cells using a mysql_fetch_array script 
while ($row=mysql_fetch_array($result))
{

$id=$row["id"];
$path=$row["path"];
$more=$row["more"];



$pictures[$id] = $path;


}

// If week increases +1 then  $wk_no ++1 (i++1) shuffle($pictures);
           shuffle($pictures);

?>

<h3>Undesign of the Week</h3>
<p class="imgcenter">


<?php // here is a loop that sorts the order of appearance and sets the size and location of images generated

for ($i = 1; $i<2; $i++)
{
	echo '<img src = "';
	echo $pictures[$i];
	echo '"width="180">';
}
?>

I don't think any shuffling is required. What you want is the same picture to show for the whole week.  By knowing the number of available images and knowing the week number (both of which can be determined at run time), and then determining the remainder of week number divided by number of pictures you'll know which image to show.  For example it's week 21; there are three pictures. 21/3 has a remainder of zero so show the zeroth picture in the array, i.e. record #1.

Ah,

 

I see

 

So I added the following

 

$number= $week_no/$count;

 

This echoes 0 as there is no remainder this week.

 

But I am not sure where to now stick $number so that it cycles the image. This number should be input into $id somehow shouldn't it so that it knows which image to show from the list.

 

Is the for loop now redundant?

Maybe I'm not explaining it clearly.  The modulus operator tells you the remainder ...

 

Suppose it's week 21 and there are four pictures in the database

 

$weeknum % $numpix = 1 because 21 divided by 4 gives a remainder of 1.

 

week20, 4 pictures, remainder = 0  = picture 0

week21, 4 pictures, remainder = 1 = picture 1

week22, 4 pictures, remainder = 2 = picture 2

week23, 4 pictures, remainder = 3 = picture 3

week24, 4 pictures, remainder = 0 = picture 0 again, completing the cycle

 

There no loop involved, and only a few lines of code. It'll work regardless of how many pictures there are in the database. The complete logic/flow is:

using a simple SELECT query and loop to generate an array of all pictures names

determine the number of pictures using count() on the array

using date("W") determine the week number

determine the remainder of week number divided by # of pictures/rows in database to determine which picture you want from the array.

Thanks a lot,

 

Got it working now

 

I realised that the remainder would always work out and output a number as a remainder that corresponded to the id of each image, it was the fact that i had an array set up to cycle the images and I was trying to get the $id into $pictures some how. So I scrapped the array altogether and removed the for loop and used a standard fetch array and put the output of $number in the sql statement.

 

<?php
$wk_no = date("W"); //gives the current week
$count_sql = 'SELECT * FROM main_week_pic';// query database
$count_result = mysql_query($count_sql);
// Get number of articles, assign value to variable
$count = mysql_num_rows($count_result);
echo "The database has this many rows: $count <br />";
echo "We are currently in week $wk_no<br />";
$number= $week_no%$count+3;
echo "$number";


$query = ("SELECT * FROM main_week_pic where id = '$number'");// query database

echo"<p class='imgcenter'><h3>Undesign of the Week</h3>";

$result = mysql_query($query) or die ("Couldn't execute query.");

// Populate cells using a mysql_fetch_array script 
while ($row=mysql_fetch_array($result))
{

$id=$row["id"];
$path=$row["path"];
$more=$row["more"];


echo '<img src = "';
	echo $path;
	echo '"width="180">';
}



?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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