Jump to content

[SOLVED] Putting Results into Columns


ryanfc

Recommended Posts

I am creating a directory of businesses for a web site.  There are 8 main categories (dining, bars, entertainment, etc) and when a user clicks on a category it takes them to a page that lists all the businesses (and phone numbers) in that category in alphabetical order.  The problem I have is that it does so in one big long column.  Can anyone help me alter the code to display the results in 3 columns?  I do want to keep them in alphabetical order, however if that is straight across or down does, it does not really matter to me.  And I am fine with losing the Character (A, B, C, etc) header above each set if needed.  Thank you for your help.

 

Here is the code currently:

 

<?php
  while ($info = mysql_fetch_array( $data )) {
      // get the first letter of the name. let's convert
      // it to uppercase to make sure that 'A' and 'a' get
      // put into the same group. 
      $letter = strtoupper(substr($info['name'],0,1));
      
      // now we will check to see if the first letter of the
      // current name is the same as the first letter of the
      // previous name. If it is not, then we make a new
      // group 'header' with the letter. Since there is no 
      // previous row on the first pass, we will automatically
      // have an underlined 'A' made (or whatever your list starts with) 
      if ($letter != $prev_row) {
         echo "<br><u>$letter</u><br>";
      } // end if
      
      // here we just echo out the name
      echo "{$info['name']} <br>";
  Print "Phone: ". $info['phone'] . "<br />";
      
      // and here we assign the current letter to $prev_row so 
      // that on the next iteration of the loop, we will have 
      // a previous row letter to compare
      $prev_row = $letter;
   } // end while
?>

Link to comment
Share on other sites

$i=0;
echo "<table><tr><td>";
while ($info = mysql_fetch_array( $data )) {
$letter = strtoupper(substr($info['name'],0,1));
if ($letter != $prev_row) {
	echo "<br><u>$letter</u><br>";
}
echo "{$info['name']} <br>";
if ($i % 50 == 0)
	echo "<td>"
Print "Phone: ". $info['phone'] . "<br />";
$prev_row = $letter;
$i++;
}
echo "</table>

 

That should make a new column every 50 results. Your code is all in there, I just took out the comments to make it smaller.

 

Also, You don't need to use substr to get one character. A string is just an array of chars so this code:

$letter = strtoupper(substr($info['name'],0,1));

 

is the same as:

$letter = $info['name'][0]);

 

Even tighter:

      if ($info['name'][0] != $prev_row) {
         echo "<br><u>$letter</u><br>";
      }

 

Link to comment
Share on other sites

ok i now have:

 

<?php
  $i=0;
echo "<table><tr><td>";
while ($info = mysql_fetch_array( $data )) {
$letter = strtoupper(substr($info['name'],0,1));
if ($letter != $prev_row) {
	echo "<br><u>$letter</u><br>";
}
echo "{$info['name']} <br>";
if ($i % 50 == 0)
	echo "<td>"
Print "Phone: " . $info['phone'] . "<br />";
$prev_row = $letter;
$i++;
}
echo "</table>";
?>

 

but i am getting this error:

Parse error: syntax error, unexpected T_PRINT, expecting ',' or ';' in /home/myevvspa/public_html/client_list.php on line 62

 

line 62 is:

Print "Phone: " . $info['phone'] . "<br />";

Link to comment
Share on other sites

messed with the code a bit to make it read as this:

 

<?php
  $i=0;
echo "<table border=1><tr><td>";
while ($info = mysql_fetch_array( $data )) {
$letter = strtoupper(substr($info['name'],0,1));

echo "{$info['name']} <br>";
if ($i % 50 == 0);
echo "{$info['phone']}";
echo "<td>";

$prev_row = $letter;
$i++;
}
echo "</td></tr></table>";
?>

 

and it is basically doing what I want it to do.  Currently I only have 2 businesses in the database for testing purposes, and each one is going into its on cell.  However, next to the second cell is a third empty cell.  How do I get rid of that?

 

 

On a side note:  If I decide I want them listed down instead of across, but still have 3 columns, is there a way to alter the code to do that?

 

Link to comment
Share on other sites

try

<?php
mysql_connect('localhost');
mysql_select_db('test');
$data = mysql_query('select * from names order by name');
$prev_row ='';
echo '<table border ="3">';
while($info = mysql_fetch_array( $data )) {
$letter = strtoupper(substr($info['name'],0,1));
if ($letter != $prev_row) {
	if($count % 3) {
		for ($i = ($count % 3); $i < 3; $i++) echo '<td> </td>';
		echo '</tr>';
		$count =0;
	}
	$prev_row = $letter;
	echo '<tr><td colspan="3">',$letter,'</td></tr>';
}
if ($count % 3 == 0) echo '<tr>';
$count++;
echo '<td>', $info['name'], '<br />Phone: ', $info['phone'], "</td>\n";
if ($count % 3 == 0) echo '</tr>';
}
if($count % 3) {
for ($i = ($count % 3); $i < 3; $i++) echo '<td> </td>';
echo '</tr>';
}
echo '</table>';
?>

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.