PDA

View Full Version : Here's an interesting php script


cica
03-31-2006, 12:10 PM
Hi-

I figured I'd try to give back to the community. I just wrote a script that will extract artist and song information from the mySql database, calculate the total space required to list the artist as a heading and place all of his/her songs underneath, then flow the information into 3 columns on a page. To see the result of this script, go here (http://www.coolchangemusic.com/index.php?pr=Songs) . This page is dynamically built with this script:


<?php
$lastartist = "" ;
$numrows = 0 ;
$numcolumns = 3 ;
$currentrow = 1 ;
$currentcolumn = 1 ;

// format table
echo "<table align=\"center\" style=\"font-size: 14px;\" background=\"images/5th.gif\"><tr><td valign=\"top\" align=\"center\" width=\"33%\">";

// return number of unique artists
$rows = mysql_query("SELECT DISTINCT ARTIST FROM UDT_SONG_LIST");
$numartists = mysql_num_rows($rows);

// return all songs from database
$results = mysql_query("SELECT * FROM UDT_SONG_LIST ORDER BY ARTIST");

// determine the total length of the resulting data
// before placing into table
$numsongs = mysql_num_rows($results);
$numrows = floor((($numartists * 2) + $numsongs)/$numcolumns) + $numcolumns ;

// loop through database results from query
while ($row = mysql_fetch_array($results))
{

// do we start a new column?
if ($currentrow > ($numrows * $currentcolumn))
{
echo "</td><td valign=\"top\" align=\"center\" width=\"33%\">";
$currentcolumn++ ;

if ($lastartist == $row[ARTIST])
{
echo '<br><b>' . $row[ARTIST] . '&nbsp (cont\'d)</b><br>';
$currentrow = $currentrow + 2 ;
}
}

// is this a new artist?
if ($lastartist != $row[ARTIST])
{echo '<br><b>' . $row[ARTIST] . '</b><br>';
$currentrow = $currentrow + 2 ;
}

// print song name
echo ' ' . $row[SONG] . '<br>';
$currentrow++ ;
$lastartist = $row[ARTIST] ;

}

echo "</td></tr></table>";
?>


Hope someone else can use this.