Jump to content

Array Problem


brad_langdon

Recommended Posts

In the following code I am trying to insert the result of the mysql query called " $loop3 " into the javascript code that follows the loop. As the code is outside of the loop it only echo's the last result from the loop.

 

I don't know much about arrays but I am pretty sure I have to store all of the results from " $loop3 " in an array so it can be used out of the loop. Can someone please explain how to do this.

 

Please explain carefully as I don't know too much about arrays. The loop and javascript snippet is about halfway down through the code.

 

 

<?php $loop = mysql_query("SHOW tables FROM $data_base") or die ('cannot select tables'); // SELECT ALL TABLES FROM CLIENT DATABASE
            while($row = mysql_fetch_array($loop)){ // SELECT ALL TABLES FROM CLIENT DATABASE
                
                $loop2 = mysql_query("SELECT * FROM $row[0] ORDER BY date02") or die ('cannot select table fields'); // SELECT ALL CONTENT FROM EACH TABLE
                while ($row2 = mysql_fetch_array($loop2)){ // SELECT ALL CONTENT FROM EACH TABLE
                    
                    
                    echo "<div class='tabs'>";
                    echo "<h2>" . $row2[title] . "</h2>";
                    
                        
                            

                            $loop3 = mysql_query("SELECT * FROM $row[0] ORDER BY date02") or die ('Cannot select table fields'); // SELECT ALL CONTENT FROM EACH TABLE
                            mysql_data_seek($loop3,0); // RESET DATA COUNTER SINCE WHILE LOOP HAS ALREADY BEEN USED
                            while ($row3 = mysql_fetch_array($loop3)){
                            
                            $date = $row3[date02]; // LIST EACH OCCUPIED DATE IN FORMAT THAT MATCHES CALENDAR JQUERY FORMAT
                            
                            $date_a = substr ($date, 0, 4); // PULL YEAR AS SEPERATE VARIABLES SO THEY CAN BE REARRANGED TO FIT CALENDAR FORMAT
                            $date_b = substr ($date, 4, 2); // PULL MONTH AS SEPERATE VARIABLES SO THEY CAN BE REARRANGED TO FIT CALENDAR FORMAT
                            $date_c = substr ($date, 6, 2); // PULL DAY AS SEPERATE VARIABLES SO THEY CAN BE REARRANGED TO FIT CALENDAR FORMAT
                            
                            $date = "\"" . $date_b . "/" . $date_c . "/" . $date_a . "\","; // RE FORMAT DATE TO FIT CALENDAR JQUERY FORMAT

                            
                            } ?>
                            
                      
                    <script type='text/javascript'>
                    function nationalDays(date) {
                    var natDays = [
                    <?php echo $date; ?> <!-- -- -- THIS IS THE PART WHERE I NEED THE ARRAY TO LIST THE RESULTS OF THE LOOP ABOVE  -- -- -->
                    ];
                    var sDate = (date.getMonth()+1).toString() + '/' + date.getDate().toString() + '/' + date.getFullYear().toString();
                    if ($.inArray(sDate, natDays) != -1) return [false,'Occupied','Not this day!'];
                    else return [true, ''];
                    }
                    </script>
                        
                        <?php
                    echo "<span class='StartDate'></span>";
                    echo "<p><h3>Booked Dates</h3>";    
                    
                            $loop2 = mysql_query("SELECT * FROM $row[0] ORDER BY date02") or die ('cannot select table fields'); // SELECT ALL CONTENT FROM EACH TABLE
                            mysql_data_seek($loop2,0); // RESET DATA COUNTER SINCE WHILE LOOP HAS ALREADY BEEN USED
                            while ($row2 = mysql_fetch_array($loop2)){
                            echo "<a class='cross' href='delete.php?cmd=delete&id=$row2[id]&table=$row[0]'></a><li class='dates'>" . $row2[date] . "</li>"; // LIST EACH OCCUPIED DATE AND LINK TO DELETE.PHP TO DELETE RECORD
                            }
                        
                         
                    echo "</p>";
                    echo "<p><a href='sessiondestroy.php?signout=true'>Logout</a></p>"; // SIGN OUT USER
                    echo "</div>";                            
                    
                    }
                }
                 

            } ?>

Link to comment
https://forums.phpfreaks.com/topic/181845-array-problem/
Share on other sites

$loop3 = mysql_query("SELECT * FROM $row[0] ORDER BY date02") or die ('Cannot select table fields'); // SELECT ALL CONTENT FROM EACH TABLE
                            mysql_data_seek($loop3,0); // RESET DATA COUNTER SINCE WHILE LOOP HAS ALREADY BEEN USED
                            while ($row3 = mysql_fetch_array($loop3)){
                            
                            $date = $row3[date02]; // LIST EACH OCCUPIED DATE IN FORMAT THAT MATCHES CALENDAR JQUERY FORMAT
                            
                            $date_a = substr ($date, 0, 4); // PULL YEAR AS SEPERATE VARIABLES SO THEY CAN BE REARRANGED TO FIT CALENDAR FORMAT
                            $date_b = substr ($date, 4, 2); // PULL MONTH AS SEPERATE VARIABLES SO THEY CAN BE REARRANGED TO FIT CALENDAR FORMAT
                            $date_c = substr ($date, 6, 2); // PULL DAY AS SEPERATE VARIABLES SO THEY CAN BE REARRANGED TO FIT CALENDAR FORMAT
                            
                            $dates[] = "\"" . $date_b . "/" . $date_c . "/" . $date_a . "\","; // RE FORMAT DATE TO FIT CALENDAR JQUERY FORMAT

                            
                            } 

if jquery needs it in json after the loop and if you have php > 5.2

$dates=json_encode($dates);

if it's < 5.2 then I'd suggest using pear JSON class to encode

in your javascript

var natDays=<?php echo $dates; ?> // no need for the brackets json takes care of this 

Link to comment
https://forums.phpfreaks.com/topic/181845-array-problem/#findComment-959123
Share on other sites

I have managed to move forward slightly using part of the code you listed. Now I get all results listed for each instance of the second loop where I should be getting different results for each instance of the second loop.

 

I hope that makes sense. So the second loop is say a div with content and each div should display the results of the array depending on which database table it is pulling he results from. Instead I am getting all results from all tables in every div... any ideas?

 

 

<?php echo "<div class='panes'>";

            $loop = mysql_query("SHOW tables FROM $data_base") or die ('cannot select tables'); // SELECT ALL TABLES FROM CLIENT DATABASE
            while($row = mysql_fetch_array($loop)){ // SELECT ALL TABLES FROM CLIENT DATABASE
                
                $loop2 = mysql_query("SELECT * FROM $row[0] ORDER BY date02") or die ('cannot select table fields'); // SELECT ALL CONTENT FROM EACH TABLE
                while ($row2 = mysql_fetch_array($loop2)){ // SELECT ALL CONTENT FROM EACH TABLE
                    
                    
                    echo "<div class='tabs'>";
                    echo "<h2>" . $row2[title] . "</h2>";
                    $abc=$row2[title];
                    
                            $loop2 = mysql_query("SELECT * FROM $row[0] WHERE title = '$abc'") or die ('cannot select table fields'); // SELECT ALL CONTENT FROM EACH TABLE
                            while ($row3 = mysql_fetch_array($loop2)){
                            
                            $date = $row3[date02]; // LIST EACH OCCUPIED DATE IN FORMAT THAT MATCHES CALENDAR JQUERY FORMAT
                            
                            $date_a = substr ($date, 0, 4); // PULL YEAR AS SEPERATE VARIABLES SO THEY CAN BE REARRANGED TO FIT CALENDAR FORMAT
                            $date_b = substr ($date, 4, 2); // PULL MONTH AS SEPERATE VARIABLES SO THEY CAN BE REARRANGED TO FIT CALENDAR FORMAT
                            $date_c = substr ($date, 6, 2); // PULL DAY AS SEPERATE VARIABLES SO THEY CAN BE REARRANGED TO FIT CALENDAR FORMAT
                            
                            
                            $dates[] = "\"" . $date_b . "/" . $date_c . "/" . $date_a . "\","; // RE FORMAT DATE TO FIT CALENDAR JQUERY FORMAT
                            
                            } ?>
                            
                            
                    <script type='text/javascript'>
                    function nationalDays(date) {
                    var natDays = [
                    <?php foreach ($dates as $dates1)
                    { print_r($dates1); } ?>
                    ];
                    var sDate = (date.getMonth()+1).toString() + '/' + date.getDate().toString() + '/' + date.getFullYear().toString();
                    if ($.inArray(sDate, natDays) != -1) return [false,'Occupied','Not this day!'];
                    else return [true, ''];
                    }
                    </script>
                        
                        <?php
                            echo "<span class='StartDate'></span>";
                            echo "<p><h3>Booked Dates</h3>";    
                    
                            $loop2 = mysql_query("SELECT * FROM $row[0] ORDER BY date02") or die ('cannot select table fields'); // SELECT ALL CONTENT FROM EACH TABLE
                            mysql_data_seek($loop2,0); // RESET DATA COUNTER SINCE WHILE LOOP HAS ALREADY BEEN USED
                            while ($row2 = mysql_fetch_array($loop2)){
                            echo "<a class='cross' href='delete.php?cmd=delete&id=$row2[id]&table=$row[0]'></a><li class='dates'>" . $row2[date] . "</li>"; // LIST EACH OCCUPIED DATE AND LINK TO DELETE.PHP TO DELETE RECORD
                            }
                        
                         
                    echo "</p>";
                    echo "<p><a href='sessiondestroy.php?signout=true'>Logout</a></p>"; // SIGN OUT USER
                    echo "</div>";                            
                    
                    }
                }
                 

            } ?>

Link to comment
https://forums.phpfreaks.com/topic/181845-array-problem/#findComment-959485
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.