How would I go about putting function calls into a variable and then echo or printing them repeatedly?
Currently I have this code which is calling the PHP function “chords” 24 times. The functions are in groups of six so I would like to condense that six. I would like to scale this up though only once I have the code looking nicer and running more efficiently.
<?php
$chord_name = 'A 7';
$i1 = '4';
$i2 = '7';
$i3 = '10';
?>
<div id="chords_wrapper" class="chords_wrapper">
<? echo $fretnumbers; ?>
<h1><? echo $chord_name; ?></h1>
<ul><?php chords (6); ?></ul>
<ul><?php chords (5); ?></ul>
<ul><?php chords (4); ?></ul>
<ul><?php chords (3); ?></ul>
<ul><?php chords (2); ?></ul>
<ul><?php chords (1); ?></ul>
</div>
<?php
$chord_name = 'A Minor';
$i1 = '3';
$i2 = '7';
$i3 = '0';
?>
<div id="chords_wrapper" class="chords_wrapper">
<? echo $fretnumbers; ?>
<h1><? echo $chord_name; ?></h1>
<ul><?php chords (6); ?></ul>
<ul><?php chords (5); ?></ul>
<ul><?php chords (4); ?></ul>
<ul><?php chords (3); ?></ul>
<ul><?php chords (2); ?></ul>
<ul><?php chords (1); ?></ul>
</div>
<?php
$chord_name = 'A Minor 7';
$i1 = '3';
$i2 = '7';
$i3 = '10';
?>
<div id="chords_wrapper" class="chords_wrapper">
<? echo $fretnumbers; ?>
<h1><? echo $chord_name; ?></h1>
<ul><?php chords (6); ?></ul>
<ul><?php chords (5); ?></ul>
<ul><?php chords (4); ?></ul>
<ul><?php chords (3); ?></ul>
<ul><?php chords (2); ?></ul>
<ul><?php chords (1); ?></ul>
</div>
<div style="width:620px; text-align:center; clear:both;">
</div>
</div>
I would like to have it look something like this so that each group of functions can be seen more easy and tidy up the code. I have tried this but it displays the php code without executing it.
<?php
$kn = '25';
$chord_dump = '<div id="chords_wrapper" class="chords_wrapper">
<? echo $fretnumbers; ?>
<h1>'.$chord_name.'</h1>
<ul><?php chords (6); ?></ul>
<ul><?php chords (5); ?></ul>
<ul><?php chords (4); ?></ul>
<ul><?php chords (3); ?></ul>
<ul><?php chords (2); ?></ul>
<ul><?php chords (1); ?></ul>
</div>';
?>
<div id="chords_set">
<?php
// A Major
$i1 = '4';
$i2 = '7';
$i3 = '0';
$chord_name = 'A Major';
echo $chord_dump;
// A 7
$i1 = '4';
$i2 = '7';
$i3 = '10';
$chord_name = 'A 7';
echo $chord_dump;
// A Minor
$i1 = '3';
$i2 = '7';
$i3 = '0';
$chord_name = 'A Minor';
echo $chord_dump;
// A Minor 7
$i1 = '3';
$i2 = '7';
$i3 = '10';
$chord_name = 'A Minor 7';
echo $chord_dump;
?>
<div style="width:620px; text-align:center; clear:both;">
</div>
</div>
The chord function
<?
function chords ($funtion_string) {
/*$chrd_tn ="CGCFCE";
$tuning_capo =0;*/
$chrd_tn = $GLOBALS['chrd_tn'];
$tuning_capo = $GLOBALS['tuning_capo'];
$notes_array = '';
// Select Chord Key Notes
$result = mysql_query("SELECT * FROM `guitar_tunings_chords` WHERE note_id - ".$GLOBALS['kn']." >= 0 AND ( (( note_id - ".$GLOBALS['kn'].") % 12 ) = ".$GLOBALS['i1']." OR ( (note_id-".$GLOBALS['kn'].") % 12 ) = ".$GLOBALS['i2']." OR ( (note_id-".$GLOBALS['kn'].") % 12 ) = ".$GLOBALS['i3']." OR ( note_id - ".$GLOBALS['kn'].") % 12 = 0) LIMIT 24") or die(mysql_error());
while ( $row = mysql_fetch_array($result) ) {
$notes2 = $row["note"];
$notes2 = str_replace("#", "z", $notes2);
$notes_array = ''.$notes2.','.$notes_array.'';
}
// Delete comma from end of chord notes array
$notes_array = substr($notes_array,0,-1);
// Counter to distinguish the nut from the fretboard
$counter = 0;
$result2 = mysql_query("SELECT *
FROM `guitar_tunings_links`
JOIN guitar_tunings_chords ON guitar_tunings_links.".$funtion_string." = guitar_tunings_chords.note
WHERE tuning = '".$chrd_tn."'") or die(mysql_error());
while ( $row = mysql_fetch_array($result2) ) {
$bridge_note = ($row["note_id"]);
}/*
?><BR>$chrd_tn = <? var_dump ($chrd_tn);
?><BR>$tuning_capo = <? var_dump ($tuning_capo);
?><BR>$funtion_string = <? var_dump ($funtion_string);
?><BR>$key_note = <? var_dump ($key_note);
?><BR>$bridge_note = <? var_dump ($bridge_note);
?><BR><?*/
// SELECT * FROM `guitar_tunings_chords` WHERE `note_id` >= 28 LIMIT 0,8
$result4 = mysql_query("SELECT * FROM `guitar_tunings_chords` WHERE `note_id` >= $bridge_note LIMIT $tuning_capo,8") or die(mysql_error());
while ( $row = mysql_fetch_array($result4) ) {
//Notes
$note = ($row["note"]);
// Replace # with z
$note = str_replace("#", "z", $note);
// Distinguish nut notes on or off
if (preg_match("/\b".$note."\b/i", $notes_array)) {
$n_nut_style = 'note_nut_on';
} else { $n_nut_style = 'note_nut_off';
}
// Distinguish fretboard notes on or off
if (preg_match("/\b".$note."\b/i", $notes_array)) {
$n_style = 'note_on';
} else { $n_style = 'note_off';
}
// Print nut notes
if ($counter < 1) {
$str_prtou = '<li class="'.$n_nut_style.'">'.$note.'</li>';
$numbers = array("0", "1", "2", "3", "4", "5", "6", "7", "8");
$str_prtou = str_replace($numbers, "", $str_prtou);
}
// Print fretboard notes
if ($counter > 0)
$str_prtou = ''.$str_prtou.'<li class="'.$n_style.'">'.$note.'</li>
';
$counter++;
}
$str_prtou = str_replace("z", "#", $str_prtou);
$numbers = array("0", "1", "2", "3", "4", "5", "6", "7", "8");
$str_prtou = str_replace($numbers, "", $str_prtou);
echo $str_prtou;
}
?>
This could be the solution, but it’s bad style:
You can then
echothe$chord_dumpvariable and it will always execute the__toStringfunction of thecord_dumpclass then:That’s done by PHP when it converts the object to string (because the variable is used with
echo, triggering the so called string-context).But instead I suggest you remove the global variables and turn them into parameters. Additionally you could normalize each output-unit into a data-structure like an
ArrayorstdClass, wrap them into an array, iterate over that array and create the output. That done you could easily extend both, what your program is doing as well as the output.