PHP display images from column value - php

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.

Related

Remove repeating data in a column

I am currently developing an ordering system where a customer can order many items. I also have an admin where he/she can see all the orders on that day. The Admin can view the name of the customer, the total payable, the products and the quantity of the product the customer have ordered.
I am currently seeing this results using my query.
Name | Payable | Product | Quantity
Test | 165 | keychain | 3
Test | 165 | Tumbler | 1
Miguel | 525 | Keychain | 3
Miguel | 525 | Magic Mug | 3
Dandel | 1010 | keychain | 3
Dandel | 1010 | T-shirt | 2
Dandel | 1010 | Keychain | 3
Dandel | 1010 | Mug | 5
This is my query.
$result = mysql_query("
SELECT reservation.firstname, reservation.lastname, reservation.payable, reservation.city, orders.product, orders.qty, reservation.date
FROM orders
INNER JOIN reservation
ON orders.confirmation = reservation.confirmation
WHERE reservation.date = CURDATE() && reservation.city = '24th Floor'
");
while($row = mysql_fetch_array($result))
{
echo '<tr>';
echo '<td style="border: 1px solid black;">'.$row['firstname'].'</td>';
echo '<td>'.$row['payable'].'</td>';
echo '<td>'.$row['product'].'</td>';
echo '<td>'.$row['qty'].'</td>';
echo '</tr>';
}
I want to get results like this. How can I do it?
Name | Payable | Product | Quantity
Test | 165 | keychain | 3
| | Tumbler | 1
Miguel | 525 | Keychain | 3
| | Magic Mug | 3
Dandel | 1010 | keychain | 3
| | T-shirt | 2
| | Keychain | 3
| | Mug | 5
You can maintain some state while iterating which keeps track of whether the current row is a new name/payable:
$last_name_seen = NULL;
while ($row = mysql_fetch_array($result)) {
$firstname = "";
$payable = "";
if ($last_name_seen === NULL || $row['firstname'] != $last_name_seen) {
$last_name_seen = $row['firstname'];
$firstname = $row['firstname'];
$payable = $row['payable'];
}
echo '<tr>';
echo '<td style="border: 1px solid black;">'.$firstname.'</td>';
echo '<td>'.$payable.'</td>';
echo '<td>'.$row['product'].'</td>';
echo '<td>'.$row['qty'].'</td>';
echo '</tr>';
}
Note that your MySQL query should have some ORDER BY clause, to generate the ordering you want, e.g.
ORDER BY Name, Product;
As the comments above also suggest, if you are using a deprecated PHP API, you should consider upgrading to something more modern. But, the logic used in the above loop would not change much with changing the MySQL API.
You have to set flag for name & payable column & use array_column(),array_unique() to get unique column values :
<?php
$result = array(array("Name"=>"Test","Payable"=>'165',"Product"=>"keychain","Quantity"=>3),array("Name"=>"Test","Payable"=>'165',"Product"=>"Tumbler","Quantity"=>1));
$name_arr = array_unique(array_column($result, 'Name'));
$payable_arr = array_unique(array_column($result, 'Payable'));
$name_flag = 0;
$pay_flag = 0;
echo "<table border='1'>";
for($i=0;$i<count($name_arr);$i++)
{
for($j=0;$j<count($payable_arr);$j++)
{
foreach($result as $list)
{
if($list['Name'] == $name_arr[$i] && $list['Payable'] == $payable_arr[$j])
{
if($name_flag==0 && $pay_flag==0)
{
echo "<tr>";
echo "<td>".$name_arr[$i]."</td>";
echo "<td>".$payable_arr[$j]."</td>";
echo "<td>".$list['Product']."</td>";
echo "<td>".$list['Quantity']."</td>";
echo "</tr>";
}
else
{
echo "<tr>";
echo "<td> </td>";
echo "<td> </td>";
echo "<td>".$list['Product']."</td>";
echo "<td>".$list['Quantity']."</td>";
echo "<tr>";
}
$name_flag++;
$pay_flag++;
}
}
}
$name_flag=0;
$pay_flag=0;
}
echo "</table>";
?>

How to dynamically use rowspan to print data in table in php and mysql?

In my program, the following query and code generate the following table like this:
+-------+---------+
|ward_id|Sub_block|
+-------+---------+
| 1 | A1 |
+-------+---------+
| 1 | B1 |
+-------+---------+
| 1 | C1 |
+-------+---------+
| 2 | D1 |
+-------+---------+
| 2 | E1 |
+-------+---------+
| 2 | F2 |
+-------+---------+
| 3 | K1 |
+-------+---------+
| 3 | G2 |
+-------+---------+
| 3 | I3 |
+-------+---------+
Here is my code that generate the above table.
if(isset($_POST["union_id"]) && !empty($_POST["union_id"]))
{
//Get all union data
$query = $mysqli->query("SELECT * FROM test_epi_table WHERE union_id = ".$_POST['union_id']);
//Count total number of rows
$rowCount = $query->num_rows;
//Display unions list
if($rowCount > 0)
{
echo "<hr>";
echo "<h3 align='center'>EPI Schedule</h3>";
echo "<table class='table table-bordered'>
<tr>
<th>Ward No</th>
<th>Sub-Block</th>
</tr>";
while($row = $query->fetch_assoc())
{
echo "<tr>";
echo "<td>" . $row['ward_no'] . "</td>";
echo "<td>" . $row['sub_block_name'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "<hr>";
}
else
{
echo "<br>";
echo "<h3 align='center'>Sorry, No Available Information!</h3>";
echo "<hr>";
}
}
Now I like to create the table like this. Mostly I think it is a rowspan task.
+-------+---------+
|ward_id|Sub_block|
+-------+---------+
| | A1 |
+ +---------+
| 1 | B1 |
+ +---------+
| | C1 |
+-------+---------+
| | D1 |
+ +---------+
| 2 | E1 |
+ +---------+
| | F2 |
+-------+---------+
| | K1 |
+ +---------+
| 3 | G2 |
+ +---------+
| | I3 |
+-------+---------+
what kind of changes do I need to add in my actual php code? Please help me out. Thanks in advance.
Use Inner query is one of the good way also you can use right outer join if you want it to be more efficient.

PHP: Troubles on indexing a nested for-loop

I am trying to print a 3-dimensional array into a table. But the indexes are kinda fked up. When I use the following (psuedo)code:
...
<<print headers and stuff>>
for ( $i = 0; $i < count( $array ); i++) {
$itemArray = $array[i];
for ( $j = 0; $j < count( $itemArray; j++) {
$innerItem = $itemArray[j];
echo <<tr start + both indexes in td>>
foreach ($innerItem as $spec) {
echo <<td with item>>
}
echo <<tr stop>>
}
}
In this example I am using i as index for the outer array and j as an index for the inner array (pretty obvious).
The result I am getting from this is as follows:
| index i | index j | title1 | title2 |
| 0 | 0 | | |
| 1 | 0 | | |
| 2 | 0 | | |
| ... | ... | | |
Whilst I would expect:
| index i | index j | title1 | title2 |
| 0 | 0 | | |
| 0 | 1 | | |
| 1 | 0 | | |
| 1 | 1 | | |
| 1 | 2 | | |
| 2 | 0 | | |
| ... | ... | | |
The (original) full code is:
echo "<h1>Combat analysis</h1>";
echo '<table cellspacing="0" cellpadding="4" border="1"><tbody>';
echo "<tr><td>#Mon</td><td>#Att</td><td>DungLVL</td><td>CharLVL</td><td>Health</td><td>Weapon</td><td>No. potions</td></tr>";
for ($battleIndex = 0; $battleIndex < count($this->combatLog); $battleIndex++) {
$battle = $this->combatLog[$battleIndex];
for ($attackIndex = 0; $attackIndex < sizeof($battle); $attackIndex++) {
$attack = $battle[$attackIndex];
echo "<tr><td>" . $battleIndex . "</td><td>" . $attackIndex . "</td>";
foreach ($attack as $stat) {
echo "<td>" . $stat . "</td>";
}
echo "</tr>";
}
}
echo "</tbody></table>";
What is going wrong?
Tested your code and runs as expected. You should do a echo '<pre>'.print_r($this->combatLog).'</pre>'; and debug the array contents.
Also I would recommend you the following:
1) You can use foreach instead of for, example: foreach ($this->combatLog as $battleIndex => $battle)
2) If you're not sure that a array contains values you should first do a: if (is_array($this->combatLog) && count($this->combatLog) > 0)
3) For simplicity and code maintenance I would first loop the multi-dimensional array and turn it into a one dimension called $attacks containing a array per each attack indexed by keys that you can recognize, ej:
$attacks=array();
$attacks[]=array(
'Mon'=>$battleIndex,
'Att'=>$attackIndex,
'DungLVL'=>isset($stat[0])?$stat[0]:null,
'CharLVL'=>isset($stat[1])?$stat[1]:null,
'Health'=>isset($stat[2])?$stat[2]:null,
'Weapon'=>isset($stat[3])?$stat[3]:null,
'Potions'=>isset($stat[4])?$stat[4]:null,
);
Then you could define some columns for example:
$columns=array(
'Mon',
'Att',
'DungLVL',
'CharLVL',
'Health',
'Weapon',
'Potions',
);
Then print the table header like this:
echo '<tr>';
foreach ($columns as $column) {
echo '<td>'.$column.'</td>';
}
echo '</tr>';
And print rows like this:
foreach ($attacks as $attack) {
echo '<tr>';
foreach ($columns as $column) {
echo '<td>'.$attack[$column].'</td>';
}
echo '</tr>';
}

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

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

Replace empty row using Mysql With 'IN' condition

This is my code
$Qemaster="select * from emaster where `branch`='$bid' and `department`='$did' and `status`!='L'";
$Remaster=mysql_query($Qemaster);
while($Rowemaster=mysql_fetch_array($Remaster)){
$empcode=$Rowemaster[id];
$name=$Rowemaster[name];
$Tleave=0;
echo "<tr>";
echo "<td rowspan='2'>".$name."</td>";
echo "<td>Leave</td>";
$Qlp="select `leave` from lpsummary where ((`month` IN(04,05,06,07,08,09,10,11,12) and `year`='$year') or (`month` IN(01,02,03) and `year`='$Nyear')) and `empcode`='$empcode'";
$Rlp=mysql_query($Qlp);
while($Rowlp=mysql_fetch_array($Rlp)){
$leave=$Rowlp['leave'];
$Tleave=$Tleave+$leave;
echo "<td>".$leave."</td>";
}
echo "<td><font color='red'>".$Tleave."</font></td>";
echo "<tr><td>Percentage</td>";
}
and my table is
------------------------------------------
| name | apr-12 | may-12 | jun-12 | jul-12 |
|------|--------|--------|--------|--------|
|Kumar | 2 | 1 | 0 | 3 |
|Rajan | 4 | 0 | 2 | |
| | | | | |
|------------------------------------------
Here under the name Rajan there is no data in jun-12 but jul-12 had the value 2...ie)empty row in the table lpsummary ...... if there is empty i wanna to replace it with as '-'... How can i do that by my code.....
In your while loop, you need to put a condition to check if a null from was returned.
while($Rowlp=mysql_fetch_array($Rlp)){
if (is_null($Rowlp['leave'])) {
$leave = '-';
} else {
$leave=$Rowlp['leave'];
$Tleave=$Tleave+$leave;
}
echo "<td>".$leave."</td>";
}

Categories