Jump to content

[SOLVED] Get data from database and diplay in a table


ericburnard

Recommended Posts

Basicly what im wanting is to get data from my sql database (which i can do) diplay the results in a table (which i can do) But what im unsure on is this. When 5 results have been diplayed in the table, how can i get it to start a new row and display the next 5 results then a new row etc etc.

Help and guidence would be awesome

Thanks again

Eric x

Link to comment
Share on other sites

<table>

<tr>

<td>row1col1</td><td>row1col2</td>etc..

</tr>

<tr>

<td>row2col1</td><td>row2col2</td>etc..

</tr>

etc...

</table>

 

That didnt make any sence sorry. I think i know what you mean but there are different amouts of data to be diplayed in each table and the amout of data will be changed by the user

Link to comment
Share on other sites

<?php
   echo "<table><tr>";
   $x = 1;
   while ($x < 26) {
      echo "<td>$x</td>";
      echo ($x % 5 == 0)? "</tr><tr>" : "";
      $x++;
   }
   echo "</tr></table>";
?>

 

That worked perfect. Thank you very much. Could you just check for me that this is ok how i have changed it (or if you would have done it different?

 

<?
include ('db.php');

mysql_connect($host,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM departments";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

   echo "<table><tr>";
   $x = 1;
   while ($x < $num) {
   
   $department=mysql_result($result,$x,"department");

      echo "<td>$department</td>";
      echo ($x % 5 == 0)? "</tr><tr>" : "";
      $x++;
   }
   echo "</tr></table>";
  
?>

Link to comment
Share on other sites

- Use <?php instead of <? shorttags do not work on all server configurations. 

 

- Use mysql_fetch_assoc instead of mysql_result.  mysql_result is terrible and only real use for it is if you are wanting to "look ahead" to the next row, which is pretty much never.  Also it gets rid of having to use mysql_numrows. 

 

- I'd also avoid using @ as it suppresses errors (proper error handling is a better method).  Not really gonna type out error handling routines for you or nothin', but controlling what is being output when things go wrong is a lot more secure. Many default error messages will reveal info about your system, setup, var names, table info, etc.. that someone could use against you. 

 

- mysql_close is not really necessary either, as php will automatically close it when the script finishes executing.  Only really useful if you are only using connection at beginning script and there's a ton of script to execute after that, but even then, the resources used is negligible. 

 

- I also like to assign my mysql_connect and mysql_select_db to a variable and include it as the optional 2nd argument in mysql_query.  Not strictly necessary for a simple script with only one connection; more a matter of discipline.

 

- Also, you should avoid using * in your query unless you are going to use all of the columns.  You only use 'department' in your loop so only select department.  Doing so will cut down on script query/execution time and resources used. 

 

<?php
include ('db.php');

errorhandlingfunction() {
   // stuff for handling errors here
}

$conn = mysql_connect($host,$username,$password) or errorhandlingfunction();
$db = mysql_select_db($database) or errorhandlingfunction();

$query="SELECT department FROM departments";
$result=mysql_query($query, $conn) or errorhandlingfunction();

echo "<table><tr>";
$x = 1;
while ($list = mysql_fetch_assoc($result)) {
   echo "<td>{$list['department']}</td>";
   echo ($x % 5 == 0)? "</tr><tr>" : "";
   $x++;
}
echo "</tr></table>";
  
?>

 

There's always room for improvement, but anyways, there's my 2 cents.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.