Help convert code form PHP to Rails


#1

hi,
Shown below is the PHP code to obtain the value for each date in the
corresponding date range.And with I’ve attached the MYSQL Database
Table.Please Have a look.I’ve tried to convert this code to rails but
not yet succeeded.Please let me know if you can do…

<?php $con=mysql_connect("localhost","root",""); mysql_select_db("dgrid_development",$con); $sql=mysql_query("select * from events"); while($row=mysql_fetch_array($sql)) { $date_array = date_range($row["when"],$row["to"]); $txt='('.$row["what"].'-'.$row["color"].')'; $arrayMixed[]= array_fill_keys($date_array, $txt); } function date_range($sd,$ed) { $tmp = array(); $sdu = strtotime($sd); $edu = strtotime($ed); while ($sdu <= $edu) { $tmp[] = date('Y-m-d',$sdu); $sdu = strtotime('+1 day',$sdu); } return ($tmp); } foreach($arrayMixed as $final) { foreach($final as $key=>$value) { $group[$key].=$value; } } print'
';
print_r($group);
print '
'; ?>

Thanks in Advance.

cheers,
Shankar.


#2

Time to learn more rails and ruby my friend. We’re not going to write
your code for you. Have an attempt and then ask questions about that
in a succinct and clear manner.

Blog: http://random8.zenunit.com/
Learn rails: http://sensei.zenunit.com/

On 05/02/2009, at 2:09 PM, Shankar G.
<removed_email_address@domain.invalid


#3

Hi Julian,

Here is what the code I tried

<% result=[]%>
<%date=[] %>

An array from database with select all query

<% @eventall.each do |test| %>
<%i1= test.when%>
<%i2 =test.to%>
<%i3=i1.to_s.gsub(’-’,’’) %>
<%i4=i2.to_s.gsub(’-’,’’)%>
<%span =i3…i4%>
<%date=span.each { |x| puts x }%>
<%date.each_with_index do |value, key|%>
<% result.push(value)%>
<%end%>

<%end%>

An array which contains ‘color’ and ‘what’ value concatinated

<% wattt = [] %>
<% for watl in @eventall%>
<% wattt << watl.what.concat(watl.color)%>
<%end%>

This is the code i did to combine both array to fill the date between

certain range to corresponding value

<%hash = {}%>
<%result.each { |d| hash[d] = [] }%>
<%result.each_with_index { |x,i| hash[x] << wattt[i] }%>
<%hash.each_key { |key| hash[key] = hash[key].join(’-’) }%>
<% hash.each do |d,v| %>
<%= d%><%= v%>

<%end%>

Hope I explained well.Tell me if I’m not clear.

Thanks,
Shankar.


#4

Ok how does it not work?

Blog: http://random8.zenunit.com/
Learn rails: http://sensei.zenunit.com/

On 05/02/2009, at 3:27 PM, Shankar G.
<removed_email_address@domain.invalid


#5

The value for the corresponding date range is not inserting
appropriately.
I shall give an example here,

consider :

for ‘id’ as 1, date of ‘when’ as 2009-02-01,date of ‘to’
2009-02-15,‘what’ as test date between days ! and ‘color’ as #DD5511
for ‘id’ as 2, date of ‘when’ as 2009-02-02,date of ‘to’
2009-02-14,‘what’ as test again and ‘color’ as #22AA99

for the above values in the database I’m getting output as,

20090213-
20090202test again#22AA99#22AA99-
20090214-
20090203test#DD5511#DD5511-
20090215
20090204tests#CBC2FE#CBC2FE—
20090205-
20090206-
20090207-
20090208-
20090209-
20090210-
20090211-
20090212-
20090201test date between days !#DD5511#DD5511

you can see some dates are left blank.I need the values to be filled
appropriately.

Thanks in advance.