/    Sign up×
Community /Pin to ProfileBookmark

Outputting Results from mysql_fetch_array

How can i display the results of a mysql_fetch_array into multiple parts for displaying? Currently if i use the fetch_array it displays the information all at that once. What i want it to do is display the information nicely in an html table that has 8 rows and 8 columns

Edit: Basically what i need to do is limit the fetch array display for example before it does this:
Type: ValueType: ValueType: ValueType: ValueType: Value
when i add a br tag to the end of the echo i get this
Type: Value
Type: Value
Type: Value

What i want done is something like this:
Type: Value Type: Value Type: Value
Type: Value Type: Value Type: Value

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@Phill_PaffordAug 01.2008 — I use something like this.

Input for this would be like:
[code=php]
$columns = "fname, lname";

// OR

$columns = "t1.fname AS fname, t2.lname AS lname";
[/code]


This function builds the table header if you need it
[code=php]
/************************************************
* Function Name: buildTableHeader
* parameters: $columns
* returns: $tableHeader
* **********************************************
* This function is used to build the table
* header information
* **********************************************
* Updated By:
* Date:
* Changes:
* **********************************************
* Notes: $column = the fields needed in the
* query, SELECT * does not work with this.
* Must be in CSV format like:
* SELECT fname, lname FROM etc...
* Can also use this
* SELECT t1.fname AS fname, t2.lname AS lname
* FROM etc...
************************************************/

function buildTableHeader($columns)
{
$colString = $columns;
$colDelimiter = '.';
$pos = strpos($colString, $colDelimiter);

if ($pos === false)
{
$columnField = explode(",", $columns);

for($ii = 0; $ii < count($columnField); $ii++)
{
// String to upper case
$tmp = strtoupper($columnField[$ii]);

$tableHeader .= "<th>$tmp</th>";
}
}
else
{
$splitColumn = explode(",", $columns);

for($i = 0; $i < count($splitColumn); $i++)
{
$columnField = explode(".", $splitColumn[$i]);

for($ii = 0; $ii < count($columnField); $ii++)
{
$colString2 = $columnField[$ii];
$colDelimiter2 = 'AS';
$pos2 = strpos($colString2, $colDelimiter2);

if ( $pos2 == true )
{
$splitAsColumn = explode(" ", $columnField[$ii]);

for($iii = 0; $iii < count($splitAsColumn); $iii++)
{
if ( $iii == 2 )
{
$tmp = strtoupper($splitAsColumn[$iii]);
$tableHeader .= "<th>$tmp</th>";
}
}
}
else
{
if ( $ii == 1 )
{
// String to upper case
$tmp = strtoupper($columnField[$ii]);

$tableHeader .= "<th>$tmp</th>";
}
}
}
}
}
return $tableHeader;
}
[/code]


parameters for this function are:
[code=php]
$rs; // Query results
$tableHeader; // from function above
$elseMsg = "NO Records Found"; // Display something if no records are returned
[/code]


This function uses the table header as a parm
[code=php]
/************************************************
* Function Name: buildOutputFromQuery
* Author: Phill Pafford
* parameters: $rs, $tableHeader, $elseMsg
* returns: $bOutput
* **********************************************
* Functionality
* **********************************************
* This function is used to build the table
* information
* **********************************************
* Updates
* **********************************************
* Updated By:
* Date:
* Changes:
* **********************************************
* Notes
* **********************************************
*
************************************************/

function buildOutputFromQuery($rs, $tableHeader, $elseMsg) {
global $anyRecords;

// Build the table to house the query results
$bOutput = '<table cellspacing="0" cellpadding="0" width="80%">';
$bOutput .= '<tr>';

// Add the table header info
$bOutput .= $tableHeader;
$bOutput .= '</tr>';

if(count($rs) >= 1)
{
$anyRecords = count($rs);

foreach ($rs as $row)
{
$bOutput .= '<tr>';

foreach($row as $column)
{
$bOutput .= '<td>' . $column . '</td>';
}

$bOutput .= '</tr>';
}
}
else
{
$bOutput .= '<span class="titleRed">' . $elseMsg . '</span><br/>';
$anyRecords = 0;
}

$bOutput .= '</table>';

// Return the query results in HTML format
return $bOutput;
}
[/code]


to see the results
[code=php]
echo $bOutput;
[/code]


any tips to improve this are welcome.
Copy linkTweet thisAlerts:
@sc908authorAug 01.2008 — Wow soo many codes for what seems a simple thing :eek:

theres like no simple/easy way to spilt the output of the fetch array into like different parts this way i can do something like echo part1 and echo part2
Copy linkTweet thisAlerts:
@LuboxAug 01.2008 — I think you need a counter inside the loop where you echo the results using table cells. Each time it reaches 3, you echo a </tr> and reset the counter.
Copy linkTweet thisAlerts:
@Phill_PaffordAug 04.2008 — code is write once use many times, aka a function :-)
×

Success!

Help @sc908 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 12.9,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @bahaedd,
tipped: article
amount: 1000 SATS,

tipper: @Balmasexy,
tipped: article
amount: 1000 SATS,

tipper: @mbsaad,
tipped: article
amount: 1000 SATS,
)...