Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Get results from table then echo them in "2 columns"
Old 12-19-2007, 11:33 AM Get results from table then echo them in "2 columns"
feraira's Avatar
BeTheBand!

Posts: 350
Trades: 0
Hey guys, I have a little page set up (http://www.betheband.org/quickcontacts.php) and I want the results to be shown in 2 different columns, how can I go about this?
feraira is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-19-2007, 12:41 PM Re: Get results from table then echo them in "2 columns"
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
You could probably just get the total number of rows your going to show, divide in half and use that number as an index. Then output the first row in a table cell, and the one in the middle in the next table cell. For the next table row, you output the seccond row in a table cell then the middle plus one in the other table cell.

Something like this
PHP Code:
$numRows 20// Example, normaly you would use a database query to find this out
$middleIndex $numRows 2// Which is 10

echo "<table>"
for($index=0$index $middleIndex$index++) {
    echo 
"<tr>";
    echo 
"<td>".$someResult[$index]."</td>";
    echo 
"<td>".$someResult[$middleIndex]."</td>";
    echo 
"</tr>";
    
$middleIndex++;
}
echo 
"</table>";

/*
Which give a table like this
0   10
1   11
2   12
3   13
4   14
5   15
6   16
7   17
8   18
9   19

If there is an unevan number of rows, lets say 19 instead of 20,
table cell number 19 above would be empty.

*/ 
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 12-19-2007, 12:48 PM Re: Get results from table then echo them in "2 columns"
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Or actually, you should calculate $middleIndex as
PHP Code:
$middleIndex = (int)(($numRows+1) / 2); 
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 12-19-2007, 01:37 PM Re: Get results from table then echo them in "2 columns"
feraira's Avatar
BeTheBand!

Posts: 350
Trades: 0
Thanks for the great response.. What if I was to display the data in a div of CSS instead?

Code:
    <div id="left">
    
<?php

	$query = safe_query("SELECT * FROM `table` ORDER BY `manager_name` ASC ");
		while($p=mysql_fetch_array($query)) {
			$user = $p['manager_name'];

?>

    <p><a href="javascript:;" onClick="opener.location.href='sendmoney.php?who=<?php echo "$user"; ?>';window.close();" target="_parent"><?php echo "$user"; ?></a></p>

<?php
	
	}

?>

  </div>
    
  <div id="right">
<?php

	$query = safe_query("SELECT * FROM `table` ORDER BY `manager_name` ASC ");
		while($p=mysql_fetch_array($query)) {
			$user = $p['manager_name'];

?>

    <p><a href="javascript:;" onClick="opener.location.href='sendmoney.php?who=<?php echo "$user"; ?>';window.close();" target="_parent"><?php echo "$user"; ?></a></p>

<?php
	
	}

?>
</div>
Is what I have at the moment, and needless to say, it just shows 2 columns of the same names.
feraira is offline
Reply With Quote
View Public Profile
 
Old 12-19-2007, 02:24 PM Re: Get results from table then echo them in "2 columns"
Skilled Talker

Posts: 62
Name: Daniel
Trades: 0
I dont know if what you're doing would be faster or not than my idea but its a shot. Instead of hitting the db twice for the information you could do this. And instead of using the * which is a wildcard character, you seem to only be wanting the manager_name so get only that.

PHP Code:
<?php
 $query 
safe_query("SELECT 'manager_name' FROM `table` ORDER BY `manager_name` ASC ");
 
$user = array();
 while(
$p=mysql_fetch_array($query)) {
  
array_push($user$p['manager_name']);
 }
 
$display "";
 for (
$user as $value) {
  
$display .= "\t\t<p><a href=\"javascript:;\" onClick=\"opener.location.href=\'sendmoney.php?who="$value ."\"';window.close();\" target=\"_parent\">"$value ."</a></p>\n";
 }
?>
 <div id="left">
<?php echo $display?>
 </div>
 
 <div id="right">
<?php echo $display?>
 </div>

Last edited by castis; 12-19-2007 at 02:25 PM.. Reason: newline character added
castis is offline
Reply With Quote
View Public Profile
 
Old 12-19-2007, 04:38 PM Re: Get results from table then echo them in "2 columns"
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
castis example will speed things up, and combined with my first example you could get it into two columns. I'll make another example, writing code the way I'm used to but feel free to change it the way you like it.

PHP Code:
$query "SELECT manager_name FROM table ORDER BY manager_name";
$result mysql_query($query);
$numRows mysql_num_rows($result);
$middleIndex = (int)(($numRows+1) / 2);

$names = array();
while(
$row mysql_fetch_assoc($result)) {
    
$names[] = $row['manager_name'];
}

// Now lets print the left column

echo "<div id=\"left\">\n";
for(
$i 0$i $middleIndex$i++) {
    echo 
"<p><a href=\"#\" "
       
" onClick=\"opener.location.href='sendmoney.php?who="
       
$names[$i] . "';window.close();\"></a></p>\n";
}
echo 
"</div>\n";

// And the right one

echo "<div id=\"right\">\n";
for(
$i middleIndex$i $numRows$i++) {
    echo 
"<p><a href=\"#\" "
       
" onClick=\"opener.location.href='sendmoney.php?who="
       
$names[$i] . "';window.close();\"></a></p>\n";
}
echo 
"</div>\n"

Last edited by lizciz; 12-19-2007 at 04:40 PM.. Reason: Made some minor mistakes, corrected them now.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 12-20-2007, 10:46 AM Re: Get results from table then echo them in "2 columns"
Skilled Talker

Posts: 62
Name: Daniel
Trades: 0
I realized last night that i put in some serously unnecessary crap in that code up there. Here's a revised version.

PHP Code:
<?php
 $query 
safe_query("SELECT 'manager_name' FROM `table` ORDER BY `manager_name` ASC ");
 
$display '';
 while(
$p=mysql_fetch_array($query)) {
  
$display .= "\t\t<p><a href=\"javascript:;\" onClick=\"opener.location.href=\'sendmoney.php?who="$p['manager_name'] ."\"';window.close();\" target=\"_parent\">"$p['manager_name'] ."</a></p>\n";
 }
?>
 <div id="left">
<?php echo $display?>
 </div>
 
 <div id="right">
<?php echo $display?>
 </div>

Last edited by castis; 12-20-2007 at 12:08 PM.. Reason: defined $display
castis is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Get results from table then echo them in "2 columns"
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.57169 seconds with 12 queries