How to manipulate a value in a loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pschmehl
Forum Newbie
Posts: 14
Joined: Fri Oct 19, 2012 8:57 pm

How to manipulate a value in a loop

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to manipulate a value in a loop

Post 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)
pschmehl
Forum Newbie
Posts: 14
Joined: Fri Oct 19, 2012 8:57 pm

Re: How to manipulate a value in a loop

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How to manipulate a value in a loop

Post 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.
(#10850)
pschmehl
Forum Newbie
Posts: 14
Joined: Fri Oct 19, 2012 8:57 pm

Re: How to manipulate a value in a loop

Post 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.....
Post Reply