Jump to content

[SOLVED] Insert Text Every 3rd Row in MySQL output


rmelino

Recommended Posts

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...

Link to comment
Share on other sites

$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

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.