Jump to content

display multiple columns


matvespa

Recommended Posts

I have this code which works fine. I want to improve it by displaying it into multiple columns. Current output:

 

TITLE 1

JOB1

---------

TITLE 2

JOB 2

---------

ETC...

 

I want it to display like this instead:

 

TITLE 1  |  TITLE 2

JOB 1    |  JOB 2

 

Here is my code:

$sql = "SELECT * FROM coaches WHERE position = 'Junior Coach'"; 
	$result = mysql_query ($sql); 

	while ($row = mysql_fetch_array($result)){ 
		$id = $row["id"];
		$firstname = $row["firstname"];
		$lastname = $row["lastname"];
		$position = $row["position"];
		$image = $row["image"];
	?>
             <table width="183" border="1">
                <tr>
                  <td align="center" height="18">
                  <?php echo "<img id='image_shadowCoaches' src=" . $image . ">"; ?><br />
			  <?php echo "<div id='Coaches_Detail'>".$firstname." ".$lastname."</div>" ?>
                 <?php echo "<div id='Coaches_DetailLower'>".$position."</div>" ?>
                  
                  </td>
                </tr>
                </table>
             <?php } ?>

Link to comment
Share on other sites

Hey Matvespa, try the following :)

 

<table cellpadding="0" cellspacing="0" border="1">
  <tr>

<?PHP

  // Create the the query
  $myQuery1 = "SELECT * FROM coaches WHERE position = 'Junior Coach'";
  // Execute the above query here
  $result   = mysql_query($sql); 
  
  // While records exists keep fetching them
  while($row = mysql_fetch_array($result)){ 

    // Give each of the records a $variable name
    $id        = $row['id'];
    $firstname = $row['firstname'];
    $lastname  = $row['lastname'];
    $position  = $row['position'];
    $image     = $row['image'];
?>

  <td align="center" height="18" width="183">
<?php echo '<img id="image_shadowCoaches" src='.$image.'>'; ?><br />
<?php echo '<div id="Coaches_Detail">'.$firstname.' '.$lastname.'</div>'; ?>
<?php echo '<div id="Coaches_DetailLower">'.$position.'</div>'; ?>
  </td>

<?php } ?>

</tr>
</table>

 

Hope it helps you, if not just let me know.

 

Thanks, Paul.

Link to comment
Share on other sites

Hey Paul,

 

Thanks you very much. It works. But unfortunately, how do i limit the no. of column per row. Like for example only allowing 2 output in each row. If my database has 3 output, there would be 2 output in row1 and 1 output in row2. Any idea?

Link to comment
Share on other sites

I was just about to say that Matvespa, he wasn't far off though :)

 

Try the following

<table cellpadding="0" cellspacing="0" border="1">
  <tr>

<?PHP

  $myQuery1 = "SELECT * FROM coaches WHERE position = 'Junior Coach'"; // Create the the query
  $result   = mysql_query($sql); // Execute the above query here
  $number   = '0'; // Set a number so we can limit the number of TD's per row
  $tbNumber = '2'; // Set how many TD's you want per row

  // While records exists keep fetching them
  while($row = mysql_fetch_array($result)){ 

    // Give each of the records a $variable name
    $id        = $row['id'];
    $firstname = $row['firstname'];
    $lastname  = $row['lastname'];
    $position  = $row['position'];
    $image     = $row['image'];
?>

  <td align="center" height="18" width="183">
<?php echo '<img id="image_shadowCoaches" src='.$image.'>'; ?><br />
<?php echo '<div id="Coaches_Detail">'.$firstname.' '.$lastname.'</div>'; ?>
<?php echo '<div id="Coaches_DetailLower">'.$position.'</div>'; ?>
  </td>

<?php if(is_int($number/$tbNumber)) { echo '</tr><tr>'; } } ?>

</tr>
</table>

 

This should work now, if not let me know.

 

Thanks, Paul.

Link to comment
Share on other sites

Sorry about that Matvespa, I over looked a simple little bit of code, I didn't increment the $number variable, use the following...

 


<table cellpadding="0" cellspacing="0" border="1">
  <tr>

<?PHP

  $myQuery1 = "SELECT * FROM coaches WHERE position = 'Junior Coach'"; // Create the the query
  $result   = mysql_query($sql); // Execute the above query here
  $number   = '1'; // Set a number so we can limit the number of TD's per row
  $tbNumber = '2'; // Set how many TD's you want per row

  // While records exists keep fetching them
  while($row = mysql_fetch_array($result)){ 

    // Give each of the records a $variable name
    $id        = $row['id'];
    $firstname = $row['firstname'];
    $lastname  = $row['lastname'];
    $position  = $row['position'];
    $image     = $row['image'];
?>

  <td align="center" height="18" width="183">
<?php echo '<img id="image_shadowCoaches" src='.$image.'>'; ?><br />
<?php echo '<div id="Coaches_Detail">'.$firstname.' '.$lastname.'</div>'; ?>
<?php echo '<div id="Coaches_DetailLower">'.$position.'</div>'; ?>
  </td>

<?php if(is_int($number/$tbNumber)) { echo '</tr><tr>'; } $number ++; } ?>

</tr>
</table>

 

Thanks, Paul.

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.