rmelino Posted June 18, 2009 Share Posted June 18, 2009 Hello, I am having an issue with how to set the output so that there is a snippet of text inserted every third row. The output should look something like this: Name Addres Phone Distance Name Addres Phone Distance Name Addres Phone Distance **SNIPPET OF TEXT** Name Addres Phone Distance Name Addres Phone Distance Name Addres Phone Distance **SNIPPET OF TEXT** etc etc The code I have is the following: <?php $connection=mysql_connect ($dbname, $username, $password); if (!$connection) { die('Not connected : ' . mysql_error()); } $db_selected = mysql_select_db($database, $connection); if (!$db_selected) { die ('Can\'t use db : ' . mysql_error()); } $query = sprintf("SELECT blah, blah, blah ( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance FROM table HAVING distance < '%s' ORDER BY distance LIMIT 0 , 8", mysql_real_escape_string($center_lat), mysql_real_escape_string($center_lng), mysql_real_escape_string($center_lat), mysql_real_escape_string($radius)); $result = mysql_query($query); if (!$result) { die('Invalid query: ' . mysql_error()); } // Error Message if No Records Found if (!mysql_num_rows($result)) { echo '<br /><p class="content">Sorry, there are no results found'; } else while ($row = @mysql_fetch_assoc($result)){ echo '<div id="div2">',"\n"; echo '<p class="name">' . $row['company'] . '</p>',"\n"; echo '<p class="radius">' . round($row['distance'],2) . ' miles from ' . $city . '</p>',"\n"; echo '<p class="address">' . $row['address'] . '</p>',"\n"; echo '<p class="phone">' . $row['phone'] . '</p>',"\n"; echo '</div>',"\n"; } ?> Any help would be greatly appreciated... Quote Link to comment https://forums.phpfreaks.com/topic/162690-solved-insert-text-every-3rd-row-in-mysql-output/ Share on other sites More sharing options...
xtopolis Posted June 18, 2009 Share Posted June 18, 2009 $i = 1; while ($row = @mysql_fetch_assoc($result)){ echo '<div id="div2">',"\n"; echo '<p class="name">' . $row['company'] . '</p>',"\n"; echo '<p class="radius">' . round($row['distance'],2) . ' miles from ' . $city . '</p>',"\n"; echo '<p class="address">' . $row['address'] . '</p>',"\n"; echo '<p class="phone">' . $row['phone'] . '</p>',"\n"; echo '</div>',"\n"; if( ($i % 3) == 0 ){ echo 'Snippet of text'; } $i++; }//while ?> Quote Link to comment https://forums.phpfreaks.com/topic/162690-solved-insert-text-every-3rd-row-in-mysql-output/#findComment-858613 Share on other sites More sharing options...
Ken2k7 Posted June 18, 2009 Share Posted June 18, 2009 I believe that would insert text at the second line the first time through. Quote Link to comment https://forums.phpfreaks.com/topic/162690-solved-insert-text-every-3rd-row-in-mysql-output/#findComment-858646 Share on other sites More sharing options...
xtopolis Posted June 18, 2009 Share Posted June 18, 2009 Hmm, did I error somewhere between this post and my previous one? <?php $i = 1; while ($x < 17){ echo 'Data ' . $i . '<br>'; if( ($i % 3) == 0 ){ echo 'Snippet of text--<br>'; } $i++; $x++; }//while ?> Data 1 Data 2 Data 3 Snippet of text-- Data 4 Data 5 Data 6 Snippet of text-- Data 7 Data 8 Data 9 Snippet of text-- Data 10 Data 11 Data 12 Snippet of text-- Data 13 Data 14 Data 15 Snippet of text-- Data 16 Data 17 Quote Link to comment https://forums.phpfreaks.com/topic/162690-solved-insert-text-every-3rd-row-in-mysql-output/#findComment-858649 Share on other sites More sharing options...
Ken2k7 Posted June 18, 2009 Share Posted June 18, 2009 Oh oops. Sorry, my mistake. Quote Link to comment https://forums.phpfreaks.com/topic/162690-solved-insert-text-every-3rd-row-in-mysql-output/#findComment-858654 Share on other sites More sharing options...
xtopolis Posted June 18, 2009 Share Posted June 18, 2009 Np, I was uncertain as well. It's because I echo "data" first, then evaluate the mod of $i that it is confusing. $i is already 3, but it hasn't checked it yet. Quote Link to comment https://forums.phpfreaks.com/topic/162690-solved-insert-text-every-3rd-row-in-mysql-output/#findComment-858656 Share on other sites More sharing options...
rmelino Posted June 18, 2009 Author Share Posted June 18, 2009 thank you very much, it works perfectly! Quote Link to comment https://forums.phpfreaks.com/topic/162690-solved-insert-text-every-3rd-row-in-mysql-output/#findComment-858703 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.