Jump to content

Foreach exception

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
dunnkers

dunnkers

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
Hello.

I have made a php code which displays the data of a table in a html table.
Code:
	$sql = mysql_query('SELECT users, email, date_joined FROM users LIMIT 0 , 20');

	while ($row = mysql_fetch_row($sql)) {

		$output .= "<tr>";

		foreach ($row as $el) $output .= "<td>".$el."</td>";

		$output .= '</tr>';

	}
Now, How can i make a exception for the foreach statement?
I want to do a function with the column datejoined. I want to output this datejoined:
ti
meFormat($row['datejoined'])
But how can i do this?
All help is appreciated.

DuNnkers.

#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
something like this.

foreach ($row as $var => $el) {
  if ($var == 'datejoined') {
    $output .= meFormat($row['datejoined']);
  } else {
    $output .= "<td>".$el."</td>"; 
  }
}

__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#3
dunnkers

dunnkers

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
Aha thanks.

#4
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
on the other hand, you shouldn't use $row inside the foreach if it's not really needed, it looks better (even though it works with it without a doubt), so I'd do this:
foreach ($row as $var => $el) {
  if ($var == 'datejoined') {
    $output .= meFormat($el);
  } else {
    $output .= "<td>".$el."</td>"; 
  }
}

__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall