473,385 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

create a 3 cell table and populate it from an array

6
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
Nov 8 '07 #1
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?
Nov 8 '07 #2
gater
6
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:
Expand|Select|Wrap|Line Numbers
  1. <?$s = "SELECT * FROM pom WHERE year =$_GET['year'] ORDER by date" ;
  2.     $r = mysql_query($s, $connect);
  3.     if(!mysql_num_rows($r))
  4.     {    echo "<p class='warning'>There are currently no entries in the Database </p>";    }
  5.     else
  6.     {
  7.  
  8.     ?>
  9.  <table class="content_box" width="100%">
  10.  <? while($i = mysql_fetch_array($r))
  11.         {
  12.     $fname=$i['fname'];
  13.     $fdir=$i['fdir'];
  14.     $f_ext=$i['f_ext']; 
  15.  
  16.      ?>
  17.     <tr>
  18.         <td><img src="<?echo $fdir;?>/<?echo $fname.$f_ext.;?>"></td>
  19.         <td><img src="<?echo $fdir;?>/<?echo $fname.$f_ext.;?>"></td>
  20.         <td><img src="<?echo $fdir;?>/<?echo $fname.$f_ext.;?>"></td>
  21.      </tr>
  22.      <?
  23.      }
  24.      }
  25.      ?>
  26.      </table>
  27.  
Nov 9 '07 #3
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.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. #-
  3. # Changed <? to <?php. The <? is called short-tags
  4. #  and is disabled by default in PHP. It is always
  5. #  better to use <?php unless you are 110% sure your
  6. #  code will never be used on a server that you can
  7. #  not change the configuration for.
  8. #-
  9.  
  10. #-
  11. # Switched the wild card sign (*) in your query out for
  12. #  the names of the columns you want to increase efficiency.
  13. #  Using * will return all columns which will return a lot
  14. #  of extra data, increasing the server overhead.
  15. # Also fixed the $_GET['year'] usage. Always use brackets {..}
  16. #  around array elements in strings.
  17. # Dates and strings should also be enclosed in single-quotes
  18. #  in MySQL queries.
  19. #-
  20. $sql = "SELECT fname, fdir, f_ext FROM pom WHERE year = '{$_GET['year']}' ORDER by date" ;
  21.  
  22. #-
  23. # Added a die() call to your mysql_query call.
  24. #  helpfull when debugging.
  25. #- 
  26. $result = mysql_query($s, $connect) or die("Query failed: ". mysql_error($connect));
  27.  
  28. #-
  29. # Changed the variable names. Always try to use
  30. #  descriptive variable names. Badly named variables can
  31. #  cause confusion while debugging or if you need
  32. #  to fix a bug in the future.
  33. #-
  34. if(!mysql_num_rows($r)) {
  35.     echo "<p class='warning'>There are currently no entries in the Database </p>";    
  36. }
  37. else {
  38.     #-
  39.     # Changed the code so that it does not exit the <?php block.
  40.     #  This improves performance, as the PHP parser is not
  41.     #  forced to be switched off and on again just to print out
  42.     #  a single line. I use a echo call instead to print the line.
  43.     #-
  44.      echo '<table class="content_box" width="100%">';
  45.  
  46.      #-
  47.      # Switched the mysql_fetch_array function for it's brother
  48.      #  mysql_fetch_assoc. The latter only returns the associative
  49.      #  array elements, as apposed to the former that returns both
  50.      #  the associative and the numerically indexed elements.
  51.      #  This may slightly increase performance.
  52.      #-
  53.     while($row = mysql_fetch_assoc($result)) {
  54.         #-
  55.         # Removed the variables you created here.
  56.         #  As you already have them in the $row array I felt it
  57.         #  was unnecessary to create separate variables for them
  58.         #-
  59.  
  60.         #-
  61.         # Again, I change the code so that it does not exit the
  62.         #  <?php block, replacing it with a echo call.
  63.         #-
  64.         echo <<<END
  65.     <tr>
  66.         <td><img src="{$row['fdir']}/{$row['fname']}.{$row['f_ext']}"></td>
  67.         <td><img src="{$row['fdir']}/{$row['fname']}.{$row['f_ext']}"></td>
  68.         <td><img src="{$row['fdir']}/{$row['fname']}.{$row['f_ext']}"></td>
  69.     </tr>
  70. END;
  71.     }
  72.  
  73.     # -
  74.     # Added the closing </table> tag here. You
  75.     #  put it at the end of the code, where it
  76.     #  will be printed no matter what, even if the
  77.     #  opening <table> tag is not printed.
  78.     #-
  79.     echo "</table>";
  80. }
  81. ?>
  82.  
Nov 9 '07 #4
gater
6
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
Nov 9 '07 #5
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.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. # Fetch the thumbs
  3. $sql = "SELECT fname, fdir, f_ext FROM pom WHERE year = '{$_GET['year']}' ORDER by date" ;
  4. $result = mysql_query($sql, $connect) or die("Query failed: ". mysql_error($connect));
  5.  
  6. if(!mysql_num_rows($result)) {
  7.     echo "<p class='warning'>There are currently no entries in the Database </p>";    
  8. }
  9. else {
  10.     # Print the table header
  11.      echo "\n<table class=\"content_box\" width=\"100%\">\n<tr>";
  12.  
  13.      # Print each cell
  14.      $cellIndex = 0;
  15.     while($row = mysql_fetch_assoc($result)) {
  16.         # Add a new row if needed
  17.         if($cellIndex % 3 == 0 and $cellIndex > 0) {
  18.             echo "\n</tr>\n<tr>";
  19.         }
  20.         $cellIndex++;
  21.  
  22.         # Print the current cell
  23.         echo "\n\t<td><img src=\"{$row['fdir']}/{$row['fname']}.{$row['f_ext']}\"></td>";
  24.     }
  25.  
  26.     # Make sure the row is not missing cells
  27.     while($cellIndex % 3 != 0) {
  28.         echo "\n\t<td>&nbsp;</td>";
  29.         $cellIndex++;
  30.     }
  31.  
  32.     # Print the table footer
  33.     echo "\n</tr>\n</table>";
  34. }
  35. ?>
  36.  
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.
Nov 9 '07 #6
gater
6
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
Nov 9 '07 #7
gater
6
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?
Expand|Select|Wrap|Line Numbers
  1. # Print each cell
  2.      $cellIndex = 0;
  3.     while($row = mysql_fetch_assoc($result)) {
  4.         # Add a new row if needed
  5.         if($cellIndex % 3 == 0 and $cellIndex > 0) {
  6.             echo "\n</tr>\n<tr>";
  7.         }
  8.         $cellIndex++;
  9.  
  10.         # Print the current cell
  11.         echo "\n\t<td><img src=\"{$row['fdir']}/{$row['fname']}.{$row['f_ext']}\"></td>";
  12.     }
  13.  
  14.     # Make sure the row is not missing cells
  15.     while($cellIndex % 3 != 0) {
  16.         echo "\n\t<td>&nbsp;</td>";
  17.         $cellIndex++;
  18.     }
  19.  
THanks again
Nov 9 '07 #8
Atli
5,058 Expert 4TB
Sure, no problem.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. #-
  3. # The cell index is a zero-based index for
  4. #  the cell currently being printed.
  5. #  We use it to calculate when a new row has
  6. #  to be printed
  7. #-
  8. $cellIndex = 0;
  9. while($row = mysql_fetch_assoc($result)) {
  10.     #-
  11.     # The first of the two boolean checks in this
  12.     #  if statement uses the Modulus operator to
  13.     #  calculate if a new row is needed.
  14.     #  The Modulus operator divides the $cellIndex
  15.     #  by the number I specify (3) and returns the
  16.     #  rest. So if the $cellIndex devided by 3 retuns
  17.     #  a natural number (a number without a fraction)
  18.     #  the calculation returns 0 and a new row is added.
  19.     #  For example: 9 / 3 = 3.00 == 0 (no fraction)
  20.     #               7 / 3 = 2.33 == 1 (fraction)
  21.     #               6 / 3 = 2.00 == 0 (no fraction)
  22.     # The later makes sure the index is higher than 0
  23.     #  so we don't print an empty row at the top.
  24.     #-
  25.     if($cellIndex % 3 == 0 and $cellIndex > 0) {
  26.         #-
  27.         # Closes the row already opened when I printed
  28.         #  the opening <table> tag, and opens a new one.
  29.         #-
  30.         echo "\n</tr>\n<tr>";
  31.     }
  32.     $cellIndex++;
  33.  
  34.     # Print the current cell
  35.     echo "\n\t<td><img src=\"{$row['fdir']}/{$row['fname']}.{$row['f_ext']}\"></td>";
  36. }
  37.  
  38. #-
  39. # This is basically the same as the if statement above
  40. #  except here, we are filling the last row with empty
  41. #  cells to avoid rendering problems in the browsers.
  42. #  The loop will run untill the $cellIndex of the current
  43. #  cell would require a new row to be printed.
  44. #-
  45. while($cellIndex % 3 != 0) {
  46.     echo "\n\t<td>&nbsp;</td>";
  47.     $cellIndex++;
  48. }
  49. ?>
  50.  
Let me know if I missed something.
Nov 9 '07 #9
gater
6
Sure, no problem.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. #-
  3. # The cell index is a zero-based index for
  4. #  the cell currently being printed.
  5. #  We use it to calculate when a new row has
  6. #  to be printed
  7. #-
  8. $cellIndex = 0;
  9. while($row = mysql_fetch_assoc($result)) {
  10.     #-
  11.     # The first of the two boolean checks in this
  12.     #  if statement uses the Modulus operator to
  13.     #  calculate if a new row is needed.
  14.     #  The Modulus operator divides the $cellIndex
  15.     #  by the number I specify (3) and returns the
  16.     #  rest. So if the $cellIndex devided by 3 retuns
  17.     #  a natural number (a number without a fraction)
  18.     #  the calculation returns 0 and a new row is added.
  19.     #  For example: 9 / 3 = 3.00 == 0 (no fraction)
  20.     #               7 / 3 = 2.33 == 1 (fraction)
  21.     #               6 / 3 = 2.00 == 0 (no fraction)
  22.     # The later makes sure the index is higher than 0
  23.     #  so we don't print an empty row at the top.
  24.     #-
  25.     if($cellIndex % 3 == 0 and $cellIndex > 0) {
  26.         #-
  27.         # Closes the row already opened when I printed
  28.         #  the opening <table> tag, and opens a new one.
  29.         #-
  30.         echo "\n</tr>\n<tr>";
  31.     }
  32.     $cellIndex++;
  33.  
  34.     # Print the current cell
  35.     echo "\n\t<td><img src=\"{$row['fdir']}/{$row['fname']}.{$row['f_ext']}\"></td>";
  36. }
  37.  
  38. #-
  39. # This is basically the same as the if statement above
  40. #  except here, we are filling the last row with empty
  41. #  cells to avoid rendering problems in the browsers.
  42. #  The loop will run untill the $cellIndex of the current
  43. #  cell would require a new row to be printed.
  44. #-
  45. while($cellIndex % 3 != 0) {
  46.     echo "\n\t<td>&nbsp;</td>";
  47.     $cellIndex++;
  48. }
  49. ?>
  50.  
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
Nov 9 '07 #10
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 :)
Nov 9 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

2
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...
16
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
7
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...
8
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...
1
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...
17
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...
7
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...
4
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...
3
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. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
0
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...
0
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
1
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...
0
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...
0
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...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.