Hi, Basically I have a loop which does the following:
$query = mysql_query("SELECT * FROM bands");
while($band = mysql_fetch_array($query)) {
$tabs = tablinks($band['id']);
echo $tabs;
}
As it goes through the loop the tablinks function is done which contains 5 additional queries. Therefore if the innitial loop is huge then there will be lots of queries. I was wondering if there's an easier way of doing this to reduce the number. I'd be greatful for your help. Thanks
Here is the tablinks function:
function tablinks($bid) {
$output = '';
$end = '';
$nbquery = mysql_query("SELECT COUNT(*) FROM tabs WHERE bid='$bid' && filetype!='Lyrics'");
$nbrows = mysql_result($nbquery, 0);
if($nbrows != 0) {
$output .= ' - [Tabs: ';
$end = "yes";
}
$nbquery = mysql_query("SELECT COUNT(*) FROM tabs WHERE bid='$bid' && filetype='Bass Tab'");
$nbrows = mysql_result($nbquery, 0);
if($nbrows != 0) {
$previous = "yes";
$output .= '<a href="tabs_songs.php?bid='.$bid.'&filetype=bass">Bass</a>';
}
$nbquery = mysql_query("SELECT COUNT(*) FROM tabs WHERE bid='$bid' && filetype='Drum Tab'");
$nbrows = mysql_result($nbquery, 0);
if($nbrows != 0) {
if($previous == "yes") {
$output .= ' | ';
}
$previous = "yes";
$output .= '<a href="tabs_songs.php?bid='.$bid.'&filetype=drum">Drum</a>';
}
$nbquery = mysql_query("SELECT COUNT(*) FROM tabs WHERE bid='$bid' && filetype='Guitar Tab'");
$nbrows = mysql_result($nbquery, 0);
if($nbrows != 0) {
if($previous == "yes") {
$output .= ' | ';
}
$output .= '<a href="tabs_songs.php?bid='.$bid.'&filetype=guitar">Guitar</a>';
}
if($end == "yes") {
$output .= ']';
}
$nbquery = mysql_query("SELECT COUNT(*) FROM tabs WHERE bid='$bid' && filetype='Lyrics'");
$nbrows = mysql_result($nbquery, 0);
if($nbrows != 0) {
$output .= ' - [<a href="tabs_songs.php?bid='.$bid.'&filetype=lyrics">Lyrics</a>]';
}
return $output;
}
$query = mysql_query("SELECT * FROM bands");
while($band = mysql_fetch_array($query)) {
$tabs = tablinks($band['id']);
echo $tabs;
}
As it goes through the loop the tablinks function is done which contains 5 additional queries. Therefore if the innitial loop is huge then there will be lots of queries. I was wondering if there's an easier way of doing this to reduce the number. I'd be greatful for your help. Thanks
Here is the tablinks function:
function tablinks($bid) {
$output = '';
$end = '';
$nbquery = mysql_query("SELECT COUNT(*) FROM tabs WHERE bid='$bid' && filetype!='Lyrics'");
$nbrows = mysql_result($nbquery, 0);
if($nbrows != 0) {
$output .= ' - [Tabs: ';
$end = "yes";
}
$nbquery = mysql_query("SELECT COUNT(*) FROM tabs WHERE bid='$bid' && filetype='Bass Tab'");
$nbrows = mysql_result($nbquery, 0);
if($nbrows != 0) {
$previous = "yes";
$output .= '<a href="tabs_songs.php?bid='.$bid.'&filetype=bass">Bass</a>';
}
$nbquery = mysql_query("SELECT COUNT(*) FROM tabs WHERE bid='$bid' && filetype='Drum Tab'");
$nbrows = mysql_result($nbquery, 0);
if($nbrows != 0) {
if($previous == "yes") {
$output .= ' | ';
}
$previous = "yes";
$output .= '<a href="tabs_songs.php?bid='.$bid.'&filetype=drum">Drum</a>';
}
$nbquery = mysql_query("SELECT COUNT(*) FROM tabs WHERE bid='$bid' && filetype='Guitar Tab'");
$nbrows = mysql_result($nbquery, 0);
if($nbrows != 0) {
if($previous == "yes") {
$output .= ' | ';
}
$output .= '<a href="tabs_songs.php?bid='.$bid.'&filetype=guitar">Guitar</a>';
}
if($end == "yes") {
$output .= ']';
}
$nbquery = mysql_query("SELECT COUNT(*) FROM tabs WHERE bid='$bid' && filetype='Lyrics'");
$nbrows = mysql_result($nbquery, 0);
if($nbrows != 0) {
$output .= ' - [<a href="tabs_songs.php?bid='.$bid.'&filetype=lyrics">Lyrics</a>]';
}
return $output;
}