Jump to content

Gridwork with SQL


Gniew

Recommended Posts

What I want to do is have a SINGLE SQL Query, that will somehow get the row and column numbers (0,0; 0,1; 0,2;etc. 1,0; 1,1; 1,2; etc) similar to that of a coordinate plane only where the upper left TD is 0,0 and the row down starts at 1,0, etc.

 

The first numbers (x and y) come from the URL (as do other numbers). Then the code adds the row/column numbers to the x and y numbers, then will display the proper data in the table where it needs to be. However, some TD's are blank.

For example, 0,5 might be blank, but 1,5 has data. In the DB, each cell with data is a new row, created when data is added. So, row 1 has values of X=1, Y=5, and say Info=25, and I'd want the "25" to appear in the TD, but there is no row for X=0, Y=5.

 

I know I could probably copy the code and change the variables manually in each TD, but that in my opinion would make the code be quite long with too much repetition, and I'd imagine would cause issues when no data exists.

class Map {
  function Image() {
$worked = mysql_query("SELECT * FROM `map` WHERE `x`=('".$_GET['x']."' + ".$col.") AND (`y`='".$_GET['y']."' + ".$row.") AND `table`='".$_GET['t']."' AND `display`='".$_GET['d']."'");
	$result = mysql_fetch_object($worked);
}
}

 

So, does anyone have any ideas on how to make this work? Or is there a way to split apart an SQL query, so that "$worked = mysql_query("SELECT * FROM `map` WHERE" would say be $a, and "AND `table`='".$_GET['t']."' AND `display`='".$_GET['d']."'"); $result = mysql_fetch_object($worked);" is $c, so that on each TD I could write something to combine $a with "`x`=('".$_GET['x']."' + 1) AND (`y`='".$_GET['y']."' + 1)" and then combine that with $c?

 

If anyone understands that...

 

My table is simple...

<table width="75%" border="0">
  <tr>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
</table>

Link to comment
https://forums.phpfreaks.com/topic/240750-gridwork-with-sql/
Share on other sites

Simple enough. I'm not sure what you want beyond this

 

To grab (5,3) you'd use

<?php 

$sql = new MySQLi( 'localhost','root','','db' );

$row = 5; $col = 3;

$q = 'SELECT * FROM `table` LIMIT ' .($row-1). ',1';

$result = $sql->query( $q );
$row = $result->fetch_row();
$result->free();

$data = $row[$col-1];

echo $data;

?>

Link to comment
https://forums.phpfreaks.com/topic/240750-gridwork-with-sql/#findComment-1236618
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.