Diplay multiple Mysql Arrays in 1 HTMl table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • GhOsTTeCh
    New Member
    • Dec 2008
    • 22

    Diplay multiple Mysql Arrays in 1 HTMl table

    Hey every1 , i have started coding in php and am a qucik learner (computer generation) i have ran into a problem and am cluless :(

    ill start by showing you the mysql table
    Code:
    --
    -- Table structure for table `01234`
    --
    
    CREATE TABLE IF NOT EXISTS `01234` (
      `ucode` char(50) NOT NULL,
      `Day` char(50) NOT NULL,
      `Period` char(50) NOT NULL,
      `sub` char(50) NOT NULL,
      `teacher` char(50) NOT NULL,
      `room` char(50) NOT NULL,
      `class` char(50) NOT NULL
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
    
    --
    -- Dumping data for table `01234`
    --
    
    INSERT INTO `01234` (`ucode`, `Day`, `Period`, `sub`, `teacher`, `room`, `class`) VALUES
    ('01234', '1', '1', 'PE', 'PETE', 'R16', '2.8'),
    ('01234', '1', '2', 'WHG', 'WHGT', 'R27', '2.8'),
    ('01234', '1', '3', 'COMM', 'COMMT', 'R12', '2.1'),
    ('01234', '1', '4', 'COMM', 'COMMT', 'R12', '2.1'),
    ('01234', '1', '5', 'SCIE', 'SCIET', 'SL1', '2.8'),
    ('01234', '1', '6', 'ENGL', 'ENGT', 'R29', '2.7'),
    ('01234', '2', '1', 'MATH', 'MATHT', 'R18', '2.5'),
    ('01234', '2', '2', 'ENGL', 'ENGT', 'R29', '2.7'),
    ('01234', '2', '3', 'RELI', 'RET', 'R15', '2.8'),
    ('01234', '2', '4', 'INST', 'PCT', 'CL1', '2.5'),
    ('01234', '2', '5', 'SPORTB', 'SPT', '', '2.2'),
    ('01234', '2', '6', 'SPORTB', 'SPT', '', '2.2'),
    ('01234', '3', '1', 'WHG', 'WHGT', 'R30', '2.8'),
    ('01234', '3', '2', 'PE', 'PETE', 'R23', '2.8'),
    ('01234', '3', '3', 'RELI', 'RET', 'R15', '2.8'),
    ('01234', '3', '4', 'SCIE', 'SCIET', 'SL1', '2.8'),
    ('01234', '3', '5', 'SCIE', 'SCIET', 'SL1', '2.8'),
    ('01234', '3', '6', 'COMM', 'COMMT', 'R12', '2.1'),
    i know i have to echo a html table in an array but i dont know how to make it look like this:
    Code:
    Period  Day1    Day2   Day3   
      1     PE      MATH   WHG
      2     WHG     ENG    PE
      3     COMM    RELI   RELI
      4     COMM    INST   SCIE
    i had a old code but it only handled basic rows that where already lyk the stated above.

    any help is appreciated if u need more info just ask.

    TY and have a very happy new year.
    P.S sorry if i put the SQL code in wrong BBcode , didnt know what to put it as?
    Last edited by Atli; Dec 31 '08, 08:34 PM. Reason: Added the second [code] tags.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    Rather than echo the results of the query directly, you need to set up an array so you can print the data in a more controlled way.

    For example:
    [code=php]
    while($row = mysql_fetch_ass oc($result)) {
    $days[$row['day']][$row['period']] = $row['sub'];
    }[/code]
    This would create an array of days, where each day contained every period for that day.
    Which would look something like:
    Code:
    Array (
      1 = Array(1 => "Subj", 2 => "Subj", 3 => "Subj"),
      2 = Array(1 => "Subj", 2 => "Subj", 3 => "Subj")
      /* etc... */
    )
    And using that as a base, it shouldn't be hard to print in whatever format you need.

    Comment

    • GhOsTTeCh
      New Member
      • Dec 2008
      • 22

      #3
      Thankyou Atli and Happy new year

      i used ur soloution and it can grab the data from the sql database.
      i echod the varible "$days" and it resulted in "ArrayArrayArra y..." so :S

      however i did this code
      Code:
       while($row = mysql_fetch_assoc($result)) {
       $days[$row['day']][$row['period']] = $row['sub'];
      
        echo"<TR bgcolor=#86D5FF class=ts onMouseOver=this.bgColor='gold'; onMouseOut=this.bgColor='#86D5FF';><td><strong>" . $row['period'] . "</strong></td><td><div align=center>" . $row['sub'] . "</div></td><td><div align=center>" . $row['sub'] . "</div></td>";
      
      }
      the subjects keep listing after one another, how can i make it that after 6 subjects , list in the column next to it.

      again thankyou to any that took the tiume to read this and even more ty to those whu replied :)

      :S is the symbol of confusion for those who dont know
      Last edited by GhOsTTeCh; Jan 1 '09, 07:00 AM. Reason: Added the meaning of :S

      Comment

      • GhOsTTeCh
        New Member
        • Dec 2008
        • 22

        #4
        had a thought.

        is there a way that you can set an array to grab results from 2 queries. and then load them as seperat varibles.

        Code:
        $day1 = mysql_query("SELECT sub FROM `01234` WHERE `Day` = 1");
        $day2 = mysql_query("SELECT sub FROM `01234` WHERE `Day` = 2");
        
        while($d1 = mysql_fetch_assoc($day1) || ($d2 = mysql_fetch_assoc($day2)))
        {
         $day1[$d1['day']][$d1['period']] = $d1['sub'];
         $day2[$d2['day']][$d2['period']] = $d2['sub'];
        
          echo"<TR bgcolor=#86D5FF class=ts onMouseOver=this.bgColor='gold'; onMouseOut=this.bgColor='#86D5FF';><td><strong>" . $d1['period'] . "</strong></td><td><div align=center>" . $d1['sub'] . "</div></td><td><div align=center>" . $d2['sub'] . "</div></td>";
        
        }
        this code ignoers day1 for sum reason

        again thank you

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          Originally posted by GhOsTTeCh
          i used ur soloution and it can grab the data from the sql database.
          i echod the varible "$days" and it resulted in "ArrayArrayArra y..." so :S
          I think it's the multi-dimensional arrays that are confusing you.
          Let me try to explain.

          Consider a typical one-dimensional array, like this:
          [code=php]
          $array = array(
          1 => "John Doe",
          2 => "Jane Smith",
          3 => "James Bond"
          );[/code]
          To print the values of that array, we would do:
          [code=php]
          foreach($array as $_index => $_value) {
          echo "$_index = $_value\n";
          }[/code]

          A multidimensiona l array is simply an array that contains other arrays.
          Like:
          [code=php]
          $array = array(
          1 => array("First" => "John", "Last" => "Doe"),
          2 => array("First" => "Jane", "Last" => "Smith"),
          3 => array("First" => "James", "Last" => "Bond"),
          );[/code]
          So, when we loop through it, like we did with our one-dimensional array, the $_value becomes an array:
          [code=php]
          foreach($array as $_index => $_value) {
          echo "$_index = {$_value['Last']}, {$_value['First']}";
          }[/code]

          So, in the code I posted before, I was essentially creating an array of days, where each day had a value that was an array of periods, where each period had a subject as it's value.

          To print that you would have to loop through the days, and loop through each period of each day so you could print the subject.

          Like:
          [code=php]
          echo "Period\t1\t2\t 3\t4\t5\t6";
          foreach($days as $_dayNo => $_day) {
          echo "\nDay $_dayNo:\t";
          foreach($_day as $_period) {
          echo "$_period\t ";
          }
          }[/code]
          Does that make sense?

          Originally posted by GhOsTTeCh
          is there a way that you can set an array to grab results from 2 queries. and then load them as seperat varibles.
          Yes. The problem in the code you posted wast that you used or (||).
          When the left-hand condition of an or is true, the right-hand condition won't be executed.
          So:
          [code=php]
          if($a = 1 || $b = 1) {
          // $a == 1
          // $b == null
          }[/code]
          To do what you are trying, you could try a do while loop:
          [code=php]
          do {
          $row1 = @mysql_fetch_as soc($result1);
          $row2 = @mysql_fetch_as soc($result2);
          if($row1) {
          // Do something with $row1
          }
          if($row2) {
          // Do something with $row2
          }
          } while ($row1 || $row2);[/code]

          Comment

          • GhOsTTeCh
            New Member
            • Dec 2008
            • 22

            #6
            Thank you Atli , although i didnt use ur exact soloution u certanley explaiend alot on sent me in the right direction

            expect to see me more on the forums :)

            Comment

            • Atli
              Recognized Expert Expert
              • Nov 2006
              • 5062

              #7
              I'm glad I could help.
              See you around :)

              Comment

              Working...