PHP MYSQL - blow up help

Hello,

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

The rows and column number are user-supplied, so it can change.

Let's say there is number 3 in column and 3 in rows.

I need to do this as a mapping, for example

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

      

Where d1-d9 is data stored in mysql database with delimiter "," in one table.

Thanks for helping me.

+1


source to share


3 answers


You can pivot comma separated values ​​from a data column to 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');
?>

      



You will need two nested loops to display the table:

<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>

      

+6


source


It won't help you solve this problem, but a word of good advice: never ever write comma-separated values ​​in a database field again. You cannot intelligently query for information stored in this way and your application code will be cluttered with ugly transformations. Instead, use a separate table with a reference to the main table and one row per value.



+6


source


assuming user set the table size to 2 rows and 3 columns and made multiple input fot 6 cells, the data that will go to the database will be

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

when you fetch data from a cell and blow it up across the extracted row, you will get 1 dimensional array with 8 elements

$ r = $ e [0] rows

$ c = $ e [1] cols

$ e [2-7] data p>

  • wrtite openning <table> tag
  • two loops, one inside the other,
  • first the code for the start of the line will be generated
  • wrtite openning <tr>
  • inside will generate code for the string.
  • opening record <td>
  • write data $ e [1 + position calculated from inner and outer loops]
  • closing record <td>
  • end of inner loop
  • wrtite closing <tr>
  • end of outer loop
  • wrtite closing <table> tag

This should give you an idea

0


source







All Articles