Use variables in PHP foreach loop - php

I use a foreach loop in php to fill a table, after that I use id and data-id to work with the line of the table.
The problem is that I can't figured how to increment a variable during the loop and add it in the id like this :
id='addr0' data-id="0",
id='addr1' data-id="1"
// etc.
Here is my loop :
foreach($result as $key => $value): ?>
<tr id='addr0' data-id="0">
<td><?php echo $value['Nom']; ?></td>
<td><?php echo $value['Prenom']; ?></td>
<td><?php echo $value['Adresse']; ?></td>
<td><?php echo $value['Date de naissance']; ?></td>
<td><?php echo $value['Numero de telephone']; ?></td>
<td data-name="del">
<button nam"del0" class='btn btn-danger glyphicon glyphicon-remove row-remove'></button>
</td>
</tr>
<?php endforeach; ?>

Try the following.
$counter = 0;
foreach($result as $key => $value): ?>
<tr id='addr<?php echo $counter?>' data-id="<?php echo $counter?>">
<td><?php echo $value['Nom']; ?></td>
<td><?php echo $value['Prenom']; ?></td>
<td><?php echo $value['Adresse']; ?></td>
<td><?php echo $value['Date de naissance']; ?></td>
<td><?php echo $value['Numero de telephone']; ?></td>
<td data-name="del">
<button name = "del<?php echo $counter?>" class='btn btn-danger glyphicon glyphicon-remove row-remove'></button>
</td>
</tr>
<?php
$counter++;
endforeach;
?>

You can get auto increment in two way.
Case 1 : If your $result is indexed array.
foreach($result as $key => $value): ?>
<tr id="addr<?php echo $key ?>" data-id="<?php echo $key ?>">
<td><?php echo $value['Nom']; ?></td>
<td><?php echo $value['Prenom']; ?></td>
<td><?php echo $value['Adresse']; ?></td>
<td><?php echo $value['Date de naissance']; ?></td>
<td><?php echo $value['Numero de telephone']; ?></td>
<td data-name="del">
<button nam"del<?php echo $key ?>" class='btn btn-danger glyphicon glyphicon-remove row-remove'></button>
</td>
</tr> <?php endforeach; ?>
Case 2 : If your $result is associative array.
<?php
$i = 0;
foreach($result as $key => $value): ?>
<tr id="addr<?php echo $i ?>" data-id="<?php echo $i ?>">
<td><?php echo $value['Nom']; ?></td>
<td><?php echo $value['Prenom']; ?></td>
<td><?php echo $value['Adresse']; ?></td>
<td><?php echo $value['Date de naissance']; ?></td>
<td><?php echo $value['Numero de telephone']; ?></td>
<td data-name="del">
<button nam"del<?php echo $i ?>" class='btn btn-danger glyphicon glyphicon-remove row-remove'></button>
</td>
</tr>
<?php
$i++;
endforeach;
?>

Related

How can populate table for status with php mysql query with 1 or 0?

I want to show a status of active and inactive with php, and the value for active is 1 and for inactive is 0; How can i pu $status = ($row['is_active'])?'<span>Active</span>':'<span>Deactivited</span>'; variable in <td><?php echo $row->role; ?> <?php echo $status; ?></td>
here is my code
<?php
$status = ($row['is_active'])?'<span class="label label-success pull-right">Active</span>':'<span class="label label-danger pull-right">Deactivited</span>';
$no = 1;
$select = $pdo->prepare("SELECT * FROM user");
$select->execute();
while($row=$select->fetch(PDO::FETCH_OBJ)){
?>
<tr>
<td><?php echo $no++; ?></td>
<td><?php echo $row->username; ?></td>
<td><?php echo $row->fullname; ?></td>
<td><?php echo $row->role; ?> <?php echo $status; ?></td>
<td>
<a href="#"
onclick="return confirm('Delete User?')"
class="btn"</a>
</td>
</tr>
<?php
}
?>
Just move the check for the $row['is_active'] into the for loop.
Here's an implementation.
<?php
$no = 1;
$select = $pdo->prepare("SELECT * FROM user");
$select->execute();
while ($row = $select->fetch(PDO::FETCH_OBJ)) {
$status = ($row->is_active) ? '<span class="label label-success pull-right">Active</span>' : '<span class="label label-danger pull-right">Deactivited</span>';
?>
<tr>
<td><?php echo $no++; ?></td>
<td><?php echo $row->username; ?></td>
<td><?php echo $row->fullname; ?></td>
<td><?php echo $row->role; ?><?php echo $status; ?></td>
<td>
<a href="#"
onclick="return confirm('Delete User?')"
class="btn"</a>
</td>
</tr>
<?php
}
?>

Id from PHP not being passed to URL

I have some data displayed from SQL to PHP, when the user clicks the eye icon, he is redirected to another page with id on the URL, I did the following code
<tr>
<td><?php echo $record['id']; ?></td>
<td><?php echo $record['firstname'];?></td>
<td><?php echo $record['Email'];?></td>
<td><?php echo $record['mobilenumber']?></td>
<td><?php echo $record['company']?></td>
<td><?php echo $record['designation']?></td>
<td><?php echo $record['state']?></td>
<td><i class="fa fa-eye" aria-hidden="true"></i></td>
</tr>
<?php } ?>
Instead of the id, some special characters are coming in the URL, how can I fix it?
Below is how your anchor tag should be.
<i class="fa fa-eye" aria-hidden="true"></i>
You need to enclose the $record variable inside php tags and echo it.
You should echo it, like you did with another values:
<?php echo $record['id']; ?>
Full code:
<tr>
<td><?php echo $record['id']; ?></td>
<td><?php echo $record['firstname'];?></td>
<td><?php echo $record['Email'];?></td>
<td><?php echo $record['mobilenumber']?></td>
<td><?php echo $record['company']?></td>
<td><?php echo $record['designation']?></td>
<td><?php echo $record['state']?></td>
<td><i class="fa fa-eye" aria-hidden="true"></i></td>
</tr>
<?php } ?>
You should echo ID
as below
<a href="detail.php?id=<?= $record['id'] ?>\">

Bootstrap table not cropping for tablet devices

I'm using Twitter-Bootstrap for a table. The table works fine for bigger screens and for mobile. For tablet it doesn't work. What I want is the following:
The image above is how it is on mobile. I want this too for tablet, but when I select tablet resolution on Chrome I get this:
It's not responsive. How can I fix this?
Code:
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>Breedte</th>
<th>Hoogte</th>
<th>Doekkleur</th>
<th>Omkastingkleur</th>
<th>Bedieningtype</th>
<th>Bedieningkant</th>
<th>Uitvoer</th>
<th>Geleider links</th>
<th>Geleider rechts</th>
<th>Opvulling</th>
<th>Onderlat-type</th>
<th>Extra</th>
<th>Prijs</th>
<th>Actie</th>
</tr>
</thead>
<tbody>
<?php foreach ($result as $offer) { ?>
<tr>
<td><?php echo $offer['width'] ?></td>
<td><?php echo $offer['height'] ?></td>
<td><?php echo $offer['clothingcolor'] ?></td>
<td><?php echo $offer['housingcolor'] ?></td>
<td><?php echo $offer['controltype'] ?></td>
<td><?php echo $offer['controlside'] ?></td>
<td><?php echo $offer['output'] ?></td>
<td><?php echo $offer['conductorleft'] ?></td>
<td><?php echo $offer['conductorright'] ?></td>
<td><?php echo $offer['stuffing'] ?></td>
<td><?php echo $offer['underlattype'] ?></td>
<td><?php echo $offer['extra'] ?></td>
<td><?php echo $offer['price'] ?></td>
<td><button type="submit" class="btn btn-link" name="getValue" data-toggle="modal" data-target=".editProduct<?php echo $offer['id']; ?>" data-id="<?php echo $offer['id']; ?>"><i class="fa fa-pencil" aria-hidden="true" style="color: red;"></i></a></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>

checkbox error in cakephp

<?php
$i= 0;
foreach($purchaseorder as $tdata):
$i++;
?>
<tr >
<td><?php echo $tdata['pay_date']; ?></td>
<td><?php echo $tdata['cylinder']; ?></td>
<td><?php echo $tdata['amount']; ?></td>
<td><?php echo $tdata['rtgs_no']; ?></td>
<td><?php echo $tdata['cheque_no']; ?></td>
<td><?php echo $tdata['dd_no']; ?></td>
<td><?php echo if ($tdata['approve']=='true')
{
?>
<img src="/../img/green.png" alt="alt-tag"/>;
<?php
}
else
{
?>
<img src="/../img/red.png" alt="alt-tag" />;
<?php } ?></td>
</tr>
<?php
endforeach;
?>
my code is when i check a checkbox and submit a green tick should be displayed in list page..if its not checked and submitted a red cross should be displayed..but i get a error in the above code..what sholud i do?
<td><?php if ($tdata['approve']=='true')
{ ?>
<img src="/../img/green.png" alt="alt-tag"/>
<?php }
else
{ ?>
<img src="/../img/red.png" alt="alt-tag" />
<?php } ?></td>
<?php
foreach($purchaseorder as $tdata):
?>
<tr >
<td><?php echo $tdata['pay_date']; ?></td>
<td><?php echo $tdata['cylinder']; ?></td>
<td><?php echo $tdata['amount']; ?></td>
<td><?php echo $tdata['rtgs_no']; ?></td>
<td><?php echo $tdata['cheque_no']; ?></td>
<td><?php echo $tdata['dd_no']; ?></td>
<td><?php if($tdata['approve']==true)
{
?>
<img src="/../img/green.png" alt="alt-tag"/>;
<?php
}
else
{ ?>
<img src="/../img/red.png" alt="alt-tag" />;
<?php
} ?>
</td>
</tr>
<?php
endforeach;
?>
try this code and if possible show me the data thats comes on "$purchaseorder" variable
First make var_dump($tdata) and look 'approve' index value. If is set $tdata['approve'] then change code:
<td><?php if ($tdata['approve']=='true')
{ ?>
<img src="/../img/green.png" alt="alt-tag"/>
<?php }
else
{ ?>
<img src="/../img/red.png" alt="alt-tag" />
<?php } ?></td>
for this
<?php
$approveImg = ($tdata['approve']=='true') ? 'green.png' : 'red.png';
?>
<td>
<img src="/../img/<?=$approveImg?>" alt="alt-tag"/>
</td>
Also check paths to images

Replace buttons with image in PHP code

I am trying to display images in place of buttons for various activities like Edit Record, Delete Record, Download, Save, Reset. I am able to show the image but the result is very output is looks very childish and nonprofessional. Actually images are coming on button as shown in image
But I want image to display like this
Here is the code which gives the 1st image output.
<tr style="background-color: rgb(253, 253, 183); color: rgb(42, 16, 179);font-size: 13px;" >
<td><?php echo $row->eq_application_no; ?></td>
<td><?php echo $row->eq_class; ?></td>
<td><?php echo $row->eq_name;?></td>
<td><?php echo $row->mid_name; ?></td>
<td><?php echo $row->last_name; ?></td>
<td><?php echo $row->eq_sex; ?></td>
<td><?php echo $row->father_name; ?></td>
<td><?php echo $row->eq_dob; ?></td>
<td><?php echo $row->age.",".$row->month.",".$row->day;?></td>
<td><?php echo $row->scat_id;?></td>
<td><?php echo $row->parent_cate_id;?></td>
<td><?php echo $row->no_of_transfer;?></td>
<td><?php echo $tc_case; ?></td>
<td><?php echo$row->last_school_type; ?></td>
<td><?php echo $row->eq_prv_acdmic;?></td>
<td><?php if($row->kv_tc_date=="1970-01-01"){echo "-";}else{echo $row->kv_tc_date;}?></td>
<td><?php echo $row->kv_tc_no;?></td>
<td><?php echo $row->last_class_cgpa;?></td>
<!--value="Edit"-->
<td><input type="button" id="<?php echo $row->es_enquiryid;?>" onclick="edit_record(<?php echo $row->es_enquiryid;?>)" style="background-image:url(images/edit24.png);width:24px;height:24px;"></td><td>
<!--value="Delete"-->
<input type="button" id="<?php echo $row->es_enquiryid;?>" onclick="delete_record(<?php echo $row->es_enquiryid;?>)" style="background-image:url(images/delete24.png);width:24px;height:24px;" ></td><td><?php
if($eligible=="Y")
{?><input type="checkbox" class="app" name="app[]" d="
<?php echo $row->es_enquiryid;?>" value="<?php echo $row->es_enquiryid;?>">
<?php }
else
{
echo"-";
}
?></td>
</tr>
<?php } ?>
</tbody>
</table>
<table>
<tr>
Add image directly then:
<img src="images/delete24.png" id="<?php echo $row->es_enquiryid;?>" onclick="delete_record(<?php echo $row->es_enquiryid;?>)" style="cursor:pointer;width:24px;height:24px;" />

Categories