palacios Posted March 10, 2011 Share Posted March 10, 2011 Dear Forum People, I have a question about PHP. I'm trying to use an array several times in combination with 'while' and 'mysql_fetch_assoc'. the reason i have this requirements is because i need to display some googlemap coordinates inside a javascript and later on the html page i want to display other data coming from the database. the code will explain the situation: this is the code that goes before the html <head> <?php //$where_query = "bedrooms_nbr = $form_bedrooms AND max_people > $form_adults AND available = '1'"; $sql_search = mysql_query("SELECT id, available, building_id, max_people, min_nights, bedrooms_nbr, bedrooms_interconn FROM property_flats WHERE $where_query GROUP BY building_id "); confirm_query($sql_search); //this is a function $num_rows = mysql_num_rows($sql_search); ?> <script type="text/javascript"> //<![CDATA[ // Sample hotel list - LAT/LNG/Name hotels = new Array( <?php while ($grow = mysql_fetch_assoc($sql_search)) { $gbuilding_id = ($grow['building_id']); //this goes on the next query --- $gsql_building = mysql_query("SELECT ordername, g1, g2 FROM property_buildings WHERE id = '$gbuilding_id'") ; confirm_query($gsql_building); //this is a function $grow_building = mysql_fetch_assoc ($gsql_building) ; $gordername = $grow_building['ordername']; $gg1 = $grow_building['g1']; $gg2 = $grow_building['g2']; echo "[". $gg1 . ", " . $gg2 . ", '". $gordername ."'], \n" ; } echo "[, , '']"; //reset($sql_search); then inside the page i repeat the same loop in a very similar way but with many more variables fetch from the db <?php // //mysql_query is in the <head> for gmap to work echo '<h3 class="">'. $num_rows .' Apartments found</h3>'; //print_r ($main_query_array) ; // $k=1; //this is for alternate colour row while ($row = mysql_fetch_assoc($sql_search)) { $id = $row['id'] ; $air_co = $row['air_co'] ; $bathrooms = $row['bathrooms'] ; $bedrooms = $row['bedrooms'] ; $ensuite = $row['ensuite'] ; $max_people = $row['max_people'] ; // etc etc .... $building_id = ($row['building_id']); //this goes on the next query --- and refers to next apartment page-- // this is to know the BUILDING NAME{{{ $sql_building = mysql_query("SELECT id, name, description, streetnum, street, areacode, picfolder, main_picture FROM property_buildings WHERE id = '$building_id'") ; confirm_query($sql_building); //this is a function $row_building = mysql_fetch_assoc ($sql_building) ; $name = $row_building['name']; $postcode = $row_building['areacode']; $description = $row_building['description']; // etc etc ..... $goto = 'apartment.php?id='.$building_id.''; // echo '<a class="previewimg fl" href="'.$goto.'" title="'.$name.'"><img src="'.$folder_main_picture.'" alt="'.$name.'" /></a> <small class="fr tr"><br /><b>Minimum Stay: '.$min_nights.' Night</b></small> <h3><a href="'.$goto.'">'.$name.'</a></h3> <div class="address">'. $streetnum .' '.$street. ' - ' . $postcode.' [ <a href="#">Show on map</a> ]</div> <p>'; $extract = substr ($description, 0, 250); // find position of last space in extract $lastSpace = strrpos($extract, ' '); // use $lastSpace to set length of new extract and add ... echo substr($extract, 0, $lastSpace).'... '; //echo '<a href="details.php?article_id='.$row['article_id'].'"> More</a>'; echo '<a href="'.$goto.'">more »</a></p> <table class="roomlist full"> <thead> <tr> <th class="tl w13">Available room types</th> <th class="tc w13">Maximum Persons</th> <th class="tr w13">Rate</th> </tr> </thead> <tbody>'; <!-- a table to show all information about an hotel ....-->' </tbody> </table> <div class="clear"> </div> </div> <hr class="thin"/> <!-- End of Search item }}}-->' ; $k++ ; } ?> please note: if i remove the loop for googlemap at the top of the page, the second loop for the hotel information dispalys correctly; and if i leave both loops working in the same page, only the google map comes up correctly. any suggestion? is it because i made a mistake in the php code? or the javascript for googlemaps messes up with the rest of the code? many thanks in advance. regards, giulio Quote Link to comment https://forums.phpfreaks.com/topic/230203-loop-with-while-into-array-more-than-1-time/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.