Hello All.
I am trying to set up a page that will display the 'picture of month' for each month of a given year. I want 3 columns of thumbs. There may be no images or up to 12 images for any given year. The images are in different directories and their location /filename/extension will be extracted from an array. I am not very familiar with the count and foreach functions but I assume this can be done using a variation of those. Any thoughts?
Thanks
10 2041 Atli 5,058
Expert 4TB
Hi Gater. Welcome to TSDN!
Everything is possible, especially in a language as flexible as PHP :)
I'm not exactly sure what you trying to do here. I understand the part where you want to show a 'Picture of the month', but I don't understand how you intend to implement it.
Could you explain in more detail?
Perhaps show us code examples?
Hi Gater. Welcome to TSDN!
Everything is possible, especially in a language as flexible as PHP :)
I'm not exactly sure what you trying to do here. I understand the part where you want to show a 'Picture of the month', but I don't understand how you intend to implement it.
Could you explain in more detail?
Perhaps show us code examples?
THanks for your welcome Atli!
I am keeping photos in a gallery, with folders for each month. Each month an image is selected as the Pic of the Month and that file information is entered into the pom DB table along with a description, the contributor etc. That image can then be 'activated' to be displayed on each page of the site in a side bar column as an include. The 'deactivated' or previous months pics are no longer shown except in their gallery folders. All that works fine.
I want to create a page to display all the pom s for a any given year. Depending on the circumstances there may be 1 image or up to twelve. I like the script to determine how many images will be displayed on the page and then create and populate the display table with the correct number of cells and populate them with the images.
Code so far looks like this: -
<?$s = "SELECT * FROM pom WHERE year =$_GET['year'] ORDER by date" ;
-
$r = mysql_query($s, $connect);
-
if(!mysql_num_rows($r))
-
{ echo "<p class='warning'>There are currently no entries in the Database </p>"; }
-
else
-
{
-
-
?>
-
<table class="content_box" width="100%">
-
<? while($i = mysql_fetch_array($r))
-
{
-
$fname=$i['fname'];
-
$fdir=$i['fdir'];
-
$f_ext=$i['f_ext'];
-
-
?>
-
<tr>
-
<td><img src="<?echo $fdir;?>/<?echo $fname.$f_ext.;?>"></td>
-
<td><img src="<?echo $fdir;?>/<?echo $fname.$f_ext.;?>"></td>
-
<td><img src="<?echo $fdir;?>/<?echo $fname.$f_ext.;?>"></td>
-
</tr>
-
<?
-
}
-
}
-
?>
-
</table>
-
Atli 5,058
Expert 4TB
Hi again.
While I'm considering your problem, I have a few notes on your code. A few changes I think you may find interesting.
I've taken your code and made a few modification, adding comments explaining what I did and why. -
<?php
-
#-
-
# Changed <? to <?php. The <? is called short-tags
-
# and is disabled by default in PHP. It is always
-
# better to use <?php unless you are 110% sure your
-
# code will never be used on a server that you can
-
# not change the configuration for.
-
#-
-
-
#-
-
# Switched the wild card sign (*) in your query out for
-
# the names of the columns you want to increase efficiency.
-
# Using * will return all columns which will return a lot
-
# of extra data, increasing the server overhead.
-
# Also fixed the $_GET['year'] usage. Always use brackets {..}
-
# around array elements in strings.
-
# Dates and strings should also be enclosed in single-quotes
-
# in MySQL queries.
-
#-
-
$sql = "SELECT fname, fdir, f_ext FROM pom WHERE year = '{$_GET['year']}' ORDER by date" ;
-
-
#-
-
# Added a die() call to your mysql_query call.
-
# helpfull when debugging.
-
#-
-
$result = mysql_query($s, $connect) or die("Query failed: ". mysql_error($connect));
-
-
#-
-
# Changed the variable names. Always try to use
-
# descriptive variable names. Badly named variables can
-
# cause confusion while debugging or if you need
-
# to fix a bug in the future.
-
#-
-
if(!mysql_num_rows($r)) {
-
echo "<p class='warning'>There are currently no entries in the Database </p>";
-
}
-
else {
-
#-
-
# Changed the code so that it does not exit the <?php block.
-
# This improves performance, as the PHP parser is not
-
# forced to be switched off and on again just to print out
-
# a single line. I use a echo call instead to print the line.
-
#-
-
echo '<table class="content_box" width="100%">';
-
-
#-
-
# Switched the mysql_fetch_array function for it's brother
-
# mysql_fetch_assoc. The latter only returns the associative
-
# array elements, as apposed to the former that returns both
-
# the associative and the numerically indexed elements.
-
# This may slightly increase performance.
-
#-
-
while($row = mysql_fetch_assoc($result)) {
-
#-
-
# Removed the variables you created here.
-
# As you already have them in the $row array I felt it
-
# was unnecessary to create separate variables for them
-
#-
-
-
#-
-
# Again, I change the code so that it does not exit the
-
# <?php block, replacing it with a echo call.
-
#-
-
echo <<<END
-
<tr>
-
<td><img src="{$row['fdir']}/{$row['fname']}.{$row['f_ext']}"></td>
-
<td><img src="{$row['fdir']}/{$row['fname']}.{$row['f_ext']}"></td>
-
<td><img src="{$row['fdir']}/{$row['fname']}.{$row['f_ext']}"></td>
-
</tr>
-
END;
-
}
-
-
# -
-
# Added the closing </table> tag here. You
-
# put it at the end of the code, where it
-
# will be printed no matter what, even if the
-
# opening <table> tag is not printed.
-
#-
-
echo "</table>";
-
}
-
?>
-
Hi,
Thanks for the detailed reply. There are a couple of things in your edits I was not aware of - The mysql_fetch_assoc
- the echo >>>end
Both of which I k now I will use frequently from here out!
I am sure your solution will be equally as enlightening!
Thanks
Atli 5,058
Expert 4TB
Ok. Assuming I am understanding you correctly, this code may be what you are looking for.
This is basically your code, using the modulus operator (%) to insert new rows after every third cell is printed. It will result in a 3 column table filled with images based on the values fetched from your database. No limit as to how many cells there are. -
<?php
-
# Fetch the thumbs
-
$sql = "SELECT fname, fdir, f_ext FROM pom WHERE year = '{$_GET['year']}' ORDER by date" ;
-
$result = mysql_query($sql, $connect) or die("Query failed: ". mysql_error($connect));
-
-
if(!mysql_num_rows($result)) {
-
echo "<p class='warning'>There are currently no entries in the Database </p>";
-
}
-
else {
-
# Print the table header
-
echo "\n<table class=\"content_box\" width=\"100%\">\n<tr>";
-
-
# Print each cell
-
$cellIndex = 0;
-
while($row = mysql_fetch_assoc($result)) {
-
# Add a new row if needed
-
if($cellIndex % 3 == 0 and $cellIndex > 0) {
-
echo "\n</tr>\n<tr>";
-
}
-
$cellIndex++;
-
-
# Print the current cell
-
echo "\n\t<td><img src=\"{$row['fdir']}/{$row['fname']}.{$row['f_ext']}\"></td>";
-
}
-
-
# Make sure the row is not missing cells
-
while($cellIndex % 3 != 0) {
-
echo "\n\t<td> </td>";
-
$cellIndex++;
-
}
-
-
# Print the table footer
-
echo "\n</tr>\n</table>";
-
}
-
?>
-
We usually don't like giving out full codes solutions to problems, but rather point people in the right direction so they can discover it themselves, but in this case I think the code is the best explanation.
Ok. Assuming I am understanding you correctly, this code may be what you are looking for.
This is basically your code, using the modulus operator (%) to insert new rows after every third cell is printed. It will result in a 3 column table filled with images based on the values fetched from your database. No limit as to how many cells there are.
We usually don't like giving out full codes solutions to problems, but rather point people in the right direction so they can discover it themselves, but in this case I think the code is the best explanation.
THanks Atli! Works a charm.
Usually I don't ask for more than a snippet, but in this case after your edit of my sloppy code .... LOL I generally write Validated HTML and Css and will now try to improve my PHP as well.
Up to this point I have been more interested in just getting stuff to work!
Thanks again
Hi Atli,
To help me understand the logic and syntax I am wondering if you could comment the following to describe the operators and functions? -
# Print each cell
-
$cellIndex = 0;
-
while($row = mysql_fetch_assoc($result)) {
-
# Add a new row if needed
-
if($cellIndex % 3 == 0 and $cellIndex > 0) {
-
echo "\n</tr>\n<tr>";
-
}
-
$cellIndex++;
-
-
# Print the current cell
-
echo "\n\t<td><img src=\"{$row['fdir']}/{$row['fname']}.{$row['f_ext']}\"></td>";
-
}
-
-
# Make sure the row is not missing cells
-
while($cellIndex % 3 != 0) {
-
echo "\n\t<td> </td>";
-
$cellIndex++;
-
}
-
THanks again
Atli 5,058
Expert 4TB
Sure, no problem. -
<?php
-
#-
-
# The cell index is a zero-based index for
-
# the cell currently being printed.
-
# We use it to calculate when a new row has
-
# to be printed
-
#-
-
$cellIndex = 0;
-
while($row = mysql_fetch_assoc($result)) {
-
#-
-
# The first of the two boolean checks in this
-
# if statement uses the Modulus operator to
-
# calculate if a new row is needed.
-
# The Modulus operator divides the $cellIndex
-
# by the number I specify (3) and returns the
-
# rest. So if the $cellIndex devided by 3 retuns
-
# a natural number (a number without a fraction)
-
# the calculation returns 0 and a new row is added.
-
# For example: 9 / 3 = 3.00 == 0 (no fraction)
-
# 7 / 3 = 2.33 == 1 (fraction)
-
# 6 / 3 = 2.00 == 0 (no fraction)
-
# The later makes sure the index is higher than 0
-
# so we don't print an empty row at the top.
-
#-
-
if($cellIndex % 3 == 0 and $cellIndex > 0) {
-
#-
-
# Closes the row already opened when I printed
-
# the opening <table> tag, and opens a new one.
-
#-
-
echo "\n</tr>\n<tr>";
-
}
-
$cellIndex++;
-
-
# Print the current cell
-
echo "\n\t<td><img src=\"{$row['fdir']}/{$row['fname']}.{$row['f_ext']}\"></td>";
-
}
-
-
#-
-
# This is basically the same as the if statement above
-
# except here, we are filling the last row with empty
-
# cells to avoid rendering problems in the browsers.
-
# The loop will run untill the $cellIndex of the current
-
# cell would require a new row to be printed.
-
#-
-
while($cellIndex % 3 != 0) {
-
echo "\n\t<td> </td>";
-
$cellIndex++;
-
}
-
?>
-
Let me know if I missed something.
Sure, no problem. -
<?php
-
#-
-
# The cell index is a zero-based index for
-
# the cell currently being printed.
-
# We use it to calculate when a new row has
-
# to be printed
-
#-
-
$cellIndex = 0;
-
while($row = mysql_fetch_assoc($result)) {
-
#-
-
# The first of the two boolean checks in this
-
# if statement uses the Modulus operator to
-
# calculate if a new row is needed.
-
# The Modulus operator divides the $cellIndex
-
# by the number I specify (3) and returns the
-
# rest. So if the $cellIndex devided by 3 retuns
-
# a natural number (a number without a fraction)
-
# the calculation returns 0 and a new row is added.
-
# For example: 9 / 3 = 3.00 == 0 (no fraction)
-
# 7 / 3 = 2.33 == 1 (fraction)
-
# 6 / 3 = 2.00 == 0 (no fraction)
-
# The later makes sure the index is higher than 0
-
# so we don't print an empty row at the top.
-
#-
-
if($cellIndex % 3 == 0 and $cellIndex > 0) {
-
#-
-
# Closes the row already opened when I printed
-
# the opening <table> tag, and opens a new one.
-
#-
-
echo "\n</tr>\n<tr>";
-
}
-
$cellIndex++;
-
-
# Print the current cell
-
echo "\n\t<td><img src=\"{$row['fdir']}/{$row['fname']}.{$row['f_ext']}\"></td>";
-
}
-
-
#-
-
# This is basically the same as the if statement above
-
# except here, we are filling the last row with empty
-
# cells to avoid rendering problems in the browsers.
-
# The loop will run untill the $cellIndex of the current
-
# cell would require a new row to be printed.
-
#-
-
while($cellIndex % 3 != 0) {
-
echo "\n\t<td> </td>";
-
$cellIndex++;
-
}
-
?>
-
Let me know if I missed something.
That's terrific! THanks. I am assuming then that $cellIndex++; adds one cell to the current num ber of cells?
I appreciate your help very much. Many times I take a snippet and plug it in and then try to figure out why it works! Reverse-engineering makes the knowledge stick pretty well... but it is time consuming! LOL
Atli 5,058
Expert 4TB
That's terrific! THanks. I am assuming then that $cellIndex++; adds one cell to the current num ber of cells?
I appreciate your help very much. Many times I take a snippet and plug it in and then try to figure out why it works! Reverse-engineering makes the knowledge stick pretty well... but it is time consuming! LOL
You assume correctly, it does indeed add one to the cell index.
I'm glad I could help you out :)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Nick |
last post by:
Loop to create an array from a dynamic form.
I'm having trouble with an application, and I'll try to explain it as
clearly as possible:
1. I have a form with two fields, say Apples and...
|
by: datactrl |
last post by:
Hi,
Is that posible to create a web page completely with javascript and open it
without request to server? Please show a simple sample. Thanks in advance!
Jack
|
by: dog |
last post by:
I've seen plenty of articles on this topic but none of them have been
able to solve my problem.
I am working with an Access 97 database on an NT4.0 machine, which has
many Access reports.
I...
|
by: ASP Yaboh |
last post by:
I have an ArrayList of data gathered from a database. I want to create a web
page from this data by creating a <table>, each cell in each row displays the
appropriate data. One of those cells in...
|
by: Gürkan Demirci |
last post by:
Hi,
i am using the VisualStudio FormDesigner to create an asp:table.
I want to populate an asp:tablecell with different controls at runtime.
In the codebehind file, there is an attribute for the...
|
by: toffee |
last post by:
Hi all,
I have a table with 12 cols and 10 rows. When a user clicks on a table cell;
the page is refreshed and displays some data below the table dependant on
whichever cell was selected.
I...
|
by: Arne Beruldsen |
last post by:
in vbnet2005 I have a datagridview. When the user clicks on a row...I would
like the contents of certain cells to populate a textbox. To do this...i
need to be able to refer to the row and...
|
by: etuncer |
last post by:
Hello All,
I have Access 2003, and am trying to build a database for my small
company. I want to be able to create a word document based on the data
entered through a form. the real question is...
|
by: jshelp |
last post by:
I have a problem I would be very grateful for any help with:
I need to create a bus timetable using data from arrays and write a table using the DOM to show only those stops and times selected.
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |