Solved

PHP Multi-Column output needed

Posted on 2007-11-27
6
971 Views
Last Modified: 2013-12-12
At the moment I get a result set of 50 in a single column:

business 1
business 2
business 3

...
business 50

But I would prefer to show the output in 2 columns:

business 1    bussiness 2
business 3    business 4
 ...

How can this be done?

This is the code for the output from MySQL DB I'm using at the moment:

      <table style="width:339px; style="line-height:20px;">
            <?php

            $result = mysql_query("SELECT bizID,bizName,bizAddr,bizCity,bizState,bizZip,bizPhone FROM biz_cars WHERE bizState='$bizState' AND bizCity='$bizCity' AND bizLive='1' GROUP BY bizName LIMIT $offset,50");
            while($r=mysql_fetch_array($result))
            {
                  $bizID = $r['bizID'];
                  $bizName = $r['bizName'];
                  $bizAddr = $r['bizAddr'];
                  $bizZip = $r['bizZip'];
                  $bizPhone = $r['bizPhone'];

                  $bizNameLink = replaceForText($bizName);

                  echo '<tr><td><span class="bigger-text">' . $bizNameLink . '
                  <br />
                      <a href="'. $urlPrefix . 'profile/' . $bizID . '.html">'.$bizAddr. '</a><br> '.$bizCity.', '.$bizState.' '.$bizZip.' <br>
                  '.  $bizPhone.'<br>
                  
            
                  </tr></td></span>';

            }
            ?>
            </table>
0
Comment
Question by:seopti
6 Comments
 
LVL 15

Expert Comment

by:babuno5
Comment Utility
try the following code
$i=0;

 while($r=mysql_fetch_array($result))

{

	$bizID = $r['bizID'];

	$bizName = $r['bizName'];

	$bizAddr = $r['bizAddr'];

	$bizZip = $r['bizZip'];

	$bizPhone = $r['bizPhone'];
 

	$bizNameLink = replaceForText($bizName);

	if($i%2 == 0)

	{

		echo '<tr>';

	}

	echo '<td><span class="bigger-text">' . $bizNameLink . '

	<br />

	  <a href="'. $urlPrefix . 'profile/' . $bizID . '.html">'.$bizAddr. '</a><br> '.$bizCity.', '.$bizState.' '.$bizZip.' <br>

	'.  $bizPhone.'<br>
 
 

	</td>';

		

	if($i%2 == 0)

	{

		echo '</tr>';

	}

	++$i;

}

Open in new window

0
 
LVL 28

Expert Comment

by:gamebits
Comment Utility
0
 

Author Comment

by:seopti
Comment Utility
babuno5: Thanks for the code, but it did nothing. Nothing has changed using the code.

gamebits: Your code looks good but I can't get it to work with my current snippet which you see above. I am not sure how to merge these 2 snippets together.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 15

Expert Comment

by:babuno5
Comment Utility
i m not sure why it did not work can show the part of the code with the changes i mentioned
or else some demo where i can see it
0
 
LVL 21

Accepted Solution

by:
nizsmo earned 500 total points
Comment Utility
Try this, and let me know if it works.
<table style="width:339px; style="line-height:20px;">

            <?php

			$counter=0;

            $result = mysql_query("SELECT bizID,bizName,bizAddr,bizCity,bizState,bizZip,bizPhone FROM biz_cars WHERE bizState='$bizState' AND bizCity='$bizCity' AND bizLive='1' GROUP BY bizName LIMIT $offset,50");

            while($r=mysql_fetch_array($result))

            {

                  $bizID = $r['bizID'];

                  $bizName = $r['bizName'];

                  $bizAddr = $r['bizAddr'];

                  $bizZip = $r['bizZip'];

                  $bizPhone = $r['bizPhone'];
 

                  $bizNameLink = replaceForText($bizName);

                  

                  if($counter==0)

                  {

				  	echo "<tr>";

                  }

                  echo '<td><span class="bigger-text">' . $bizNameLink . '

                  <br />

                      <a href="'. $urlPrefix . 'profile/' . $bizID . '.html">'.$bizAddr. '</a><br> '.$bizCity.', '.$bizState.' '.$bizZip.' <br>

                  '.  $bizPhone.'<br>

                  </span></td>';

                  

                  if($counter==1)

                  {

                  	echo "</tr>";

                  	$counter = 0;

                  	continue;

                  }

				  $counter++;

            }

            ?>

</table>

Open in new window

0
 
LVL 7

Expert Comment

by:dttri
Comment Utility
Hi,
I don't think this is a valid SQL statement:

"SELECT bizID,bizName,bizAddr,bizCity,bizState,bizZip,bizPhone FROM biz_cars WHERE bizState='$bizState' AND bizCity='$bizCity' AND bizLive='1' GROUP BY bizName LIMIT $offset,50"

Because when you use "GROUP BY bizName" then all fields in the SELECT (except bizName) must be in aggregate function.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now