Current location: Hot Scripts Forums » Programming Languages » PHP » Display a multi-dimensional array from mysql


Display a multi-dimensional array from mysql

Reply
  #1 (permalink)  
Old 08-29-10, 12:28 AM
subuntug subuntug is offline
Newbie Coder
 
Join Date: Aug 2010
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Display a multi-dimensional array from mysql

Hello;

I have a Multi-Dimensional Array from mysql table and need to display it as html table.

here is the variable holding my array data:

PHP Code:

  $students_marks mysql_query("SELECT * FROM v_exam_marks);

        
$num_marks = mysql_num_rows($students_marks);

        for(
$i=0; $i<$nbr_note$i++)
        {
            
$student = mysql_result($students_marks$i, 'student');
            
$module = mysql_result($students_marks$i, 'module');
            
$mark = mysql_result($students_marks$i, 'mark');
                
   
$myArray[$student][$i] = array($module$mark);
                        
        } 

When I do
PHP Code:

 echo $student"|"$module"|""$mark"."<br />"
I get:

cbota|B211|16.5
cbota|C305|16.5
cmolina|B211|6
cmolina|C305|3
cmolina|D318|12.22
ifattel|B211|6
ifattelC305|3
ifattel|D318|6.33
jdvaux|B211|8.88
jdvaux|C305|8.88
jdvaux|D318|9
jGary|D318|18
lRanni|D318|8


What I want it's to display my data in HTML table like this:

HTML Code:
<table>
<tr><th>&nbsp;</th><th>B211</th><th>C305</th><th>D318</th></tr>
<tr><th>cbota</th><td>16.5</td><td>16.5</td><td>&nbsp;</td></tr>
<tr><th>cmolina</th><td>6</td><td>3</td>12.22<td>&nbsp;</td></tr>
<tr><th>ifattel</th><td>6</td><td>3</td><td>6.33</td></tr>
<tr><th>jdvaux</th><td>8.88</td>&nbsp;<td>8.88</td><td>9</td></tr>
<tr><th>jGary</th><td>&nbsp;</td><td>&nbsp;</td><td>18</td></tr>
<tr><th>cbota</th><td>&nbsp;</td><td>&nbsp;</td><td>8</td></tr>
</table>

How can I do with php, to get this kind of HTML table from my data base query.

Thanks in advances for your help
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 08-29-10, 04:19 PM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
Try this, and let me know how it goes: Un-Tested.
PHP Code:

$students_marks mysql_query("SELECT * FROM v_exam_marks);

        
$num_marks = mysql_num_rows($students_marks);
if(
$num_marks > 0) {
       while(
$r = mysql_fetch_assoc($student_marks))
        {         
         
$modules[$r['student']][$r['module']] = $r['mark'];
        }

if(is_array(
$modules)) {
echo "
<table>\n<tr>\n<th>&nbsp;</th>\n<th>B211</th>\n<th>C305</th>\n<th>D318</th>\n</tr>\n";
  foreach(
$modules as $k => $v) {
   echo '<tr><td>
$k</td>
   echo (isset(
$modules[$k]['B211'])) ? '<td>' . $modules[$k]['B211'] . '</td>' : '<td>&nbsp;</td>';
  echo (isset(
$modules[$k]['C305'])) ? '<td>' . $modules[$k]['C305'] . '</td>' : '<td>&nbsp;</td>';
   echo (isset(
$modules[$k]['D318'])) ? '<td>' . $modules[$k]['D318'] . '</td>' : '<td>&nbsp;</td>';
  echo '</tr>';
 }
echo '</table>';         
}

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
The Following User Says Thank You to Jcbones For This Useful Post:
subuntug (08-29-10)
  #3 (permalink)  
Old 08-29-10, 08:55 PM
subuntug subuntug is offline
Newbie Coder
 
Join Date: Aug 2010
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Hello;

I tested it, and it works. Thank you very much.
But, still a problem, in the code you propose, the
modules are hard-coded. I need to take the name of the
modules from variables, because I have many other
modules in mysql table and they can change.

So, is there a way to catch the name of each module
directly from mysql table. For instance remove:
PHP Code:

$modules[$k]['B211'
and replace it with something like
PHP Code:

$modules[$k][$module
Thank you in advance for your precious help.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks

Tags
array, multi-dimensional, mysql, php, table


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help reading multi-dimensional array l3rady JavaScript 3 06-11-10 05:10 AM
[SOLVED] PHP Storing multi Dimensional array data in MySQL scott2500uk PHP 3 04-11-08 05:17 AM
Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' Dr. Forensics PHP 3 07-15-06 04:54 PM
linking to iframe not working :( j0d JavaScript 5 01-19-04 09:14 PM


All times are GMT -5. The time now is 04:03 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.