PHP outputs garbage in MySQL loop

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
mitchkill
Forum Newbie
Posts: 2
Joined: Mon Jul 07, 2003 8:58 am

PHP outputs garbage in MySQL loop

Post by mitchkill »

I am attempting to convert a ColdFusion script to a PHP script. The purpose of the script is to allow some one to choose a book and chapter from the Bible to display. One select box contains all 66 books of the Bible. When someone selects a book, the chapter select box is updated to display the correct number of chapters for each book of the Bible. Now here is my difficulty: Becuase I am dynamically updating the chapters select box, I must load all 1189 chapters into Java Script arrays. Thus I must loop through a MySQL query to populate this chapter array. However, when I try to do the, at random points in the script, garbage characters begin to print in places or all of the sudden the query will move from book 49 (or any number book for that matter) to book 19. Every time I refresh, it does something different. Occasionally, it will work. However after it works, when I hit refresh, the browser says that the page cannot be found. The table seems to appear fine in PHPMyAdmin. The code for all of this is here:

Code: Select all

<? 
db_connect("localhost", "BibleDB", "", ""); 

$getbooks = db_query("SELECT N.Book, N.BookTitle, C.Chapter, 
                                     C.ChapterNum 
                                   FROM BookNames N, BibleChapters C 
                                   WHERE N.Book=C.Book 
                                   ORDER BY N.Book, C.Chapter"); 
$getchapters = db_query("SELECT Chapter, ChapterNum 
                                       FROM BibleChapters 
                                       ORDER BY Chapter"); 

// Puts the 66 books into an <OPTION> statement 
$BookListBox = db_list_box($getbooks, "Book", "BookTitle");
db_data_seek($getbooks, 0); 
db_disconnect(); 
?>

Code: Select all

&lt;script language="JavaScript"&gt; 
&lt;!-- 
// For each book, create an array to hold the chapters. 
// Each book array will be identified by the book number 

&lt;? 
$bibleQU = db_fetch_array($getbooks); 
while($bibleQU) &#123; ?&gt; 
      // Create the array 
      BookArray&lt;? echo $bibleQU&#1111;'Book'] ?&gt; = new Array(); 
      BookArrayValue&lt;? echo $bibleQU&#1111;'Book'] ?&gt; = new Array(); 

     &lt;? 
     $i = 0; 
     $temp = $bibleQU&#1111;'Book']; ?&gt; 
     // Populate the array 
     &lt;? while($bibleQU&#1111;'Book'] == $temp) ?&gt; &lt;? &#123; 
          $i++; ?&gt;
          BookArray&lt;? echo $bibleQU&#1111;'Book'] ?&gt;&#1111;&lt;? echo $i; ?&gt;] = '&lt;?  echo $bibleQU&#1111;'Chapter'] ?&gt;'; 

          BookArrayValue&lt;? echo $bibleQU&#1111;'Book'] ?&gt;&#1111;&lt;? echo $i; ?&gt;] = &lt;? echo $bibleQU&#1111;'ChapterNum'] ?&gt;; 


     &lt;? $bibleQU = db_fetch_array($getbooks);
      &#125;
&#125; ?&gt;

// Function to populate the chapters for the book selected 
function PopulateChapter() &#123; 
// Only process the function if the first item is not selected. 
     if (document.Passage.Book.selectedIndex != 0) &#123; 
// Find the book number 
        var ThisBook = document.Passage.Book&#1111;document.Passage.Book.selectedIndex].value; 

        // Set the length of the chapters drop down equal to the length of the book's array 
        document.Passage.Chapter.length = eval("BookArray" + ThisBook + ".length"); 
        // Put '---' as the first option in the chapter drop-down 
        document.Passage.Chapter&#1111;0].value = ""; 
        document.Passage.Chapter&#1111;0].text = "---"; 
        document. Passage.Chapter&#1111;0].selected = true; 
        // Loop through the book's array and populate the chapter drop down. 
        for (i=1; i&lt;eval("BookArray" + ThisBook + ".length"); i++) &#123; 
           document.Passage.Chapter&#1111;i].value = eval("BookArray" + ThisBook + "&#1111;i]"); 
           document.Passage.Chapter&#1111;i].text = eval("BookArrayValue" + ThisBook + "&#1111;i]"); 
        &#125; 
      &#125; 
&#125; 

function VerifyChapter() &#123; 
    if (document.Passage.Chapter&#1111;document.Passage.Chapter.selectedIndex].value == "") &#123; 
        alert("Please choose a valid chapter."); 
        return false; 
    &#125; 
    else &#123; 
        document.Passage.submit(); 
    &#125; 
&#125; 

//--&gt; 
&lt;/script&gt;
This same idea works perfectly in ColdFusion, but it does not seem to work in PHP. Anybody have any ideas on what could be going wrong here? Do I need to change some settings to allow PHP to handle all 1189 records? Thanks!


__________________
MitchKill
II Cor. 12:9

"For God so loved the world that he gave his only begotten Son, that whosoever believeth on him should not perish but have everlasting life." John 3:16
User avatar
trollll
Forum Contributor
Posts: 181
Joined: Tue Jun 10, 2003 11:56 pm
Location: Round Rock, TX
Contact:

Post by trollll »

A little difficult to see what exactly will happen from here. You may want to use multidimensional arrays in javascript and have php print everything, turning this:

Code: Select all

BookArray&lt;? echo $bibleQU&#1111;'Book'] ?&gt; = new Array();
BookArrayValue&lt;? echo $bibleQU&#1111;'Book'] ?&gt; = new Array();
into something more like this:

Code: Select all

echo "BookArray[$bookNumber] = new Array();\n";
echo "BookArrayValue[$bookNumber] = new Array();\n";
When things get a bit difficult to read I find it a bit difficult to see if logic or syntax caused the problem. :)
mitchkill
Forum Newbie
Posts: 2
Joined: Mon Jul 07, 2003 8:58 am

Post by mitchkill »

Wow! In spite of my attempt to make my code as cluttered and unreadable as possible, someone comes up with a solution! You were right about the multidimensional array. It worked. I am not sure why the other method did not work with PHP but nonetheless this is a much cleaner way of doing it...and it works! Praise God! Thank you very much. :lol:
Post Reply