xchristensen Posted August 31, 2007 Share Posted August 31, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/67510-solved-php-mysql-2-col-result-formatting/ Share on other sites More sharing options...
pocobueno1388 Posted August 31, 2007 Share Posted August 31, 2007 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>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/67510-solved-php-mysql-2-col-result-formatting/#findComment-338970 Share on other sites More sharing options...
marcus Posted August 31, 2007 Share Posted August 31, 2007 $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. Quote Link to comment https://forums.phpfreaks.com/topic/67510-solved-php-mysql-2-col-result-formatting/#findComment-338971 Share on other sites More sharing options...
xchristensen Posted August 31, 2007 Author Share Posted August 31, 2007 pocobueno1388, it seemed cut my first column in half and not show a second column. mgallforever, how will that script post to 2 columns? I guess im not to sure? Quote Link to comment https://forums.phpfreaks.com/topic/67510-solved-php-mysql-2-col-result-formatting/#findComment-338972 Share on other sites More sharing options...
marcus Posted August 31, 2007 Share Posted August 31, 2007 I just provided an easier example. Quote Link to comment https://forums.phpfreaks.com/topic/67510-solved-php-mysql-2-col-result-formatting/#findComment-338974 Share on other sites More sharing options...
pocobueno1388 Posted August 31, 2007 Share Posted August 31, 2007 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>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/67510-solved-php-mysql-2-col-result-formatting/#findComment-338975 Share on other sites More sharing options...
xchristensen Posted August 31, 2007 Author Share Posted August 31, 2007 poco, the two columns are there! Now its strange, i have 8 rows in my database and i would think it would split them up? 2 columns of 4? But as of the moment, it has 5 in column 1, and 3 in column 2. Quote Link to comment https://forums.phpfreaks.com/topic/67510-solved-php-mysql-2-col-result-formatting/#findComment-338978 Share on other sites More sharing options...
sasa Posted August 31, 2007 Share Posted August 31, 2007 change <?php if ($i == $split_num) echo '</td><td>'; $i++; } to <?php if (++$i == $split_num) echo '</td><td>'; } Quote Link to comment https://forums.phpfreaks.com/topic/67510-solved-php-mysql-2-col-result-formatting/#findComment-338979 Share on other sites More sharing options...
xchristensen Posted August 31, 2007 Author Share Posted August 31, 2007 thanks so much poco, and sasa! Coding problem solved! Quote Link to comment https://forums.phpfreaks.com/topic/67510-solved-php-mysql-2-col-result-formatting/#findComment-338982 Share on other sites More sharing options...
pocobueno1388 Posted August 31, 2007 Share Posted August 31, 2007 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>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/67510-solved-php-mysql-2-col-result-formatting/#findComment-338983 Share on other sites More sharing options...
sasa Posted August 31, 2007 Share Posted August 31, 2007 ups change $split_num = $num/2; to $split_num = floor($num/2); Quote Link to comment https://forums.phpfreaks.com/topic/67510-solved-php-mysql-2-col-result-formatting/#findComment-338989 Share on other sites More sharing options...
pocobueno1388 Posted August 31, 2007 Share Posted August 31, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/67510-solved-php-mysql-2-col-result-formatting/#findComment-338992 Share on other sites More sharing options...
sasa Posted September 1, 2007 Share Posted September 1, 2007 if the $num is 9 then $split_num is 4.5 but $i is 1 or 2 or 3 or 4 or 5 and so on $i == $split_num is newer true Quote Link to comment https://forums.phpfreaks.com/topic/67510-solved-php-mysql-2-col-result-formatting/#findComment-339280 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.