Hello,
I have one problem in displaying data when run mysql query.
When I use cross join query it brings data of cars then I use while loop like this

while($row=mysql_fetch_array($result))
{

echo "I want to print result";

}

The proble is in displaying result, so instead of displaying result in rows I want to display result in 2/3 columns
example

instead of displaying like this
echo row1;
echo row2;
echo row3;
.................
................

I want to display result like this

echo row1 echo row2 echo row3
echo row4 echo row5 echo row6
echo row7 echo row8 echo row9
................ ................. ................

Any one know how to do it. thank in advance

    Set up a counter in your while loop and use the modulus operator on your counter to decide when you want to open a new table row and/or close the existing one.

    For example, you could do something like this:

    echo '<table>';
    
    $counter = 0;
    $cells_per_row = 3;
    while($row=mysql_fetch_array($result)) 
    {
        $counter++;
        if(($counter % $cells_per_row) == 1)  { echo '<tr>'; }
        echo '<td>' . (whatever you echo from your $row array) . '</td>';
        if(($counter %  $cells_per_row) == 0) { echo '</tr>'; }
    } 
    // just in case we haven't closed the last row
    // this would happen if our result set isn't divisible by $cells_per_row
    if(($counter % $cells_per_row) != 0) { echo '</tr>'; }
    
    echo '</table>';
      Jasp182 wrote:

      Set up a counter in your while loop and use the modulus operator on your counter to decide when you want to open a new table row and/or close the existing one.

      For example, you could do something like this:

      echo '<table>';
      
      $counter = 0;
      $cells_per_row = 3;
      while($row=mysql_fetch_array($result)) 
      {
          $counter++;
          if(($counter % $cells_per_row) == 1)  { echo '<tr>'; }
          echo '<td>' . (whatever you echo from your $row array) . '</td>';
          if(($counter %  $cells_per_row) == 0) { echo '</tr>'; }
      } 
      // just in case we haven't closed the last row
      // this would happen if our result set isn't divisible by $cells_per_row
      if(($counter % $cells_per_row) != 0) { echo '</tr>'; }
      
      echo '</table>';

      Hello,
      thanks for your reply and exact answer. It realy worked and very usefull. very thanks for your help.

        Jasp182 wrote:

        Set up a counter in your while loop and use the modulus operator on your counter to decide when you want to open a new table row and/or close the existing one.

        For example, you could do something like this:

        echo '<table>';
        
        $counter = 0;
        $cells_per_row = 3;
        while($row=mysql_fetch_array($result)) 
        {
            $counter++;
            if(($counter % $cells_per_row) == 1)  { echo '<tr>'; }
            echo '<td>' . (whatever you echo from your $row array) . '</td>';
            if(($counter %  $cells_per_row) == 0) { echo '</tr>'; }
        } 
        // just in case we haven't closed the last row
        // this would happen if our result set isn't divisible by $cells_per_row
        if(($counter % $cells_per_row) != 0) { echo '</tr>'; }
        
        echo '</table>';

        Problem displaying extra text field (cross join query)


        SELECT heading_title, opening_day, opening_time FROM heading_openinghour CROSS JOIN timetble
        WHERE heading_openinghour.heading_id=timetble.heading_id;

        while ($row=mysql_fetch_array($result))
        {
        $heading;
        if($heading!=$row['0'])
        {
        echo $row['0'];
        $heading=$row['0'];
        }

        echo $row['1'];
        echo $row['2'];

        }

        I am using cross join query to select heading from first table
        and sub headings from second table.The query works fine but I want to run
        query in a way that when query finish printing one heading and all
        of its sub heading then it print an extra text field under that sub headings and
        then next line starts.
        any idea how to do this.

        Example

        heading1
        sub heading1
        sub heading2
        sub heading3
        insert text field here

        heading2

        sub heading1
        sub heading2
        sub heading3
        sub heading4
        sub heading5
        insert text field here

        heading2

        sub heading1
        insert text field here

          Your second question is a perfect example of why I do all my programming in objects. Instead of having to make a complex query inline to get my results, I build an object that can contain functions that will build arrays of other objects and so on.

          The only thing I could suggest that would emulate objects inline would be to build arrays with the results of your queries and then nest a query for your subheadings so that the array of subheadings is an item of the heading array. Does that make any sense? Classes and objects would make the task you describe extremely simple.

            Jasp182 wrote:

            Your second question is a perfect example of why I do all my programming in objects. Instead of having to make a complex query inline to get my results, I build an object that can contain functions that will build arrays of other objects and so on.

            The only thing I could suggest that would emulate objects inline would be to build arrays with the results of your queries and then nest a query for your subheadings so that the array of subheadings is an item of the heading array. Does that make any sense? Classes and objects would make the task you describe extremely simple.

            Yeah you are right. I am not very advanced programmer, so never think to use classes, but for future I am thinking to adapt Object oriented programming.
            If I do with classes then I need to spend some time with classes tutorial. Can you give me a brief idea (like some kind of quick tutorial) that by using it I can quiclky sort out my problem this time , and for future I will try to move my programming towards classes and with spare time I will look at it deeply.
            Idea I need is only that like

            suppose I have class like

            Class main section
            {
            include ".........";
            SELECT heading_title, opening_day, opening_time FROM heading_openinghour CROSS JOIN timetble
            WHERE heading_openinghour.heading_id=timetble.heading_id;

            function heading()
            {
            while($row=.....)
            {

            print heading 1;
            return;

            }

            function sub_heading()
            {

            print sub_heading;
            return;

            }

            }

            Now from here still not clear how I will save it in array and use it like When sub heading finish then add text box and again restart.

            Can you give me a brief idea.

            Thanks again for your help. I realy appreciate your time.

              It's a very good idea to read a book on OOP and/or study an object oriented application programming language like Java. Java practically forces you to use an oo approach, whereas php doesn't and apparently most people who program in php do not. I do every single bit of my programming in objects so my html files aren't cluttered with code, and I can reuse all my classes in many places. There are a lot of concepts that are specific to programming - inheritance, polymorphism, interfaces, etc. But for the most part, the basic idea is as simple as those goofy analogies all the books start off with that relate the concept of classes and objects to things like cars and animals.

                Jasp182 wrote:

                It's a very good idea to read a book on OOP and/or study an object oriented application programming language like Java. Java practically forces you to use an oo approach, whereas php doesn't and apparently most people who program in php do not. I do every single bit of my programming in objects so my html files aren't cluttered with code, and I can reuse all my classes in many places. There are a lot of concepts that are specific to programming - inheritance, polymorphism, interfaces, etc. But for the most part, the basic idea is as simple as those goofy analogies all the books start off with that relate the concept of classes and objects to things like cars and animals.

                thanks for your help and advice. I have done Java programming in my degree and have read a lot in object oriented programming. Now I will look at it again using PHP, hopefully I will start reusing it soon. thanks again

                  9 days later

                  Hi, I have the same problem to display results in multiple column too, can someone please review my codes and advice where should I insert the code provided by Jasp182 ? Or, should I place it under the system.class.php?

                  Thank you so much in advance.

                  <?php
                  require_once ('global.php');

                  $title = $mvscript['site_name'] . ' - Flash Games, Free Online Games';
                  echo $tpl->pull_template("header");
                  ?>

                  <table class="content-table" width="100%" cellspacing="1" cellpadding="4">
                  <tr>
                  <td class="content-header">Games</td>
                  </tr>
                  <tr>
                  <td>Fun Flash Games - The largest source of great free online games including Adventure Games, Action Games, Sports games, Puzzle Games, Shooting Games, Arcade Games and more. All are free to play online.
                  </td>
                  </tr>
                  </table><br />

                  <table class="content-table" width="100%" cellspacing="1" cellpadding="4">
                  <tr>
                  <td class="content-header">All Games List</td>
                  </tr>
                  <tr>
                  <td><a href="http://www.musicvideofun.com/games_adventure.php">Adventure Flash Games</a>
                  </td>
                  </tr>
                  </table><br />

                  <table class="content-table" width="100%" cellspacing="1" cellpadding="4">
                  <tr>
                  <td class="content-header">Games</td>
                  </tr>
                  <tr>
                  <td><center>

                  <?php require("flash/system.class.php"); ?>
                  <?php $sys = new GamesSystem();
                  if(isset($GET['act']) && isset($GET['id']) && $GET['act'] == 'play'){
                  $sys->addPlay($
                  GET['id']);
                  }
                  $sys->Load(); ?>

                  <div class="wrapper">

                  <div class="head">
                    <div class="top" style="height: auto !important; height: 100%">
                  
                  		<div class="image">
                  
                  		  <div align="center"></div>
                  	  </div>
                  
                  
                          	  </div>
                  
                  		<div class="clear"></div>

                  </div>

                  	<div class="menu">
                  
                  		<div style="float: left;margin-top: 3px;margin-left: 5px;">
                  			<?php
                  			$plays = $sys->getPlaysToday();
                  			if($plays != ''){
                  				?>
                  				There have been <?php echo $plays; ?> games played today.
                  				<?php
                  			} ?>
                  		</div>
                  
                  		<div style="float:right">
                  
                  			<select name="games" id="games" onchange="switchme()">
                  				<option selected>Select a Game</option>
                  				<option class="headname" value="<?php echo $_SERVER['PHP_SELF']; ?>">Games List</option>
                  				<?php
                  				// makes list of options
                  				echo $sys->makeOptionList();
                  				?>
                  			</select>
                  
                  		</div>
                  		<div class="clear"></div>
                  
                  	</div>

                  </div>
                  <div class="clear"></div>

                  <div class="clear"></div>

                  <div class="body"> 
                  
                  	<div class="core">
                  
                  	<?php
                  
                  	if(!isset($_GET['act']) || $_GET['act'] != 'play'){
                  		echo $sys->makeGamesList();
                  	} else {
                  		if(isset($_GET['id'])){
                  			echo $sys->makeGameHtml($_GET['id'], $_GET['cid']);
                  		} else {
                  			echo '<div style="margin: 30px;">
                  			<strong>Error: </strong>Invalid Input<br /><br />
                  			<a href="'.$_SERVER['PHP_SELF'].'">Click here to return Home</a>
                  			</div>';
                  		}
                  	}
                  
                  	?>
                  
                  	<div align="center"></div>
                  	</div>
                  
                  	 <div class="right">
                  		<?php
                  		echo $sys->doMostPlayed();
                  		/* for most played, you can also use
                  
                  		echo $sys->doMostPlayed(x);
                  
                  		Where x = the ammount of games you wish to display.
                  		This same methods works for doNewestGames()
                  
                  		Default for these functions is 10
                  
                  		*/
                  		echo $sys->doNewestGames();
                  		?>
                  	</div>
                  
                  	<div class="clear"></div>

                  </div>

                  </div>
                  <!-- Script for select box changes -->
                  <script type="text/javascript">
                  <!--<![CDATA[
                  function switchme(){
                  if(document.getElementById('games').value != ''){
                  window.location = document.getElementById('games').value;
                  }
                  }
                  //]]>-->
                  </script>

                  	</td>
                  </tr></center>

                  </table><br />

                  <?php
                  echo $tpl->pull_template("footer");
                  ?>

                    Write a Reply...