1

Greetings,

I have data stored on mysql with delimiter "," in 1 table. I have rows and column stored on database too. Now i have to output the data using rows and column number stored on database to draw the table.

Rows and column number are user input, so it may varies.

Let say, there is number 3 on column and 3 on rows.

I need to do it like display like,

|___d1__|___d2__|___d3__|
|___d4__|___d5__|___d6__|
|___d7__|___d8__|___d9__|

Where d1-d9 would be the data stored on mysql database with delimiter "," in one table.

Thanks for helping me.

1
  • this sort of thing gives me a splitting headache. (sorry, couldn't resist) Commented Jan 23, 2009 at 5:18

3 Answers 3

6

You can turn the comma separated values from your data column into an array using the explode() function:

<?php
  $result = mysql_query('SELECT rows, columns, data from table_name where id=1');
  $record = mysql_fetch_assoc($result);

  $rows = $record['rows'];
  $columns = $record['columns'];

  $data = explode(',' , $record['data']);

  if (sizeof($data) != $rows * $columns) die('invalid data');
?>

To display the table, you need two nested for-loops:

<table>
<?php for ($row = 0; $row < $rows; $row++) : ?>
    <tr>
    <?php for ($column = 0; $column < $columns; $column++) : ?>
        <td>
            <?php echo $data[$row * $columns + $column]; ?>
        </td>
    <?php endfor ?>
    </tr>
<?php endfor ?>
</table>
Sign up to request clarification or add additional context in comments.

1 Comment

Hats off for understanding the question, I guess this was the most difficult part. :-)
6

This won't help you solve this very problem, but a word of good advice: never EVER write comma seperated values into a database field. You can't sensibly query information stored like this, and your application code will be cluttered with ugly conversions. Instead, use a seperate table with a reference to the main table and one row per value.

1 Comment

It does help him indeed, he can instead write a procedure to fix the tables and then write sensible queries instead of trying to go this way
0

assuming that user set table size for 2 rows and 3 columns and makes some input fot 6 cells, data which will go to database will be

2,3,d1,d2,d3,d4,d5,d6

when you will fetch data from cell and make explode on fetched string you will get 1 dimension array with 8 elements

$r = $e[0] rows

$c = $e[1] cols

$e[2-7] data

  • wrtite openning < table > tag
  • two loops, one in another,
  • first one will generate code for start of row
  • wrtite openning < tr > tag
  • inside one will genereate code for row.
  • write opening < td > tag
  • write data $e[1 + position calulated from inside and outside loops]
  • write closing < td > tag
  • end of inside loop
  • wrtite closing < tr > tag
  • end of outside loop
  • wrtite closing < table > tag

It should give you the idea

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.