I want add a blank rows between the data to separated it on PHPExcel
the data is like this picture
i want to separate jhonny and barry with blank rows
How's the code to do that ?
A very rough example, but hope you'll understand the logic.
$user_id = 'no_possible_duplicate_user_id';
foreach(){
if($user_id != $current_iteration_user_id){
//code for new row here as a separator
}
// here is the code for displaying each record on an excel row
$user_id = $current_iteration_user_id;
}
check unique ID of user and separate by push blank array or null value.
$id= null;
foreach ($variable as $key => $value) {
if ($id == $value->id || $id == null) {
$insert[] = array(); //insert data
}esle{
$insert[] = array();//balank array
$insert[] = array(); //insert data
}
$id = $value->id;
}
Related
As you can see I have a majority of the step done. My goal is to create a basic shopping list using 0 for not checked off, and 1 for checked off. If a user inputs the name of the list and the item they wish check off the list the 0 will be replaced by a 1. I know I have to loop over each row in the csv and then edit the item the user inputs in the terminal. However I think I did something slightly wrong in the while loop and need help on where to fix. Here is my code:
<?php
$args = $argv;
if(!isset($args[2])) {
echo 'Not given a shopping list title or item to edit!' . PHP_EOL;
exit(1);
}
$listName = $args[1];
$fileName = $listName . '.csv';
$path = __DIR__ . '/../storage/' . $fileName;
$resource = fopen($path, 'a+');
$item = $args[2];
$rows = [];
while(($row = fgetcsv($resource)) !== false) {
$rows[] = $row;
if($rows === $item) {
$item[1] = 1;
}
while($rows == 0) {
fputcsv($resource, $rows);
}
}
fclose($resource);
Here is my csv:
bananas,0
apples,0
lettuce,0
potatoes,0
pasta,0
rice,0
tofu,0
tempeh,0
berries,0
For example if the user wishes to check off rice the 0 will be replaced with a 1.
Edit: The code works up until if($rows === $item) statement. What I am doing with while(($row=fgetcsv($resource)) !== false) { $rows[] = $row; } is get each row of the csv in one array because when I ran the code I only got the first line of the code. And then I add it to the empty rows array to make each entry (there are 9 of them to be in the same array. So when a user wishes to check an item off the list each item will be in the same array and easier to refer to.
This part
if($rows === $item) {
$item[1] = 1;
}
while($rows == 0) {
fputcsv($resource, $rows);
}
I am unsure about because I want to target the 1st index point of whatever item the user types in and wants to check off.
I have an array data where I am storing the result of an SQL query as below :
$stmt = sqlsrv_query($db,$sql);
$data = [];
while($row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)) {
if(!empty($row)) { $data[] = $row; } }
then I want to create a group key which is the concatenation of specific keys of my array data as below :
foreach ($data as $d) {
$group_key = $d['id'].'_'.$d['Country'].'_'.$d['Order Numer'];
//rest of my code
}
it works fine but I want to choose the keys dynamically instead of setting up manually id, Country and Order Number...
let's say I have an array $PostKeys = ["id","Country","Order Number"]; this array will vary depending on the values selected by user...
What I did is :
$PostKeys = ["id","Country","Order Number"];
foreach ($data as $d) {
foreach($PostKeys as $value)
{ $array_group_key[] = $d[$value] ; }
$group_key = implode("_",$array_group_key);
// rest of my code
}
I am supposed to get the same result but there is always mismatch. I didn't figure out where is the issue exactly. Any suggestions please ? Thank you very much.
You need to empty $array_group_key each time through the loop. Otherwise, you're appending to the results from all the previous rows.
foreach ($data as $d) {
$array_group_key = [];
foreach($PostKeys as $value)
{
$array_group_key[] = $d[$value] ;
}
}
I develop a module under prestashop and I am looking to record a multidimensional table in a bdd.
I have a vendor column with a unique ID and row groups with a unique ID.
I have to fill in information in each box:
when I fill in some input the var_dump returns me this:
The first number is the group ID and the second number is the supplier ID.
I am trying to save this information in a table that looks like this:
To save it I would go through a class that I created new Objectif()
Here is the code I have already done. but I'm blocking on the recordings:
public function postProcess()
{
$obj = new Objectif();
if (Tools::isSubmit('objectif')) {
foreach ($_POST as $k => $item) {
$explo = explode('_', $k);
$group_id = $explo[0];
$supplier_id = $explo[1];
if ($group_id != '' && $supplier_id != '' && $item != '') {
$obj->id_group = $group_id;
$obj->id_supplier = $supplier_id;
$obj->objs = $item;
$obj->save();
}
}
}
}
Thank you for help.
Assuming that you always have this pattern group_id _ supplier_id:
foreach ($_POST as $k => $item) {
$obj = new Objectif();
$explo = explode('_', $k);
$group_id = $explo[0];
$supplier_id = $explo[1];
if ($group_id != '' && $supplier_id != '' && $item != '') {
$obj->id_group = $group_id;
$obj->id_supplier = $supplier_id;
$obj->objs = $item;
$obj->save();
}
}
Now you have all information you need.
When reading a sheet with phpExcel using the toArray method, hidden rows are also parsed.
Is there a method I can use before toArray to remove hidden rows?
Code so far, using Codeigniter
$this->load->library('excel');
$objPHPExcel = PHPExcel_IOFactory::load($upload_data['full_path']);
foreach ($objPHPExcel->getAllSheets() as $sheet) {
$sheets[$sheet->getTitle()] = $sheet->toArray();
}
$data = array();
foreach ($sheets['Data'] as $key => $row) {
if ($key > 0) {
$item = array();
$item['name'] = $row[1];
$item['code'] = $row[2];
$data[] = $item;
}
}
When converting the sheet to Array using PHPExcel_Worksheet::toArray you will get all of the rows, regardless if they are visible or not.
If you want to filter only the visible rows you will have to iterate over the rows and check for each of them if they are visible or not. You can check the visibility of a row using
$sheet->getRowDimension($row_id)->getVisible()
$row_id starts with 1 (and not 0), same is in excel
Here is an example of how to use it in your code. I changed a bit the way you get the Data sheet, since you don't need to iterate over the sheets, you can just get that specific sheet using the getSheetByName function.
$data_sheet = $objPHPExcel->getSheetByName('Data');
$data_array = $data_sheet->toArray();
$data = [];
foreach ($data_sheet->getRowIterator() as $row_id => $row) {
if ($data_sheet->getRowDimension($row_id)->getVisible()) {
// I guess you don't need the Headers row, note that now it's row number 1
if ($row_id > 1) {
$item = array();
$item['name'] = $data_array[$row_id-1][1];
$item['code'] = $data_array[$row_id-1][2];
$data[] = $item;
}
}
}
I am making a photography website. In the DB there is an images table, events table and category table which as linked via foreign keys. At the moment i generate events from when a photo was taken from the database and turn them into anchor links.
<?php
while ( $a_row = mysql_fetch_row( $result ) ){
foreach ( $a_row as $field ){
?>
<php? echo $field; ?>
<?php
}
}
?>
When the link is clicked a script gets the variable from get in the url: /images.php?**event**=eventCalledParty
foreach($_GET as $value)
{
$event = $value;
}
$event = mysql_real_escape_string($event);
My question is - If i was to implement categories and have a url that looks like:
/images.php?event=eventCalledParty&category=categoryCalledFashionPhotography
How would i seperate these two variables from the url to use in my queries.
Thank You.
These will automatically show up in these variables...
$_GET['event']
$_GET['category']
PHP does all of this work for you.
$event = mysql_real_escape_string($_GET['event']);
$category = mysql_real_escape_string($_GET['category']);
Each url parameter becomes a separate key in $_GET.
So, for /images.php?event=eventCalledParty&category=categoryCalledFashionPhotography you will get:
$_GET['event'] => 'eventCalledParty'
$_GET['category'] => 'categoryCalledFashionPhotography'
and you can act accordingly:
if ($_GET['category'] == 'categoryCalledFashionPhotography') {
//do stuff here
}
Im a bit rusty on php but I think in your foreach what you want is to get the name of teh field as well as its value.
foreach($_GET as $name->$value)
{
if($name == 'event') {
$event = mysql_real_escape_string($value);
}
else if($name == 'category')
{
$category = mysql_real_escape_string($category);
}
}
foreach($_GET as $key => $value)
{
$event[$key] = mysql_real_escape_string($value);
}