SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict peterb's Avatar
    Join Date
    Jun 2003
    Location
    Athens, Greece
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    converting type problem

    Hello all,

    I have a table that I am trying to extract a year from the rows if they exist.

    e.g.
    1857, the saga of new technology

    I have tried to extract and do a type change but it doesn't seem to work.

    PHP Code:
    //  show oldest book
    $count5 mysql_query("select subject from $db_name.$table_name  ") or die();

    $row5 mysql_num_rows($count5);

    // get current year
    $curr_year date('Y');

    for(
    $i=0;$i<$row5;$i++) {
        
    $temp substr($row['subject'], 16);
        
    $subject_year = (int)$temp;
        
    /*
        if ($subject_year > 1000 && $subject_year < $curr_year){
            $oldest_book[$i] = substr($row['subject'], 1, 4);
        }else{
            $oldest_book[$i] = "";
        } 
        */

    It only displays zeroes. The subject is a utf-8 string coming out of a table array.

    Can someone tell me what I'm doing wrong?

  2. #2
    SitePoint Zealot zalucius's Avatar
    Join Date
    Jul 2007
    Location
    Denmark
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    substring index starts at position 0 (zero) not position 1.

    Change the
    Code PHP:
    substr($row['subject'], 1, 6)

    to
    Code PHP:
    substr($row['subject'], 0, 6)
    zalucius

  3. #3
    SitePoint Addict peterb's Avatar
    Join Date
    Jun 2003
    Location
    Athens, Greece
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello zalucius,

    Thanks for responding. It appears that you caught one of my mistakes.

    Here is the corrected version.

    PHP Code:
    //  get books by year to determine oldest book 
    $result mysql_query("select subject from $db_name.$table_name "
                or die(
    'Query failed: ' mysql_error()."<br /><br />");

    // # of rows returned
    $numofrows mysql_num_rows($result);

    // create array
    $oldest_book = array();

    // get current year for comparison
    $curr_year date('Y');

    // extract year and compare
    for($i=0;$i<$numofrows;$i++) {
    $row5 mysql_fetch_array ($result);
        
    $temp substr($row5["subject"], 06);
        
    $subject_year = (int)$temp;
        
    //print $i." - ".$subject_year."<br />";
        
        //if ($subject_year > 1000 && $subject_year < $curr_year)
        
    if ($subject_year != 0)
        {
            
    $oldest_book[$i] = $subject_year;
        }else{
            
    $oldest_book[$i] = "";
        } 
        
    }
    //print "<pre>";var_dump($oldest_book);print "</pre>"; 

  4. #4
    SitePoint Zealot zalucius's Avatar
    Join Date
    Jul 2007
    Location
    Denmark
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you considered a change to your database, and store the year in a seperate field?
    zalucius

  5. #5
    SitePoint Addict peterb's Avatar
    Join Date
    Jun 2003
    Location
    Athens, Greece
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That would be a nice suggestion but I have simply inherited this from the people who use it. Another reason for this was because not every record includes a year.

    Not a problem, thanks.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •