Coordinates from MySQL to HTML table (PHP, PDO, MySQL) - php

I am creating game which uses MySQL database to create "playing field".
In my MySQL table I have two columns pointX and pointY, both INT. I could also use POINT, but in my case these two columns are better solution.
| id | pointX | pointY | player | game |
|----|--------|--------|--------|------|
| 1 | -2 | 1 | 7 | 10 |
| 2 | -3 | 2 | 5 | 10 |
| 3 | 2 | -2 | 2 | 10 |
| 4 | -2 | -1 | 1 | 10 |
I should produce HTML table from this MySQL table. Something like this, but with no coordinateheaders (below those are only for easier understanding):
|-----|----|----|----|----|----|----|
| Y/X | -3 | -2 | -1 | 0 | 1 | 2 |
|-----|----|----|----|----|----|----|
| -2 | | | | | | 2 |
|-----|----|----|----|----|----|----|
| -1 | | 1 | | | | |
|-----|----|----|----|----|----|----|
| 0 | | | | | | |
|-----|----|----|----|----|----|----|
| 1 | | 7 | | | | |
|-----|----|----|----|----|----|----|
| 2 | 5 | | | | | |
|-----|----|----|----|----|----|----|
Plus.. every <td> should have attribute data-cell, which includes coordinates, as example data-cell="-2x-1".
What is the best way to get started?

$rng = $dbh->prepare('
SELECT MIN(pointX) AS minX, MIN(pointY) AS minY,
MAX(pointX) AS maxX, MAX(pointY) AS maxY
FROM field
WHERE game = ?
LOCK IN SHARE MODE
');
$qry = $dbh->prepare('
SELECT pointX, pointY, player
FROM field
WHERE game = ?
ORDER BY pointX, pointY
');
$dbh->beginTransaction();
$rng->execute([$game_id]);
$qry->execute([$game_id]);
$dbh->rollBack();
$limits = $rng->fetch();
$player = $qry->fetch();
echo '<table>';
for ($y = $limits['minY']; $y <= $limits['maxY']; $y++) {
echo '<tr>';
for ($x = $limits['minX']; $x <= $limits['maxX']; $x++) {
printf('<td data-cell="%dx%+dy">', $x, $y);
if ($player and $player['pointX'] == $x and $player['pointY'] == $y) {
echo htmlentities($player['player']);
$player = $qry->fetch();
} else {
echo ' ';
}
echo '</td>';
}
echo '</tr>';
}
echo '</table>';

It looks like you are already off to a good start. Assuming the number of cells for X and Y as $xcells and $ycells, the next step I would take is to build your table like so:
echo "<table>";
for ($i = -3; $i < $ycells; i++) {
echo "<tr>";
for ($j = -3; $j < $xcells; j++) {
echo "<td data-cell='" . $i . "y" . $j . "x'>";
}
echo "</tr>";
}
echo "</table>";

Related

cannot match date day based on array data on database on php

I have a leave date data in one month with various types of dates I try to match the data by date this month. when I make it based on row data only one date appears. I am a little less aware of the logic if it is an array.
My table
| ID | id | start_date | end_date |
| ____|________|______________|______________|
| 1 | x1 | 2018-11-05 | 2018-11-05 |
| 1 | x1 | 2018-11-12 | 2018-11-15 |
| 3 | x1 | 2018-11-19 | 2018-11-21 |
My script
$timesheet = $this->db->select('*')
->where('MONTH(start_date)', 11)
->where('YEAR(start_date)', 2018)
->where('id', 'x1')
->get();
$result = $timesheet->row_array();
$day_start=date_create($result['start_date']);
$day_end=date_create($result['end_date']);
for ($x = 1; $x <= 30; $x++) {
if($x >=$day_start->format('d') and $x <= $day_end->format('d')){
echo "<td class='bg-warning'>Y</td>";
}else{
echo "<td>N</td>";
}
}
/** MY result data **/
| Date | ... | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | ... |
----------------------------------------------------------------
| Result| ... | N | N | N | Y | Y | N | N | N | N | ... |
/** the results I expected **/
| Date | ... | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | ... |
----------------------------------------------------------------
| Result| ... | N | Y | N | Y | Y | N | Y | Y | Y | ... |
If you want to merge all of the records together, you will need to build up an array of the days (I create it using array_fill() and set each one to "N" to start).
You then iterate over each result row and add in the Y's to the appropriate elements. You then loop over from the start to end day using a for() loop to fill in the days with a 'Y'.
Finally you can output the $dates array which has all of the various rows added into it.
$dates = array_fill(1, 30, "N");
foreach ($timesheet->result_array() as $result) {
$start = (int)$result['start_date']->format('d');
$end = (int)$result['end_date']->format('d');
for ( $i = $start; $i <= $end; $i++ ) {
$dates[$i] = "Y";
}
}
foreach ( $dates as $day ) {
if($day == "Y") {
echo "<td class='bg-warning'>Y</td>";
}else{
echo "<td>N</td>";
}
}
You will probably want to change the array_fill() to have the correct number of days for the month you are working with, but this is something you can sort out as and when you need it.

Limit the number of Keywords in PHP

I need help limiting the number of keywords on my page. I can call the keywords from the database ex. $keywords is available for them. I want to get them for every blog post.
My database table is formatted like this
| id | title | ... | keywords |
|---------------------------------- |
| 1 | title1 | ... | word-t1-1 |
| | | | word-t1-2 |
| | | | word-t1-3 |
| | | | |
| 2 | title2 | ... | word-t2-1 |
| 3 | title3 | ... | word-t3-1 |
| 4 | title4 | ... | word-t4-1 |
I have this code:
<?php
$kw1 = trim($keywords);
$ct1 = explode("\n", $kw1);
$searchkeys = "";
for ($i = 0; $i <= sizeof($ct1); $i++) {
$searchkeys = $searchkeys.''.$ct1[$i].', ';
}
//echo $searchkeys;
?>
The code inside for loop shows the keywords as hyperlink
I want to know how to limit the number of printed keywords while if I define it in the for loop ex.
for ( $i = 0; i < 25; $i++){ somecode; }
Sometimes there aren't 25 keywords,if run the code it prints
keyword1, keyword2,,,,,,,,,,,,,,
My suggestion would be use the min() function like so:
for ( $i = 0; $i < min(count($arr), 25); $i++ ) { // code here }

Number auto increase with other while function (PHP)

I want the queqe id auto increase start from 1
I have an mysql table call t1
mysql table t1 Data as below:
+----------+------------------+-------------+
| ID | Name | Status |
+----------+------------------+-------------+
| 1 | ABBCCC | 1 |
| 2 | BASDASD | 1 |
| 3 | ABBCCC | 1 |
| 4 | ABBCCC | 2 |
+-------------------------------------------+
I loop data in php like this:
$quserCA = DB::query("SELECT * FROM ".DB::table('jnbook_book')." WHERE Name = 'ABBCCC' ORDER BY id DESC LIMIT 20");
$nqCA = mysql_num_rows($quserCA);
while($ruserCA = DB::fetch($quserCA)){
$CAlist[] = $ruserCA;
}
$x = 1;
while($x <= $nqCA) {
//echo "The number is: $x <br>";
$x++;
}
I loop this in my htm like this:
<table>
<tr>
<td>Queqe ID</td><td>ID</td><td>Status</td>
</tr>
<!--{loop $CAlist $value}-->
<tr>
<td>{$x}</td><td>{$value[id]}</td><td>{$value[status]}</td>
</tr>
<!--{/loop}-->
</table>
But after that my table output as below show
+---------------+-------------------+----------------+
| Queqe ID | ID | Status |
+---------------+-------------------+----------------+
| 1 | 1 | 1 |
| 1 | 3 | 1 |
| 1 | 4 | 2 |
+----------------------------------------------------+
Actually what I want the table output as below
(I want the queqe id auto increase start from 1):
+----------+-----------------+-----------------+
| Queqe ID | ID | Status |
+----------+-----------------+-----------------+
| 1 | 1 | 1 |
| 2 | 3 | 1 |
| 3 | 4 | 2 |
+----------------------------------------------+
Thank you.
This should be done something like:
$x = 1;
while($ruserCA = DB::fetch($quserCA)){
// add a field, say `x` with number of a record:
$ruserCA['x'] = $x++;
$CAlist[] = $ruserCA;
}
In a template:
<td>{$value[x]}</td><td>{$value[id]}</td><td>{$value[status]}</td>

PHP display images from column value

Table
+-----+--------+---------+
| ID | Name | Images |
+-----+--------+---------+
| 001 | John | 5 |
| 002 | Mark | 3 |
+-----+--------+---------+
i would like to display like this
Jon, 001-1.jpg | 001-2.jpg | 001-2.jpg | 001-3.jpg | 001-4.jpg | 001-5.jpg |
Mark, 002-1.jpg | 002-2.jpg | 002-2.jpg | 002-3.jpg |
the images value on database table will be the number of images return to create images link
You can use this though. Didn't know why you found it difficult.
$c = 0
while (false !== ($data = fetch_array_as_row_function()))
{
echo "<tr>";
echo "<td>", $data["name"], "</td>";
for ($i = 0; $i < $data["images"]; $i++)
echo "<td>00", $c,"-", $i, "</td>";
echo "</tr>";
}
Here, the function fetch_array_as_row_function() is something equivalent to what mysql_fetch_array() does.

How do I check if a value currently exist in a while loop?

Let's say I have this table in my database...
id | name | beginventory | sold | datesold
101 | rock | 100 | 5 | 2011-9-12
201 | paper | 50 | 10 | 2011-9-13
301 | pen | 30 | 20 | 2011-10-1
101 | rock | 100 | 10 | 2011-10-1
101 | rock | 100 | 10 | 2011-10-2
201 | paper | 50 | 15 | 2011-10-3
I want to display an output of...
name | current | sold | remaining | date sold
rock | 100 | 5 | 95 | 2011-9-12
paper | 50 | 10 | 40 | 2011-9-13
pen | 30 | 20 | 10 | 2011-10-1
rock | 95 | 10 | 85 | 2011-10-1
rock | 85 | 10 | 75 | 2011-10-2
paper | 40 | 15 | 25 | 2011-10-3
Here my script (doesn't work)
while($info = mysql_query($getinfo)){
$name = $info['name'].'<br />';
$beginventory = $info['beginventory'].'<br />';
$sold = $info['sold'].'<br />'
$remaining = $beginventory - $sold.'<br />';
$date = $info['datesold'].'<br />'
echo '<tr>';
echo '<td>'.$name.'</td>';
echo '<td>'.$current.'</td>';
echo '<td>'.$sold.'</td>';
echo '<td>'.$remaining.'</td>';
echo '<td>'.$date.'</td>';
echo '</tr>';
}
My question is how do I "tell" PHP in a while loop that if the "name" already exist, instead of using $beginventory to get $remaining it should use the previous $remaining to get the correct value for current.
I know it is easier if I stored the values in the database, this is my last option if I can't figure this out.
$left = array();
while($info = mysql_query($getinfo)){
if (!isset($left[$info['name']])) $left[$info['name']] = $info['beginventory'];
$name = $info['name'].'<br />';
$beginventory = $left[$info['name']].'<br />';
$sold = $info['sold'].'<br />'
$remaining = $beginventory - $sold.'<br />';
$date = $info['datesold'].'<br />'
echo '<tr>';
echo '<td>'.$name.'</td>';
echo '<td>'.$current.'</td>';
echo '<td>'.$sold.'</td>';
echo '<td>'.$remaining.'</td>';
echo '<td>'.$date.'</td>';
echo '</tr>';
$left[$info['name']] -= $info['sold'];
}

Categories