gokhul Posted April 30, 2009 Share Posted April 30, 2009 Hi Please check my code once. mport_request_variables('p','p_'); $connect=mysql_connect('localhost','gkkilaru','gkkilaru') or die('could not open:'. mysql_error()); mysql_select_db('gkkilaru_db'); $result=mysql_query("SELECT * FROM human WHERE $p_Searchtype='$p_Search'"); if (mysql_num_rows($result)>0) { echo "<table cellpadding='1' cellspacing='1' bordercolor=pink width='800' border='1'>"; echo "<tr align='center'>"; echo "<td><b>KEGG_ID</b></td>"; echo "<td><b>Gene_Name</b></td>"; echo "<td><b>Uniprot_ID</b></td>"; echo "</tr>"; while($row = mysql_fetch_object($result)) { echo "<tr align='center'><td>"; echo $row->KEGG_ID; echo "</td><td>"; echo $row->Gene_Name; echo "</td><td>"; echo $row->Uniprot_ID; echo "</td><td>"; } } else { echo "<tr align='center'><td>"; echo "<p><font size='4' color='red'>"; echo "No Records Found!</p>"; echo "</td></tr>"; } //mysql_close($connection); } break; } I am getting the results horizontally. Can someone help me with how to view my results vertically? In the code above, I gave only 3 variables but in real time I need to work on large sets of data. Please help me. Link to comment https://forums.phpfreaks.com/topic/156331-results-as-columns/ Share on other sites More sharing options...
harristweed Posted April 30, 2009 Share Posted April 30, 2009 while($row = mysql_fetch_object($result)) { echo "<tr align='center'><td>"; echo $row->KEGG_ID; echo "</td><td>"; echo $row->Gene_Name; echo "</td><td>"; echo $row->Uniprot_ID; echo "</td><td>"; } Try: while($row = mysql_fetch_object($result)) { echo "<tr align='center'><td>"; echo $row->KEGG_ID; echo "</td><td>"; echo $row->Gene_Name; echo "</td><td>"; echo $row->Uniprot_ID; echo "</td></tr>"; } Link to comment https://forums.phpfreaks.com/topic/156331-results-as-columns/#findComment-823113 Share on other sites More sharing options...
gokhul Posted May 1, 2009 Author Share Posted May 1, 2009 I didnt find any difference between my code and yours. Link to comment https://forums.phpfreaks.com/topic/156331-results-as-columns/#findComment-823571 Share on other sites More sharing options...
Ken2k7 Posted May 1, 2009 Share Posted May 1, 2009 I didnt find any difference between my code and yours. Really? Buy a new pair of glasses or contacts maybe? Link to comment https://forums.phpfreaks.com/topic/156331-results-as-columns/#findComment-823580 Share on other sites More sharing options...
revraz Posted May 1, 2009 Share Posted May 1, 2009 He is closing your TR where you didn't. HTML issue. Link to comment https://forums.phpfreaks.com/topic/156331-results-as-columns/#findComment-823584 Share on other sites More sharing options...
gokhul Posted May 1, 2009 Author Share Posted May 1, 2009 its showing the same result...in the horizontal form itself. Link to comment https://forums.phpfreaks.com/topic/156331-results-as-columns/#findComment-823730 Share on other sites More sharing options...
gokhul Posted May 2, 2009 Author Share Posted May 2, 2009 please anyone....solve this Link to comment https://forums.phpfreaks.com/topic/156331-results-as-columns/#findComment-823988 Share on other sites More sharing options...
Ken2k7 Posted May 2, 2009 Share Posted May 2, 2009 if (mysql_num_rows($result)>0) { echo "<table cellpadding='1' cellspacing='1' bordercolor=pink width='800' border='1'>"; echo "<tr align='center'>"; echo "<td><b>KEGG_ID</b></td>"; echo "<td><b>Gene_Name</b></td>"; echo "<td><b>Uniprot_ID</b></td>"; echo "</tr>"; $first_col = $second_col = $third_col = '<tr align="center">'; while($row = mysql_fetch_object($result)) { $first_col .= '<td>' . $row->KEGG_ID . '</td>'; $second_col .= '<td>' . $row->Gene_Name . '</td>'; $third_col .= '<td>' . $row->Uniprot_ID . '</td>'; } $first_col .= '</tr>'; $second_col .= '</tr>'; $third_col .= '</tr>'; echo $first_col . $second_col . $third_col; } Does that work for you? Link to comment https://forums.phpfreaks.com/topic/156331-results-as-columns/#findComment-823995 Share on other sites More sharing options...
gokhul Posted May 2, 2009 Author Share Posted May 2, 2009 the results are coming in columns but the titles are in row forms itself. the result is like this keggid genename uniprotid xxx yyy zzz but i want the result as keggid xxx genename yyy uniprotid zzz thanks to everyone...who was helping me. Link to comment https://forums.phpfreaks.com/topic/156331-results-as-columns/#findComment-824178 Share on other sites More sharing options...
Mikedean Posted May 2, 2009 Share Posted May 2, 2009 I turned it in to a function so you're able to use it somewhere else a lot easier. function returnResultsInColumns( $mysqlResult ) { $queryResults = NULL; if( mysql_num_rows($mysqlResult) > 0 ) { while($resultRow = mysql_fetch_array($mysqlResult)) { for ($intPos = 0; $intPos < mysql_num_fields($mysqlResult); $intPos++) { $fieldName = mysql_field_name($mysqlResult, $intPos); $currentArrayPos = count( $queryResults[$fieldName] ); $queryResults[$fieldName][$currentArrayPos] = $resultRow[$fieldName]; } } $htmlTable = "<table>"; foreach( $queryResults as $field => $results ) { $htmlTable.= "<tr>"; $htmlTable.= "<td>" . $field . "</td><td>" . implode( ", ", $results ) . "</td>"; $htmlTable.= "</tr>"; } $htmlTable.= "</table>"; return $htmlTable; } else { return "The query returned no results."; } } // Pass the result of the MySQL query into the function. $tableLayout = returnResultsInColumns( $result ); echo $tableLayout; It's untested but should work . Link to comment https://forums.phpfreaks.com/topic/156331-results-as-columns/#findComment-824184 Share on other sites More sharing options...
gokhul Posted May 2, 2009 Author Share Posted May 2, 2009 hey thank you... i got my desired results.... but it is giving an error like undefined index: genename and undefined index:unprot_id near the line $currentArrayPos = count( $queryResults[$fieldName] ); can u check this out. Link to comment https://forums.phpfreaks.com/topic/156331-results-as-columns/#findComment-824190 Share on other sites More sharing options...
Mikedean Posted May 2, 2009 Share Posted May 2, 2009 Cool, change it to: $currentArrayPos = ( isset( $queryResults[$fieldName] ) ? count( $queryResults[$fieldName] ) : 0 ); Link to comment https://forums.phpfreaks.com/topic/156331-results-as-columns/#findComment-824194 Share on other sites More sharing options...
gokhul Posted May 2, 2009 Author Share Posted May 2, 2009 mike.....thanks a lot thankkkkkkkkkkkksssssssssssssssss its working mike. one more doubt is like can we put the results in a table? please help regarding this one issue. plzzzzzzzzzzzzzzzzzzzzzzz mike. Link to comment https://forums.phpfreaks.com/topic/156331-results-as-columns/#findComment-824200 Share on other sites More sharing options...
gokhul Posted May 3, 2009 Author Share Posted May 3, 2009 hello, i am getting the results in two columns after using the following code... import_request_variables('p','p_'); $connect=mysql_connect('localhost','gkkilaru','gkkilaru') or die('could not open:'. mysql_error()); mysql_select_db('gkkilaru_db'); $result=mysql_query("SELECT * FROM human WHERE $p_Searchtype='$p_Search'"); function returnResultsInColumns( $mysqlResult ) { $queryResults = NULL; if( mysql_num_rows($mysqlResult) > 0 ) { while($resultRow = mysql_fetch_array($mysqlResult)) { for ($intPos = 0; $intPos < mysql_num_fields($mysqlResult); $intPos++) { $fieldName = mysql_field_name($mysqlResult, $intPos); $currentArrayPos = ( isset( $queryResults[$fieldName] ) ? count( $queryResults[$fieldName] ) : 0); $queryResults[$fieldName][$currentArrayPos] = $resultRow[$fieldName]; } } $htmlTable = "<table>"; foreach( $queryResults as $field => $results ) { $htmlTable.= "<tr>"; $htmlTable.= "<td>" . $field . "</td><td>" . implode( ", ", $results ) . "</td>"; $htmlTable.= "</tr>"; } $htmlTable.= "</table>"; return $htmlTable; } else { return "The query returned no results."; } } // Pass the result of the MySQL query into the function. $tableLayout = returnResultsInColumns($result); echo $tableLayout; can anyone let me know the code for how to place a border or table for the result obtained? Link to comment https://forums.phpfreaks.com/topic/156331-results-as-columns/#findComment-825069 Share on other sites More sharing options...
Ken2k7 Posted May 3, 2009 Share Posted May 3, 2009 Is there a reason you need that for loop inside that while loop? Mikedean did that because he doesn't know your table columns. You really shouldn't need it. It's a waste. If you want a border, just add the border attribute to the table tag. One more thing, read rule #13 - http://www.phpfreaks.com/page/forum-rules Link to comment https://forums.phpfreaks.com/topic/156331-results-as-columns/#findComment-825092 Share on other sites More sharing options...
gokhul Posted May 3, 2009 Author Share Posted May 3, 2009 do you mean to $htmltable ="<table>" ? if so...i did...but the table is at one place and the results are at one place..in the page. can you tell me where to place exactly? Link to comment https://forums.phpfreaks.com/topic/156331-results-as-columns/#findComment-825237 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.