I need help with this for loop. I want it to print controls on the side of the row for each item listed but its listing 5 instead of 2 sets of controls.
<tbody>
<?php
//get list of supplies
$numOfRows = 0;
$result = mysqli_query($conn,"SELECT * FROM supplies");
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
foreach ($row as $item) {
echo '<td>' . $item . '</td>';
$numOfRows ++;
}
//controls
for ($i = 0;$i <= $numOfRows; $i++) {
echo '<td><a><i class="fas fa-edit"></i></a><a><i class="fas fa-trash-alt">
</i></a></td>';
}
}
echo '</tr>';
?>
</tbody>
review your code, second for is using numOfRows variable which is not right at all, you are iterating as many rows your table have is not just 5 times, if you have 100 records then you'll see that TD printed 100 times, BTW you can put those 2 buttons into a single TD that way you will not need the second for at all
I don't think there is another loop required for controls. Try this i hope it'll help you out. Thanks
<?php
$result = mysqli_query($conn,"SELECT * FROM supplies");
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
foreach ($row as $item) {
echo '<td>' . $item . '</td>';
echo '<td><a><i class="fas fa-edit"></i></a><a><i class="fas fa-trash-alt">
</i></a></td>';
}
}
}
echo '</tr>';
?>
If you want each item on it's own row showing the item ($item) in the first column and the edit/delete links in the 2nd column you only need the while to go through each row found.
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo '<td>' . $item . '</td>';
echo '<td>
<a><i class="fas fa-edit"></i></a>
<a><i class="fas fa-trash-alt"></i></a>
</td>';
echo '</tr>';
}
// In your code you have the
// "<tr>" element inside of your loop.
// It needs to come before your loop. Try the code below
// This is where you need <tr>
echo "<tr>";
while ($row = mysqli_fetch_assoc($result)) {
// this is where you had tr // echo <tr>
foreach ($row as $item) {
echo '<td>' . $item . '</td>';`enter code here`
$numOfRows ++;
}
//controls
for ($i = 0;$i <= $numOfRows; $i++) {
echo '<td><a><i class="fas fa-edit"></i></a><a><i class="fas fa-trash-alt">
</i></a></td>';
}
}
echo '</tr>';
Related
Does it overwrite the $line array? If yes, will renaming the array fix the problem? Furthermore, how can I call data from specific fields in this array through jquery?
I need to make an INTERACTIVE VISUAL to represent all the data being read from these files. How can I go about doing this?
This is the php code:
<?php
echo "<html><body><table>\n\n";
$f = fopen("LoanStats3a.csv", "r");
while (($line = fgetcsv($f)) !== false) {
echo "<tr>";
foreach ($line as $cell) {
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
}
fclose($f);
echo "\n</table></body></html>";
?>
<?php
echo "<html><body><table>\n\n";
$g = fopen("LoanStats3b.csv", "r");
while (($line = fgetcsv($g)) !== false) {
echo "<tr>";
foreach ($line as $cell) {
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
}
fclose($g);
echo "\n</table></body></html>";
?>
<?php
echo "<html><body><table>\n\n";
$h = fopen("LoanStats3c.csv", "r");
while (($line = fgetcsv($h)) !== false) {
echo "<tr>";
foreach ($line as $cell) {
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
}
fclose($h);
echo "\n</table></body></html>";
?>
<?php
echo "<html><body><table>\n\n";
$i = fopen("RejectStatsA.csv", "r");
while (($line = fgetcsv($i)) !== false) {
echo "<tr>";
foreach ($line as $cell) {
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
}
fclose($i);
echo "\n</table></body></html>";
?>
<?php
echo "<html><body><table>\n\n";
$j = fopen("RejectStatsB.csv", "r");
while (($line = fgetcsv($j)) !== false) {
echo "<tr>";
foreach ($line as $cell) {
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
}
fclose($j);
echo "\n</table></body></html>";
?>
I want to get simple fb graph data and load that into a table with php, but somehow i receive 4 same error messages why so ever and I dont know what they mean.
Notice: Undefined offset: 0 in index.php on line 52
Notice: Undefined offset: 0 in index.php on line 55
Notice: Undefined offset: 0 in index.php on line 59
Notice: Undefined offset: 0 in index.php on line 60
Here are the lines where the error / warnings appear ..
echo "<table class='table table-hover table-responsive table-bordered'>";
//count the number of events
$events_count = count($obj['data']);
for($x=0; $x<$events_count; $x++){
//fb events will be here
}
echo"</table>";
$start_date = date( 'l, F d, Y', strtotime($obj['data'][$x]['start_time']));
// in my case, I had to subtract 9 hours to sync the time set in facebook
$start_time = date('g:i a', strtotime($obj['data'][$x]['start_time']) - 60 * 60 * 9);
$pic_big = isset($obj['data'][$x]['cover']['source']) ? $obj['data'][$x]['cover']['source'] : "https://graph.facebook.com/{$fb_page_id}/picture?type=large";
$eid = $obj['data'][$x]['id'];
$name = $obj['data'][$x]['name'];
$description = isset($obj['data'][$x]['description']) ? $obj['data'][$x]['description'] : "";
// place
$place_name = isset($obj['data'][$x]['place']['name']) ? $obj['data'][$x]['place']['name'] : "";
$city = isset($obj['data'][$x]['place']['location']['city']) ? $obj['data'][$x]['place']['location']['city'] : "";
$country = isset($obj['data'][$x]['place']['location']['country']) ? $obj['data'][$x]['place']['location']['country'] : "";
$zip = isset($obj['data'][$x]['place']['location']['zip']) ? $obj['data'][$x]['place']['location']['zip'] : "";
$location="";
if($place_name && $city && $country && $zip){
$location="{$place_name}, {$city}, {$country}, {$zip}";
}else{
$location="Location not set or event data is too old.";
}
echo "<tr>";
echo "<td rowspan='6' style='width:20em;'>";
echo "<img src='{$pic_big}' width='200px' />";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td style='width:15em;'>What:</td>";
echo "<td><b>{$name}</b></td>";
echo "</tr>";
echo "<tr>";
echo "<td>When:</td>";
echo "<td>{$start_date} at {$start_time}</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Where:</td>";
echo "<td>{$location}</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Description:</td>";
echo "<td>{$description}</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Facebook Link:</td>";
echo "<td>";
echo "<a href='https://www.facebook.com/events/{$eid}/' target='_blank'>View on Facebook</a>";
echo "</td>";
echo "</tr>";
?>
Thanks for help and fast answer
Add your code inside loop.
Try
<?php
echo "<table class='table table-hover table-responsive table-bordered'>";
//count the number of events
$events_count = count($obj['data']);
for($x=0; $x<$events_count; $x++)
{
//fb events will be here
$start_date = date( 'l, F d, Y', strtotime($obj['data'][$x]['start_time']));
// in my case, I had to subtract 9 hours to sync the time set in facebook
$start_time = date('g:i a', strtotime($obj['data'][$x]['start_time']) - 60 * 60 * 9);
$pic_big = isset($obj['data'][$x]['cover']['source']) ? $obj['data'][$x]['cover']['source'] : "https://graph.facebook.com/{$fb_page_id}/picture?type=large";
$eid = $obj['data'][$x]['id'];
$name = $obj['data'][$x]['name'];
$description = isset($obj['data'][$x]['description']) ? $obj['data'][$x]['description'] : "";
// place
$place_name = isset($obj['data'][$x]['place']['name']) ? $obj['data'][$x]['place']['name'] : "";
$city = isset($obj['data'][$x]['place']['location']['city']) ? $obj['data'][$x]['place']['location']['city'] : "";
$country = isset($obj['data'][$x]['place']['location']['country']) ? $obj['data'][$x]['place']['location']['country'] : "";
$zip = isset($obj['data'][$x]['place']['location']['zip']) ? $obj['data'][$x]['place']['location']['zip'] : "";
$location="";
if($place_name && $city && $country && $zip){
$location="{$place_name}, {$city}, {$country}, {$zip}";
}else{
$location="Location not set or event data is too old.";
}
echo "<tr>";
echo "<td rowspan='6' style='width:20em;'>";
echo "<img src='{$pic_big}' width='200px' />";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td style='width:15em;'>What:</td>";
echo "<td><b>{$name}</b></td>";
echo "</tr>";
echo "<tr>";
echo "<td>When:</td>";
echo "<td>{$start_date} at {$start_time}</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Where:</td>";
echo "<td>{$location}</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Description:</td>";
echo "<td>{$description}</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Facebook Link:</td>";
echo "<td>";
echo "<a href='https://www.facebook.com/events/{$eid}/' target='_blank'>View on Facebook</a>";
echo "</td>";
echo "</tr>";
}
echo"</table>";
?>
You should close for correctly.
You can't use $x var out of for.
Your for loop is:
for($x=0; $x<$events_counts; $x++) {
// fb events will be here
}
// other code below
// $x is not available here!
So, basically you have } and you're trying to access $x which is declarated (unless it's global) in scope of for, but it doesn't exists below closing bracket. Put your code in for loop and it should works.
I need to check/uncheck a table checkbox based on a value stored in a database. I'm not an experienced programmer. I've been Google-ing this for a whole day with no luck. Hope someone's going to help me.
Here's the code :
echo "<table data-toggle='table' data-click-to-select='true' id='potable'>";
echo "<thead>";
echo "<tr>";
echo "<th data-field='state' data-checkbox='true'></th>";
echo "<th data-field='code'>Code</th>";
echo "<th data-field='description'>Description</th>";
echo "<th data-field='verb'>Verb</th>";
echo "<th>Actions</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
echo "<tr>";
echo "<td>"; ?>
<script>
var select = <?php echo "{$selected}"; ?>;
if (select == 1) {
// INSERT HERE JAVASCRIPT LINE THAT CHECKS THE CURRENT CHECKBOX
else {
//UNCHECK THE CURRENT CHECKBOX
}
</script>
<?php
echo "</td>";
echo "<td>{$code}</td>";
echo "<td>{$description}</td>";
echo "<td>{$verb}</td>";
echo "<a type='button' class='btn btn-primary left-margin' href='po-info.php?id={$id}'>Info</a>";
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
Finally managed to make it work
Used
echo "<th data-field='selected' data-checkbox='true' data-formatter='stateFormatter'></th>";
then
<script>
function stateFormatter(value, row, index) {
if (value == 1) {
return {
checked: true
};
} else {
return {
checked: false
};
}
return value;
}
</script>
Try
$("th[data-checkbox='true'] input[type=checkbox]").attr('checked', <?=$selected?'true':'false'?>);
<table>
<?php
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
$checked_ids = array();
$checked_ids[] = $id;
$chk_str = '<input data-index="%s" name="chk" type="checkbox">';
echo "<tr>";
echo "<td>";
printf($chk_str, $id);
echo "</td>";
echo "</tr>";
}
?>
</table>
<input type="hidden" id="db_checked" value="<?php echo implode(",",$checked_ids); ?>" />
<script>
// need jquery.js
$(function () {
var $table = $('#potable'),
var string = $('#db_checked').val();
var array = string.split(',');
for (var ck_id in array) {
$table.bootstrapTable('check', parseInt(ck_id, 10));
// check or uncheck
}
}
</script>
maybe "data-index" is write at "tr" tags
I guys, I need yo help for this. I have this code, wich shows me the products I have in my shopping cart:
for($i=0;$i<count($_SESSION['productos']);$i++)
{
$id = $_SESSION['productos'][$i];
$prods = mysql_query('SELECT * from productos where idprod='.$id.'');
$row = mysql_fetch_array($prods);
echo "<tr>";
echo "<td>" . $row['nombre']; echo "</td>";
if($_GET[action]=="suma")
{
$_SESSION['unidades'][$i] = $_SESSION['unidades'][$i] + 1;
}
elseif($_GET[action]=="resta")
{
$_SESSION['unidades'][$i] = $_SESSION['unidades'][$i] - 1;
}
echo "<td><input name=".$i." type='text' value=" . $_SESSION['unidades'][$i]; echo " size='5'/></td>";
echo "<td><a href='carro_detalle.php?action=suma'><img src='images/flecharriba.png' width='10x' height='10px'/></a></td>";
echo "<td><a href='carro_detalle.php?action=resta'><img src='images/flechabajo.png' width='10px' height='10px'/></a></td>";
echo "<td>" . $row['precio']; echo "</td>";
echo "<td>" . $row['precio'] * $_SESSION['unidades'][$i]; echo "</td>";
echo "</tr>";
}
I need to sum 1 unity or rest 1 unity to $_SESSION['unidades'][$i] just to the selected product, when I click the two images respectively.
The thing is that when I click, it adds me 1 to all the products. Any easy way of doing this without using $_GET vars? I dont have high knowledge about JavasScript.
Thanks!!
You probably should remove your condition from the loop and put it outside of it:
if($_GET[action]=="suma")
{
$_SESSION['unidades'][$i] = $_SESSION['unidades'][$i] + 1;
}
elseif($_GET[action]=="resta")
{
$_SESSION['unidades'][$i] = $_SESSION['unidades'][$i] - 1;
}
for($i=0;$i<count($_SESSION['productos']);$i++)
{
$id = $_SESSION['productos'][$i];
$prods = mysql_query('SELECT * from productos where idprod='.$id.'');
$row = mysql_fetch_array($prods);
echo "<tr>";
echo "<td>" . $row['nombre']; echo "</td>";
echo "<td><input name=".$i." type='text' value=" . $_SESSION['unidades'][$i]; echo " size='5'/></td>";
echo "<td><a href='carro_detalle.php?action=suma'><img src='images/flecharriba.png' width='10x' height='10px'/></a></td>";
echo "<td><a href='carro_detalle.php?action=resta'><img src='images/flechabajo.png' width='10px' height='10px'/></a></td>";
echo "<td>" . $row['precio']; echo "</td>";
echo "<td>" . $row['precio'] * $_SESSION['unidades'][$i]; echo "</td>";
echo "</tr>";
}
I want to create a javascript that will change the value of anotif to 0 once clicked:
Approved:<?php if($anotif<1){
echo 0;
} else {
echo '<a class="anotif" onClick="validator()" href="?anotif">'.$anotif.'</a>';
} ?><br/>
But I am not sure how do that, considering that I'm so new in javascript. I've googled around a bit but I wasn't able to find the answer.
function validator(){
if(document.anotif.clicked){ // I'm not really sure if this is correct.
// if clicked, change value to zero
}
To be more precise, I want it to look like this once clicked:
From:
Approved:3
To:
Approved:0
Approved:<?php if($anotif<1){
echo 0;
} else {
echo '<a class="anotif" onClick="validator()" href="?anotif">'.$anotif.'</a>';
} ?><br/>
This code will lead me to ?anotif for me to view some data from the database in a table. Once approved: is clicked, it must be set to zero at the same time I move to the other page.
The problem is it requires refreshing for it to be set to 0 by the system.
Here is the rest of the code:
$firstname = getuserfield('txtFname');
$lastname = getuserfield('txtLname');
echo 'Hello '.$firstname.' '.$lastname.'.<br/>';
$anotif = 0;
$dnotif = 0;
$anotif = $anotif + getuserfield('approved_notif');
$dnotif = $dnotif + getuserfield('disapproved_notif');
?>
<h3>Notifications:</h3>
Approved:<?php if($anotif<1){echo 0;} else { echo '<a class="anotif" onClick="validator(this);" href="?anotif">'.$anotif.'</a>';}?><br/> //problem is over here
<?php
if(isset($_GET['anotif'])) {
$query = "SELECT * FROM hrf_leave WHERE empid = '$empid' AND formStatus = 1 AND checked = 0";
$query_run = mysql_query($query) or die($query."<br/><br/>".mysql_error());
echo "<table border=1>
<tr>
<th>Type of Leave</th>
<th>Specific Reason</th>
<th>Date From</th>
<th>Date To</th>
<th>Number of Days</th>
</tr>";
echo "<tr>";
while($record = mysql_fetch_array($query_run)){
$leave_id = $record['leave_id'];
echo "<td>" . $record['type_of_leave'] . "</td>";
echo "<td>" . $record['specific_reason'] . "</td>";
echo "<td>" . $record['date_from'] . "</td>";
echo "<td>" . $record['date_to'] . "</td>";
echo "<td>" . $record['num_of_days'] . "</td>";
echo "</tr>";
$query2 = "UPDATE hrf_leave SET checked=1 WHERE leave_id = $leave_id";
if($query_run2 = mysql_query($query2)){
$anotif = 0;
$query3 = "UPDATE hrms_emp_info SET approved_notif=$anotif WHERE empid = $empid";
if($query_run3 = mysql_query($query3)){
}
}
}
echo "</table>";
}
?>
Disapproved:<?php if($dnotif<1){echo 0;} else { echo ''.$dnotif.'<br/>';} ?> //and here
<?php
if(isset($_GET['dnotif'])) {
$query = "SELECT * FROM hrf_leave WHERE empid = '$empid' AND formStatus = 2 AND checked=0";
$query_run = mysql_query($query);
echo "<table border=1>
<tr>
<th>Type of Leave</th>
<th>Specific Reason</th>
<th>Date From</th>
<th>Date To</th>
<th>Number of Days</th>
</tr>";
echo "<tr>";
while($record = mysql_fetch_array($query_run)){
$leave_id = $record['leave_id'];
echo "<td>" . $record['type_of_leave'] . "</td>";
echo "<td>" . $record['specific_reason'] . "</td>";
echo "<td>" . $record['date_from'] . "</td>";
echo "<td>" . $record['date_to'] . "</td>";
echo "<td>" . $record['num_of_days'] . "</td>";
echo "</tr>";
$query2 = "UPDATE hrf_leave SET checked=1 WHERE leave_id = $leave_id";
if($query_run2 = mysql_query($query2)){
$dnotif = $dnotif-1;
$query3 = "UPDATE hrms_emp_info SET disapproved_notif=$dnotif WHERE empid = $empid";
if($query_run3 = mysql_query($query3)){
}
}
}
echo "</table>";
}
You could change your anchor a bit like this:
<a class="anotif" onClick="return validator(this)" href="?anotif">
Then change the validator() function like this:
function validator(element)
{
// element is clicked already, so you can change its value
element.innerHTML = '0';
// prevent click from changing page
return false;
}
However, the real problem here is that the page will be changed to ?anotif and whatever you changed will be wasted effort.
Updated the code with a way to prevent the click from changing the page and just reducing the number to '0'.