tags:

views:

2065

answers:

6

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.

A: 

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

MoreThanChaos
A: 

Rows number are stored on column name "rows", Columns number are stored on column name "columns".

Other data are stored on column name "data".

I'm begineer in PHP, I don't quite get what you mean? Mind explaining more to me?

Please and Thanks.

John C
Please use the comment system for this. Using answers to comment on other answers is the wrong approach.
Tomalak
+6  A: 

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.

Alexander Malfait
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
Vinko Vrsalovic
+6  A: 

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>
foxy
Hats off for understanding the question, I guess this was the most difficult part. :-)
Tomalak
A: 

Thank you very much foxy!

John C
Please use the comments for this, don't use answers.
Tomalak
@Tomalak: commenting needs 50 reputation (he has 19 as of this writing). He should instead edit his question.
CesarB
A: 

How can i explode field value of table in select query ?

for e.g. i have 1 field in table named "coordinates" which contains latitude , longitude.

Now i want to use this latitude and longitude in select query.

Can i seperate this values and use it in select query ?

PHP Freelancer India