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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.