Foreach loop using php - php

I am encountering a problem when printing out the array that is stored inside $description.
Here is my code:
<?php foreach($descriptions as $description) { foreach ($languages as $language) { ?>
<div class="tabs-add language_id_<?php echo $language['language_id']; ?>" id="tab-language-<?php echo $module_row; ?>-<?php echo $language['language_id']; ?>">
<table id="general" class="form">
<tr>
<td><?php echo $entry_title; ?></td>
<td><input type="text" name="announcement_module[<?php echo $module_row; ?>][title][<?php echo $language['language_id']; ?>]" id="title-<?php echo $module_row; ?>-<?php echo $language['language_id']; ?>" value="<?php echo isset($module['title'][$language['language_id']]) ? $module['title'][$language['language_id']] : ''; ?>" /></td>
</tr>
<tr>
<td><?php echo $entry_description; ?> <a onclick="removeMessage(this);"><?php echo $button_remove; ?></a></td>
<td><textarea name="announcement_module[<?php echo $module_row; ?>][description][<?php echo $description_row;?>][<?php echo $language['language_id']; ?>]" id="description-<?php echo $module_row; ?>-<?php echo $description_row;?>-<?php echo $language['language_id']; ?>"><?php echo isset($module['description'][$description_row][$language['language_id']]) ? $module['description'][$description_row][$language['language_id']] : ''; ?></textarea></td>
</tr>
</table>
</div>
I want to print out the $entry_description as many times the user wants. But I can't seem to print out more than one at a time. How do I solve this?

Related

Getting extra blank spaces while printing table

I am trying to print a table using Datatable Jquery , and it is giving me some extra spaces unnecessary.
<tbody>
<?php foreach($products as $product): ?>
<tr class="data-item show">
<td class="one wide tdalign collapsing">
<div id="divwhlprodmttrcheck" class="ui checkbox">
<input id="cbwhlprodmttrcheck" type="checkbox" name="inventory[]" value="<?php echo $product->productId ?>">
<label style="padding-left:0;"></label>
</div>
</td>
<td id="tdwhlprodmtin<?php echo $product->productId ?>" ><?php echo $product->ProductName ?></td>
<td id="tdwhlprodmtinsku<?php echo $product->productId ?>"><?php echo $product->SKU ?></td>
<td id="tdwhlprodmtincat<?php echo $product->productId ?>"><?php echo $product->Category ?></td>
<td id="tdwhlprodmtinsc<?php echo $product->productId ?>"><?php echo $product->Subcategory ?></td>
<td id="tdwhlprodmtinbr<?php echo $product->productId ?>"><?php echo $product->BrandName ?>
#if($product->BrandDeleted == 1)
<i class="warning red circle icon brand" data-content="Brand has been deleted" data-variation="inverted"></i>
#endif
</td>
<td id="tdwhlprodmtinsp<?php echo $product->productId ?>"><?php echo $product->SupplierName ?>
#if($product->SuppDeleted == 1)
<i class="warning red circle icon supp" data-content="Supplier has been deleted" data-variation="inverted"></i>
#endif
</td>
<td id="tdwhlprodmtinst<?php echo $product->productId ?>"><?php echo $product->AvailableStock ?></td>
<td id="tdwhlprodmtinpr<?php echo $product->productId ?>"><?php echo $product->Price ?></td>
#if($product->Discount == 0)
<td id="tdwhlprodmtinpr<?php echo $product->productId ?>">NA</td>
#else
<td id="tdwhlprodmtinpr<?php echo $product->productId ?>"><?php echo $product->Discount ?>%</td>
#endif
<input type="hidden" id="inhidsuppmain{{$product->productId}}" value="{{$product->SupplierName}}" >
<input type="hidden" id="inhidbrndmain{{$product->productId}}" value="{{$product->BrandName}}">
<input type="hidden" id="tdwhlprodmtprice<?php echo $product->productId?>" value="<?php echo $product->Price ?>" name="price">
<input type="hidden" id="inhiddiscountmain{{$product->productId}}" value="{{$product->Discount}}" >
<input type="hidden" id="inhidIsOnSalemain{{$product->productId}}" value="{{$product->onsale}}" >
<input type="hidden" id="inhidIsOnClearancemain{{$product->productId}}" value="{{$product->onclearance}}" >
<input type="hidden" name="inID" class="inid<?php echo $product->productId ?>" id="tdwhlprodmtinid<?php echo $product->productId ?>" value="<?php echo $product->productId; ?>">
<td class="tdalign collapsing"><i class="link black write icon" data-content="edit" data-variation="inverted" name="<?php echo $product->productId ?>" id="tdwhlprodmt<?php echo $product->productId; ?>" onclick="produps(4); getinValues('<?php echo $product->productId ?>'); gettingMulid('<?php echo $product->productId ?>')"></i>
</td>
</tr>
<?php endforeach; ?>
If I remove the code between <tbody> there is no spaces while code inside gives me spaces as below
I have even tried balancing it with margin-bottom , but no result yet.
Any help is appreciated.

Display only one name for there respective details

[1] => Array
(
[spid] => 21
[name] => Gaurav
[partner_acc_name] => SkyTechMasters
)
[2] => Array
(
[spid] => 21
[name] => Gaurav
[partner_acc_name] => TestPartner
)
This is the array i have show above.
i want that the "Gaurav" name should be display one for the "Partner_acc_name".
Code-
<tr class="all_partner" for="<?php echo $get_partner_forecast_val['spid']; ?>">
<td for="<?php echo $get_partner_forecast_val['spid']; ?>"rowspan="<?php echo ($product_no*2)+2; ?>"><?php echo $get_partner_forecast_val['name'];?></td>
<td for="<?php echo $get_partner_forecast_val['spid']; ?>"rowspan="<?php echo ($product_no*2)+2; ?>"><?php echo $get_partner_forecast_val['partner_acc_name'];?></td>
<td for="<?php echo $get_partner_forecast_val['spid']; ?>" rowspan="<?php echo ($product_no*2)+2; ?>"><?php echo $get_partner_forecast_val['crm_acc_name'];?></td>
This is the code.
well i solve this answer now with the help of Jquery.
Try this:
<?php
$temp = array();
foreach($partner_forecast as $get_partner_forecast_val) {
?>
<tr class="all_partner" for="<?php echo $get_partner_forecast_val['spid']; ?>">
<td for="<?php echo $get_partner_forecast_val['spid']; ?>" rowspan="<?php echo ($product_no*2)+2; ?>">
<?php if(!in_array($get_partner_forecast_val['name'], $temp)) echo $get_partner_forecast_val['name'];?>
</td>
<td for="<?php echo $get_partner_forecast_val['spid']; ?>" rowspan="<?php echo ($product_no*2)+2; ?>">
<?php echo $get_partner_forecast_val['partner_acc_name'];?>
</td>
<td for="<?php echo $get_partner_forecast_val['spid']; ?>" rowspan="<?php echo ($product_no*2)+2; ?>">
<?php echo $get_partner_forecast_val['crm_acc_name'];?>
</td>
</tr>
<?php
if(!in_array($get_partner_forecast_val['name'], $temp))
temp[] = $get_partner_forecast_val['name']; // add name to array which has been displayed so that we can check in next record whether its nedd to diaply or not
}
?>

only showing certain level users

I am showing a list of users information. The users are set at different levels (1-5). I only want it to display information for users that are level 1. Here is basically the code.
<?php while ($rrows = mysql_fetch_array($rs_results)) {?>
<tr>
<td><input name="u[]" type="checkbox" value="<?php echo $rrows['id']; ?>" id="u[]"></td>
<td><?php echo $rrows['date']; ?></td>
<td><?php echo $rrows['user_level']; ?></td>
<td><?php echo $rrows['user_name'];?></td>
<td><?php echo $rrows['user_email']; ?></td>
<td> <span id="approve<?php echo $rrows['id']; ?>">
<?php if(!$rrows['approved']) { echo "Pending"; } else {echo "Active"; }?>
</span> </td>
<td><span id="ban<?php echo $rrows['id']; ?>">
<?php if(!$rrows['banned']) { echo "no"; } else {echo "yes"; }?>
</span>
</td>
</tr>
How would I say to display only information of users who's 'user_level' = '1'?
You could filter in two ways:
directly in the query, as indicated by #Fred
after fetching the recordset:
while ($rrows = mysql_fetch_array($rs_results)){
if ($rrows['user_level'] == 1){
// display as previously
You would need to put an if statement inside your while loop that checks the user level.
//php mysql while
If ($rrows['user_level'] == 1) {?>
//html code
}?>
Try put the row to if block, like in the following code. It's ugly practice. Will be better if You use condition in the mysql query.
<?php while ($rrows = mysql_fetch_array($rs_results)) {?>
<?if($rrows['user_level']==1){?>
<tr>
<td><input name="u[]" type="checkbox" value="<?php echo $rrows['id']; ?>" id="u[]"></td>
<td><?php echo $rrows['date']; ?></td>
<td><?php echo $rrows['user_level']; ?></td>
<td><?php echo $rrows['user_name'];?></td>
<td><?php echo $rrows['user_email']; ?></td>
<td> <span id="approve<?php echo $rrows['id']; ?>">
<?php if(!$rrows['approved']) { echo "Pending"; } else {echo "Active"; }?>
</span> </td>
<td><span id="ban<?php echo $rrows['id']; ?>">
<?php if(!$rrows['banned']) { echo "no"; } else {echo "yes"; }?>
</span> </td>
</tr>
<?} /*end if*/?>
<?} /*end while*/?>

Need help in editing a data in mysql using php

I've already searching about my problem for 2 days, but nothings happen.
I need your help to fix my problem in editing a data using mysql and php
prod_list.php
<?php
$result = mysql_query("SELECT * FROM product ORDER BY prod_id DESC");
while($row = mysql_fetch_array($result)) {
$image = $row['7'];
?>
<tr>
<td><?php echo $row["0"]; ?></td>
<td><?php echo $row["1"]; ?></td>
<td><?php echo $row["2"]; ?></td>
<td><?php echo $row["3"]; ?></td>
<td><?php echo $row["4"]; ?></td>
<td><?php echo $row["5"]; ?></td>
<td><?php echo $row["6"]; ?></td>
<td> <img src="\sns/uploads/<?php echo $image; ?>" width="30" height="25"></td>
<td><?php echo $row["8"]; ?></td>
<td><?php echo "<a href='index.php?page=9adnk3b8nc&prodid={$row['0']}' class='btn btn-info btn-fill btn-sm pull-right'>Edit</a>"; ?></td>
</tr>
<?php } ?>
When I click the edit. nothing shows up. this is my edit form ..i tried some other edit codes but all of these comes up an error : Undefined variable "prod_id" .
edit_product.php
<div class="content">
<?
$prod_id=$_GET["prod_id"];
$query="select * from product where prod_id='$prod_id'";
$result=mysql_query($query);
while ($row = mysql_fetch_array($result)) {
?>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
<?php
if(isset($_POST['submit']))
{
}
?>
</div>
<div class="col-md-8">
<div class="form-group">
<label>Product ID</label>
<input type="hidden" class="form-control" name="id" autocomplete="off" placeholder="Product Name" value="<?php echo $row['prod_id']; ?>">
</div>
</div>
Its because of variable mismatch.
The Edit link has parameter prodid and in edit_product.php it is $_GET["prod_id"]
Change $_GET["prod_id"] to $_GET["prodid"] and it should work.
Change
index.php?page=9adnk3b8nc&prodid={$row['0']}'
to
index.php?page=9adnk3b8nc&prod_id={$row['0']}'

PHP Stored Procedure error when calling sequentially or more than once

I have two menus in php that uses stored procedure. Let's name it Menu1 and Menu2. This code this for Menu1: This is also the code for Menu 2.
<?php
$sql=$mysqli->query("call selectproducts()");
$i=1;
while($row=mysqli_fetch_array($sql)){
$id=$row['prodid'];
$date=$row['prodname'];
$item=$row['proddescription'];
$qtyleft=$row['prodsupplier'];
$qty_sold=$row['proddate'];
$price=$row['prodprice'];
$sales=$row['prodquantity'];
if($i%2){
?>
<tr id="<?php echo $id; ?>" class="edit_tr">
<?php } else { ?>
<tr id="<?php echo $id; ?>" bgcolor="#f2f2f2" class="edit_tr">
<?php } ?>
<td class="edit_td">
<span class="text"><?php echo $date; ?></span>
</td>
<td>
<span class="text"><?php echo $item; ?></span>
</td>
<td>
<span class="text"><?php echo $qtyleft; ?></span>
</td>
<td>
<span id="last_<?php echo $id; ?>" class="text">
<?php
echo $qty_sold;
?>
</span>
<input type="text" value="<?php echo $rtrt; ?>" class="editbox" id="last_input_<?php echo $id; ?>"/>
</td>
<td>
<span id="first_<?php echo $id; ?>" class="text"><?php echo $price; ?></span>
<input type="text" value="<?php echo $price; ?>" class="editbox" id="first_input_<?php echo $id; ?>" />
</td>
<td>
<span class="text"><?php echo $dailysales; ?>
<?php
echo $sales;
?>
</span>
</td>
</tr>
<?php
$i++;
}
?>
My Problem is when i call stored procedure in Menu1 it works, but in Menu2 it has error.
Based on my research, this code might has error because I am calling the stored procedure sequentially.
How to modify this code to be able to call the stored procedure the second time around? I'm really confused with this one. It seems like I need to close the stored procedure after the execution of first before I can call stored procedure again. I really don't know how to do this.
I'm guessing that you're getting an "out of sync" error?
You need to release resources by calling close() on your result set before you can make another call to the database on the same connection. Since you named your result variable $sql, you need to make a call $sql->close().
For example:
<?php
if( $result = $mysqli->query( "call selectproducts()" ) ) {
$i = 1;
while( $row=mysqli_fetch_array( $result ) ) {
$id=$row[ 'prodid' ];
$date=$row[ 'prodname' ];
$item=$row[ 'proddescription' ];
$qtyleft=$row[ 'prodsupplier' ];
$qty_sold=$row[ 'proddate' ];
$price=$row[ 'prodprice' ];
$sales=$row[ 'prodquantity' ];
if( $i % 2 ) {
?>
<tr id="<?php echo $id; ?>" class="edit_tr">
<?php
} else {
?>
<tr id="<?php echo $id; ?>" bgcolor="#f2f2f2" class="edit_tr">
<?php
}
?>
<td class="edit_td"><span class="text"><?php echo $date; ?></span></td>
<td><span class="text"><?php echo $item; ?></span></td>
<td><span class="text"><?php echo $qtyleft; ?></span></td>
<td>
<span id="last_<?php echo $id; ?>" class="text">
<?php echo $qty_sold; ?>
</span>
<input type="text" value="<?php echo $rtrt; ?>" class="editbox" id="last_input_<?php echo $id; ?>"/>
</td>
<td>
<span id="first_<?php echo $id; ?>" class="text"><?php echo $price; ?></span>
<input type="text" value="<?php echo $price; ?>" class="editbox" id="first_input_<?php echo $id; ?>" />
</td>
<td>
<span class="text"><?php echo $dailysales; ?><?php echo $sales; ?></span>
</td>
</tr>
<?php
$i++;
}
$result->close();
}
?>

Categories