Jump to content

Help please: Substr a variable inside an array...


frimax

Recommended Posts

Hi all,

I have a little problem to solve... I use the following script to get the latest values from the 'history' table of my database.

 

My func works fine but i don't know how to Substr the value of the 'date' values :-[ ... I need to integrate the following func to get the clean output of my date value when it lists the array data: $date = substr ($date,0,9);

 

Can anyone help me please? Thanks in advance...

 

$query = $DbLink->query("SELECT * FROM history ORDER BY date DESC");
$i = 0;

while ($historyx = mysql_fetch_array($query)) {
if ($i == 21) {
break;
}
  for ($i=0; $i< 2; $i++) {
  $output_value .= '<li>'.$historyx['id'].'--'.'$'.$historyx['login'].'--'.$historyx['date'].'</li>';
   $i++;
  }
}

 

Frimax

Hi,

 

The date returned from the database is a string, so it is possible to pass the $historyx['date'] to substr to extract the value, even as part of the $output_value assignment:

 

$output_value .= '<li>'.$historyx['id'].'--'.'$'.$historyx['login'].'--'. substr($historyx['date'], 0, 9) .'</li>';

 

However, it would be more readable to use the date() function to format the output into however you want it displayed. You can do something like

 

$history_date = date ("j M, Y", strtotime ($historyx['date']));

which would set $history_date to "3 Feb, 2007" for example. Read the date docs for a whole bunch of formatting options: http://www.php.net/manual/en/function.date.php

 

Also, I'm not fully understanding what you are wanting to achieve with the nested for loop and the "if ($i == 21)" conditional break. Does $i ever get to 21 as it will keep getting reset in the for loop? Unless I'm missing something I'd do away with the for loop all together:

 

$query = $DbLink->query("SELECT * FROM history ORDER BY date DESC");

while ($historyx = mysql_fetch_array($query)) 
{
  $output_value .= '<li>'.$historyx['id'].'--'.'$'.$historyx['login'].'--'.$historyx['date'].'</li>';
}

 

Cheers,

Darren.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.