-1

I have to show a music sheets and I use Open Sheet Music Display for that. There is JavaScript code used to display the sheet. Now I want to load an MXML file from my database. Normally this was the code that it needed to display it:

      <script src="../scripts/opensheetmusicdisplay.min.js"></script>
        <div id="osmdCanvas"></div>
        <script >
        var osmd = new opensheetmusicdisplay.OpenSheetMusicDisplay('osmdCanvas');
            osmd.setOptions({
            backend: 'svg',
            drawTitle: true,
            });

        osmd.load('../xml/Band_Of_Brothers.musicxml').then(function () {
        osmd.render();
        });
        </script>

But now I try to do it with my database item I replaced the normal location to that database element in the way shown below. But it doesn't work anymore. How can I solve it?

        <script src="../scripts/opensheetmusicdisplay.min.js"></script>
            <div id="osmdCanvas"></div>
            <script >
                <?php
                $query = 'SELECT `sheets_xml` FROM `imslp_sheets` WHERE 1';
                $result = $conn->query($query);
                if ($result->num_rows > 0) {
                    while ($row = $result->fetch_assoc()) {
                        $thisXmlSheet = $row['sheets_xml'];
                    }
                }
                echo "
            var osmd = new opensheetmusicdisplay.OpenSheetMusicDisplay('osmdCanvas');
                osmd.setOptions({
                backend: 'svg',
                drawTitle: true,
                });

            osmd.load('$thisXmlSheet').then(function () {
            osmd.render();
            });";
                ?>
        </script>
  • 1
    What does the resulting JavaScript code look like? Use the [browser console (dev tools)](//webmasters.stackexchange.com/q/8525) (hit `F12`), read any errors. – Sebastian Simon Nov 13 '22 at 19:21
  • Does this answer your question? [How can I combine two strings together in PHP?](https://stackoverflow.com/questions/8336858/how-can-i-combine-two-strings-together-in-php) – Blackhole Nov 13 '22 at 19:21
  • @SebastianSimon there are 2 errors? The first "failed to load resource: the server responded with a status of 404 (not found)" The 2nd error says "uncaught (in promise) Error: could not retrieve requested URL 404 at i.onreadystatechange" in the `opensheetmusicdisplay.min.js` –  Nov 13 '22 at 19:24
  • Is it true that the `$thisXmlSheet` contains something like `xxxxx.musicxml` ? (not `../xml/Band_Of_Brothers.musicxml`), right ? – Ken Lee Nov 13 '22 at 19:28
  • @KenLee yes that's right. In this case it's Band_Of_Brothers.musicxml –  Nov 13 '22 at 19:29
  • I see. Please see my suggested answer – Ken Lee Nov 13 '22 at 19:31

1 Answers1

1

I think it is the path problem.

So, please change using the following instead:

<?php
$query = 'SELECT `sheets_xml` FROM `imslp_sheets` WHERE 1';
$result = $conn->query($query);

if ($result->num_rows > 0) {
  while ($row = $result->fetch_assoc()) {
    $thisXmlSheet = $row['sheets_xml'];
  }
}
?>
 <script src="../scripts/opensheetmusicdisplay.min.js"></script>
        <div id="osmdCanvas"></div>
        <script >
        var osmd = new opensheetmusicdisplay.OpenSheetMusicDisplay('osmdCanvas');
            osmd.setOptions({
            backend: 'svg',
            drawTitle: true,
            });

        osmd.load('../xml/<?php echo $thisXmlSheet; ?>').then(function () {
        osmd.render();
        });
        </script>
Ken Lee
  • 6,985
  • 3
  • 10
  • 29