Jump to content

[SOLVED] PHP / MYSQL - 2 COL Result Formatting


xchristensen

Recommended Posts

Hey everyone, i have a script in PHP that im looking for assistance with.

 

Here it is!

<?
include("admin/dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM profiles WHERE ap=1";
$result=mysql_query($query);
$num=mysql_numrows($result); 
mysql_close();
?>

<?
$i=0;
while ($i < $num) {
$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$displayname=mysql_result($result,$i,"displayname");
$pictureurl=mysql_result($result,$i,"pictureurl");
?>
<b>Name:</b> <? echo "$displayname"; ?>
<br>
<img src="<? echo "$pictureurl"; ?>">
<bR>
<?
++$i;
} 
?>

 

Now, it works perfectly! But, not to what i want it to do.

 

I would like for this to show the results in 2 columns, not just 1! I know its possible as someone showed me a little bit ago but i forgot the steps to accomplish the task.

 

Any help on this would be greatly appreciated! Thanks again,

Jon

Link to comment
https://forums.phpfreaks.com/topic/67510-solved-php-mysql-2-col-result-formatting/
Share on other sites

Give this a try:

 

<?php
include("admin/dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query="SELECT * FROM profiles WHERE ap=1";
$result=mysql_query($query);
$num=mysql_num_rows($result); 
$num = $num/2;

$i=0;
while ($i < $num) {
$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$displayname=mysql_result($result,$i,"displayname");
$pictureurl=mysql_result($result,$i,"pictureurl");
?>

<table width="100%">
<td>
<b>Name:</b> <? echo "$displayname"; ?>
<br>
<img src="<? echo "$pictureurl"; ?>">
<p>

<?php

if ($i == $num) echo '</td><td>';

$i++;
} 

echo '</td></table>';
?>

$sql = "SELECT * FROM `this` WHERE `that`='grapejuice'";
$res = mysql_query($sql) or die(mysql_error());

while($row = mysql_fetch_assoc($res)){
$first = $row['first'];
$last = $row['last'];
$dname = $row['displayname'];
$picurl = $row['pictureurl'];
echo "Like hello your name is $first $last, your display name is $dname and your picurl is $picurl, get used to it!\n";
}

 

Might I add if you're going to use the $i < $num, make sure the less than sign has the inequality of less than or equal to. Because 1 < 3 will produce results 1 and 2.

EDIT: Okay, try it now.

 

Oops, try this:

 

<?php
include("admin/dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query="SELECT * FROM profiles WHERE ap=1";
$result=mysql_query($query);
$num=mysql_num_rows($result); 
$split_num = $num/2;

$i=0;

echo '<table width="100%"><td>';

while ($i < $num) {
$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$displayname=mysql_result($result,$i,"displayname");
$pictureurl=mysql_result($result,$i,"pictureurl");
?>

<b>Name:</b> <? echo "$displayname"; ?>
<br>
<img src="<? echo "$pictureurl"; ?>">
<p>

<?php

if ($i == $split_num) echo '</td><td>';

$i++;
} 

echo '</td></table>';
?>

EDIT: Oops, nevermind...haha.

 

This should do the trick:

 

<?php
include("admin/dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query="SELECT * FROM profiles WHERE ap=1";
$result=mysql_query($query);
$num=mysql_num_rows($result); 
$split_num = $num/2;

$i=0;

echo '<table width="100%"><td>';

while ($i < $num) {
$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$displayname=mysql_result($result,$i,"displayname");
$pictureurl=mysql_result($result,$i,"pictureurl");


if ($i == $split_num) echo '</td><td>';
?>

<b>Name:</b> <? echo "$displayname"; ?>
<br>
<img src="<? echo "$pictureurl"; ?>">
<p>

<?php

$i++;
} 

echo '</td></table>';
?>

Sasa - That shouldn't be necessary, we aren't working with fractions or decimals.

 

All they needed to do was place this code:

if ($i == $split_num) echo '</td><td>';

 

at the beginning of the while loop, that should fix everything. That is what I did to the code in my last post.

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.