Jump to content

Split query into two table columns


nepzap2

Recommended Posts

ok guys

 

I have a query that pretty much retrieves staff contact data from the database and echo's it out in a table with 1 row and 4 columns. What  I'm trying to do is split the returned data into two tables right next to each other  both with 1 row and 4 columns. Any help is greatly appreciated.

 

Code:

<?php

		//WHERE category='Affiliate'

		include 'db_config.php';
		$sql = "SELECT * 
				FROM staff_contact_table 
				WHERE active = 1
				ORDER BY last_name";	   
		$result = mysql_query($sql);

	?>
        <?php
		while ($row = mysql_fetch_assoc($result)) {

		$name = $row['first_name'];
		$last_name = $row['last_name'];
		$title = $row['title'];
		$office = $row['office'];
		$lab = $row['lab'];
		$officeTel = $row['office_phone'];
		$labTel = $row['lab_phone'];
		$url = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="$1">$1</a>', $row['url']);
		$email = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})','<a href="mailto:\\1">\\1</a>', $row['email']);
		$title = $row['title'];



		echo "<table>
				<tr>
					<td><b>$last_name, $name</b></td>
					<td>Office/Lab $office, $lab</td>
					<td>Office/Lab Tel # $officeTel, $labTel</td>
					<td>Email: <a href='mailto:[email protected]'>[email protected]</a></td>

				</tr>
			   </table>";


		}


		?>

Link to comment
https://forums.phpfreaks.com/topic/164373-split-query-into-two-table-columns/
Share on other sites

Try this:

<?php
	$i=0;
         while ($row = mysql_fetch_assoc($result)) {
                        
         $name = $row['first_name'];
         $last_name = $row['last_name'];
         $title = $row['title'];
         $office = $row['office'];
         $lab = $row['lab'];
         $officeTel = $row['office_phone'];
         $labTel = $row['lab_phone'];
         $url = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="$1">$1</a>', $row['url']);
         $email = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})','<a href="mailto:\\1">\\1</a>', $row['email']);
         $title = $row['title'];
         $align = ($i%2) ? "right" : "left"; //switch right and left for odds and evens. Starts on Left
      

         echo "<table width=\"49%\" align=\"$align\">
               <tr>
                  <td><b>$last_name, $name</b></td>
                  <td>Office/Lab $office, $lab</td>
                  <td>Office/Lab Tel # $officeTel, $labTel</td>
                  <td>Email: <a href='mailto:[email protected]'>[email protected]</a></td>
                  
               </tr>
               </table>";
      
              $i++;//Incriment i 
         }
         
         
         ?>

Sorry, got playing with it, it won't work...

Retry:

<?php
echo "<table width=\"100%\">\n<tr>\n";
$r=0;
$c=0;
while ($row = mysql_fetch_assoc($result)) {
$name = $row['first_name'];
$last_name = $row['last_name'];
$title = $row['title'];
$office = $row['office'];
$lab = $row['lab'];
$officeTel = $row['office_phone'];
$labTel = $row['lab_phone'];
$url = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="$1">$1</a>', $row['url']);
$email = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})','<a href="mailto:\\1">\\1</a>', $row['email']);
$title = $row['title'];
if($c==1){ $c=0; echo "</tr>\n<tr>"; }
echo "<td><table>
               <tr>
                  <td><b>$last_name, $name</b></td>
                  <td>Office/Lab $office, $lab</td>
                  <td>Office/Lab Tel # $officeTel, $labTel</td>
                  <td>Email: <a href='mailto:[email protected]'>[email protected]</a></td>
                  
               </tr>
               </table></td>";
	$c++;
}
echo "</tr></table>";
?>

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.