PHP-using <table>

using <table> in php code

Page 1 of 1

12 Replies - 16572 Views - Last Post: 25 October 2010 - 03:48 AM Rate Topic: ***** 1 Votes

#1 noprobz09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-September 09

PHP-using <table>

Posted 12 October 2010 - 07:30 PM

I am a beginner in PHP, I am currently developing an Online Grading System.
I just to have ask any suggestions how to align the html table using PHP

just like this:

Lname|Fname| Chapter1 and so on depending on the record on the database
aaa bbb 34
... ... ...

here's my code:
<table width="100">
<tbody>
<?php
 $trapTable=mysql_query("SELECT * FROM trap WHERE subCode='$code' AND instructor='$user'");
 while($trapResult=mysql_fetch_assoc($trapTable))
 {  ?>

<th>
 <?php
    //echo'<tr><th>';
    echo$trapResult['trapRecordName'];
    //echo'</th></tr>';
?>
</th>
<?php
    $recName=$trapResult['trapRecordName'];
    $gradeSql=mysql_query("SELECT *FROM gradebook WHERE recordName='$recName' AND instructor='$user' AND subCode='$code'");
    while($gradeResult=mysql_fetch_assoc($gradeSql))
     {
      echo'<tr>';
      echo'<td>';
      echo$gradeResult['studScore'];
      echo'</td>';
      echo'</tr>';
     }

 }
?>

</tbody>
</table>



My problem for that code is the table aligning...Anyone can suggest me...
YOUR SUGGESTION WILL MUCH APPRECIATED...GOD BLESS!!!

Is This A Good Question/Topic? 0
  • +

Replies To: PHP-using <table>

#2 no2pencil  Icon User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6549
  • View blog
  • Posts: 30,679
  • Joined: 10-May 07

Re: PHP-using <table>

Posted 12 October 2010 - 07:42 PM

Not being able to see the values returned makes this a little more difficult, but I would use the following :

<?php
 if(!empty($code) && !empty($user)) {
   $sql="SELECT * FROM trap WHERE subCode='$code' AND instructor='$user'"
   $result=mysql_query($sql);
 }
 if(!$result) die("There was an error in your sql statement<br>".$sql);
 echo "<table width=\"100\">";
 while($trapResult=mysql_fetch_assoc($result)) {
 echo "<th>".$recName."</th>";
 $gradeSql="SELECT * FROM gradebook WHERE recordName='$recName' AND instructor='$user' AND subCode='$code'");
 $result=mysql_query($gradeSql);
 if(!$result) die("There was an error in your sql statement<br>".$sql);
 while($gradeResult=mysql_fetch_assoc($result)) {
   echo "<tr><td>".$gradeResult['studScore']."</td></tr>";
 }
 echo "</table>";
?>


Was This Post Helpful? 0
  • +
  • -

#3 noprobz09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-September 09

Re: PHP-using <table>

Posted 13 October 2010 - 08:31 PM

Thank you for your quick suggestion, I test your codes but it gives me like this:
------
chapter1
12
3
21
-----
Actually, I have 2 records saved into trap table, chapter1 and chapter2.
In my codes, it gives me correct output just like this:
----------
chapter1
12
3
21
chapter2
10
9
2
--------
What I want, is to display like this:
---------------------
chapter1 chapter2
12
10

3
9

21
2

----------------------
In fact, Im very thankful for your suggestion...KEEP IT UP and GOD BLESS

This post has been edited by noprobz09: 13 October 2010 - 08:36 PM

Was This Post Helpful? 0
  • +
  • -

#4 D-2010  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 9
  • Joined: 28-January 10

Re: PHP-using <table>

Posted 14 October 2010 - 12:24 AM

View Postnoprobz09, on 13 October 2010 - 07:31 PM, said:

Thank you for your quick suggestion, I test your codes but it gives me like this:
------
chapter1
12
3
21
-----
Actually, I have 2 records saved into trap table, chapter1 and chapter2.
In my codes, it gives me correct output just like this:
----------
chapter1
12
3
21
chapter2
10
9
2
--------
What I want, is to display like this:
---------------------
chapter1 chapter2
12
10

3
9

21
2

----------------------
In fact, Im very thankful for your suggestion...KEEP IT UP and GOD BLESS


Hi, what u need to do is create an array of student records no by chapters.

<?php 
$trapTable=mysql_query("SELECT * FROM trap WHERE subCode='$code' AND instructor='$user'");
while($trapResult=mysql_fetch_assoc($trapTable)) {   
   $recName = $trapResult['trapRecordName'];
   $chapters[] = $recName;   
   $gradeSql=mysql_query("SELECT * FROM gradebook WHERE recordName='$recName' AND instructor='$user' AND subCode='$code'");
   
   while($gradeResult=mysql_fetch_assoc($gradeSql)) {      
      $id = $gradeResult['studID']; /* you need the right col name for this */
      /* this creates an array with $std[12345]['chaper1'] = score */
      $std[$id][$recName] = $gradeResult['studScore'];
      $i++;
   }
}
?>

<table width="100">
<tbody>

<tr>
<?php
// this creates the header
foreach ($chapters as $recordName){  
?>
<th>
<?php   
   echo $recordName;
?>
</th>
<?php } ?>
</tr>

<tr>
<?php
// then for each student we print out their scores for each chapter
foreach($std as $studentRecord){    
   foreach ($chapters as $recordName){      
      echo'<td>';
      echo $studentRecord[$recordName];
      echo'</td>';      
   }
}
?>
</tr>
</tbody>
</table>



I havnt tested this, but I think it will work ...
Was This Post Helpful? 0
  • +
  • -

#5 noprobz09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-September 09

Re: PHP-using <table>

Posted 15 October 2010 - 05:03 AM

Thank you D-2010 for your brilliant suggestion:

<?php 
$trapTable=mysql_query("SELECT * FROM trap WHERE subCode='$code' AND instructor='$user'");
while($trapResult=mysql_fetch_assoc($trapTable)) {   
   $recName = $trapResult['trapRecordName'];
   $chapters[] = $recName;   
   $gradeSql=mysql_query("SELECT * FROM gradebook WHERE recordName='$recName' AND instructor='$user' AND subCode='$code'");
   
   while($gradeResult=mysql_fetch_assoc($gradeSql)) {      
      $id = $gradeResult['studID']; /* you need the right col name for this */
      /* this creates an array with $std[12345]['chaper1'] = score */
      $std[$id][$recName] = $gradeResult['studScore'];
      $i++;
   }
}
?>

<table width="100">
<tbody>

<tr>
<?php
// this creates the header
foreach ($chapters as $recordName){  
?>
<th>
<?php   
   echo $recordName;
?>
</th>
<?php } ?>
</tr>

<tr>
<?php
// then for each student we print out their scores for each chapter
foreach($std as $studentRecord){    
   foreach ($chapters as $recordName){      
      echo'<td>';
      echo $studentRecord[$recordName];
      echo'</td>';      
   }
}
?>
</tr>
</tbody>
</table>


In your codes above, The first record under chapter1 and chapter2...gives an exact table heading<th> aligment, but the second up to last record, it does not.,I think its just a little bit logical error with your codes,..I try to fix this as soon as possible...despites that, THANK YOU for sharing your brilliant idea... GOD BLESS!!!

This post has been edited by noprobz09: 15 October 2010 - 05:13 AM

Was This Post Helpful? 0
  • +
  • -

#6 D-2010  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 9
  • Joined: 28-January 10

Re: PHP-using <table>

Posted 15 October 2010 - 08:40 AM

happy to help, and yea, the second <tr> should be inside the first foreach loop ... silly me :P

<?php
foreach($std as $studentRecord){   
<tr>
   foreach ($chapters as $recordName){     
      echo'<td>';
      echo $studentRecord[$recordName];
      echo'</td>';     
   }
</tr>
}
?>


This post has been edited by D-2010: 15 October 2010 - 08:41 AM

Was This Post Helpful? 0
  • +
  • -

#7 noprobz09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-September 09

Re: PHP-using <table>

Posted 16 October 2010 - 04:55 AM

Good Day! thanks for your brilliant code!...I already solved my problem...thanks for the great help!
:smartass:
Was This Post Helpful? 0
  • +
  • -

#8 noprobz09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-September 09

Re: PHP-using <table>

Posted 19 October 2010 - 06:35 PM

hmmm... I used your code in my project, it works...but How could I display the name of the students with their corresponding records...the name of the students is in the STUDENTFILE...please suggest me...IM SORRY FOR THE DISTURBANCE...I need help...
Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3714
  • Posts: 13,468
  • Joined: 08-August 08

Re: PHP-using <table>

Posted 20 October 2010 - 04:54 AM

View PostD-2010, on 15 October 2010 - 10:40 AM, said:

happy to help, and yea, the second <tr> should be inside the first foreach loop ... silly me :P

<?php
foreach($std as $studentRecord){   
<tr>
   foreach ($chapters as $recordName){     
      echo'<td>';
      echo $studentRecord[$recordName];
      echo'</td>';     
   }
</tr>
}
?>


This will not work, and it's a result of breaking in/out of php so often that you forgot what language you were in! As a rule of thumb, if you break out of php more than twice in a hundred lines, you're asking for trouble.
<?php
foreach($std as $studentRecord){   
echo '<tr>';
   foreach ($chapters as $recordName){     
      echo'<td>'.$studentRecord[$recordName].'</td>';     
   }
echo '</tr>';
}
?>



View Postnoprobz09, on 19 October 2010 - 08:35 PM, said:

...but How could I display the name of the students with their corresponding records...the name of the students is in the STUDENTFILE

I'd store it in the $std array for later output:
while($gradeResult=mysql_fetch_assoc($gradeSql)) {     
  $std[$gradeResult['studID']]['studScore'] = $gradeResult['studScore'];
  $std[$gradeResult['studID']]['studName'] = $gradeResult['studName'];
}


foreach($std as $studentRecord){   
echo '<tr>';
   foreach ($chapters as $recordName){     
      echo'<td>'.$studentRecord[$recordName].'</td>';     
   }
echo '</tr>';
}
?>



This post has been edited by CTphpnwb: 20 October 2010 - 05:07 AM

Was This Post Helpful? 0
  • +
  • -

#10 noprobz09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-September 09

Re: PHP-using <table>

Posted 21 October 2010 - 01:20 AM

thanks, I will try your code in my project...
Was This Post Helpful? 0
  • +
  • -

#11 noprobz09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-September 09

Re: PHP-using <table>

Posted 23 October 2010 - 06:04 PM

I had used this code:
<?php 
$trapTable=mysql_query("SELECT * FROM trap WHERE subCode='$code' AND instructor='$user'");
while($trapResult=mysql_fetch_assoc($trapTable)) {   
   $recName = $trapResult['trapRecordName'];
   $chapters[] = $recName;   
   $gradeSql=mysql_query("SELECT * FROM gradebook WHERE recordName='$recName' AND instructor='$user' AND subCode='$code'");
   
   while($gradeResult=mysql_fetch_assoc($gradeSql)) {      
      $id = $gradeResult['studID']; /* you need the right col name for this */
      /* this creates an array with $std[12345]['chaper1'] = score */
      $std[$id][$recName] = $gradeResult['studScore'];
      $i++;
   }
}
?>

<table width="100">
<tbody>

<tr>
<?php
// this creates the header
foreach ($chapters as $recordName){  
?>
<th>
<?php   
   echo $recordName;
?>
</th>
<?php } ?>
</tr>

<tr>
<?php
// then for each student we print out their scores for each chapter
foreach($std as $studentRecord){    
   foreach ($chapters as $recordName){      
      echo'<td>';
      echo $studentRecord[$recordName];
      echo'</td>';      
   }
}
?>
</tr>
</tbody>
</table>




this code is give me a right output but how could I display the name of the student containing on that record?...the name of the student file is in STUDENTFILE...hooohh! I need your brilliant ideas...tnanks in advance...
Was This Post Helpful? 0
  • +
  • -

#12 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3714
  • Posts: 13,468
  • Joined: 08-August 08

Re: PHP-using <table>

Posted 23 October 2010 - 07:28 PM

In 46 lines of code, you break out of php five times! That's almost once every nine lines. It hurts my eyes to look at your code, and I've no doubt that all that breaking in/out of php is causing extra confusion for you.

You need to simplify your approach to writing code, and you can start by not breaking out of php just to output a tag and then back in to echo a variable!
Here's lines 21 through 46 of your code simplified:
<?php
// this creates the header
foreach ($chapters as $recordName){  
   echo '<th>'.$recordName.'</th>';
}
echo '</tr><tr>';
// then for each student we print out their scores for each chapter
foreach($std as $studentRecord){    
   foreach ($chapters as $recordName){      
      echo'<td>'.$studentRecord[$recordName].'</td>';      
   }
}
?>
</tr>
</tbody>
</table>


Do that with the rest and you might even solve your own problem!

Now, what is STUDENTFILE?
Was This Post Helpful? 0
  • +
  • -

#13 noprobz09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-September 09

Re: PHP-using <table>

Posted 25 October 2010 - 03:48 AM

View PostCTphpnwb, on 23 October 2010 - 06:28 PM, said:

In 46 lines of code, you break out of php five times! That's almost once every nine lines. It hurts my eyes to look at your code, and I've no doubt that all that breaking in/out of php is causing extra confusion for you.

You need to simplify your approach to writing code, and you can start by not breaking out of php just to output a tag and then back in to echo a variable!
Here's lines 21 through 46 of your code simplified:
<?php
// this creates the header
foreach ($chapters as $recordName){  
   echo '<th>'.$recordName.'</th>';
}
echo '</tr><tr>';
// then for each student we print out their scores for each chapter
foreach($std as $studentRecord){    
   foreach ($chapters as $recordName){      
      echo'<td>'.$studentRecord[$recordName].'</td>';      
   }
}
?>
</tr>
</tbody>
</table>


Do that with the rest and you might even solve your own problem!

Now, what is STUDENTFILE?

sorry for my spaghetti code!, Actually I am a newbie for this programming language, and I am developing an online grading system in order to improve and apply my studies about this language which is PHP, STUDENTFILE is the database file where the name of the students are stored, but I have another idea which I think, it will be success and can comply the output what I want...Thanks for your advice and brilliant idea...GOD BLESS!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1