Page 1 of 1

How to manipulate a value in a loop

Posted: Fri Oct 19, 2012 9:25 pm
by pschmehl
I'm an amateur coder at best. I'm working on an internal website that will do selects on a db to retrieve information. One of the returned fields contains a timestamp of the form unixtime.microseconds. I'd like to convert that to human readable mm/dd/yyyy h:m:s. I've tried several things, but everything I've tried breaks the displayed result. The results are returned in a table with titles in the top row followed by the data returned from the db in rows.

Note: I have a decent understanding of programming basics. I steal most of my code from the web and then adapt it to my use plus reading copious amounts of documentation trying to figure all this out.

Here's the working code:

Code: Select all

// printing table rows
        while($row = mysql_fetch_row($result))
        {
                echo "<tr>";
                                
        // $row is array... foreach( .. ) puts every element
        // of $row to $cell variable
        foreach($row as $cell)
        echo "<td>$cell</td>";
                                
        echo "</tr>\n";
        }
        echo "</table>\n";
        mysql_free_result($result);
If it matters, the third column is the one with the timestamp.micros format. I've tried an if else loop something like this:

Code: Select all

// printing table rows
        while($row = mysql_fetch_row($result))
        {
                echo "<tr>";
                                
        // $row is array... foreach( .. ) puts every element
        // of $row to $cell variable
        foreach($row as $cell) {
            $pattern='/\d{10}\.\d{6}/';
            $timestamp=preg_match($pattern,$cell);
            $HRDate=date(n/j/Y h:m:s,$timestamp);
            if($HRDate) {
                echo "<td>$HRDate</td>";
            }else{
                echo "<td>$cell</td>";
            }
        }                        
        echo "</tr>\n";
        }
        echo "</table>\n";
        mysql_free_result($result);
But it didn't work the way I expected at all. It returned one empty row.

I also tried an if for the specific row:

Code: Select all

// printing table rows
        while($row = mysql_fetch_row($result))
        {
                echo "<tr>";
                                
        // $row is array... foreach( .. ) puts every element
        // of $row to $cell variable
        foreach($row as $cell) {
            if($row==2) {
                $HRDate=date(n/j/Y h:m:s);
                echo "<td>$HRDate</td>";
            }else{
                echo "<td>$cell</td>";
            }
        }                        
        echo "</tr>\n";
        }
        echo "</table>\n";
        mysql_free_result($result);
I'm sure this is something simple for you everyday coders, but it's got me stumped.

Re: How to manipulate a value in a loop

Posted: Fri Oct 19, 2012 9:29 pm
by requinix

Code: Select all

date(n/j/Y h:m:s,$timestamp)
That's a syntax error - the script shouldn't be working at all.

Literal strings always need quotes.

Code: Select all

date("n/j/Y h:m:s",$timestamp)

Re: How to manipulate a value in a loop

Posted: Fri Oct 19, 2012 9:47 pm
by pschmehl
requinix wrote:

Code: Select all

date(n/j/Y h:m:s,$timestamp)
That's a syntax error - the script shouldn't be working at all.

Literal strings always need quotes.

Code: Select all

date("n/j/Y h:m:s",$timestamp)
Sorry, I left the quotes off of my example posted here. I used them in the test.

Re: How to manipulate a value in a loop

Posted: Fri Oct 19, 2012 9:49 pm
by Christopher
I think you want:

Code: Select all

        foreach($row as $key => $cell) {
            if($key == 2) {
Though I'd recommend using mysql_ fetch_ assoc() because this is clearer and probably would reduce errors if the schema changes:

Code: Select all

        foreach($row as $name => $cell) {
            if($name == 'thedate') {
And finally, please use PDO instead.

Re: How to manipulate a value in a loop

Posted: Fri Oct 19, 2012 10:37 pm
by pschmehl
Christopher wrote:I think you want:

Code: Select all

        foreach($row as $key => $cell) {
            if($key == 2) {
Though I'd recommend using mysql_ fetch_ assoc because this is clearer and probably would reduce errors if the schema changes:

Code: Select all

        foreach($row as $name => $cell) {
            if($name == 'thedate') {
And finally, please use PDO instead.
Thank you! That solved the problem.

PDO - I'm not sure if I'll live long enough to understand that well enough to use it. I'm 65 now.....