ryan king Posted January 25, 2009 Share Posted January 25, 2009 Hello all, I have a zipcode radius script that I have implemented into my website. Everything is working fine except when you enter a zip from the form it displays the results wrong. I want it to display 4 results per row then start a new row and loop through this to the end. if you just hit "GO" on the form then it displays all results and it displays them correctly. Giving it a zipcode in the form just makes the results go strait down the page. What am I doing wrong? the variable $key is somehow not going through my table loop correctly. you can check the what I mean here. http://idilla.com/ziptest.html try not entering anything and hit GO to see what it is suppsoed to do and then use zipcode 36606 to see what it does on a form entry here is my code: <?php print "<link href='/skins/Cobalt/Cobalt.css' rel='stylesheet' type='text/css' media='screen'>"; require('include/jamroom-include.inc.php'); require_once('zipcode.class.php'); // zip code class $row_count = 0; $z = new zipcode_class; $zips = $z->get_zips_in_range($_POST['zip_code'], $_POST['miles'], _ZIPS_SORT_BY_DISTANCE_ASC, true); // add searched for zip to $zips array $zips[$_POST['zip_code']] = 0; $row_count = 0; foreach ($zips as $key => $value){ //find all locations within range using returned zipcode values $sql_events = mysql_query("SELECT * FROM jamroom_band_info WHERE band_zipcode='$key'") or die (mysql_error()); $entriesperline=4; $counter=1; print "<table>"; while ($row = mysql_fetch_array($sql_events)) { if($counter%$entriesperline==1) { print "<tr><td align='center'><div class='body-zip'><a href=\"members/{$row['band_id']}\">{$row['band_name']}</a><br><a href=\"members/{$row['band_id']}\"><img border='0' src=\"image.php?band_id={$row['band_id']}&mode=band_image&width=100&theme=Sage\">{$row['band_image']}</a></div></td>"; } else if($counter%$entriesperline==0) { print "<td align='center'><div class='body-zip'><a href=\"members/{$row['band_id']}\">{$row['band_name']}</a><br><a href=\"members/{$row['band_id']}\"><img border='0' src=\"image.php?band_id={$row['band_id']}&mode=band_image&width=100&theme=Sage\">{$row['band_image']}</a></td></div></tr>"; } else { print "<td align='center'><div class='body-zip'><a href=\"members/{$row['band_id']}\">{$row['band_name']}</a><br><a href=\"members/{$row['band_id']}\"><img border='0' src=\"image.php?band_id={$row['band_id']}&mode=band_image&width=100&theme=Sage\">{$row['band_image']}</a></div></td>"; } $counter++; } //exit loop $counter--; if($counter%$entriesperline!=0) { print "</tr>"; } print "</table>"; } ?> any help to fix this issue would be greatly apreciated! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/142331-solved-results-display-wrong-from-form/ Share on other sites More sharing options...
sasa Posted January 25, 2009 Share Posted January 25, 2009 move lines $entriesperline=4; $counter=1; print "<table>"; before foreach loop Quote Link to comment https://forums.phpfreaks.com/topic/142331-solved-results-display-wrong-from-form/#findComment-745779 Share on other sites More sharing options...
ryan king Posted January 25, 2009 Author Share Posted January 25, 2009 I tried that already. didnt work. Ive moved it eveywhere I could think of with no change. Quote Link to comment https://forums.phpfreaks.com/topic/142331-solved-results-display-wrong-from-form/#findComment-745780 Share on other sites More sharing options...
uniflare Posted January 25, 2009 Share Posted January 25, 2009 Ok basically it is because when you use the foreach loop for each zip code that gets returned, you are also going through each zipcode that matches. Try to understand every time you loop the zip code, you start a table, you grab all the results from the zip code, display them, then end the table, then you start on the next zip code (Start table, grab,display,endtable), and so on. so you have 1 row (result/column) per table, which is why all the results are on one line. Thankfully you can fix this quite nicely: [1] Logic Ok so you need to grab the results, all into one single variable (an array of data). Then you would want to display the data in the correct fashion. -- At the moment you are trying to get the results at the same time your displaying the data. [2] The Fix First get the results and store them sequentially to an array like so: <?php // this is just to initialise the variable: $result_array = array(); // first we gather all the results into a single array: foreach ($zips as $key => $value){ //find all locations within range using returned zipcode values $sql_events = mysql_query("SELECT * FROM jamroom_band_info WHERE band_zipcode='$key'") or die (mysql_error()); while ($row = mysql_fetch_array($sql_events)) { // we just add this row to the array: $result_array[] = $row; } } ?> Then you would slightly change the display code to reflect the changes, like so: <?php print "<table>"; // loop through each result row. foreach ($result_array As $row){ if($counter%$entriesperline==1){ print "<tr><td align='center'><div class='body-zip'><a href=\"members/{$row['band_id']}\">{$row['band_name']}</a><br><a href=\"members/{$row['band_id']}\"><img border='0' src=\"image.php?band_id={$row['band_id']}&mode=band_image&width=100&theme=Sage\">{$row['band_image']}</a></div></td>"; }else if($counter%$entriesperline==0){ print "<td align='center'><div class='body-zip'><a href=\"members/{$row['band_id']}\">{$row['band_name']}</a><br><a href=\"members/{$row['band_id']}\"><img border='0' src=\"image.php?band_id={$row['band_id']}&mode=band_image&width=100&theme=Sage\">{$row['band_image']}</a></td></div></tr>"; }else{ print "<td align='center'><div class='body-zip'><a href=\"members/{$row['band_id']}\">{$row['band_name']}</a><br><a href=\"members/{$row['band_id']}\"><img border='0' src=\"image.php?band_id={$row['band_id']}&mode=band_image&width=100&theme=Sage\">{$row['band_image']}</a></div></td>"; } $counter++; } //exit loop if($counter%$entriesperline!=0){ print "</tr>"; } print "</table>"; ?> === All in all you should have something like: <?php print "<link href='/skins/Cobalt/Cobalt.css' rel='stylesheet' type='text/css' media='screen'>"; require('include/jamroom-include.inc.php'); require_once('zipcode.class.php'); // zip code class $row_count = 0; $z = new zipcode_class; $zips = $z->get_zips_in_range($_POST['zip_code'], $_POST['miles'], _ZIPS_SORT_BY_DISTANCE_ASC, true); // add searched for zip to $zips array $zips[$_POST['zip_code']] = 0; $row_count = 0; // this is just to initialise the variable: $result_array = array(); // first we gather all the results into a single array: foreach ($zips as $key => $value){ //find all locations within range using returned zipcode values $sql_events = mysql_query("SELECT * FROM jamroom_band_info WHERE band_zipcode='$key'") or die (mysql_error()); while ($row = mysql_fetch_array($sql_events)) { // we just add this row to the array: $result_array[] = $row; } } // config i guess $entriesperline=4; $counter=1; print "<table>"; // loop through each result row. foreach ($result_array As $row){ if($counter%$entriesperline==1){ print "<tr><td align='center'><div class='body-zip'><a href=\"members/{$row['band_id']}\">{$row['band_name']}</a><br><a href=\"members/{$row['band_id']}\"><img border='0' src=\"image.php?band_id={$row['band_id']}&mode=band_image&width=100&theme=Sage\">{$row['band_image']}</a></div></td>"; }else if($counter%$entriesperline==0){ print "<td align='center'><div class='body-zip'><a href=\"members/{$row['band_id']}\">{$row['band_name']}</a><br><a href=\"members/{$row['band_id']}\"><img border='0' src=\"image.php?band_id={$row['band_id']}&mode=band_image&width=100&theme=Sage\">{$row['band_image']}</a></td></div></tr>"; }else{ print "<td align='center'><div class='body-zip'><a href=\"members/{$row['band_id']}\">{$row['band_name']}</a><br><a href=\"members/{$row['band_id']}\"><img border='0' src=\"image.php?band_id={$row['band_id']}&mode=band_image&width=100&theme=Sage\">{$row['band_image']}</a></div></td>"; } $counter++; } //exit loop if($counter%$entriesperline!=0){ print "</tr>"; } print "</table>"; ?> Dont just copy the code, learn from the teachings . Hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/142331-solved-results-display-wrong-from-form/#findComment-745818 Share on other sites More sharing options...
ryan king Posted January 25, 2009 Author Share Posted January 25, 2009 thanks uniflare! that works perfect! I understand your logic as well. This morning I thought that this was happening becuase if I got rid of the $key variable and just manualy gave it a zipocode, it would display fine. I figured I needed to turn my result into an array but I had no idea how to achieve this since it seem to me like $key was the array.. so I guess I needed to compile the results of my array into another array! Your a genius! Thank you for taking the time to help me understand it as well, Im kind of a noob at this php/sql stuff. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/142331-solved-results-display-wrong-from-form/#findComment-745962 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.