# Help convert code form PHP to Rails

**URL:** <https://www.ruby-forum.com/t/help-convert-code-form-php-to-rails/159499>\
**Category:** Rails\
**Created:** [February 5, 2009, 4:09am UTC](https://www.ruby-forum.com/t/help-convert-code-form-php-to-rails/159499 "2009-02-05T04:09:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lookshankar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7bcc69/32.png) [@lookshankar](https://www.ruby-forum.com/u/lookshankar)\
**Post date:** [February 5, 2009, 4:09am UTC](https://www.ruby-forum.com/t/help-convert-code-form-php-to-rails/159499/1 "2009-02-05T04:09:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lookshankar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7bcc69/32.png) [@lookshankar](https://www.ruby-forum.com/u/lookshankar)\
**Post date:** [February 5, 2009, 5:13am UTC](https://www.ruby-forum.com/t/help-convert-code-form-php-to-rails/159499/2 "2009-02-05T05:13:57Z")

</div>

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/](http://random8.zenunit.com/)  
Learn rails: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)

On 05/02/2009, at 2:09 PM, Shankar G.  
\<removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![lookshankar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7bcc69/32.png) [@lookshankar](https://www.ruby-forum.com/u/lookshankar)\
**Post date:** [February 5, 2009, 5:27am UTC](https://www.ruby-forum.com/t/help-convert-code-form-php-to-rails/159499/3 "2009-02-05T05:27:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lookshankar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7bcc69/32.png) [@lookshankar](https://www.ruby-forum.com/u/lookshankar)\
**Post date:** [February 5, 2009, 6:21am UTC](https://www.ruby-forum.com/t/help-convert-code-form-php-to-rails/159499/4 "2009-02-05T06:21:11Z")

</div>

Ok how does it not work?

Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
Learn rails: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)

On 05/02/2009, at 3:27 PM, Shankar G.  
\<removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![lookshankar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7bcc69/32.png) [@lookshankar](https://www.ruby-forum.com/u/lookshankar)\
**Post date:** [February 5, 2009, 6:30am UTC](https://www.ruby-forum.com/t/help-convert-code-form-php-to-rails/159499/5 "2009-02-05T06:30:33Z")

</div>

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.
