SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Dynamic titles

  1. #1
    SitePoint Member
    Join Date
    Jan 2005
    Location
    none
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Dynamic titles

    Hi
    I'm using header and footer files for my site layout. i've got a problem with header file when i want to grab title from database.
    here's Ex. code:

    $title = "$song_name";
    include "header.php";

    $result = @mysql_query("select * from tracks where song_id='$id' limit 0,1");
    if (!$result) {
    die("Error performing query: " . mysql_error());
    }
    if(mysql_num_rows($result)==0){
    echo "no items available";
    }
    else {
    while($row = mysql_fetch_array($result)){
    $song_name = $row["song_name"];
    $lyrics = $row["lyrics"];

    echo "$song_name ";
    echo "$lyrics";
    }
    ?>

    so how can I change my $title value with song name from database? I know it's possible to define $title after all that mysql stuff, but then header.php wont display title.
    What would be the best solution for me? please help

    Thanks

  2. #2

    Join Date
    Oct 2003
    Location
    €uroLand
    Posts
    1,340
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, as you are iterating over your data rows you have apparently more than just one song name. Which one do you want to "pass" to header.php then?

  3. #3
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looks like song_id is a unique identifyier so you'll only ever have 0 or 1 matching results.
    You just need to reshuffle your logic, so something like:
    PHP Code:
    $result = @mysql_query("select * from tracks where song_id='$id'");
    if (!
    $result) {
      die(
    "Error performing query: " mysql_error());
    }
    $title 'Default Title';
    if(
    mysql_num_rows($result)!=0){
      
    $row mysql_fetch_array($result);
      
    $title $row['song_title'];
    }
    require_once 
    'header.php';
    if(empty(
    $row)) {
       echo 
    'Song not found.';
    } else {
      echo 
    $row['song_title'];
      echo 
    $row['lyrics'];


  4. #4

    Join Date
    Oct 2003
    Location
    €uroLand
    Posts
    1,340
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by markl999
    Looks like song_id is a unique identifyier so you'll only ever have 0 or 1 matching results.
    I should have paid more attention to the sql query .

  5. #5
    SitePoint Member
    Join Date
    Jan 2005
    Location
    none
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thx a lot


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
  •