Software Design
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreSoftware Design

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 19th, 2002, 12:43 AM
binaryencode binaryencode is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Location: Weaverville, NC
Posts: 2 binaryencode User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to binaryencode
Question generating HTML tables

I am writing a Ruby script that outputs a table of pictures in its current directory. So far it goes like this:
Code:
#!/usr/bin/env ruby -w

html_file = File.open "pictures.html", "w"
html_file.puts(
  "<html>",
  "<head><title>The Cross Family Cross-Country Trip</title></head>",
  "<!--",
  "**************************************",
  "This file was generated by pictures.rb",
  "**************************************",
  "-->",
  "<body>",
  "<div align=center>",
   "<h2>.: Pictures :.</h2>",
  "<table border=0>",
  "<tr>"
)

# Sort pictures by modified date
pictures = Dir["*.{jpg,jpeg}"].sort do |a,b|
    File.mtime(a) <=> File.mtime(b)
end

for picture in pictures
    html_file.puts(
      "<td height=110 width=150>",
      "<a href=\"#{picture}\">",
      "<img height=100 length=140 src=\"#{picture}\" border=0></a>",
      "</td>"
    )
end
 
html_file.puts(
  "</table>",
  "</div><div align=right>",
  "This page was generated by a <a href=\"http://ruby-lang.org\">",
  "Ruby</a> script available <a href=\"pictures.rb\">here</a>.",
  "</div>",
  "</body>",
  "</html>"
)

My problem is that I need it to make a new row every 4 pictures or so, for it to fit in the browser. HOW?! This has been giving me some trouble for the past couple of hours.

Thanks in Advance

Reply With Quote
  #2  
Old September 19th, 2002, 01:55 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,507 Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 7 m 35 sec
Reputation Power: 865
This is an algorithm forum more than a ruby forum, so I guess I'll try to explain an algorithm for how to do this first. How about using a variable (say count) to keep track of the number of files output so far. When the value is a multiple of 4, then print <tr> and </tr> at the beginning and end of the loop. Something like this, I guess (since I really can't program in ruby):
Code:
#!/usr/bin/env ruby -w

html_file = File.open "pictures.html", "w"
html_file.puts(
  "<html>",
  "<head><title>The Cross Family Cross-Country Trip</title></head>",
  "<!--",
  "**************************************",
  "This file was generated by pictures.rb",
  "**************************************",
  "-->",
  "<body>",
  "<div align=center>",
   "<h2>.: Pictures :.</h2>",
  "<table border=0>",
  "<tr>"
)

# Sort pictures by modified date
pictures = Dir["*.{jpg,jpeg}"].sort do |a,b|
    File.mtime(a) <=> File.mtime(b)
end

count = 1

for picture in pictures
    if (count % 4 == 0)
        html_file.puts(
            "<tr>"
        )
    html_file.puts(
      "<td height=110 width=150>",
      "<a href=\"#{picture}\">",
      "<img height=100 length=140 src=\"#{picture}\" border=0></a>",
      "</td>"
    )
    if (count % 4 == 0)
        html_file.puts(
            "</tr>"
        )
    count += 1
end
html_file.puts(
    "</tr>"
)
 
html_file.puts(
  "</table>",
  "</div><div align=right>",
  "This page was generated by a <a href=\"http://ruby-lang.org\">",
  "Ruby</a> script available <a href=\"pictures.rb\">here</a>.",
  "</div>",
  "</body>",
  "</html>"
)

Reply With Quote
  #3  
Old September 19th, 2002, 02:54 AM
binaryencode binaryencode is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Location: Weaverville, NC
Posts: 2 binaryencode User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to binaryencode
Thanks

Thanks, I ended up using that same consept in a hackish way of counting by the array index:
Code:
for picture in pictures
  html_file.puts(
    "<td height=110 width=150>",
    "<a href=\"#{picture}\">",
    "<img height=100 length=140 src=\"#{picture}\"   border=0></a>",
    "</td>"
   )

  # Create new row every four pictures
  while (pictures.index(picture)+1).divmod(4).last == 0
    html_file.puts "</tr>"
    html_file.puts "<tr>" until picture = pictures.last
  end
end

That is a lot uglier than most Ruby has to be, but a least my mind can rest now.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreSoftware Design > generating HTML tables


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway