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
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>';
?>

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

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>';
?>

Link to comment
Share on other sites

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>';
?>

Link to comment
Share on other sites

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.

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.